diff options
author | 2017-10-19 11:15:25 +0200 | |
---|---|---|
committer | 2017-10-19 11:19:50 +0200 | |
commit | ef503a1e29a540c7318efb5f2018efbf00706198 (patch) | |
tree | 05930a5d43f45c0e769c9ba256ba82f00d85b6ac /src/lib/Tray.js | |
parent | remove unnecessary platform check (diff) | |
download | ferdium-app-ef503a1e29a540c7318efb5f2018efbf00706198.tar.gz ferdium-app-ef503a1e29a540c7318efb5f2018efbf00706198.tar.zst ferdium-app-ef503a1e29a540c7318efb5f2018efbf00706198.zip |
Add option to disable system tray icon
Diffstat (limited to 'src/lib/Tray.js')
-rw-r--r-- | src/lib/Tray.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/lib/Tray.js b/src/lib/Tray.js new file mode 100644 index 000000000..631342b24 --- /dev/null +++ b/src/lib/Tray.js | |||
@@ -0,0 +1,64 @@ | |||
1 | import { app, Tray, Menu } from 'electron'; | ||
2 | import path from 'path'; | ||
3 | |||
4 | const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png'; | ||
5 | const INDICATOR_TRAY_PLAIN = 'tray'; | ||
6 | const INDICATOR_TRAY_UNREAD = 'tray-unread'; | ||
7 | |||
8 | function getAsset(type, asset) { | ||
9 | return path.join( | ||
10 | __dirname, '..', 'assets', 'images', type, process.platform, `${asset}.${FILE_EXTENSION}`, | ||
11 | ); | ||
12 | } | ||
13 | |||
14 | export default class TrayIcon { | ||
15 | mainWindow = null; | ||
16 | trayIcon = null; | ||
17 | |||
18 | constructor(mainWindow) { | ||
19 | this.mainWindow = mainWindow; | ||
20 | } | ||
21 | |||
22 | show() { | ||
23 | this.trayIcon = new Tray(getAsset('tray', INDICATOR_TRAY_PLAIN)); | ||
24 | const trayMenuTemplate = [ | ||
25 | { | ||
26 | label: 'Show Franz', | ||
27 | click() { | ||
28 | this.mainWindow.show(); | ||
29 | }, | ||
30 | }, { | ||
31 | label: 'Quit Franz', | ||
32 | click() { | ||
33 | app.quit(); | ||
34 | }, | ||
35 | }, | ||
36 | ]; | ||
37 | |||
38 | const trayMenu = Menu.buildFromTemplate(trayMenuTemplate); | ||
39 | this.trayIcon.setContextMenu(trayMenu); | ||
40 | |||
41 | this.trayIcon.on('click', () => { | ||
42 | this.mainWindow.show(); | ||
43 | }); | ||
44 | } | ||
45 | |||
46 | hide() { | ||
47 | if (this.trayIcon) { | ||
48 | this.trayIcon.destroy(); | ||
49 | this.trayIcon = null; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | setIndicator(indicator) { | ||
54 | if (!this.trayIcon) return; | ||
55 | |||
56 | this.trayIcon.setImage(getAsset('tray', indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN)); | ||
57 | |||
58 | if (process.platform === 'darwin') { | ||
59 | this.trayIcon.setPressedImage( | ||
60 | getAsset('tray', `${indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`), | ||
61 | ); | ||
62 | } | ||
63 | } | ||
64 | } | ||