aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/ipc-api/appIndicator.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/electron/ipc-api/appIndicator.js')
-rw-r--r--src/electron/ipc-api/appIndicator.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/electron/ipc-api/appIndicator.js b/src/electron/ipc-api/appIndicator.js
new file mode 100644
index 000000000..576234d25
--- /dev/null
+++ b/src/electron/ipc-api/appIndicator.js
@@ -0,0 +1,80 @@
1import { app, ipcMain, Tray, Menu } from 'electron';
2import path from 'path';
3
4const INDICATOR_TRAY_PLAIN = 'tray';
5const INDICATOR_TRAY_UNREAD = 'tray-unread';
6const INDICATOR_TASKBAR = 'taskbar';
7
8const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png';
9let trayIcon;
10
11function getAsset(type, asset) {
12 return path.join(
13 __dirname, '..', '..', 'assets', 'images', type, process.platform, `${asset}.${FILE_EXTENSION}`,
14 );
15}
16
17export default (params) => {
18 trayIcon = new Tray(getAsset('tray', INDICATOR_TRAY_PLAIN));
19 const trayMenuTemplate = [
20 {
21 label: 'Show Franz',
22 click() {
23 params.mainWindow.show();
24 },
25 }, {
26 label: 'Quit Franz',
27 click() {
28 app.quit();
29 },
30 },
31 ];
32
33 const trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
34 trayIcon.setContextMenu(trayMenu);
35
36 trayIcon.on('click', () => {
37 params.mainWindow.show();
38 });
39
40 ipcMain.on('updateAppIndicator', (event, args) => {
41 // Update badge
42 if (process.platform === 'darwin'
43 && typeof (args.indicator) === 'string') {
44 app.dock.setBadge(args.indicator);
45 }
46
47 if ((process.platform === 'darwin'
48 || process.platform === 'linux')
49 && typeof (args.indicator) === 'number'
50 ) {
51 app.setBadgeCount(args.indicator);
52 }
53
54 if (process.platform === 'win32') {
55 if (typeof args.indicator === 'number'
56 && args.indicator !== 0) {
57 params.mainWindow.setOverlayIcon(
58 getAsset('taskbar', `${INDICATOR_TASKBAR}-${(args.indicator >= 10 ? 10 : args.indicator)}`),
59 '',
60 );
61 } else if (typeof args.indicator === 'string') {
62 params.mainWindow.setOverlayIcon(
63 getAsset('taskbar', `${INDICATOR_TASKBAR}-alert`),
64 '',
65 );
66 } else {
67 params.mainWindow.setOverlayIcon(null, '');
68 }
69 }
70
71 // Update system tray
72 trayIcon.setImage(getAsset('tray', args.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN));
73
74 if (process.platform === 'darwin') {
75 trayIcon.setPressedImage(
76 getAsset('tray', `${args.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`),
77 );
78 }
79 });
80};