Skip to content

Commit

Permalink
Merge pull request #155 from dannyqiu/fix-theme-change
Browse files Browse the repository at this point in the history
Change tray icon on mac theme change
  • Loading branch information
adlk authored Oct 30, 2017
2 parents 099a0bb + 099e018 commit e6ed06b
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/lib/Tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit e6ed06b

Please sign in to comment.