aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/Menu.js1
-rw-r--r--src/lib/Tray.js73
2 files changed, 74 insertions, 0 deletions
diff --git a/src/lib/Menu.js b/src/lib/Menu.js
index 9f23c4d70..a6cde4d36 100644
--- a/src/lib/Menu.js
+++ b/src/lib/Menu.js
@@ -49,6 +49,7 @@ const template = [
49 }, 49 },
50 { 50 {
51 role: 'zoomin', 51 role: 'zoomin',
52 accelerator: 'CommandOrControl+=',
52 }, 53 },
53 { 54 {
54 role: 'zoomout', 55 role: 'zoomout',
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}