[gnome-shell/wip/new-notfications: 5/14] messageTray: Add an indicatorCount property to the tray



commit 8eefdb6a269e15603ebd26d99d4db03a393764cb
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Dec 4 22:04:13 2013 -0500

    messageTray: Add an indicatorCount property to the tray
    
    So we don't have to do the property tracking here...

 js/ui/messageTray.js      |   24 +++++++++++++++++++++++-
 js/ui/overviewControls.js |   34 +++++-----------------------------
 2 files changed, 28 insertions(+), 30 deletions(-)
---
diff --git a/js/ui/messageTray.js b/js/ui/messageTray.js
index 1d87066..dc630b3 100644
--- a/js/ui/messageTray.js
+++ b/js/ui/messageTray.js
@@ -2032,7 +2032,8 @@ const MessageTray = new Lang.Class({
             summaryItem: new SummaryItem(source),
             notifyId: 0,
             destroyId: 0,
-            mutedChangedId: 0
+            mutedChangedId: 0,
+            countChangedId: 0,
         };
         let summaryItem = obj.summaryItem;
 
@@ -2057,6 +2058,9 @@ const MessageTray = new Lang.Class({
                         return source != notification.source;
                     });
             }));
+        obj.countChangedId = source.connect('count-updated', Lang.bind(this, function() {
+            this.emit('indicator-count-updated');
+        }));
 
         summaryItem.actor.connect('clicked', Lang.bind(this,
             function(actor, button) {
@@ -2076,6 +2080,7 @@ const MessageTray = new Lang.Class({
         Meta.later_add(Meta.LaterType.BEFORE_REDRAW, Lang.bind(this, function() { this._updateState(); 
return false; }));
 
         this.emit('source-added', source);
+        this.emit('indicator-count-updated');
 
         this._updateNoMessagesLabel();
     },
@@ -2093,10 +2098,12 @@ const MessageTray = new Lang.Class({
         source.disconnect(obj.notifyId);
         source.disconnect(obj.destroyId);
         source.disconnect(obj.mutedChangedId);
+        source.disconnect(obj.countChangedId);
 
         summaryItem.destroy();
 
         this.emit('source-removed', source);
+        this.emit('indicator-count-updated');
 
         this._updateNoMessagesLabel();
     },
@@ -2121,6 +2128,21 @@ const MessageTray = new Lang.Class({
         this._removeSource(source);
     },
 
+    get hasChatSources() {
+        return this._sources.keys().some(function(source) {
+            return source.isChat;
+        });
+    },
+
+    get indicatorCount() {
+        if (!this._sources.size())
+            return 0;
+
+        return this._sources.keys().map(function(source) {
+            return source.indicatorCount;
+        }).reduce(function(a, b) { return a + b });
+    },
+
     _onNotificationDestroy: function(notification) {
         if (this._notification == notification && (this._notificationState == State.SHOWN || 
this._notificationState == State.SHOWING)) {
             this._updateNotificationTimeout(0);
diff --git a/js/ui/overviewControls.js b/js/ui/overviewControls.js
index ade0f19..de6142d 100644
--- a/js/ui/overviewControls.js
+++ b/js/ui/overviewControls.js
@@ -446,45 +446,21 @@ const MessagesIndicator = new Lang.Class({
         this.actor.add_actor(this._container);
         this.actor.add_actor(this._highlight);
 
-        Main.messageTray.connect('source-added', Lang.bind(this, this._onSourceAdded));
-        Main.messageTray.connect('source-removed', Lang.bind(this, this._onSourceRemoved));
-
-        let sources = Main.messageTray.getSources();
-        sources.forEach(Lang.bind(this, function(source) { this._onSourceAdded(null, source); }));
+        Main.messageTray.connect('indicator-count-updated', Lang.bind(this, this._sync));
+        this._sync();
 
         this._viewSelector.connect('page-changed', Lang.bind(this, this._updateVisibility));
         Main.overview.connect('showing', Lang.bind(this, this._updateVisibility));
     },
 
-    _onSourceAdded: function(tray, source) {
-        if (source.trayIcon)
-            return;
-
-        source.connect('count-updated', Lang.bind(this, this._updateCount));
-        this._sources.push(source);
-        this._updateCount();
-    },
-
-    _onSourceRemoved: function(tray, source) {
-        this._sources.splice(this._sources.indexOf(source), 1);
-        this._updateCount();
-    },
-
-    _updateCount: function() {
-        let count = 0;
-        let hasChats = false;
-        this._sources.forEach(Lang.bind(this,
-            function(source) {
-                count += source.indicatorCount;
-                hasChats |= source.isChat;
-            }));
-
+    _sync: function() {
+        let count = Main.messageTray.indicatorCount;
         this._count = count;
         this._label.text = ngettext("%d new message",
                                     "%d new messages",
                                    count).format(count);
 
-        this._icon.visible = hasChats;
+        this._icon.visible = Main.messageTray.hasChatSources;
         this._updateVisibility();
     },
 


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