Commit 7a43196e authored by Philip Chimento's avatar Philip Chimento 🚮

Merge branch 'gtk-template-callbacks' into 'master'

overrides: initial commit of Gtk template callbacks

See merge request !124
parents 1cdb06c7 bcfa6ce7
Pipeline #9406 passed with stages
in 37 minutes and 47 seconds
......@@ -10,6 +10,7 @@
<object class="GtkLabel" id="label-child">
<property name="label">Complex!</property>
<property name="visible">True</property>
<signal name="grab-focus" handler="templateCallback" swapped="no"/>
</object>
</child>
<child>
......
......@@ -17,6 +17,7 @@ const template = ' \
<object class="GtkLabel" id="label-child"> \
<property name="label">Complex!</property> \
<property name="visible">True</property> \
<signal name="grab-focus" handler="templateCallback" swapped="no"/> \
</object> \
</child> \
<child> \
......@@ -39,7 +40,11 @@ const MyComplexGtkSubclass = GObject.registerClass({
Children: ['label-child', 'label-child2'],
InternalChildren: ['internal-label-child'],
CssName: 'complex-subclass',
}, class MyComplexGtkSubclass extends Gtk.Grid {});
}, class MyComplexGtkSubclass extends Gtk.Grid {
templateCallback(widget) {
this.callbackEmittedBy = widget;
}
});
// Sadly, putting this in the body of the class will prevent calling
// get_template_child, since MyComplexGtkSubclass will be bound to the ES6
......@@ -62,6 +67,10 @@ const MyComplexGtkSubclassFromResource = GObject.registerClass({
expect(this.label_child2).toEqual(jasmine.anything());
expect(this._internal_label_child).toEqual(jasmine.anything());
}
templateCallback(widget) {
this.callbackEmittedBy = widget;
}
});
const SubclassSubclass = GObject.registerClass(
......@@ -74,6 +83,7 @@ function validateTemplate(description, ClassName, pending=false) {
beforeEach(function () {
win = new Gtk.Window({ type: Gtk.WindowType.TOPLEVEL });
content = new ClassName();
content.label_child.emit('grab-focus');
win.add(content);
});
......@@ -91,6 +101,10 @@ function validateTemplate(description, ClassName, pending=false) {
.toEqual('Complex and internal!');
});
it('connects template callbacks to the correct handler', function () {
expect(content.callbackEmittedBy).toBe(content.label_child);
});
afterEach(function () {
win.destroy();
});
......
......@@ -45,6 +45,20 @@ function _init() {
}
Gtk.Widget.prototype._init = function(params) {
if (this.constructor[Gtk.template]) {
Gtk.Widget.set_connect_func.call(this.constructor, (builder, obj, signalName, handlerName, connectObj, flags) => {
if (connectObj !== null) {
throw new Error('Unsupported template signal attribute "object"');
} else if (flags & GObject.ConnectFlags.SWAPPED) {
throw new Error('Unsupported template signal flag "swapped"');
} else if (flags & GObject.ConnectFlags.AFTER) {
obj.connect_after(signalName, this[handlerName].bind(this));
} else {
obj.connect(signalName, this[handlerName].bind(this));
}
});
}
GObject.Object.prototype._init.call(this, params);
if (this.constructor[Gtk.template]) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment