[gnome-shell-extensions] cleanup: Simplify action handling



commit 0be8b1099566f1a3daaf9e1943b96afc94be478d
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Feb 13 02:52:12 2022 +0100

    cleanup: Simplify action handling
    
    GTK4 has dedicated API for widget-specific actions, make use of that
    instead of explicitly managing an action group.
    
    Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell-extensions/-/merge_requests/215>

 extensions/auto-move-windows/prefs.js   | 50 +++++++++++--------------
 extensions/workspace-indicator/prefs.js | 65 ++++++++++++++++-----------------
 2 files changed, 53 insertions(+), 62 deletions(-)
---
diff --git a/extensions/auto-move-windows/prefs.js b/extensions/auto-move-windows/prefs.js
index fd787af..1e3603d 100644
--- a/extensions/auto-move-windows/prefs.js
+++ b/extensions/auto-move-windows/prefs.js
@@ -14,6 +14,17 @@ const WORKSPACE_MAX = 36; // compiled in limit of mutter
 
 const AutoMoveSettingsWidget = GObject.registerClass(
 class AutoMoveSettingsWidget extends Adw.PreferencesGroup {
+    static _classInit(klass) {
+        super._classInit(klass);
+
+        klass.install_action('rules.add', null, self => self._addNewRule());
+        klass.install_action('rules.remove', 's',
+            (self, name, param) => self._removeRule(param.unpack()));
+        klass.install_action('rules.update', null, self => self._saveRules());
+
+        return klass;
+    }
+
     _init() {
         super._init({
             title: _('Workspace Rules'),
@@ -27,29 +38,6 @@ class AutoMoveSettingsWidget extends Adw.PreferencesGroup {
 
         this._list.append(new NewRuleRow());
 
-        this._actionGroup = new Gio.SimpleActionGroup();
-        this._list.insert_action_group('rules', this._actionGroup);
-
-        let action;
-        action = new Gio.SimpleAction({ name: 'add' });
-        action.connect('activate', this._onAddActivated.bind(this));
-        this._actionGroup.add_action(action);
-
-        action = new Gio.SimpleAction({
-            name: 'remove',
-            parameter_type: new GLib.VariantType('s'),
-        });
-        action.connect('activate', this._onRemoveActivated.bind(this));
-        this._actionGroup.add_action(action);
-
-        action = new Gio.SimpleAction({ name: 'update' });
-        action.connect('activate', () => {
-            this._settings.set_strv(SETTINGS_KEY,
-                this._getRuleRows().map(row => `${row.id}:${row.value}`));
-        });
-        this._actionGroup.add_action(action);
-        this._updateAction = action;
-
         this._settings = ExtensionUtils.getSettings();
         this._changedId = this._settings.connect('changed',
             this._sync.bind(this));
@@ -58,7 +46,7 @@ class AutoMoveSettingsWidget extends Adw.PreferencesGroup {
         this.connect('destroy', () => this._settings.run_dispose());
     }
 
-    _onAddActivated() {
+    _addNewRule() {
         const dialog = new NewRuleDialog(this.get_root());
         dialog.connect('response', (dlg, id) => {
             const appInfo = id === Gtk.ResponseType.OK
@@ -74,15 +62,19 @@ class AutoMoveSettingsWidget extends Adw.PreferencesGroup {
         dialog.show();
     }
 
-    _onRemoveActivated(action, param) {
-        const removed = param.deepUnpack();
+    _removeRule(removedId) {
         this._settings.set_strv(SETTINGS_KEY,
             this._settings.get_strv(SETTINGS_KEY).filter(entry => {
                 const [id] = entry.split(':');
-                return id !== removed;
+                return id !== removedId;
             }));
     }
 
+    _saveRules() {
+        this._settings.set_strv(SETTINGS_KEY,
+            this._getRuleRows().map(row => `${row.id}:${row.value}`));
+    }
+
     _getRuleRows() {
         return [...this._list].filter(row => !!row.id);
     }
@@ -95,7 +87,7 @@ class AutoMoveSettingsWidget extends Adw.PreferencesGroup {
         });
 
         this._settings.block_signal_handler(this._changedId);
-        this._updateAction.enabled = false;
+        this.action_set_enabled('rules.update', false);
 
         newRules.forEach(({ id, value }, index) => {
             const row = oldRules.find(r => r.id === id);
@@ -113,7 +105,7 @@ class AutoMoveSettingsWidget extends Adw.PreferencesGroup {
         removed.forEach(r => this._list.remove(r));
 
         this._settings.unblock_signal_handler(this._changedId);
-        this._updateAction.enabled = true;
+        this.action_set_enabled('rules.update', true);
     }
 });
 
diff --git a/extensions/workspace-indicator/prefs.js b/extensions/workspace-indicator/prefs.js
index 8734e1a..8baa9de 100644
--- a/extensions/workspace-indicator/prefs.js
+++ b/extensions/workspace-indicator/prefs.js
@@ -13,6 +13,19 @@ const WORKSPACE_KEY = 'workspace-names';
 
 const WorkspaceSettingsWidget = GObject.registerClass(
 class WorkspaceSettingsWidget extends Adw.PreferencesGroup {
+    static _classInit(klass) {
+        klass = super._classInit(klass);
+
+        klass.install_action('workspaces.add', null,
+            self => self._addNewName());
+        klass.install_action('workspaces.remove', 's',
+            (self, name, param) => self._removeName(param.unpack()));
+        klass.install_action('workspaces.update', null,
+            self => self._saveNames());
+
+        return klass;
+    }
+
     _init() {
         super._init({
             title: _('Workspace Names'),
@@ -27,39 +40,6 @@ class WorkspaceSettingsWidget extends Adw.PreferencesGroup {
 
         this._list.append(new NewWorkspaceRow());
 
-        this._actionGroup = new Gio.SimpleActionGroup();
-        this._list.insert_action_group('workspaces', this._actionGroup);
-
-        let action;
-        action = new Gio.SimpleAction({ name: 'add' });
-        action.connect('activate', () => {
-            const names = this._settings.get_strv(WORKSPACE_KEY);
-            this._settings.set_strv(WORKSPACE_KEY, [
-                ...names,
-                _('Workspace %d').format(names.length + 1),
-            ]);
-        });
-        this._actionGroup.add_action(action);
-
-        action = new Gio.SimpleAction({
-            name: 'remove',
-            parameter_type: new GLib.VariantType('s'),
-        });
-        action.connect('activate', (a, param) => {
-            const removed = param.deepUnpack();
-            this._settings.set_strv(WORKSPACE_KEY,
-                this._settings.get_strv(WORKSPACE_KEY)
-                    .filter(name => name !== removed));
-        });
-        this._actionGroup.add_action(action);
-
-        action = new Gio.SimpleAction({ name: 'update' });
-        action.connect('activate', () => {
-            const names = this._getWorkspaceRows().map(row => row.name);
-            this._settings.set_strv(WORKSPACE_KEY, names);
-        });
-        this._actionGroup.add_action(action);
-
         this._settings = new Gio.Settings({
             schema_id: WORKSPACE_SCHEMA,
         });
@@ -68,6 +48,25 @@ class WorkspaceSettingsWidget extends Adw.PreferencesGroup {
         this._sync();
     }
 
+    _addNewName() {
+        const names = this._settings.get_strv(WORKSPACE_KEY);
+        this._settings.set_strv(WORKSPACE_KEY, [
+            ...names,
+            _('Workspace %d').format(names.length + 1),
+        ]);
+    }
+
+    _removeName(removedName) {
+        this._settings.set_strv(WORKSPACE_KEY,
+            this._settings.get_strv(WORKSPACE_KEY)
+                .filter(name => name !== removedName));
+    }
+
+    _saveNames() {
+        const names = this._getWorkspaceRows().map(row => row.name);
+        this._settings.set_strv(WORKSPACE_KEY, names);
+    }
+
     _getWorkspaceRows() {
         return [...this._list].filter(row => row.name);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]