Add API to define widget children from template
In addition to #450, this issue tracks adding an API which replicates the current behavior in GJS for assigning children.
Background
Our existing children assignment occurs prior to super()
returning in the constructor
so children cannot be defined as class fields.
Proposed API Additions
namespace Gtk {
/**
* Classes which use `.register()` instead of `GObject.registerClass()` can use
* `Gtk.defineChildren(this, MyWidget)` to emulate the behavior of `GObject.registerClass()`.
*
* `target` is an optional parameter to define the children on and defaults to `widget`.
*/
export function defineChildren(widget: Gtk.Widget, Class: Function, target?: object): void;
}
Example usage
var Foo = GObject.registerClass({
Template: '...',
Children: ['a', 'b'],
}, class Foo extends Gtk.Box {
a;
b;
constructor() {
super();
Gtk.defineChildren(this, Foo);
}
});
Pending #453
export class Foo extends Gtk.Box {
a;
b;
constructor() {
super();
Gtk.defineChildren(this, Foo);
}
}
Foo.register({
Template: '...',
Children: ['a', 'b'],
});
export class Bar extends Gtk.Box {
static [Gtk.template] = '...';
static [Gtk.children] = ['a', 'b'];
a;
b;
constructor() {
super();
Gtk.defineChildren(this, Foo);
}
}
Bar.register();
Edited by Evan Welsh