[polari] app: Use hooks to control sensitivity of "room" actions



commit 0b0039f93e559c924d4021f0a3b806096a127a76
Author: Florian Müllner <florian muellner gmail com>
Date:   Sun Jul 21 18:04:14 2013 +0200

    app: Use hooks to control sensitivity of "room" actions

 src/application.js |   19 +++++++++++++++++++
 src/mainWindow.js  |   14 --------------
 2 files changed, 19 insertions(+), 14 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index b538abe..410bfa1 100644
--- a/src/application.js
+++ b/src/application.js
@@ -2,6 +2,7 @@ const Gio = imports.gi.Gio;
 const GLib = imports.gi.GLib;
 const Gtk = imports.gi.Gtk;
 
+const ChatroomManager = imports.chatroomManager;
 const Config = imports.config;
 const Connections = imports.connections;
 const Format = imports.format;
@@ -33,6 +34,8 @@ const Application = new Lang.Class({
         let resource = Gio.Resource.load(Config.RESOURCE_DIR + '/polari.gresource');
         resource._register();
 
+        this._chatroomManager = ChatroomManager.getDefault();
+
         let builder = new Gtk.Builder();
         builder.add_from_resource('/org/gnome/polari/app-menu.ui');
         this.set_app_menu(builder.get_object('app-menu'));
@@ -45,9 +48,11 @@ const Application = new Lang.Class({
             activate: Lang.bind(this, this._onMessageUser) },
           { name: 'leave-room',
             activate: Lang.bind(this, this._onLeaveRoom),
+            create_hook: Lang.bind(this, this._roomActionsCreateHook),
             accel: '<Primary>w' },
           { name: 'user-list',
             activate: Lang.bind(this, this._onToggleAction),
+            create_hook: Lang.bind(this, this._roomActionsCreateHook),
             state: GLib.Variant.new('b', false),
             accel: 'F9' },
           { name: 'connections',
@@ -102,6 +107,20 @@ const Application = new Lang.Class({
             this._window.window.present();
     },
 
+    _updateRoomAction: function(action) {
+        action.enabled = this._chatroomManager.roomCount > 0;
+        if (action.state && !action.enabled)
+            action.change_state(GLib.Variant.new('b', false));
+    },
+
+    _roomActionsCreateHook: function(action) {
+        this._chatroomManager.connect('active-changed', Lang.bind(this,
+            function() {
+                this._updateRoomAction(action);
+            }));
+        this._updateRoomAction(action);
+    },
+
     _onJoinRoom: function() {
         this._window.showJoinRoomDialog();
     },
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 63ea75e..5075531 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -41,8 +41,6 @@ const MainWindow = new Lang.Class({
         this._nicknameChangedId = 0;
         this._channelChangedId = 0;
 
-        this._updateActionStates();
-
         this._titlebar = builder.get_object('titlebar');
         this._revealer = builder.get_object('room_list_revealer');
         this._chatStack = builder.get_object('chat_stack');
@@ -143,7 +141,6 @@ const MainWindow = new Lang.Class({
 
         this._updateTitlebar();
         this._updateAccount();
-        this._updateActionStates();
     },
 
     _setNick: function(nick) {
@@ -250,16 +247,5 @@ const MainWindow = new Lang.Class({
 
         this._sendButton.grab_default();
         this._entry.grab_focus();
-    },
-
-    _updateActionStates: function() {
-        let actionNames = ['leave-room', 'user-list'];
-        actionNames.forEach(Lang.bind(this,
-            function(actionName) {
-                let action = this.window.application.lookup_action(actionName);
-                action.enabled = this._room != null;
-                if (action.state && !action.enabled)
-                    action.change_state(GLib.Variant.new('b', false));
-            }));
     }
 });


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