Skip to content

Commit eab6ffe

Browse files
authored
Merge pull request element-hq#11258 from vector-im/t3chguy/hide_tray_icon
Add ability to hide tray icon on non-Mac (which has no tray icon)
2 parents 88339fc + cb5ef44 commit eab6ffe

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

‎electron_app/src/electron-main.js‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,14 @@ const store = new Store({ name: "electron-config" });
8686

8787
let mainWindow = null;
8888
global.appQuitting = false;
89-
global.minimizeToTray = store.get('minimizeToTray', true);
9089

90+
// It's important to call `path.join` so we don't end up with the packaged asar in the final path.
91+
const iconFile = `riot.${process.platform === 'win32' ? 'ico' : 'png'}`;
92+
const iconPath = path.join(__dirname, "..", "..", "img", iconFile);
93+
const trayConfig = {
94+
icon_path: iconPath,
95+
brand: vectorConfig.brand || 'Riot',
96+
};
9197

9298
// handle uncaught errors otherwise it displays
9399
// stack traces in popup dialogs, which is terrible (which
@@ -168,10 +174,16 @@ ipcMain.on('ipcCall', async function(ev, payload) {
168174
}
169175
break;
170176
case 'getMinimizeToTrayEnabled':
171-
ret = global.minimizeToTray;
177+
ret = tray.hasTray();
172178
break;
173179
case 'setMinimizeToTrayEnabled':
174-
store.set('minimizeToTray', global.minimizeToTray = args[0]);
180+
if (args[0]) {
181+
// Create trayIcon icon
182+
tray.create(trayConfig);
183+
} else {
184+
tray.destroy();
185+
}
186+
store.set('minimizeToTray', args[0]);
175187
break;
176188
case 'getAutoHideMenuBarEnabled':
177189
ret = global.mainWindow.isMenuBarAutoHide();
@@ -330,11 +342,6 @@ app.on('ready', () => {
330342
console.log('No update_base_url is defined: auto update is disabled');
331343
}
332344

333-
// It's important to call `path.join` so we don't end up with the packaged
334-
// asar in the final path.
335-
const iconFile = `riot.${process.platform === 'win32' ? 'ico' : 'png'}`;
336-
const iconPath = path.join(__dirname, "..", "..", "img", iconFile);
337-
338345
// Load the previous window state with fallback to defaults
339346
const mainWindowState = windowStateKeeper({
340347
defaultWidth: 1024,
@@ -367,15 +374,8 @@ app.on('ready', () => {
367374
mainWindow.loadURL('vector://vector/webapp/');
368375
Menu.setApplicationMenu(vectorMenu);
369376

370-
// explicitly hide because setApplicationMenu on Linux otherwise shows...
371-
// https://github.com/electron/electron/issues/9621
372-
mainWindow.hide();
373-
374377
// Create trayIcon icon
375-
tray.create({
376-
icon_path: iconPath,
377-
brand: vectorConfig.brand || 'Riot',
378-
});
378+
if (store.get('minimizeToTray', true)) tray.create(trayConfig);
379379

380380
mainWindow.once('ready-to-show', () => {
381381
mainWindowState.manage(mainWindow);
@@ -392,7 +392,8 @@ app.on('ready', () => {
392392
mainWindow = global.mainWindow = null;
393393
});
394394
mainWindow.on('close', (e) => {
395-
if (global.minimizeToTray && !global.appQuitting && (tray.hasTray() || process.platform === 'darwin')) {
395+
// If we are not quitting and have a tray icon then minimize to tray
396+
if (!global.appQuitting && tray.hasTray()) {
396397
// On Mac, closing the window just hides it
397398
// (this is generally how single-window Mac apps
398399
// behave, eg. Mail.app)

‎electron_app/src/tray.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ exports.hasTray = function hasTray() {
2626
return (trayIcon !== null);
2727
};
2828

29+
exports.destroy = function() {
30+
if (trayIcon) {
31+
trayIcon.destroy();
32+
trayIcon = null;
33+
}
34+
};
35+
2936
exports.create = function(config) {
3037
// no trays on darwin
3138
if (process.platform === 'darwin' || trayIcon) return;

‎src/vector/platform/ElectronPlatform.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ export default class ElectronPlatform extends VectorBasePlatform {
212212
}
213213

214214
supportsMinimizeToTray(): boolean {
215-
return true;
215+
// Things other than Mac support tray icons
216+
return !navigator.platform.toUpperCase().includes('MAC');
216217
}
217218

218219
async getMinimizeToTrayEnabled(): boolean {

0 commit comments

Comments
 (0)