diff options
author | Martin BernĂ¡t <martin.bernat@gmail.com> | 2021-11-16 00:38:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-16 05:08:29 +0530 |
commit | 67952acdfa066bbb4b1aebd923245815b962a4cf (patch) | |
tree | 449e7e5931159fded75c9370fbf41fb00908a2bd | |
parent | 5.6.4-nightly.6 [skip ci] (diff) | |
download | ferdium-app-67952acdfa066bbb4b1aebd923245815b962a4cf.tar.gz ferdium-app-67952acdfa066bbb4b1aebd923245815b962a4cf.tar.zst ferdium-app-67952acdfa066bbb4b1aebd923245815b962a4cf.zip |
Fix tray icon not showing/hiding the Ferdi window (#2234)
* Implement toggle behavior also in tray menu on right mouse click
-rw-r--r-- | src/lib/Menu.js | 6 | ||||
-rw-r--r-- | src/lib/Tray.js | 98 | ||||
-rw-r--r-- | src/stores/AppStore.js | 5 |
3 files changed, 63 insertions, 46 deletions
diff --git a/src/lib/Menu.js b/src/lib/Menu.js index 7d4c228f5..892b2ff3c 100644 --- a/src/lib/Menu.js +++ b/src/lib/Menu.js | |||
@@ -1,5 +1,5 @@ | |||
1 | import { clipboard } from 'electron'; | 1 | import { clipboard } from 'electron'; |
2 | import { app, Menu, dialog, systemPreferences } from '@electron/remote'; | 2 | import { app, Menu, dialog, systemPreferences, getCurrentWindow } from '@electron/remote'; |
3 | import { autorun, observable } from 'mobx'; | 3 | import { autorun, observable } from 'mobx'; |
4 | import { defineMessages } from 'react-intl'; | 4 | import { defineMessages } from 'react-intl'; |
5 | import { | 5 | import { |
@@ -950,8 +950,8 @@ class FranzMenu { | |||
950 | click: () => { | 950 | click: () => { |
951 | this.actions.service.setActive({ serviceId: service.id }); | 951 | this.actions.service.setActive({ serviceId: service.id }); |
952 | 952 | ||
953 | if (isMac && i === 0) { | 953 | if (isMac && i === 0) { // feat(Mac): Open Window with Cmd+1 |
954 | app.mainWindow.restore(); | 954 | getCurrentWindow().restore(); |
955 | } | 955 | } |
956 | }, | 956 | }, |
957 | }); | 957 | }); |
diff --git a/src/lib/Tray.js b/src/lib/Tray.js index eb6732c30..b36f4de7e 100644 --- a/src/lib/Tray.js +++ b/src/lib/Tray.js | |||
@@ -6,6 +6,7 @@ import { | |||
6 | systemPreferences, | 6 | systemPreferences, |
7 | Tray, | 7 | Tray, |
8 | ipcMain, | 8 | ipcMain, |
9 | BrowserWindow, | ||
9 | } from 'electron'; | 10 | } from 'electron'; |
10 | import { join } from 'path'; | 11 | import { join } from 'path'; |
11 | import macosVersion from 'macos-version'; | 12 | import macosVersion from 'macos-version'; |
@@ -28,30 +29,22 @@ export default class TrayIcon { | |||
28 | 29 | ||
29 | visible = false; | 30 | visible = false; |
30 | 31 | ||
31 | trayMenuTemplate = [ | 32 | isAppMuted = false; |
33 | |||
34 | mainWindow = null; | ||
35 | |||
36 | trayMenuTemplate = (tray) => [ | ||
32 | { | 37 | { |
33 | label: 'Show Ferdi', | 38 | label: (tray.mainWindow.isVisible() && tray.mainWindow.isFocused()) ? 'Hide Ferdi' : 'Show Ferdi', |
34 | click() { | 39 | click() { |
35 | if (!app.mainWindow) { | 40 | tray._toggleWindow(); |
36 | return; | ||
37 | } | ||
38 | if (app.mainWindow.isMinimized()) { | ||
39 | app.mainWindow.restore(); | ||
40 | } else if (app.mainWindow.isVisible()) { | ||
41 | app.mainWindow.hide(); | ||
42 | } else { | ||
43 | app.mainWindow.show(); | ||
44 | app.mainWindow.focus(); | ||
45 | } | ||
46 | }, | 41 | }, |
47 | }, | 42 | }, |
48 | { | 43 | { |
49 | label: 'Disable Notifications & Audio', | 44 | label: tray.isAppMuted ? 'Enable Notifications && Audio' : 'Disable Notifications && Audio', |
50 | click() { | 45 | click() { |
51 | if (!app.mainWindow) { | 46 | if (!tray.mainWindow) return; |
52 | return; | 47 | tray.mainWindow.webContents.send('muteApp'); |
53 | } | ||
54 | app.mainWindow.webContents.send('muteApp'); | ||
55 | }, | 48 | }, |
56 | }, | 49 | }, |
57 | { | 50 | { |
@@ -66,24 +59,44 @@ export default class TrayIcon { | |||
66 | ipcMain.on('initialAppSettings', (event, appSettings) => { | 59 | ipcMain.on('initialAppSettings', (event, appSettings) => { |
67 | this._updateTrayMenu(appSettings); | 60 | this._updateTrayMenu(appSettings); |
68 | }); | 61 | }); |
69 | |||
70 | ipcMain.on('updateAppSettings', (event, appSettings) => { | 62 | ipcMain.on('updateAppSettings', (event, appSettings) => { |
71 | this._updateTrayMenu(appSettings); | 63 | this._updateTrayMenu(appSettings); |
72 | }); | 64 | }); |
65 | |||
66 | this.mainWindow = BrowserWindow.getAllWindows()[0]; | ||
67 | |||
68 | // listen to window events to be able to set correct string | ||
69 | // to tray menu ('Hide Ferdi' / 'Show Ferdi') | ||
70 | this.mainWindow.on('hide', () => { | ||
71 | this._updateTrayMenu(null); | ||
72 | }); | ||
73 | this.mainWindow.on('restore', () => { | ||
74 | this._updateTrayMenu(null); | ||
75 | }); | ||
76 | this.mainWindow.on('minimize', () => { | ||
77 | this._updateTrayMenu(null); | ||
78 | }); | ||
79 | this.mainWindow.on('show', () => { | ||
80 | this._updateTrayMenu(null); | ||
81 | }); | ||
82 | this.mainWindow.on('focus', () => { | ||
83 | this._updateTrayMenu(null); | ||
84 | }); | ||
85 | this.mainWindow.on('blur', () => { | ||
86 | this._updateTrayMenu(null); | ||
87 | }); | ||
73 | } | 88 | } |
74 | 89 | ||
75 | _updateTrayMenu(appSettings) { | 90 | _updateTrayMenu(appSettings) { |
76 | if (!this.trayIcon) return; | 91 | if (!this.trayIcon) return; |
77 | 92 | ||
78 | if (appSettings.type === 'app') { | 93 | if (appSettings && appSettings.type === 'app') { |
79 | const { isAppMuted } = appSettings.data; | 94 | this.isAppMuted = appSettings.data.isAppMuted; // save current state after a change |
80 | this.trayMenuTemplate[1].label = isAppMuted | 95 | } |
81 | ? 'Enable Notifications && Audio' | 96 | |
82 | : 'Disable Notifications && Audio'; | 97 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate(this)); |
83 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); | 98 | if (isLinux) { |
84 | if (isLinux) { | 99 | this.trayIcon.setContextMenu(this.trayMenu); |
85 | this.trayIcon.setContextMenu(this.trayMenu); | ||
86 | } | ||
87 | } | 100 | } |
88 | } | 101 | } |
89 | 102 | ||
@@ -96,26 +109,15 @@ export default class TrayIcon { | |||
96 | if (this.trayIcon) return; | 109 | if (this.trayIcon) return; |
97 | 110 | ||
98 | this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN)); | 111 | this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN)); |
99 | |||
100 | this.trayIcon.setToolTip('Ferdi'); | 112 | this.trayIcon.setToolTip('Ferdi'); |
101 | 113 | ||
102 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); | 114 | this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate(this)); |
103 | if (isLinux) { | 115 | if (isLinux) { |
104 | this.trayIcon.setContextMenu(this.trayMenu); | 116 | this.trayIcon.setContextMenu(this.trayMenu); |
105 | } | 117 | } |
106 | 118 | ||
107 | this.trayIcon.on('click', () => { | 119 | this.trayIcon.on('click', () => { |
108 | if (!app.mainWindow) { | 120 | this._toggleWindow(); |
109 | return; | ||
110 | } | ||
111 | if (app.mainWindow.isMinimized()) { | ||
112 | app.mainWindow.restore(); | ||
113 | } else if (app.mainWindow.isVisible()) { | ||
114 | app.mainWindow.hide(); | ||
115 | } else { | ||
116 | app.mainWindow.show(); | ||
117 | app.mainWindow.focus(); | ||
118 | } | ||
119 | }); | 121 | }); |
120 | 122 | ||
121 | if (isMac || isWindows) { | 123 | if (isMac || isWindows) { |
@@ -134,6 +136,20 @@ export default class TrayIcon { | |||
134 | } | 136 | } |
135 | } | 137 | } |
136 | 138 | ||
139 | _toggleWindow() { | ||
140 | const mainWindow = BrowserWindow.getAllWindows()[0]; | ||
141 | if (!mainWindow) return; | ||
142 | |||
143 | if (mainWindow.isMinimized()) { | ||
144 | mainWindow.restore(); | ||
145 | } else if (mainWindow.isVisible() && mainWindow.isFocused()) { | ||
146 | mainWindow.hide(); | ||
147 | } else { | ||
148 | mainWindow.show(); | ||
149 | mainWindow.focus(); | ||
150 | } | ||
151 | } | ||
152 | |||
137 | hide() { | 153 | hide() { |
138 | this.visible = false; | 154 | this.visible = false; |
139 | this._hide(); | 155 | this._hide(); |
diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index 81cef3775..d652276ea 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js | |||
@@ -322,10 +322,11 @@ export default class AppStore extends Store { | |||
322 | this.actions.service.setActive({ | 322 | this.actions.service.setActive({ |
323 | serviceId, | 323 | serviceId, |
324 | }); | 324 | }); |
325 | if (!app.mainWindow.isVisible()) { | 325 | |
326 | if (!mainWindow.isVisible()) { | ||
326 | mainWindow.show(); | 327 | mainWindow.show(); |
327 | } | 328 | } |
328 | if (app.mainWindow.isMinimized()) { | 329 | if (mainWindow.isMinimized()) { |
329 | mainWindow.restore(); | 330 | mainWindow.restore(); |
330 | } | 331 | } |
331 | mainWindow.focus(); | 332 | mainWindow.focus(); |