[polari/wip/fmuellner/misc-cleanups: 8/11] roomList: Simplify actions code a bit



commit cb15f78bea67a9440edbb926c5ee4a17afbf0bf7
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Jul 14 06:02:20 2016 +0200

    roomList: Simplify actions code a bit
    
    We have several handlers that select a specific row by index, so split
    out some shared code into a helper method. We can also save on some
    code repetition by iterating over an array of ActionEntry-like objects
    to look up actions and connect to their ::activate signal.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=769582

 src/roomList.js |   84 ++++++++++++++++++++++--------------------------------
 1 files changed, 34 insertions(+), 50 deletions(-)
---
diff --git a/src/roomList.js b/src/roomList.js
index c2cc63d..6caaea6 100644
--- a/src/roomList.js
+++ b/src/roomList.js
@@ -380,56 +380,34 @@ const RoomList = new Lang.Class({
                                   Lang.bind(this, this._activeRoomChanged));
 
         let app = Gio.Application.get_default();
-        this._leaveAction = app.lookup_action('leave-room');
-        this._leaveAction.connect('activate',
-                                  Lang.bind(this, this._onLeaveActivated));
-
-        let action;
-        action = app.lookup_action('next-room');
-        action.connect('activate', Lang.bind(this,
-            function() {
-                this._moveSelection(Gtk.DirectionType.DOWN);
-            }));
-        action = app.lookup_action('previous-room');
-        action.connect('activate', Lang.bind(this,
-            function() {
-                this._moveSelection(Gtk.DirectionType.UP);
-            }));
-        action = app.lookup_action('first-room');
-        action.connect('activate', Lang.bind(this,
-            function() {
-                let row = this._getRoomRowAtIndex(0);
-                if (row)
-                    this.select_row(row);
-            }));
-        action = app.lookup_action('last-room');
-        action.connect('activate', Lang.bind(this,
-            function() {
-                let nRows = this._roomManager.roomCount;
-                let row = this._getRoomRowAtIndex(nRows - 1);
-                if (row)
-                    this.select_row(row);
-            }));
-        action = app.lookup_action('nth-room');
-        action.connect('activate', Lang.bind(this,
-            function(action, param) {
-                let n = param.get_int32();
-                if (n > this._roomManager.roomCount)
-                    return;
-                this.select_row(this._getRoomRowAtIndex(n - 1));
-            }));
-        action = app.lookup_action('next-pending-room');
-        action.connect('activate', Lang.bind(this,
-            function() {
-                this._moveSelectionFull(Gtk.DirectionType.DOWN,
-                                        (row) => { return row.hasPending; });
-            }));
-        action = app.lookup_action('previous-pending-room');
-        action.connect('activate', Lang.bind(this,
-            function() {
-                this._moveSelectionFull(Gtk.DirectionType.UP,
-                                        (row) => { return row.hasPending; });
-            }));
+        let actions = [
+            { name: 'leave-room',
+              handler: Lang.bind(this, this._onLeaveActivated) },
+            { name: 'next-room',
+              handler: () => { this._moveSelection(Gtk.DirectionType.DOWN); } },
+            { name: 'previous-room',
+              handler: () => { this._moveSelection(Gtk.DirectionType.UP); } },
+            { name: 'first-room',
+              handler: () => { this._selectRoomAtIndex(0); } },
+            { name: 'last-room',
+              handler: () => {
+                  let nRows = this._roomManager.roomCount;
+                  this._selectRoomAtIndex(nRows - 1);
+              } },
+            { name: 'nth-room',
+              handler: (a, param) => {
+                  this._selectRoomAtIndex(param.get_int32() - 1);
+              } },
+            { name: 'next-pending-room',
+              handler: () => { this._moveSelectionFull(Gtk.DirectionType.DOWN,
+                                                       row => row.hasPending); } },
+            { name: 'previous-pending-room',
+              handler: () => { this._moveSelectionFull(Gtk.DirectionType.UP,
+                                                       row => row.hasPending); } }
+        ];
+        actions.forEach(a => {
+            app.lookup_action(a.name).connect('activate', a.handler);
+        });
     },
 
     _onLeaveActivated: function(action, param) {
@@ -460,6 +438,12 @@ const RoomList = new Lang.Class({
         return this.get_row_at_index(this._roomToRowIndex(index));
     },
 
+    _selectRoomAtIndex: function(index) {
+        let row = this._getRoomRowAtIndex(index);
+        if (row)
+            this.select_row(row);
+    },
+
     _moveSelection: function(direction) {
         this._moveSelectionFull(direction, () => { return true; });
     },


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