Skip to content

Commit 3847388

Browse files
authored
Merge pull request element-hq#6805 from vector-im/t3chguy/electron_changes
[electron] Fix desktop app --hidden flag
2 parents 9dd37ac + e0bb618 commit 3847388

1 file changed

Lines changed: 49 additions & 39 deletions

File tree

‎electron_app/src/electron-main.js‎

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const checkSquirrelHooks = require('./squirrelhooks');
2323
if (checkSquirrelHooks()) return;
2424

2525
const argv = require('minimist')(process.argv);
26-
const electron = require('electron');
26+
const {app, ipcMain, powerSaveBlocker, BrowserWindow, Menu} = require('electron');
2727
const AutoLaunch = require('auto-launch');
2828

2929
const tray = require('./tray');
@@ -33,8 +33,8 @@ const updater = require('./updater');
3333

3434
const windowStateKeeper = require('electron-window-state');
3535

36-
if (argv.profile) {
37-
electron.app.setPath('userData', `${electron.app.getPath('userData')}-${argv.profile}`);
36+
if (argv['profile']) {
37+
app.setPath('userData', `${app.getPath('userData')}-${argv['profile']}`);
3838
}
3939

4040
let vectorConfig = {};
@@ -62,14 +62,14 @@ process.on('uncaughtException', function(error) {
6262
});
6363

6464
let focusHandlerAttached = false;
65-
electron.ipcMain.on('setBadgeCount', function(ev, count) {
66-
electron.app.setBadgeCount(count);
67-
if (count === 0) {
65+
ipcMain.on('setBadgeCount', function(ev, count) {
66+
app.setBadgeCount(count);
67+
if (count === 0 && mainWindow) {
6868
mainWindow.flashFrame(false);
6969
}
7070
});
7171

72-
electron.ipcMain.on('loudNotification', function() {
72+
ipcMain.on('loudNotification', function() {
7373
if (process.platform === 'win32' && mainWindow && !mainWindow.isFocused() && !focusHandlerAttached) {
7474
mainWindow.flashFrame(true);
7575
mainWindow.once('focus', () => {
@@ -81,26 +81,29 @@ electron.ipcMain.on('loudNotification', function() {
8181
});
8282

8383
let powerSaveBlockerId;
84-
electron.ipcMain.on('app_onAction', function(ev, payload) {
84+
ipcMain.on('app_onAction', function(ev, payload) {
8585
switch (payload.action) {
8686
case 'call_state':
87-
if (powerSaveBlockerId && electron.powerSaveBlocker.isStarted(powerSaveBlockerId)) {
87+
if (powerSaveBlockerId && powerSaveBlocker.isStarted(powerSaveBlockerId)) {
8888
if (payload.state === 'ended') {
89-
electron.powerSaveBlocker.stop(powerSaveBlockerId);
89+
powerSaveBlocker.stop(powerSaveBlockerId);
9090
}
9191
} else {
9292
if (payload.state === 'connected') {
93-
powerSaveBlockerId = electron.powerSaveBlocker.start('prevent-display-sleep');
93+
powerSaveBlockerId = powerSaveBlocker.start('prevent-display-sleep');
9494
}
9595
}
9696
break;
9797
}
9898
});
9999

100100

101-
electron.app.commandLine.appendSwitch('--enable-usermedia-screen-capturing');
101+
app.commandLine.appendSwitch('--enable-usermedia-screen-capturing');
102+
103+
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
104+
// If other instance launched with --hidden then skip showing window
105+
if (commandLine.includes('--hidden')) return;
102106

103-
const shouldQuit = electron.app.makeSingleInstance((commandLine, workingDirectory) => {
104107
// Someone tried to run a second instance, we should focus our window.
105108
if (mainWindow) {
106109
if (!mainWindow.isVisible()) mainWindow.show();
@@ -111,7 +114,7 @@ const shouldQuit = electron.app.makeSingleInstance((commandLine, workingDirector
111114

112115
if (shouldQuit) {
113116
console.log('Other instance detected: exiting');
114-
electron.app.exit();
117+
app.exit();
115118
}
116119

117120

@@ -136,7 +139,7 @@ const settings = {
136139
},
137140
};
138141

139-
electron.ipcMain.on('settings_get', async function(ev) {
142+
ipcMain.on('settings_get', async function(ev) {
140143
const data = {};
141144

142145
try {
@@ -145,34 +148,37 @@ electron.ipcMain.on('settings_get', async function(ev) {
145148
}));
146149

147150
ev.sender.send('settings', data);
148-
} catch(e) { console.error(e); }
151+
} catch (e) {
152+
console.error(e);
153+
}
149154
});
150155

151-
electron.ipcMain.on('settings_set', function(ev, key, value) {
156+
ipcMain.on('settings_set', function(ev, key, value) {
152157
console.log(key, value);
153158
if (settings[key] && settings[key].set) {
154159
settings[key].set(value);
155160
}
156161
});
157162

158-
electron.app.on('ready', () => {
159-
160-
if (argv.devtools) {
163+
app.on('ready', () => {
164+
if (argv['devtools']) {
161165
try {
162-
const { default: installExtension, REACT_DEVELOPER_TOOLS, REACT_PERF } = require('electron-devtools-installer');
163-
installExtension(REACT_DEVELOPER_TOOLS)
166+
const { default: installExt, REACT_DEVELOPER_TOOLS, REACT_PERF } = require('electron-devtools-installer');
167+
installExt(REACT_DEVELOPER_TOOLS)
164168
.then((name) => console.log(`Added Extension: ${name}`))
165169
.catch((err) => console.log('An error occurred: ', err));
166-
installExtension(REACT_PERF)
170+
installExt(REACT_PERF)
167171
.then((name) => console.log(`Added Extension: ${name}`))
168172
.catch((err) => console.log('An error occurred: ', err));
169-
} catch(e) {console.log(e);}
173+
} catch (e) {
174+
console.log(e);
175+
}
170176
}
171177

172178

173-
if (vectorConfig.update_base_url) {
174-
console.log(`Starting auto update with base URL: ${vectorConfig.update_base_url}`);
175-
updater.start(vectorConfig.update_base_url);
179+
if (vectorConfig['update_base_url']) {
180+
console.log(`Starting auto update with base URL: ${vectorConfig['update_base_url']}`);
181+
updater.start(vectorConfig['update_base_url']);
176182
} else {
177183
console.log('No update_base_url is defined: auto update is disabled');
178184
}
@@ -185,7 +191,7 @@ electron.app.on('ready', () => {
185191
defaultHeight: 768,
186192
});
187193

188-
mainWindow = global.mainWindow = new electron.BrowserWindow({
194+
mainWindow = global.mainWindow = new BrowserWindow({
189195
icon: iconPath,
190196
show: false,
191197
autoHideMenuBar: true,
@@ -196,7 +202,7 @@ electron.app.on('ready', () => {
196202
height: mainWindowState.height,
197203
});
198204
mainWindow.loadURL(`file://${__dirname}/../../webapp/index.html`);
199-
electron.Menu.setApplicationMenu(vectorMenu);
205+
Menu.setApplicationMenu(vectorMenu);
200206

201207
// explicitly hide because setApplicationMenu on Linux otherwise shows...
202208
// https://github.com/electron/electron/issues/9621
@@ -208,11 +214,16 @@ electron.app.on('ready', () => {
208214
brand: vectorConfig.brand || 'Riot',
209215
});
210216

211-
if (!argv.hidden) {
212-
mainWindow.once('ready-to-show', () => {
217+
mainWindow.once('ready-to-show', () => {
218+
mainWindowState.manage(mainWindow);
219+
220+
if (!argv['hidden']) {
213221
mainWindow.show();
214-
});
215-
}
222+
} else {
223+
// hide here explicitly because window manage above sometimes shows it
224+
mainWindow.hide();
225+
}
226+
});
216227

217228
mainWindow.on('closed', () => {
218229
mainWindow = global.mainWindow = null;
@@ -240,18 +251,17 @@ electron.app.on('ready', () => {
240251
}
241252

242253
webContentsHandler(mainWindow.webContents);
243-
mainWindowState.manage(mainWindow);
244254
});
245255

246-
electron.app.on('window-all-closed', () => {
247-
electron.app.quit();
256+
app.on('window-all-closed', () => {
257+
app.quit();
248258
});
249259

250-
electron.app.on('activate', () => {
260+
app.on('activate', () => {
251261
mainWindow.show();
252262
});
253263

254-
electron.app.on('before-quit', () => {
264+
app.on('before-quit', () => {
255265
global.appQuitting = true;
256266
if (mainWindow) {
257267
mainWindow.webContents.send('before-quit');
@@ -262,4 +272,4 @@ electron.app.on('before-quit', () => {
262272
// installer uses for the shortcut icon.
263273
// This makes notifications work on windows 8.1 (and is
264274
// a noop on other platforms).
265-
electron.app.setAppUserModelId('com.squirrel.riot-web.Riot');
275+
app.setAppUserModelId('com.squirrel.riot-web.Riot');

0 commit comments

Comments
 (0)