Skip to content

Commit 1498872

Browse files
committed
Add ability to hide tray icon on non-Mac (which has no tray icon)
1 parent 6ada5e4 commit 1498872

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

‎electron_app/src/electron-main.js‎

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

8787
let mainWindow = null;
8888
global.appQuitting = false;
89-
global.minimizeToTray = store.get('minimizeToTray', true);
89+
global.showTrayIcon = store.get("showTrayIcon", true);
90+
global.minimizeToTray = global.showTrayIcon && store.get('minimizeToTray', true);
9091

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

92100
// handle uncaught errors otherwise it displays
93101
// stack traces in popup dialogs, which is terrible (which
@@ -166,6 +174,20 @@ ipcMain.on('ipcCall', async function(ev, payload) {
166174
launcher.disable();
167175
}
168176
break;
177+
case 'getTrayIconEnabled':
178+
ret = global.showTrayIcon;
179+
break;
180+
case 'setTrayIconEnabled':
181+
if (args[0]) {
182+
// Create trayIcon icon
183+
tray.create(trayConfig);
184+
} else {
185+
tray.destroy();
186+
}
187+
store.set('minimizeToTray', global.showTrayIcon = args[0]);
188+
// re-evaluate whether we should be minimizing to tray
189+
global.minimizeToTray = global.showTrayIcon && store.get('minimizeToTray', true);
190+
break;
169191
case 'getMinimizeToTrayEnabled':
170192
ret = global.minimizeToTray;
171193
break;
@@ -329,11 +351,6 @@ app.on('ready', () => {
329351
console.log('No update_base_url is defined: auto update is disabled');
330352
}
331353

332-
// It's important to call `path.join` so we don't end up with the packaged
333-
// asar in the final path.
334-
const iconFile = `riot.${process.platform === 'win32' ? 'ico' : 'png'}`;
335-
const iconPath = path.join(__dirname, "..", "..", "img", iconFile);
336-
337354
// Load the previous window state with fallback to defaults
338355
const mainWindowState = windowStateKeeper({
339356
defaultWidth: 1024,
@@ -371,10 +388,7 @@ app.on('ready', () => {
371388
mainWindow.hide();
372389

373390
// Create trayIcon icon
374-
tray.create({
375-
icon_path: iconPath,
376-
brand: vectorConfig.brand || 'Riot',
377-
});
391+
if (global.showTrayIcon) tray.create(trayConfig);
378392

379393
mainWindow.once('ready-to-show', () => {
380394
mainWindowState.manage(mainWindow);

‎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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ export default class ElectronPlatform extends VectorBasePlatform {
211211
return this._ipcCall('setAutoHideMenuBarEnabled', enabled);
212212
}
213213

214+
supportsTrayIcon(): boolean {
215+
// Things other than Mac support tray icons
216+
return !navigator.platform.toUpperCase().includes('MAC');
217+
}
218+
219+
async getTrayIconEnabled(): boolean {
220+
return this._ipcCall('getTrayIconEnabled');
221+
}
222+
223+
async setTrayIconEnabled(enabled: boolean): void {
224+
return this._ipcCall('setTrayIconEnabled', enabled);
225+
}
226+
214227
supportsMinimizeToTray(): boolean {
215228
return true;
216229
}

0 commit comments

Comments
 (0)