Merge pull request 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
electron_app/src
src/vector/platform

View file

@ -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)

View file

@ -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;

View file

@ -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 {