From 67d28d4e592b36794163c24867df8b35e75587c9 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Thu, 14 Apr 2016 15:29:08 -0400 Subject: [PATCH] fix(tray): Don't recreate tray / change image unnecessarily --- src/browser/application.coffee | 4 +-- src/browser/system-tray-manager.es6 | 51 ++++++++++++++++++----------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/browser/application.coffee b/src/browser/application.coffee index ede0637ff..ad9f1d62d 100644 --- a/src/browser/application.coffee +++ b/src/browser/application.coffee @@ -335,7 +335,7 @@ class Application # Destroy hot windows so that they can't block the app from quitting. # (Electron will wait for them to finish loading before quitting.) @windowManager.unregisterAllHotWindows() - @systemTrayManager?.destroy() + @systemTrayManager.destroyTray() # Called after the app has closed all windows. app.on 'will-quit', => @@ -356,7 +356,7 @@ class Application # System Tray ipcMain.on 'update-system-tray', (event, args...) => - @systemTrayManager?.updateTray(args...) + @systemTrayManager.updateTraySettings(args...) ipcMain.on 'set-badge-value', (event, value) => app.dock?.setBadge?(value) diff --git a/src/browser/system-tray-manager.es6 b/src/browser/system-tray-manager.es6 index 5f0126305..4f18fc3c7 100644 --- a/src/browser/system-tray-manager.es6 +++ b/src/browser/system-tray-manager.es6 @@ -52,22 +52,28 @@ class SystemTrayManager { this._platform = platform; this._application = application; this._iconPath = null; + this._unreadString = null; this._tray = null; - this._initTray(); + this.initTray(); - this._application.config.onDidChange('core.workspace.systemTray', () => { - this.destroy() - this._initTray() - }) + this._application.config.onDidChange('core.workspace.systemTray', ({newValue}) => { + if (newValue === false) { + this.destroyTray(); + } else { + this.initTray(); + } + }); } - _initTray() { - if (this._application.config.get('core.workspace.systemTray') !== false) { + initTray() { + const enabled = (this._application.config.get('core.workspace.systemTray') !== false); + const created = (this._tray !== null); + + if (enabled && !created) { this._tray = new Tray(_getIcon(this._iconPath)); - this._tray.setToolTip(_getTooltip()); + this._tray.setToolTip(_getTooltip(this._unreadString)); this._tray.addListener('click', this._onClick); this._tray.setContextMenu(Menu.buildFromTemplate(_getMenuTemplate(this._platform, this._application))); - this._unsubscribe = ()=> this._tray.removeListener('click', this._onClick); } } @@ -77,19 +83,26 @@ class SystemTrayManager { } }; - updateTray(iconPath, unreadString, isTemplateImg) { - if (!this._tray) return; - this._iconPath = iconPath; + updateTraySettings(iconPath, unreadString, isTemplateImg) { + if ((this._iconPath === iconPath) && (this._unreadString === unreadString)) return; - const icon = _getIcon(this._iconPath, isTemplateImg); - const tooltip = _getTooltip(unreadString); - this._tray.setImage(icon); - this._tray.setToolTip(tooltip); + this._iconPath = iconPath; + this._unreadString = unreadString; + + if (this._tray) { + const icon = _getIcon(this._iconPath, isTemplateImg); + const tooltip = _getTooltip(unreadString); + this._tray.setImage(icon); + this._tray.setToolTip(tooltip); + } } - destroy() { - if (this._tray) this._tray.destroy(); - this._unsubscribe(); + destroyTray() { + if (this._tray) { + this._tray.removeListener('click', this._onClick); + this._tray.destroy(); + this._tray = null; + } } }