messageTray: Add an indicatorCount property to the tray

So we don't have to do the property tracking here...
This commit is contained in:
Jasper St. Pierre 2013-12-04 22:04:13 -05:00
parent 0b414308fb
commit f7223763d2
2 changed files with 30 additions and 30 deletions

View File

@ -1821,7 +1821,8 @@ const MessageTray = new Lang.Class({
summaryItem: new SummaryItem(source), summaryItem: new SummaryItem(source),
notifyId: 0, notifyId: 0,
destroyId: 0, destroyId: 0,
mutedChangedId: 0 mutedChangedId: 0,
countChangedId: 0,
}; };
let summaryItem = obj.summaryItem; let summaryItem = obj.summaryItem;
@ -1846,6 +1847,9 @@ const MessageTray = new Lang.Class({
return source != notification.source; 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, summaryItem.actor.connect('clicked', Lang.bind(this,
function(actor, button) { function(actor, button) {
@ -1865,6 +1869,7 @@ const MessageTray = new Lang.Class({
Meta.later_add(Meta.LaterType.BEFORE_REDRAW, Lang.bind(this, function() { this._updateState(); return false; })); Meta.later_add(Meta.LaterType.BEFORE_REDRAW, Lang.bind(this, function() { this._updateState(); return false; }));
this.emit('source-added', source); this.emit('source-added', source);
this.emit('indicator-count-updated');
this._updateNoMessagesLabel(); this._updateNoMessagesLabel();
}, },
@ -1883,10 +1888,12 @@ const MessageTray = new Lang.Class({
source.disconnect(obj.notifyId); source.disconnect(obj.notifyId);
source.disconnect(obj.destroyId); source.disconnect(obj.destroyId);
source.disconnect(obj.mutedChangedId); source.disconnect(obj.mutedChangedId);
source.disconnect(obj.countChangedId);
summaryItem.destroy(); summaryItem.destroy();
this.emit('source-removed', source); this.emit('source-removed', source);
this.emit('indicator-count-updated');
this._updateNoMessagesLabel(); this._updateNoMessagesLabel();
}, },
@ -1911,6 +1918,23 @@ const MessageTray = new Lang.Class({
this._removeSource(source); this._removeSource(source);
}, },
get hasChatSources() {
for (let source of this._sources.keys())
if (source.isChat)
return true;
return false;
},
get indicatorCount() {
if (!this._sources.size)
return 0;
let count = 0;
for (let source of this._sources.keys())
count += source.indicatorCount;
return count;
},
_onNotificationDestroy: function(notification) { _onNotificationDestroy: function(notification) {
if (this._notification == notification && (this._notificationState == State.SHOWN || this._notificationState == State.SHOWING)) { if (this._notification == notification && (this._notificationState == State.SHOWN || this._notificationState == State.SHOWING)) {
this._updateNotificationTimeout(0); this._updateNotificationTimeout(0);

View File

@ -451,45 +451,21 @@ const MessagesIndicator = new Lang.Class({
this.actor.add_actor(this._container); this.actor.add_actor(this._container);
this.actor.add_actor(this._highlight); this.actor.add_actor(this._highlight);
Main.messageTray.connect('source-added', Lang.bind(this, this._onSourceAdded)); Main.messageTray.connect('indicator-count-updated', Lang.bind(this, this._sync));
Main.messageTray.connect('source-removed', Lang.bind(this, this._onSourceRemoved)); this._sync();
let sources = Main.messageTray.getSources();
sources.forEach(Lang.bind(this, function(source) { this._onSourceAdded(null, source); }));
this._viewSelector.connect('page-changed', Lang.bind(this, this._updateVisibility)); this._viewSelector.connect('page-changed', Lang.bind(this, this._updateVisibility));
Main.overview.connect('showing', Lang.bind(this, this._updateVisibility)); Main.overview.connect('showing', Lang.bind(this, this._updateVisibility));
}, },
_onSourceAdded: function(tray, source) { _sync: function() {
if (source.trayIcon) let count = Main.messageTray.indicatorCount;
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;
}));
this._count = count; this._count = count;
this._label.text = ngettext("%d new message", this._label.text = ngettext("%d new message",
"%d new messages", "%d new messages",
count).format(count); count).format(count);
this._icon.visible = hasChats; this._icon.visible = Main.messageTray.hasChatSources;
this._updateVisibility(); this._updateVisibility();
}, },