Cannot activate actions installed with gtk_widget_class_install_action
GtkWidget
has convenient static methods to define instance actions at class level. Contrary to gtk_widget_class_install_property_action
, gtk_widget_class_install_action
is unusable with GJS.
System information
GJS 1.68.1.
Bug information
Steps to reproduce
Get "core dumped" when trying to activate the action:
imports.gi.versions.Gtk = '4.0';
const { GLib, GObject, Gtk } = imports.gi;
const DemoWidget = GObject.registerClass(class extends Gtk.Widget {
_init(params) {
super._init(params);
new Gtk.Label({ label: "No label", halign: Gtk.Align.CENTER, valign: Gtk.Align.CENTER }).set_parent(this);
}
vfunc_unrealize() {
super.vfunc_unrealize();
this.get_first_child().unparent();
}
});
DemoWidget.set_layout_manager_type(Gtk.BinLayout.$gtype);
DemoWidget.install_action('misc.change-label', 's', (widget, actionName, parameter) => {
log('callback');
widget.get_first_child().label = parameter.unpack();
});
Gtk.init();
let widget = new DemoWidget();
let window = new Gtk.Window({
title: "Demo Window",
defaultWidth: 400, defaultHeight: 200,
child: widget,
visible: true,
});
widget.activate_action('misc.change-label', GLib.Variant.new_string("New label"));
// core dumped
let loop = GLib.MainLoop.new(null, false);
window.connect('close-request', () => loop.quit());
loop.run();
Another example with one difference.
After installing the action, call gtk_widget_class_query_action
twice.
The first time, the log seems to indicate that the action is correctly installed.
But the second time, there is a TypeError
.
Then gtk_widget_activate_action_variant
returns false (the action does not exist).
imports.gi.versions.Gtk = '4.0';
const { GLib, GObject, Gtk } = imports.gi;
const DemoWidget = GObject.registerClass(class extends Gtk.Widget {
_init(params) {
super._init(params);
new Gtk.Label({ label: "No label", halign: Gtk.Align.CENTER, valign: Gtk.Align.CENTER }).set_parent(this);
}
vfunc_unrealize() {
super.vfunc_unrealize();
this.get_first_child().unparent();
}
});
DemoWidget.set_layout_manager_type(Gtk.BinLayout.$gtype);
DemoWidget.install_action('misc.change-label', 's', (widget, actionName, parameter) => {
log('callback');
widget.get_first_child().label = parameter.unpack();
});
Gtk.init();
let widget = new DemoWidget();
let window = new Gtk.Window({
title: "Demo Window",
defaultWidth: 400, defaultHeight: 200,
child: widget,
visible: true,
});
log(DemoWidget.query_action(0));
// JS LOG: true,[object GType for 'Gjs_anonymous_...'],misc.change-label,[boxed instance wrapper GIName:GLib.VariantType jsobj@...],
try {
log(DemoWidget.query_action(0));
} catch(e) {
log(e.message);
// Gjs-CRITICAL **: JS ERROR: TypeError: malformed UTF-8 character sequence at offset 0
}
log(widget.activate_action('misc.change-label', GLib.Variant.new_string("New label")));
// false
let loop = GLib.MainLoop.new(null, false);
window.connect('close-request', () => loop.quit());
loop.run();