Skip to content

Commit

Permalink
fix(Tray): macOS: Make tray icon color dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
adlk committed Dec 7, 2021
1 parent 78ac70a commit a16b5fe
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15.14.0
16.13.0
Binary file removed src/assets/images/tray/darwin/tray-active.png
Binary file not shown.
Binary file removed src/assets/images/tray/darwin/tray-active@2x.png
Binary file not shown.
Binary file removed src/assets/images/tray/darwin/tray-unread-active.png
Binary file not shown.
Binary file not shown.
Binary file modified src/assets/images/tray/darwin/tray-unread.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/assets/images/tray/darwin/tray-unread@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/assets/images/tray/darwin/tray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/assets/images/tray/darwin/tray@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 17 additions & 8 deletions src/lib/Tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import path from 'path';
import macosVersion from 'macos-version';
import { isMac } from '../environment';

const debug = require('debug')('Franz:Tray');

const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png';
const INDICATOR_TRAY_PLAIN = 'tray';
const INDICATOR_TRAY_UNREAD = 'tray-unread';
Expand Down Expand Up @@ -51,6 +53,11 @@ export default class TrayIcon {

if (process.platform === 'darwin') {
this.themeChangeSubscriberId = systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => {
debug('Subscribe to theme change');
this._refreshIcon();
});
this.themeChangeSubscriberId = systemPreferences.subscribeNotification('AppleAquaColorVariantChanged', () => {
debug('Subscribe to theme change');
this._refreshIcon();
});
}
Expand All @@ -77,12 +84,6 @@ export default class TrayIcon {
if (!this.trayIcon) return;

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', `${this.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`),
);
}
}

_getAsset(type, asset) {
Expand All @@ -92,8 +93,16 @@ export default class TrayIcon {
platform = `${platform}-dark`;
}

return nativeImage.createFromPath(path.join(
const imagePath = path.join(
__dirname, '..', 'assets', 'images', type, platform, `${asset}.${FILE_EXTENSION}`,
));
);

const image = nativeImage.createFromPath(imagePath);

if (isMac) {
image.setTemplateImage(true);
}

return image;
}
}

0 comments on commit a16b5fe

Please sign in to comment.