[gnome-maps] Utils: Simplify initActions



commit 48c91a6be5eb1a13a01a5ca4a3103b9f06d1afb5
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Tue Nov 11 00:26:20 2014 +0100

    Utils: Simplify initActions
    
    Rename initActions to addActions (showing that the util calls add_action
    in the end) and make it less boilerplaty to use.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=739933

 src/application.js |    7 ++--
 src/mainWindow.js  |   79 +++++++++++++++++++++-------------------------------
 src/utils.js       |   35 +++++++++++++++++------
 3 files changed, 61 insertions(+), 60 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 452767d..f22c169 100644
--- a/src/application.js
+++ b/src/application.js
@@ -130,10 +130,9 @@ const Application = new Lang.Class({
         application = this;
         this._initServices();
 
-        Utils.initActions(this, [{
-            properties: { name: 'quit' },
-            signalHandlers: { activate: this._onQuitActivate }
-        }], this);
+        Utils.addActions(this, {
+            'quit': { onActivate: this._onQuitActivate }
+        });
 
         this._initPlaceStore();
         this._initAppMenu();
diff --git a/src/mainWindow.js b/src/mainWindow.js
index c0ecd29..5020517 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -138,54 +138,39 @@ const MainWindow = new Lang.Class({
     },
 
     _initActions: function() {
-        Utils.initActions(this.window, [
-            {
-                properties: { name: 'close' },
-                signalHandlers: { activate: this.window.close.bind(this.window) }
-            }, {
-                properties: { name: 'about' },
-                signalHandlers: { activate: this._onAboutActivate }
-            }, {
-                properties: {
-                    name: 'map-type-menu',
-                    state: GLib.Variant.new('b', false)
-                },
-                signalHandlers: { activate: this._onMapTypeMenuActivate }
-            }, {
-                properties: {
-                    name: 'map-type',
-                    parameter_type: GLib.VariantType.new('s'),
-                    state: GLib.Variant.new('s', 'STREET')
-                },
-                signalHandlers: { activate: this._onMapTypeActivate }
-            }, {
-                properties: { name: 'goto-user-location' },
-                signalHandlers: { activate: this._onGotoUserLocationActivate }
-            }, {
-                properties: {
-                    name: 'toggle-sidebar',
-                    state: GLib.Variant.new_boolean(false)
-                },
-                signalHandlers: {
-                    'change-state': this._onToggleSidebarChangeState
-                }
-            }, {
-                properties: { name: 'zoom-in' },
-                signalHandlers: {
-                    activate:  this.mapView.view.zoom_in.bind(this.mapView.view)
-                }
-            },{
-                properties: { name: 'zoom-out' },
-                signalHandlers: {
-                    activate:  this.mapView.view.zoom_out.bind(this.mapView.view)
-                }
-            }, {
-                properties: { name: 'find' },
-                signalHandlers: {
-                    activate: this._placeEntry.grab_focus.bind(this._placeEntry)
-                }
+        Utils.addActions(this.window, {
+            'close': {
+                onActivate: this.window.close.bind(this.window)
             },
-        ], this);
+            'about': {
+                onActivate: this._onAboutActivate.bind(this)
+            },
+            'map-type-menu': {
+                state: ['b', false],
+                onActivate: this._onMapTypeMenuActivate.bind(this)
+            },
+            'map-type': {
+                paramType: 's',
+                state: ['s', 'STREET'],
+                onActivate: this._onMapTypeActivate.bind(this)
+            },
+            'goto-user-location': {
+                onActivate: this._onGotoUserLocationActivate.bind(this)
+            },
+            'toggle-sidebar': {
+                state: ['b', false],
+                onChangeState: this._onToggleSidebarChangeState.bind(this)
+            },
+            'zoom-in': {
+                onActivate: this.mapView.view.zoom_in.bind(this.mapView.view)
+            },
+            'zoom-out': {
+                onActivate:  this.mapView.view.zoom_out.bind(this.mapView.view)
+            },
+            'find': {
+                onActivate: this._placeEntry.grab_focus.bind(this._placeEntry)
+            }
+        });
 
         let action = this.window.lookup_action('goto-user-location');
         Application.geoclue.bind_property('connected',
diff --git a/src/utils.js b/src/utils.js
index d9ac452..0c5fc4a 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -88,17 +88,34 @@ function clearGtkClutterActorBg(actor) {
                                                        alpha: 0 }));
 }
 
-function initActions(actionMap, simpleActionEntries, context) {
-    simpleActionEntries.forEach(function(entry) {
-        let action = new Gio.SimpleAction(entry.properties);
-
-        for(let signalHandler in entry.signalHandlers) {
-            let callback = entry.signalHandlers[signalHandler];
-            action.connect(signalHandler, callback.bind(context));
-        }
+function addActions(actionMap, entries) {
+    for(let name in entries) {
+        let entry = entries[name];
+        let action = createAction(name, entry);
 
         actionMap.add_action(action);
-    });
+    }
+}
+
+function createAction(name, { state, paramType, onActivate, onChangeState }) {
+    let entry = { name: name };
+
+    if(Array.isArray(state)) {
+        let [type, value] = state;
+        entry.state = new GLib.Variant.new(type, value);
+    }
+
+    if(paramType !== undefined)
+        entry.parameter_type = GLib.VariantType.new(paramType);
+
+    let action = new Gio.SimpleAction(entry);
+
+    if(onActivate)
+        action.connect('activate', onActivate);
+    if(onChangeState)
+        action.connect('change-state', onChangeState);
+
+    return action;
 }
 
 function CreateActorFromIconName(name) {


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