From 149887221490f8f1da9e7932b58abc72baa736ad Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 29 Oct 2019 11:37:42 +0000 Subject: [PATCH 1/4] Add ability to hide tray icon on non-Mac (which has no tray icon) --- electron_app/src/electron-main.js | 34 +++++++++++++++++-------- electron_app/src/tray.js | 7 +++++ src/vector/platform/ElectronPlatform.js | 13 ++++++++++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index f061acd5..828a8f38 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -86,8 +86,16 @@ const store = new Store({ name: "electron-config" }); let mainWindow = null; global.appQuitting = false; -global.minimizeToTray = store.get('minimizeToTray', true); +global.showTrayIcon = store.get("showTrayIcon", true); +global.minimizeToTray = global.showTrayIcon && store.get('minimizeToTray', true); +// It's important to call `path.join` so we don't end up with the packaged asar in the final path. +const iconFile = `riot.${process.platform === 'win32' ? 'ico' : 'png'}`; +const iconPath = path.join(__dirname, "..", "img", iconFile); +const trayConfig = { + icon_path: iconPath, + brand: vectorConfig.brand || 'Riot', +}; // handle uncaught errors otherwise it displays // stack traces in popup dialogs, which is terrible (which @@ -166,6 +174,20 @@ ipcMain.on('ipcCall', async function(ev, payload) { launcher.disable(); } break; + case 'getTrayIconEnabled': + ret = global.showTrayIcon; + break; + case 'setTrayIconEnabled': + if (args[0]) { + // Create trayIcon icon + tray.create(trayConfig); + } else { + tray.destroy(); + } + store.set('minimizeToTray', global.showTrayIcon = args[0]); + // re-evaluate whether we should be minimizing to tray + global.minimizeToTray = global.showTrayIcon && store.get('minimizeToTray', true); + break; case 'getMinimizeToTrayEnabled': ret = global.minimizeToTray; break; @@ -329,11 +351,6 @@ app.on('ready', () => { console.log('No update_base_url is defined: auto update is disabled'); } - // It's important to call `path.join` so we don't end up with the packaged - // asar in the final path. - const iconFile = `riot.${process.platform === 'win32' ? 'ico' : 'png'}`; - const iconPath = path.join(__dirname, "..", "..", "img", iconFile); - // Load the previous window state with fallback to defaults const mainWindowState = windowStateKeeper({ defaultWidth: 1024, @@ -371,10 +388,7 @@ app.on('ready', () => { mainWindow.hide(); // Create trayIcon icon - tray.create({ - icon_path: iconPath, - brand: vectorConfig.brand || 'Riot', - }); + if (global.showTrayIcon) tray.create(trayConfig); mainWindow.once('ready-to-show', () => { mainWindowState.manage(mainWindow); diff --git a/electron_app/src/tray.js b/electron_app/src/tray.js index 61e05972..04aaa1f1 100644 --- a/electron_app/src/tray.js +++ b/electron_app/src/tray.js @@ -26,6 +26,13 @@ exports.hasTray = function hasTray() { return (trayIcon !== null); }; +exports.destroy = function() { + if (trayIcon) { + trayIcon.destroy(); + trayIcon = null; + } +}; + exports.create = function(config) { // no trays on darwin if (process.platform === 'darwin' || trayIcon) return; diff --git a/src/vector/platform/ElectronPlatform.js b/src/vector/platform/ElectronPlatform.js index 8b01f864..7ee7d93f 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -211,6 +211,19 @@ export default class ElectronPlatform extends VectorBasePlatform { return this._ipcCall('setAutoHideMenuBarEnabled', enabled); } + supportsTrayIcon(): boolean { + // Things other than Mac support tray icons + return !navigator.platform.toUpperCase().includes('MAC'); + } + + async getTrayIconEnabled(): boolean { + return this._ipcCall('getTrayIconEnabled'); + } + + async setTrayIconEnabled(enabled: boolean): void { + return this._ipcCall('setTrayIconEnabled', enabled); + } + supportsMinimizeToTray(): boolean { return true; } From 4b0fa940bbaac66e1607fb17b38b58daa8e3ac96 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 29 Oct 2019 12:00:40 +0000 Subject: [PATCH 2/4] revert icon path change --- electron_app/src/electron-main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index 828a8f38..95f49e40 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -91,7 +91,7 @@ global.minimizeToTray = global.showTrayIcon && store.get('minimizeToTray', true) // It's important to call `path.join` so we don't end up with the packaged asar in the final path. const iconFile = `riot.${process.platform === 'win32' ? 'ico' : 'png'}`; -const iconPath = path.join(__dirname, "..", "img", iconFile); +const iconPath = path.join(__dirname, "..", "..", "img", iconFile); const trayConfig = { icon_path: iconPath, brand: vectorConfig.brand || 'Riot', From 9820e59559103ff2cd0906a1f05dae89ae5735d0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 30 Oct 2019 14:32:28 +0000 Subject: [PATCH 3/4] Merge hide-to-tray-icon with show-tray-icon --- electron_app/src/electron-main.js | 23 +++++++---------------- src/vector/platform/ElectronPlatform.js | 14 +------------- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index 95f49e40..d86ddbfb 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -86,8 +86,6 @@ const store = new Store({ name: "electron-config" }); let mainWindow = null; global.appQuitting = false; -global.showTrayIcon = store.get("showTrayIcon", true); -global.minimizeToTray = global.showTrayIcon && store.get('minimizeToTray', true); // It's important to call `path.join` so we don't end up with the packaged asar in the final path. const iconFile = `riot.${process.platform === 'win32' ? 'ico' : 'png'}`; @@ -174,25 +172,17 @@ ipcMain.on('ipcCall', async function(ev, payload) { launcher.disable(); } break; - case 'getTrayIconEnabled': - ret = global.showTrayIcon; + case 'getMinimizeToTrayEnabled': + ret = tray.hasTray(); break; - case 'setTrayIconEnabled': + case 'setMinimizeToTrayEnabled': if (args[0]) { // Create trayIcon icon tray.create(trayConfig); } else { tray.destroy(); } - store.set('minimizeToTray', global.showTrayIcon = args[0]); - // re-evaluate whether we should be minimizing to tray - global.minimizeToTray = global.showTrayIcon && store.get('minimizeToTray', true); - break; - case 'getMinimizeToTrayEnabled': - ret = global.minimizeToTray; - break; - case 'setMinimizeToTrayEnabled': - store.set('minimizeToTray', global.minimizeToTray = args[0]); + store.set('minimizeToTray', args[0]); break; case 'getAutoHideMenuBarEnabled': ret = global.mainWindow.isMenuBarAutoHide(); @@ -388,7 +378,7 @@ app.on('ready', () => { mainWindow.hide(); // Create trayIcon icon - if (global.showTrayIcon) tray.create(trayConfig); + if (store.get('minimizeToTray', true)) tray.create(trayConfig); mainWindow.once('ready-to-show', () => { mainWindowState.manage(mainWindow); @@ -405,7 +395,8 @@ app.on('ready', () => { mainWindow = global.mainWindow = null; }); mainWindow.on('close', (e) => { - if (global.minimizeToTray && !global.appQuitting && (tray.hasTray() || process.platform === 'darwin')) { + // If we are not quitting and have a tray icon then minimize to tray + if (!global.appQuitting && tray.hasTray()) { // On Mac, closing the window just hides it // (this is generally how single-window Mac apps // behave, eg. Mail.app) diff --git a/src/vector/platform/ElectronPlatform.js b/src/vector/platform/ElectronPlatform.js index 7ee7d93f..dd4df4e6 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -211,23 +211,11 @@ export default class ElectronPlatform extends VectorBasePlatform { return this._ipcCall('setAutoHideMenuBarEnabled', enabled); } - supportsTrayIcon(): boolean { + supportsMinimizeToTray(): boolean { // Things other than Mac support tray icons return !navigator.platform.toUpperCase().includes('MAC'); } - async getTrayIconEnabled(): boolean { - return this._ipcCall('getTrayIconEnabled'); - } - - async setTrayIconEnabled(enabled: boolean): void { - return this._ipcCall('setTrayIconEnabled', enabled); - } - - supportsMinimizeToTray(): boolean { - return true; - } - async getMinimizeToTrayEnabled(): boolean { return this._ipcCall('getMinimizeToTrayEnabled'); } From cb5ef44d3f8f91bb081e5f41db4f538048dc03a0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 30 Oct 2019 14:33:20 +0000 Subject: [PATCH 4/4] Remove outdated workaround which on modern electron makes window not show --- electron_app/src/electron-main.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index d86ddbfb..089ee804 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -373,10 +373,6 @@ app.on('ready', () => { mainWindow.loadURL('vector://vector/webapp/'); Menu.setApplicationMenu(vectorMenu); - // explicitly hide because setApplicationMenu on Linux otherwise shows... - // https://github.com/electron/electron/issues/9621 - mainWindow.hide(); - // Create trayIcon icon if (store.get('minimizeToTray', true)) tray.create(trayConfig);