aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/Tray.js
diff options
context:
space:
mode:
authorLibravatar Martin BernĂ¡t <martin.bernat@gmail.com>2021-11-16 00:38:29 +0100
committerLibravatar GitHub <noreply@github.com>2021-11-16 05:08:29 +0530
commit67952acdfa066bbb4b1aebd923245815b962a4cf (patch)
tree449e7e5931159fded75c9370fbf41fb00908a2bd /src/lib/Tray.js
parent5.6.4-nightly.6 [skip ci] (diff)
downloadferdium-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
Diffstat (limited to 'src/lib/Tray.js')
-rw-r--r--src/lib/Tray.js98
1 files changed, 57 insertions, 41 deletions
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';
10import { join } from 'path'; 11import { join } from 'path';
11import macosVersion from 'macos-version'; 12import 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();