aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/Tray.js
diff options
context:
space:
mode:
authorLibravatar Stefan Malzner <stefan@adlk.io>2017-10-25 09:52:33 +0200
committerLibravatar GitHub <noreply@github.com>2017-10-25 09:52:33 +0200
commiteaf3c8f5d1deee878b0da87df8a083143115994f (patch)
tree4ce5f5dff3791a41463e711f683fe5b7129550e8 /src/lib/Tray.js
parentMerge pull request #58 from Blizzke/nl-BE (diff)
parentBump version to 5.0.0-beta.11 (diff)
downloadferdium-app-eaf3c8f5d1deee878b0da87df8a083143115994f.tar.gz
ferdium-app-eaf3c8f5d1deee878b0da87df8a083143115994f.tar.zst
ferdium-app-eaf3c8f5d1deee878b0da87df8a083143115994f.zip
Merge pull request #108 from meetfranz/develop
Franz 5.0.0-beta.11
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}