From c79697367c53eeb2844886385866310599a210f7 Mon Sep 17 00:00:00 2001 From: Karl Glatz Date: Mon, 16 Jan 2017 17:31:51 +0100 Subject: [PATCH 1/4] tray: added basic close-to-tray support for windows and linux #2799 --- electron/src/electron-main.js | 10 ++++++- electron/src/tray.js | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 electron/src/tray.js diff --git a/electron/src/electron-main.js b/electron/src/electron-main.js index 675640a5202..21b1a6ccaba 100644 --- a/electron/src/electron-main.js +++ b/electron/src/electron-main.js @@ -26,6 +26,8 @@ if (check_squirrel_hooks()) return; const electron = require('electron'); const url = require('url'); +const tray = require('./tray'); + const VectorMenu = require('./vectormenu'); let vectorConfig = {}; @@ -170,6 +172,12 @@ electron.app.on('ready', () => { mainWindow.loadURL(`file://${__dirname}/../../webapp/index.html`); electron.Menu.setApplicationMenu(VectorMenu); + // Create trayIcon icon + tray.create(mainWindow, { + icon_path: icon_path, + brand: vectorConfig.brand || 'Riot' + }); + mainWindow.once('ready-to-show', () => { mainWindow.show(); }); @@ -177,7 +185,7 @@ electron.app.on('ready', () => { mainWindow = null; }); mainWindow.on('close', (e) => { - if (process.platform == 'darwin' && !appQuitting) { + if (!appQuitting && (tray.hasTray() || process.platform == 'darwin')) { // On Mac, closing the window just hides it // (this is generally how single-window Mac apps // behave, eg. Mail.app) diff --git a/electron/src/tray.js b/electron/src/tray.js new file mode 100644 index 00000000000..78dac455e38 --- /dev/null +++ b/electron/src/tray.js @@ -0,0 +1,49 @@ +const path = require('path'); +const electron = require('electron'); + +const app = electron.app; +const Tray = electron.Tray; +const MenuItem = electron.MenuItem; + +let trayIcon = null; + +exports.hasTray = function hasTray() { + return (trayIcon !== null); +} + +exports.create = function (win, config) { + // no trays on darwin + if (process.platform === 'darwin' || trayIcon) { + return; + } + + const toggleWin = function () { + if (win.isVisible()) { + win.hide(); + } else { + win.show(); + win.focus(); + } + }; + + const contextMenu = electron.Menu.buildFromTemplate([ + { + label: 'Show/Hide ' + config.brand, + click: toggleWin + }, + { + type: 'separator' + }, + { + label: 'Quit', + click: function () { + app.quit(); + } + } + ]); + + trayIcon = new Tray(config.icon_path); + trayIcon.setToolTip(config.brand); + trayIcon.setContextMenu(contextMenu); + trayIcon.on('click', toggleWin); +}; From e211a448b475991b1e0f5aac7dc831618d58727b Mon Sep 17 00:00:00 2001 From: Karl Glatz Date: Mon, 16 Jan 2017 18:16:10 +0100 Subject: [PATCH 2/4] tray: added single instance support which prevents the app from starting multiple times; instead of starting the running instance is shown/focused --- electron/src/electron-main.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/electron/src/electron-main.js b/electron/src/electron-main.js index 21b1a6ccaba..ffd3e396422 100644 --- a/electron/src/electron-main.js +++ b/electron/src/electron-main.js @@ -152,6 +152,19 @@ electron.ipcMain.on('install_update', installUpdate); electron.app.commandLine.appendSwitch('--enable-usermedia-screen-capturing'); +const shouldQuit = electron.app.makeSingleInstance((commandLine, workingDirectory) => { + // Someone tried to run a second instance, we should focus our window. + if (mainWindow) { + if (!mainWindow.isVisible()) mainWindow.show(); + if (mainWindow.isMinimized()) mainWindow.restore(); + mainWindow.focus(); + } +}); + +if (shouldQuit) { + electron.app.quit() +} + electron.app.on('ready', () => { if (vectorConfig.update_base_url) { console.log("Starting auto update with base URL: " + vectorConfig.update_base_url); From 6c9d9087e71c578bea5fcd3475b6a73e935b7edc Mon Sep 17 00:00:00 2001 From: Karl Glatz Date: Mon, 16 Jan 2017 20:29:17 +0100 Subject: [PATCH 3/4] fixed enable notifications in electron bug #2945 --- src/vector/platform/ElectronPlatform.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/vector/platform/ElectronPlatform.js b/src/vector/platform/ElectronPlatform.js index 68df88b0960..cb1d9598e12 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -131,4 +131,10 @@ export default class ElectronPlatform extends VectorBasePlatform { screenCaptureErrorString() { return null; } + + requestNotificationPermission() : Promise { + const defer = q.defer(); + defer.resolve('granted'); + return defer.promise; + } } From a0b5d06e2f2c105a77b21684c392e4ea4543f962 Mon Sep 17 00:00:00 2001 From: Karl Glatz Date: Mon, 16 Jan 2017 20:34:11 +0100 Subject: [PATCH 4/4] tray: add show-window call to clicks of notifications (window might be hidden in the tray now) --- src/vector/platform/ElectronPlatform.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/vector/platform/ElectronPlatform.js b/src/vector/platform/ElectronPlatform.js index cb1d9598e12..bfd4ef4deb5 100644 --- a/src/vector/platform/ElectronPlatform.js +++ b/src/vector/platform/ElectronPlatform.js @@ -97,7 +97,10 @@ export default class ElectronPlatform extends VectorBasePlatform { room_id: room.roomId }); global.focus(); - electron.remote.getCurrentWindow().restore(); + let currentWin = electron.remote.getCurrentWindow(); + currentWin.show(); + currentWin.restore(); + currentWin.focus(); }; return notification;