diff --git a/electron_app/src/electron-main.js b/electron_app/src/electron-main.js index ddc40c2c1a..48a425c917 100644 --- a/electron_app/src/electron-main.js +++ b/electron_app/src/electron-main.js @@ -86,8 +86,14 @@ const store = new Store({ name: "electron-config" }); let mainWindow = null; global.appQuitting = false; -global.minimizeToTray = 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 @@ -168,10 +174,16 @@ ipcMain.on('ipcCall', async function(ev, payload) { } break; case 'getMinimizeToTrayEnabled': - ret = global.minimizeToTray; + ret = tray.hasTray(); break; case 'setMinimizeToTrayEnabled': - store.set('minimizeToTray', global.minimizeToTray = args[0]); + if (args[0]) { + // Create trayIcon icon + tray.create(trayConfig); + } else { + tray.destroy(); + } + store.set('minimizeToTray', args[0]); break; case 'getAutoHideMenuBarEnabled': ret = global.mainWindow.isMenuBarAutoHide(); @@ -330,11 +342,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, @@ -367,15 +374,8 @@ 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 - tray.create({ - icon_path: iconPath, - brand: vectorConfig.brand || 'Riot', - }); + if (store.get('minimizeToTray', true)) tray.create(trayConfig); mainWindow.once('ready-to-show', () => { mainWindowState.manage(mainWindow); @@ -392,7 +392,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/electron_app/src/tray.js b/electron_app/src/tray.js index 61e059723d..04aaa1f179 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 8b01f86417..dd4df4e63d 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -212,7 +212,8 @@ export default class ElectronPlatform extends VectorBasePlatform { } supportsMinimizeToTray(): boolean { - return true; + // Things other than Mac support tray icons + return !navigator.platform.toUpperCase().includes('MAC'); } async getMinimizeToTrayEnabled(): boolean {