diff options
Diffstat (limited to 'src/electron/ipc-api/appIndicator.ts')
-rw-r--r-- | src/electron/ipc-api/appIndicator.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/electron/ipc-api/appIndicator.ts b/src/electron/ipc-api/appIndicator.ts new file mode 100644 index 000000000..a51ed8161 --- /dev/null +++ b/src/electron/ipc-api/appIndicator.ts | |||
@@ -0,0 +1,81 @@ | |||
1 | import { app, ipcMain } from 'electron'; | ||
2 | import { join } from 'path'; | ||
3 | import { autorun } from 'mobx'; | ||
4 | import { isMac, isWindows, isLinux } from '../../environment'; | ||
5 | |||
6 | const INDICATOR_TASKBAR = 'taskbar'; | ||
7 | const FILE_EXTENSION = isWindows ? 'ico' : 'png'; | ||
8 | |||
9 | let isTrayIconEnabled: boolean; | ||
10 | |||
11 | function getAsset(type: 'tray' | 'taskbar', asset: string) { | ||
12 | return join( | ||
13 | __dirname, | ||
14 | '..', | ||
15 | '..', | ||
16 | 'assets', | ||
17 | 'images', | ||
18 | type, | ||
19 | process.platform, | ||
20 | `${asset}.${FILE_EXTENSION}`, | ||
21 | ); | ||
22 | } | ||
23 | |||
24 | export default params => { | ||
25 | autorun(() => { | ||
26 | isTrayIconEnabled = params.settings.app.get('enableSystemTray'); | ||
27 | |||
28 | if (!isTrayIconEnabled) { | ||
29 | params.trayIcon.hide(); | ||
30 | } else if (isTrayIconEnabled) { | ||
31 | params.trayIcon.show(); | ||
32 | } | ||
33 | }); | ||
34 | |||
35 | ipcMain.on('updateAppIndicator', (_event, args) => { | ||
36 | // Flash TaskBar for windows, bounce Dock on Mac | ||
37 | if (!(app as any).mainWindow.isFocused() && params.settings.app.get('notifyTaskBarOnMessage')) { | ||
38 | if (isWindows) { | ||
39 | (app as any).mainWindow.flashFrame(true); | ||
40 | (app as any).mainWindow.once('focus', () => | ||
41 | (app as any).mainWindow.flashFrame(false), | ||
42 | ); | ||
43 | } else if (isMac) { | ||
44 | app.dock.bounce('informational'); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | // Update badge | ||
49 | if (isMac && typeof args.indicator === 'string') { | ||
50 | app.dock.setBadge(args.indicator); | ||
51 | } | ||
52 | |||
53 | if ((isMac || isLinux) && typeof args.indicator === 'number') { | ||
54 | app.badgeCount = args.indicator; | ||
55 | } | ||
56 | |||
57 | if (isWindows) { | ||
58 | if (typeof args.indicator === 'number' && args.indicator !== 0) { | ||
59 | params.mainWindow.setOverlayIcon( | ||
60 | getAsset( | ||
61 | 'taskbar', | ||
62 | `${INDICATOR_TASKBAR}-${ | ||
63 | args.indicator >= 10 ? 10 : args.indicator | ||
64 | }`, | ||
65 | ), | ||
66 | '', | ||
67 | ); | ||
68 | } else if (typeof args.indicator === 'string') { | ||
69 | params.mainWindow.setOverlayIcon( | ||
70 | getAsset('taskbar', `${INDICATOR_TASKBAR}-alert`), | ||
71 | '', | ||
72 | ); | ||
73 | } else { | ||
74 | params.mainWindow.setOverlayIcon(null, ''); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | // Update Tray | ||
79 | params.trayIcon.setIndicator(args.indicator); | ||
80 | }); | ||
81 | }; | ||