summaryrefslogtreecommitdiffstats
path: root/src/lib/Tray.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Tray.js')
-rw-r--r--src/lib/Tray.js100
1 files changed, 68 insertions, 32 deletions
diff --git a/src/lib/Tray.js b/src/lib/Tray.js
index e7afc3552..63382483e 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,24 +29,27 @@ 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:
39 tray.mainWindow.isVisible() && tray.mainWindow.isFocused()
40 ? 'Hide Ferdi'
41 : 'Show Ferdi',
34 click() { 42 click() {
35 if (app.mainWindow.isMinimized()) { 43 tray._toggleWindow();
36 app.mainWindow.restore();
37 } else if (app.mainWindow.isVisible()) {
38 app.mainWindow.hide();
39 } else {
40 app.mainWindow.show();
41 app.mainWindow.focus();
42 }
43 }, 44 },
44 }, 45 },
45 { 46 {
46 label: 'Disable Notifications & Audio', 47 label: tray.isAppMuted
48 ? 'Enable Notifications && Audio'
49 : 'Disable Notifications && Audio',
47 click() { 50 click() {
48 app.mainWindow.webContents.send('muteApp'); 51 if (!tray.mainWindow) return;
52 tray.mainWindow.webContents.send('muteApp');
49 }, 53 },
50 }, 54 },
51 { 55 {
@@ -60,24 +64,44 @@ export default class TrayIcon {
60 ipcMain.on('initialAppSettings', (event, appSettings) => { 64 ipcMain.on('initialAppSettings', (event, appSettings) => {
61 this._updateTrayMenu(appSettings); 65 this._updateTrayMenu(appSettings);
62 }); 66 });
63
64 ipcMain.on('updateAppSettings', (event, appSettings) => { 67 ipcMain.on('updateAppSettings', (event, appSettings) => {
65 this._updateTrayMenu(appSettings); 68 this._updateTrayMenu(appSettings);
66 }); 69 });
70
71 this.mainWindow = BrowserWindow.getAllWindows()[0];
72
73 // listen to window events to be able to set correct string
74 // to tray menu ('Hide Ferdi' / 'Show Ferdi')
75 this.mainWindow.on('hide', () => {
76 this._updateTrayMenu(null);
77 });
78 this.mainWindow.on('restore', () => {
79 this._updateTrayMenu(null);
80 });
81 this.mainWindow.on('minimize', () => {
82 this._updateTrayMenu(null);
83 });
84 this.mainWindow.on('show', () => {
85 this._updateTrayMenu(null);
86 });
87 this.mainWindow.on('focus', () => {
88 this._updateTrayMenu(null);
89 });
90 this.mainWindow.on('blur', () => {
91 this._updateTrayMenu(null);
92 });
67 } 93 }
68 94
69 _updateTrayMenu(appSettings) { 95 _updateTrayMenu(appSettings) {
70 if (!this.trayIcon) return; 96 if (!this.trayIcon) return;
71 97
72 if (appSettings.type === 'app') { 98 if (appSettings && appSettings.type === 'app') {
73 const { isAppMuted } = appSettings.data; 99 this.isAppMuted = appSettings.data.isAppMuted; // save current state after a change
74 this.trayMenuTemplate[1].label = isAppMuted 100 }
75 ? 'Enable Notifications && Audio' 101
76 : 'Disable Notifications && Audio'; 102 this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate(this));
77 this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); 103 if (isLinux) {
78 if (isLinux) { 104 this.trayIcon.setContextMenu(this.trayMenu);
79 this.trayIcon.setContextMenu(this.trayMenu);
80 }
81 } 105 }
82 } 106 }
83 107
@@ -90,23 +114,15 @@ export default class TrayIcon {
90 if (this.trayIcon) return; 114 if (this.trayIcon) return;
91 115
92 this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN)); 116 this.trayIcon = new Tray(this._getAsset('tray', INDICATOR_TRAY_PLAIN));
93
94 this.trayIcon.setToolTip('Ferdi'); 117 this.trayIcon.setToolTip('Ferdi');
95 118
96 this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate); 119 this.trayMenu = Menu.buildFromTemplate(this.trayMenuTemplate(this));
97 if (isLinux) { 120 if (isLinux) {
98 this.trayIcon.setContextMenu(this.trayMenu); 121 this.trayIcon.setContextMenu(this.trayMenu);
99 } 122 }
100 123
101 this.trayIcon.on('click', () => { 124 this.trayIcon.on('click', () => {
102 if (app.mainWindow.isMinimized()) { 125 this._toggleWindow();
103 app.mainWindow.restore();
104 } else if (app.mainWindow.isVisible()) {
105 app.mainWindow.hide();
106 } else {
107 app.mainWindow.show();
108 app.mainWindow.focus();
109 }
110 }); 126 });
111 127
112 if (isMac || isWindows) { 128 if (isMac || isWindows) {
@@ -125,6 +141,26 @@ export default class TrayIcon {
125 } 141 }
126 } 142 }
127 143
144 _toggleWindow() {
145 const mainWindow = BrowserWindow.getAllWindows()[0];
146 if (!mainWindow) return;
147
148 if (mainWindow.isMinimized()) {
149 mainWindow.restore();
150 } else if (mainWindow.isVisible() && mainWindow.isFocused()) {
151 if (isMac && mainWindow.isFullScreen()) {
152 mainWindow.once('show', () => mainWindow?.setFullScreen(true));
153 mainWindow.once('leave-full-screen', () => mainWindow?.hide());
154 mainWindow.setFullScreen(false);
155 } else {
156 mainWindow.hide();
157 }
158 } else {
159 mainWindow.show();
160 mainWindow.focus();
161 }
162 }
163
128 hide() { 164 hide() {
129 this.visible = false; 165 this.visible = false;
130 this._hide(); 166 this._hide();