fix(tray): Don't recreate tray / change image unnecessarily

This commit is contained in:
Ben Gotow 2016-04-14 15:29:08 -04:00
parent e134483d75
commit 67d28d4e59
2 changed files with 34 additions and 21 deletions

View file

@ -335,7 +335,7 @@ class Application
# Destroy hot windows so that they can't block the app from quitting. # Destroy hot windows so that they can't block the app from quitting.
# (Electron will wait for them to finish loading before quitting.) # (Electron will wait for them to finish loading before quitting.)
@windowManager.unregisterAllHotWindows() @windowManager.unregisterAllHotWindows()
@systemTrayManager?.destroy() @systemTrayManager.destroyTray()
# Called after the app has closed all windows. # Called after the app has closed all windows.
app.on 'will-quit', => app.on 'will-quit', =>
@ -356,7 +356,7 @@ class Application
# System Tray # System Tray
ipcMain.on 'update-system-tray', (event, args...) => ipcMain.on 'update-system-tray', (event, args...) =>
@systemTrayManager?.updateTray(args...) @systemTrayManager.updateTraySettings(args...)
ipcMain.on 'set-badge-value', (event, value) => ipcMain.on 'set-badge-value', (event, value) =>
app.dock?.setBadge?(value) app.dock?.setBadge?(value)

View file

@ -52,22 +52,28 @@ class SystemTrayManager {
this._platform = platform; this._platform = platform;
this._application = application; this._application = application;
this._iconPath = null; this._iconPath = null;
this._unreadString = null;
this._tray = null; this._tray = null;
this._initTray(); this.initTray();
this._application.config.onDidChange('core.workspace.systemTray', () => { this._application.config.onDidChange('core.workspace.systemTray', ({newValue}) => {
this.destroy() if (newValue === false) {
this._initTray() this.destroyTray();
}) } else {
this.initTray();
}
});
} }
_initTray() { initTray() {
if (this._application.config.get('core.workspace.systemTray') !== false) { 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 = new Tray(_getIcon(this._iconPath));
this._tray.setToolTip(_getTooltip()); this._tray.setToolTip(_getTooltip(this._unreadString));
this._tray.addListener('click', this._onClick); this._tray.addListener('click', this._onClick);
this._tray.setContextMenu(Menu.buildFromTemplate(_getMenuTemplate(this._platform, this._application))); 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) { updateTraySettings(iconPath, unreadString, isTemplateImg) {
if (!this._tray) return; if ((this._iconPath === iconPath) && (this._unreadString === unreadString)) return;
this._iconPath = iconPath;
this._iconPath = iconPath;
this._unreadString = unreadString;
if (this._tray) {
const icon = _getIcon(this._iconPath, isTemplateImg); const icon = _getIcon(this._iconPath, isTemplateImg);
const tooltip = _getTooltip(unreadString); const tooltip = _getTooltip(unreadString);
this._tray.setImage(icon); this._tray.setImage(icon);
this._tray.setToolTip(tooltip); this._tray.setToolTip(tooltip);
} }
}
destroy() { destroyTray() {
if (this._tray) this._tray.destroy(); if (this._tray) {
this._unsubscribe(); this._tray.removeListener('click', this._onClick);
this._tray.destroy();
this._tray = null;
}
} }
} }