Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic close-to-tray support for windows and linux #2960

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion electron/src/electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -150,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);
Expand All @@ -170,14 +185,20 @@ 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();
});
mainWindow.on('closed', () => {
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)
Expand Down
49 changes: 49 additions & 0 deletions electron/src/tray.js
Original file line number Diff line number Diff line change
@@ -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);
};
11 changes: 10 additions & 1 deletion src/vector/platform/ElectronPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,4 +134,10 @@ export default class ElectronPlatform extends VectorBasePlatform {
screenCaptureErrorString() {
return null;
}

requestNotificationPermission() : Promise {
const defer = q.defer();
defer.resolve('granted');
return defer.promise;
}
}