From c007ee31bbe56d44b69b4122c715caabe68efcb5 Mon Sep 17 00:00:00 2001 From: Danny Qiu Date: Fri, 27 Oct 2017 14:00:05 -0400 Subject: Change tray icon on mac theme change --- src/lib/Tray.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'src/lib/Tray.js') diff --git a/src/lib/Tray.js b/src/lib/Tray.js index 525ce592e..2efe71a71 100644 --- a/src/lib/Tray.js +++ b/src/lib/Tray.js @@ -7,6 +7,8 @@ const INDICATOR_TRAY_UNREAD = 'tray-unread'; export default class TrayIcon { trayIcon = null; + indicator = 0; + themeChangeSubscriberId = null; show() { if (this.trayIcon) return; @@ -32,28 +34,43 @@ export default class TrayIcon { this.trayIcon.on('click', () => { app.mainWindow.show(); }); + + if (process.platform === 'darwin') { + this.themeChangeSubscriberId = systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => { + this._refreshIcon(); + }); + } } hide() { - if (this.trayIcon) { - this.trayIcon.destroy(); - this.trayIcon = null; + if (!this.trayIcon) return; + + this.trayIcon.destroy(); + this.trayIcon = null; + + if (process.platform === 'darwin' && this.themeChangeSubscriberId) { + systemPreferences.unsubscribeNotification(this.themeChangeSubscriberId); + this.themeChangeSubscriberId = null; } } setIndicator(indicator) { + this.indicator = indicator; + this._refreshIcon(); + } + + _refreshIcon() { if (!this.trayIcon) return; - this.trayIcon.setImage(this._getAsset('tray', indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN)); + this.trayIcon.setImage(this._getAsset('tray', this.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN)); if (process.platform === 'darwin') { this.trayIcon.setPressedImage( - this._getAsset('tray', `${indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`), + this._getAsset('tray', `${this.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`), ); } } - _getAsset(type, asset) { let platform = process.platform; -- cgit v1.2.3-54-g00ecf