[gjs: 1/2] Add Gio.ActionMap.add_action_entries override
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 1/2] Add Gio.ActionMap.add_action_entries override
- Date: Mon, 4 Jul 2022 03:51:40 +0000 (UTC)
commit dac3eb6c1f0a96d6c49554723abb035d588930a0
Author: Sonny Piers <sonny fastmail net>
Date: Tue Mar 1 10:52:10 2022 +0100
Add Gio.ActionMap.add_action_entries override
installed-tests/js/testGio.js | 93 +++++++++++++++++++++++++++++++++++++++++++
modules/core/overrides/Gio.js | 24 +++++++++++
2 files changed, 117 insertions(+)
---
diff --git a/installed-tests/js/testGio.js b/installed-tests/js/testGio.js
index aa57e4f1f..1ea2510cf 100644
--- a/installed-tests/js/testGio.js
+++ b/installed-tests/js/testGio.js
@@ -197,3 +197,96 @@ describe('Gio.Settings overrides', function () {
});
});
});
+
+describe('Gio.add_action_entries override', function () {
+ it('registers each entry as an action', function () {
+ const app = new Gio.Application();
+
+ const entries = [
+ {
+ name: 'foo',
+ parameter_type: 's',
+ },
+ {
+ name: 'bar',
+ state: 'false',
+ },
+ ];
+
+ app.add_action_entries(entries);
+
+ expect(app.lookup_action('foo').name).toEqual(entries[0].name);
+ expect(app.lookup_action('foo').parameter_type.dup_string()).toEqual(entries[0].parameter_type);
+
+ expect(app.lookup_action('bar').name).toEqual(entries[1].name);
+ expect(app.lookup_action('bar').state.print(true)).toEqual(entries[1].state);
+ });
+
+ it('connects and binds the activate handler', function (done) {
+ const app = new Gio.Application();
+ let action;
+
+ const entries = [
+ {
+ name: 'foo',
+ parameter_type: 's',
+ activate() {
+ expect(this).toBe(action);
+ done();
+ },
+ },
+ ];
+
+ app.add_action_entries(entries);
+ action = app.lookup_action('foo');
+
+ action.activate(new GLib.Variant('s', 'hello'));
+ });
+
+ it('connects and binds the change_state handler', function (done) {
+ const app = new Gio.Application();
+ let action;
+
+ const entries = [
+ {
+ name: 'bar',
+ state: 'false',
+ change_state() {
+ expect(this).toBe(action);
+ done();
+ },
+ },
+ ];
+
+ app.add_action_entries(entries);
+ action = app.lookup_action('bar');
+
+ action.change_state(new GLib.Variant('b', 'true'));
+ });
+
+ it('throw an error if the parameter_type is invalid', function () {
+ const app = new Gio.Application();
+
+ const entries = [
+ {
+ name: 'foo',
+ parameter_type: '(((',
+ },
+ ];
+
+ expect(() => app.add_action_entries(entries)).toThrow();
+ });
+
+ it('throw an error if the state is invalid', function () {
+ const app = new Gio.Application();
+
+ const entries = [
+ {
+ name: 'bar',
+ state: 'foo',
+ },
+ ];
+
+ expect(() => app.add_action_entries(entries)).toThrow();
+ });
+});
diff --git a/modules/core/overrides/Gio.js b/modules/core/overrides/Gio.js
index 511d6c325..8db01c22b 100644
--- a/modules/core/overrides/Gio.js
+++ b/modules/core/overrides/Gio.js
@@ -643,4 +643,28 @@ function _init() {
get_child: createCheckedMethod('get_child', '_checkChild'),
});
+
+ // ActionMap
+ // add_action_entries is not introspectable
+ // https://gitlab.gnome.org/GNOME/gjs/-/issues/407
+ Gio.ActionMap.prototype.add_action_entries = function add_action_entries(entries) {
+ for (const {name, activate, parameter_type, state, change_state} of entries) {
+ if (typeof parameter_type === 'string' && !GLib.variant_type_string_is_valid(parameter_type))
+ throw new Error(`parameter_type "${parameter_type}" is not a valid VariantType`);
+
+ const action = new Gio.SimpleAction({
+ name,
+ parameter_type: typeof parameter_type === 'string' ? new GLib.VariantType(parameter_type) :
null,
+ state: typeof state === 'string' ? GLib.Variant.parse(null, state, null, null) : null,
+ });
+
+ if (typeof activate === 'function')
+ action.connect('activate', activate.bind(action));
+
+ if (typeof change_state === 'function')
+ action.connect('change-state', change_state.bind(action));
+
+ this.add_action(action);
+ }
+ };
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]