diff options
Diffstat (limited to 'src/lib/Tray.js')
-rw-r--r-- | src/lib/Tray.js | 69 |
1 files changed, 56 insertions, 13 deletions
diff --git a/src/lib/Tray.js b/src/lib/Tray.js index d6d49b0c8..c6d9db004 100644 --- a/src/lib/Tray.js +++ b/src/lib/Tray.js | |||
@@ -2,6 +2,11 @@ import { | |||
2 | app, Menu, nativeImage, nativeTheme, systemPreferences, Tray, ipcMain, | 2 | app, Menu, nativeImage, nativeTheme, systemPreferences, Tray, ipcMain, |
3 | } from 'electron'; | 3 | } from 'electron'; |
4 | import path from 'path'; | 4 | import path from 'path'; |
5 | import { | ||
6 | isMac, | ||
7 | isWindows, | ||
8 | isLinux, | ||
9 | } from '../environment'; | ||
5 | 10 | ||
6 | const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png'; | 11 | const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png'; |
7 | const INDICATOR_TRAY_PLAIN = 'tray'; | 12 | const INDICATOR_TRAY_PLAIN = 'tray'; |
@@ -17,15 +22,20 @@ export default class TrayIcon { | |||
17 | 22 | ||
18 | trayMenu = null; | 23 | trayMenu = null; |
19 | 24 | ||
25 | visible = false; | ||
26 | |||
20 | trayMenuTemplate = [ | 27 | trayMenuTemplate = [ |
21 | { | 28 | { |
22 | label: 'Show Ferdi', | 29 | label: 'Show Ferdi', |
23 | click() { | 30 | click() { |
24 | if (app.mainWindow.isMinimized()) { | 31 | if (app.mainWindow.isMinimized()) { |
25 | app.mainWindow.restore(); | 32 | app.mainWindow.restore(); |
33 | } else if (app.mainWindow.isVisible()) { | ||
34 | app.mainWindow.hide(); | ||
35 | } else { | ||
36 | app.mainWindow.show(); | ||
37 | app.mainWindow.focus(); | ||
26 | } | 38 | } |
27 | app.mainWindow.show(); | ||
28 | app.mainWindow.focus(); | ||
29 | }, | 39 | }, |
30 | }, | 40 | }, |
31 | { | 41 | { |
@@ -42,15 +52,35 @@ export default class TrayIcon { | |||
42 | }, | 52 | }, |
43 | ]; | 53 | ]; |
44 | 54 | ||
55 | constructor() { | ||
56 | ipcMain.on('initialAppSettings', (event, appSettings) => { | ||
57 | this._updateTrayMenu(appSettings); | ||
58 | }); | ||
59 | |||
60 | ipcMain.on('updateAppSettings', (event, appSettings) => { | ||
61 | this._updateTrayMenu(appSettings); | ||
62 | }); | ||
63 | } | ||
64 | |||
45 | _updateTrayMenu(appSettings) { | 65 | _updateTrayMenu(appSettings) { |
66 | if (!this.trayIcon) return; | ||
67 | |||
46 | if (appSettings.type === 'app') { | 68 | if (appSettings.type === 'app') { |
47 | const { isAppMuted } = appSettings.data; | 69 | const { isAppMuted } = appSettings.data; |
48 | this.trayMenuTemplate[1].label = isAppMuted ? 'Enable Notifications && Audio' : 'Disable Notifications && Audio'; | 70 | this.trayMenuTemplate[1].label = isAppMuted ? 'Enable Notifications && Audio' : 'Disable Notifications && Audio'; |
49 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); | 71 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); |
72 | if (isLinux) { | ||
73 | this.trayIcon.setContextMenu(this.trayMenu); | ||
74 | } | ||
50 | } | 75 | } |
51 | } | 76 | } |
52 | 77 | ||
53 | show() { | 78 | show() { |
79 | this.visible = true; | ||
80 | this._show(); | ||
81 | } | ||
82 | |||
83 | _show() { | ||
54 | if (this.trayIcon) return; | 84 | if (this.trayIcon) return; |
55 | 85 | ||
56 | this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN)); | 86 | this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN)); |
@@ -58,14 +88,9 @@ export default class TrayIcon { | |||
58 | this.trayIcon.setToolTip('Ferdi'); | 88 | this.trayIcon.setToolTip('Ferdi'); |
59 | 89 | ||
60 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); | 90 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); |
61 | 91 | if (isLinux) { | |
62 | ipcMain.on('initialAppSettings', (event, appSettings) => { | 92 | this.trayIcon.setContextMenu(this.trayMenu); |
63 | this._updateTrayMenu(appSettings); | 93 | } |
64 | }); | ||
65 | |||
66 | ipcMain.on('updateAppSettings', (event, appSettings) => { | ||
67 | this._updateTrayMenu(appSettings); | ||
68 | }); | ||
69 | 94 | ||
70 | this.trayIcon.on('click', () => { | 95 | this.trayIcon.on('click', () => { |
71 | if (app.mainWindow.isMinimized()) { | 96 | if (app.mainWindow.isMinimized()) { |
@@ -78,9 +103,11 @@ export default class TrayIcon { | |||
78 | } | 103 | } |
79 | }); | 104 | }); |
80 | 105 | ||
81 | this.trayIcon.on('right-click', () => { | 106 | if (isMac || isWindows) { |
82 | this.trayIcon.popUpContextMenu(this.trayMenu); | 107 | this.trayIcon.on('right-click', () => { |
83 | }); | 108 | this.trayIcon.popUpContextMenu(this.trayMenu); |
109 | }); | ||
110 | } | ||
84 | 111 | ||
85 | if (process.platform === 'darwin') { | 112 | if (process.platform === 'darwin') { |
86 | this.themeChangeSubscriberId = systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => { | 113 | this.themeChangeSubscriberId = systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => { |
@@ -90,6 +117,11 @@ export default class TrayIcon { | |||
90 | } | 117 | } |
91 | 118 | ||
92 | hide() { | 119 | hide() { |
120 | this.visible = false; | ||
121 | this._hide(); | ||
122 | } | ||
123 | |||
124 | _hide() { | ||
93 | if (!this.trayIcon) return; | 125 | if (!this.trayIcon) return; |
94 | 126 | ||
95 | this.trayIcon.destroy(); | 127 | this.trayIcon.destroy(); |
@@ -101,6 +133,17 @@ export default class TrayIcon { | |||
101 | } | 133 | } |
102 | } | 134 | } |
103 | 135 | ||
136 | recreateIfVisible() { | ||
137 | if (this.visible) { | ||
138 | this._hide(); | ||
139 | setTimeout(() => { | ||
140 | if (this.visible) { | ||
141 | this._show(); | ||
142 | } | ||
143 | }, 100); | ||
144 | } | ||
145 | } | ||
146 | |||
104 | setIndicator(indicator) { | 147 | setIndicator(indicator) { |
105 | this.indicator = indicator; | 148 | this.indicator = indicator; |
106 | this._refreshIcon(); | 149 | this._refreshIcon(); |