Merge pull request #11258 from vector-im/t3chguy/hide_tray_icon

Add ability to hide tray icon on non-Mac (which has no tray icon)
This commit is contained in:
Michael Telatynski 2019-10-30 16:31:47 +00:00 committed by GitHub
commit eab6ffe7db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 18 deletions

View File

@ -86,8 +86,14 @@ const store = new Store({ name: "electron-config" });
let mainWindow = null; let mainWindow = null;
global.appQuitting = false; 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 // handle uncaught errors otherwise it displays
// stack traces in popup dialogs, which is terrible (which // stack traces in popup dialogs, which is terrible (which
@ -168,10 +174,16 @@ ipcMain.on('ipcCall', async function(ev, payload) {
} }
break; break;
case 'getMinimizeToTrayEnabled': case 'getMinimizeToTrayEnabled':
ret = global.minimizeToTray; ret = tray.hasTray();
break; break;
case 'setMinimizeToTrayEnabled': 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; break;
case 'getAutoHideMenuBarEnabled': case 'getAutoHideMenuBarEnabled':
ret = global.mainWindow.isMenuBarAutoHide(); ret = global.mainWindow.isMenuBarAutoHide();
@ -330,11 +342,6 @@ app.on('ready', () => {
console.log('No update_base_url is defined: auto update is disabled'); 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 // Load the previous window state with fallback to defaults
const mainWindowState = windowStateKeeper({ const mainWindowState = windowStateKeeper({
defaultWidth: 1024, defaultWidth: 1024,
@ -367,15 +374,8 @@ app.on('ready', () => {
mainWindow.loadURL('vector://vector/webapp/'); mainWindow.loadURL('vector://vector/webapp/');
Menu.setApplicationMenu(vectorMenu); Menu.setApplicationMenu(vectorMenu);
// explicitly hide because setApplicationMenu on Linux otherwise shows...
// https://github.com/electron/electron/issues/9621
mainWindow.hide();
// Create trayIcon icon // Create trayIcon icon
tray.create({ if (store.get('minimizeToTray', true)) tray.create(trayConfig);
icon_path: iconPath,
brand: vectorConfig.brand || 'Riot',
});
mainWindow.once('ready-to-show', () => { mainWindow.once('ready-to-show', () => {
mainWindowState.manage(mainWindow); mainWindowState.manage(mainWindow);
@ -392,7 +392,8 @@ app.on('ready', () => {
mainWindow = global.mainWindow = null; mainWindow = global.mainWindow = null;
}); });
mainWindow.on('close', (e) => { 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 // On Mac, closing the window just hides it
// (this is generally how single-window Mac apps // (this is generally how single-window Mac apps
// behave, eg. Mail.app) // behave, eg. Mail.app)

View File

@ -26,6 +26,13 @@ exports.hasTray = function hasTray() {
return (trayIcon !== null); return (trayIcon !== null);
}; };
exports.destroy = function() {
if (trayIcon) {
trayIcon.destroy();
trayIcon = null;
}
};
exports.create = function(config) { exports.create = function(config) {
// no trays on darwin // no trays on darwin
if (process.platform === 'darwin' || trayIcon) return; if (process.platform === 'darwin' || trayIcon) return;

View File

@ -212,7 +212,8 @@ export default class ElectronPlatform extends VectorBasePlatform {
} }
supportsMinimizeToTray(): boolean { supportsMinimizeToTray(): boolean {
return true; // Things other than Mac support tray icons
return !navigator.platform.toUpperCase().includes('MAC');
} }
async getMinimizeToTrayEnabled(): boolean { async getMinimizeToTrayEnabled(): boolean {