aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/Tray.js
diff options
context:
space:
mode:
authorLibravatar Stefan Malzner <stefan@adlk.io>2017-10-25 11:52:30 +0200
committerLibravatar Stefan Malzner <stefan@adlk.io>2017-10-25 11:52:30 +0200
commitca74c830e3fda49ec8dffbc68af069621865e228 (patch)
tree4dbe6c763fea423dc743eb3220754cd2be8f003f /src/lib/Tray.js
parentre-reset environment variables (diff)
parentMerge pull request #108 from meetfranz/develop (diff)
downloadferdium-app-ca74c830e3fda49ec8dffbc68af069621865e228.tar.gz
ferdium-app-ca74c830e3fda49ec8dffbc68af069621865e228.tar.zst
ferdium-app-ca74c830e3fda49ec8dffbc68af069621865e228.zip
Merge branch 'master' into chore/travis-setup
Diffstat (limited to 'src/lib/Tray.js')
-rw-r--r--src/lib/Tray.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/lib/Tray.js b/src/lib/Tray.js
new file mode 100644
index 000000000..67150971e
--- /dev/null
+++ b/src/lib/Tray.js
@@ -0,0 +1,73 @@
1import { app, Tray, Menu, systemPreferences } from 'electron';
2import path from 'path';
3
4const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png';
5const INDICATOR_TRAY_PLAIN = 'tray';
6const INDICATOR_TRAY_UNREAD = 'tray-unread';
7
8export default class TrayIcon {
9 mainWindow = null;
10 trayIcon = null;
11
12 constructor(mainWindow) {
13 this.mainWindow = mainWindow;
14 }
15
16 show() {
17 if (this.trayIcon) return;
18
19 this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN));
20 const trayMenuTemplate = [
21 {
22 label: 'Show Franz',
23 click() {
24 this.mainWindow.show();
25 },
26 }, {
27 label: 'Quit Franz',
28 click() {
29 app.quit();
30 },
31 },
32 ];
33
34 const trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
35 this.trayIcon.setContextMenu(trayMenu);
36
37 this.trayIcon.on('click', () => {
38 this.mainWindow.show();
39 });
40 }
41
42 hide() {
43 if (this.trayIcon) {
44 this.trayIcon.destroy();
45 this.trayIcon = null;
46 }
47 }
48
49 setIndicator(indicator) {
50 if (!this.trayIcon) return;
51
52 this.trayIcon.setImage(this._getAsset('tray', indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN));
53
54 if (process.platform === 'darwin') {
55 this.trayIcon.setPressedImage(
56 this._getAsset('tray', `${indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`),
57 );
58 }
59 }
60
61
62 _getAsset(type, asset) {
63 let platform = process.platform;
64
65 if (platform === 'darwin' && systemPreferences.isDarkMode()) {
66 platform = `${platform}-dark`;
67 }
68
69 return path.join(
70 __dirname, '..', 'assets', 'images', type, platform, `${asset}.${FILE_EXTENSION}`,
71 );
72 }
73}