romgrk / node-gtk

GTK+ bindings for NodeJS (via GObject introspection)
MIT License
494 stars 42 forks source link

Need help in understanding templates #312

Closed rexkogitans closed 3 years ago

rexkogitans commented 3 years ago

Gtk 4 now uses templates to connect the event handlers mentioned in an XML UI file automatically. However, I need some help to understand how to do in JS. This is what I have come up with so far:

#!/usr/bin/env node

const gi = require('node-gtk')
const GLib = gi.require('GLib', '2.0')
const Gtk = gi.require('Gtk', '4.0')

class Foo extends Gtk.Box {

    GTypeName = "example1"
    __gtype__ = "example1"  // I think that this is correct
    #btn_hello = null;

    constructor() {
        let xml = `<interface>
  <template class="example1" parent="GtkBox">
    <child>
      <object class="GtkButton" id="hello_button">
        <property name="label">Hello World</property>
        <signal name="clicked" handler="btn_hello_clicked" swapped="no" />
      </object>
    </child>
  </template>
</interface>
`
        super()
        this.setTemplate(xml)  // does not work
        //this.initTemplate()
    }

    btn_hello_clicked() {
        console.log("btn clicked")
    }

}

var myapp = null;

function onActivate() {
    myapp = new Foo()
}

const loop = GLib.MainLoop.new(null, false)
const app = new Gtk.Application("rexkogitans.mwe-node-gtk", 0)
app.on('activate', onActivate)
const status = app.run([])

gi.startLoop()

loop.run()

Can someone tell me what to do exactly in the constructor to load the XML UI data and connect the event handlers automatically?

Kind regards

binyamin commented 3 years ago

@rexkogitans I'm not sure exactly how it would be done, correctly. As it happens, there seems to be a bug making it so that the relevant methods don't exist.


@romgrk This is related to #283 (see later comments), where some methods aren't exposed

rexkogitans commented 3 years ago

I tried to overly expose everything, but as I lack in time I could not go into deeper. In bootstrap.js, line 382 says

return undefined;

I replaced it by

return makeStruct(info);

Running the program ends in the same error as before.

rexkogitans commented 3 years ago

Addendum; IMHO, the equivalent to pyobject's gi/_gtktemplate.py is missing.

romgrk commented 3 years ago

For now, there is no equivalent. PyGObject does some magic to make templates work, we're not at that stage yet.

To create a GTK4 application, the best model for now is node-gtk-template, in particular window.js for your use case.

Closing but don't hesitate if you have more questions.