Skip to content

Commit 1bcdf31

Browse files
authored
Merge pull request element-hq#3993 from vector-im/t3chguy/tidy_electron_files
tidy electron files, they weren't pwetty
2 parents 445a55c + ff5adef commit 1bcdf31

4 files changed

Lines changed: 134 additions & 163 deletions

File tree

‎electron_app/src/electron-main.js‎

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @flow
2-
31
/*
42
Copyright 2016 Aviral Dasgupta
53
Copyright 2016 OpenMarket Ltd
@@ -20,14 +18,13 @@ limitations under the License.
2018
// Squirrel on windows starts the app with various flags
2119
// as hooks to tell us when we've been installed/uninstalled
2220
// etc.
23-
const check_squirrel_hooks = require('./squirrelhooks');
24-
if (check_squirrel_hooks()) return;
21+
const checkSquirrelHooks = require('./squirrelhooks');
22+
if (checkSquirrelHooks()) return;
2523

2624
const electron = require('electron');
2725

2826
const tray = require('./tray');
29-
30-
const VectorMenu = require('./vectormenu');
27+
const vectorMenu = require('./vectormenu');
3128
const webContentsHandler = require('./webcontents-handler');
3229

3330
const windowStateKeeper = require('electron-window-state');
@@ -48,6 +45,47 @@ const INITIAL_UPDATE_DELAY_MS = 30 * 1000;
4845
let mainWindow = null;
4946
let appQuitting = false;
5047

48+
function safeOpenURL(target) {
49+
// openExternal passes the target to open/start/xdg-open,
50+
// so put fairly stringent limits on what can be opened
51+
// (for instance, open /bin/sh does indeed open a terminal
52+
// with a shell, albeit with no arguments)
53+
const parsedUrl = url.parse(target);
54+
if (PERMITTED_URL_SCHEMES.indexOf(parsedUrl.protocol) > -1) {
55+
// explicitly use the URL re-assembled by the url library,
56+
// so we know the url parser has understood all the parts
57+
// of the input string
58+
const newTarget = url.format(parsedUrl);
59+
electron.shell.openExternal(newTarget);
60+
}
61+
}
62+
63+
function onWindowOrNavigate(ev, target) {
64+
// always prevent the default: if something goes wrong,
65+
// we don't want to end up opening it in the electron
66+
// app, as we could end up opening any sort of random
67+
// url in a window that has node scripting access.
68+
ev.preventDefault();
69+
safeOpenURL(target);
70+
}
71+
72+
function onLinkContextMenu(ev, params) {
73+
const popupMenu = new electron.Menu();
74+
75+
popupMenu.append(new electron.MenuItem({
76+
label: params.linkURL,
77+
click() { safeOpenURL(params.linkURL); },
78+
}));
79+
80+
popupMenu.append(new electron.MenuItem({
81+
label: 'Copy Link Address',
82+
click() { electron.clipboard.writeText(params.linkURL); },
83+
}));
84+
85+
popupMenu.popup();
86+
ev.preventDefault();
87+
}
88+
5189
function installUpdate() {
5290
// for some reason, quitAndInstall does not fire the
5391
// before-quit event, so we need to set the flag here.
@@ -59,38 +97,37 @@ function pollForUpdates() {
5997
try {
6098
electron.autoUpdater.checkForUpdates();
6199
} catch (e) {
62-
console.log("Couldn't check for update", e);
100+
console.log('Couldn\'t check for update', e);
63101
}
64102
}
65103

66-
function startAutoUpdate(update_base_url) {
67-
if (update_base_url.slice(-1) !== '/') {
68-
update_base_url = update_base_url + '/';
104+
function startAutoUpdate(updateBaseUrl) {
105+
if (updateBaseUrl.slice(-1) !== '/') {
106+
updateBaseUrl = updateBaseUrl + '/';
69107
}
70108
try {
71109
// For reasons best known to Squirrel, the way it checks for updates
72110
// is completely different between macOS and windows. On macOS, it
73111
// hits a URL that either gives it a 200 with some json or
74112
// 204 No Content. On windows it takes a base path and looks for
75113
// files under that path.
76-
if (process.platform == 'darwin') {
114+
if (process.platform === 'darwin') {
77115
// include the current version in the URL we hit. Electron doesn't add
78116
// it anywhere (apart from the User-Agent) so it's up to us. We could
79117
// (and previously did) just use the User-Agent, but this doesn't
80118
// rely on NSURLConnection setting the User-Agent to what we expect,
81119
// and also acts as a convenient cache-buster to ensure that when the
82120
// app updates it always gets a fresh value to avoid update-looping.
83121
electron.autoUpdater.setFeedURL(
84-
update_base_url +
85-
'macos/?localVersion=' + encodeURIComponent(electron.app.getVersion())
86-
);
87-
} else if (process.platform == 'win32') {
88-
electron.autoUpdater.setFeedURL(update_base_url + 'win32/' + process.arch + '/');
122+
`${updateBaseUrl}macos/?localVersion=${encodeURIComponent(electron.app.getVersion())}`);
123+
124+
} else if (process.platform === 'win32') {
125+
electron.autoUpdater.setFeedURL(`${updateBaseUrl}win32/${process.arch}/`);
89126
} else {
90127
// Squirrel / electron only supports auto-update on these two platforms.
91128
// I'm not even going to try to guess which feed style they'd use if they
92129
// implemented it on Linux, or if it would be different again.
93-
console.log("Auto update not supported on this platform");
130+
console.log('Auto update not supported on this platform');
94131
}
95132
// We check for updates ourselves rather than using 'updater' because we need to
96133
// do it in the main process (and we don't really need to check every 10 minutes:
@@ -103,7 +140,7 @@ function startAutoUpdate(update_base_url) {
103140
setInterval(pollForUpdates, UPDATE_POLL_INTERVAL_MS);
104141
} catch (err) {
105142
// will fail if running in debug mode
106-
console.log("Couldn't enable update checking", err);
143+
console.log('Couldn\'t enable update checking', err);
107144
}
108145
}
109146

@@ -114,7 +151,7 @@ function startAutoUpdate(update_base_url) {
114151
// Assuming we generally run from the console when developing,
115152
// this is far preferable.
116153
process.on('uncaughtException', function(error) {
117-
console.log("Unhandled exception", error);
154+
console.log('Unhandled exception', error);
118155
});
119156

120157
electron.ipcMain.on('install_update', installUpdate);
@@ -168,30 +205,28 @@ const shouldQuit = electron.app.makeSingleInstance((commandLine, workingDirector
168205
});
169206

170207
if (shouldQuit) {
171-
console.log("Other instance detected: exiting");
208+
console.log('Other instance detected: exiting');
172209
electron.app.quit();
173210
}
174211

175212
electron.app.on('ready', () => {
176213
if (vectorConfig.update_base_url) {
177-
console.log("Starting auto update with base URL: " + vectorConfig.update_base_url);
214+
console.log(`Starting auto update with base URL: ${vectorConfig.update_base_url}`);
178215
startAutoUpdate(vectorConfig.update_base_url);
179216
} else {
180-
console.log("No update_base_url is defined: auto update is disabled");
217+
console.log('No update_base_url is defined: auto update is disabled');
181218
}
182219

183-
const icon_path = `${__dirname}/../img/riot.` + (
184-
process.platform == 'win32' ? 'ico' : 'png'
185-
);
220+
const iconPath = `${__dirname}/../img/riot.${process.platform === 'win32' ? 'ico' : 'png'}`;
186221

187222
// Load the previous window state with fallback to defaults
188-
let mainWindowState = windowStateKeeper({
223+
const mainWindowState = windowStateKeeper({
189224
defaultWidth: 1024,
190225
defaultHeight: 768,
191226
});
192227

193228
mainWindow = new electron.BrowserWindow({
194-
icon: icon_path,
229+
icon: iconPath,
195230
show: false,
196231
autoHideMenuBar: true,
197232

@@ -201,12 +236,12 @@ electron.app.on('ready', () => {
201236
height: mainWindowState.height,
202237
});
203238
mainWindow.loadURL(`file://${__dirname}/../../webapp/index.html`);
204-
electron.Menu.setApplicationMenu(VectorMenu);
239+
electron.Menu.setApplicationMenu(vectorMenu);
205240

206241
// Create trayIcon icon
207242
tray.create(mainWindow, {
208-
icon_path: icon_path,
209-
brand: vectorConfig.brand || 'Riot'
243+
icon_path: iconPath,
244+
brand: vectorConfig.brand || 'Riot',
210245
});
211246

212247
if (!process.argv.includes('--hidden')) {
@@ -219,7 +254,7 @@ electron.app.on('ready', () => {
219254
mainWindow = null;
220255
});
221256
mainWindow.on('close', (e) => {
222-
if (!appQuitting && (tray.hasTray() || process.platform == 'darwin')) {
257+
if (!appQuitting && (tray.hasTray() || process.platform === 'darwin')) {
223258
// On Mac, closing the window just hides it
224259
// (this is generally how single-window Mac apps
225260
// behave, eg. Mail.app)

‎electron_app/src/squirrelhooks.js‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,30 @@ limitations under the License.
1616

1717
const path = require('path');
1818
const spawn = require('child_process').spawn;
19-
const app = require('electron').app;
19+
const {app} = require('electron');
2020

21-
function run_update_exe(args, done) {
21+
function runUpdateExe(args, done) {
2222
// Invokes Squirrel's Update.exe which will do things for us like create shortcuts
2323
// Note that there's an Update.exe in the app-x.x.x directory and one in the parent
2424
// directory: we need to run the one in the parent directory, because it discovers
2525
// information about the app by inspecting the directory it's run from.
2626
const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe');
27-
console.log('Spawning `%s` with args `%s`', updateExe, args);
27+
console.log(`Spawning '${updateExe}' with args '${args}'`);
2828
spawn(updateExe, args, {
29-
detached: true
29+
detached: true,
3030
}).on('close', done);
31-
};
31+
}
3232

33-
function check_squirrel_hooks() {
34-
if (process.platform != 'win32') return false;
33+
function checkSquirrelHooks() {
34+
if (process.platform !== 'win32') return false;
3535

3636
const cmd = process.argv[1];
3737
const target = path.basename(process.execPath);
3838
if (cmd === '--squirrel-install' || cmd === '--squirrel-updated') {
39-
run_update_exe(['--createShortcut=' + target + ''], app.quit);
39+
runUpdateExe(['--createShortcut=' + target + ''], app.quit);
4040
return true;
4141
} else if (cmd === '--squirrel-uninstall') {
42-
run_update_exe(['--removeShortcut=' + target + ''], app.quit);
42+
runUpdateExe(['--removeShortcut=' + target + ''], app.quit);
4343
return true;
4444
} else if (cmd === '--squirrel-obsolete') {
4545
app.quit();
@@ -48,4 +48,4 @@ function check_squirrel_hooks() {
4848
return false;
4949
}
5050

51-
module.exports = check_squirrel_hooks;
51+
module.exports = checkSquirrelHooks;

‎electron_app/src/tray.js‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ exports.hasTray = function hasTray() {
2525

2626
exports.create = function(win, config) {
2727
// no trays on darwin
28-
if (process.platform === 'darwin' || trayIcon) {
29-
return;
30-
}
28+
if (process.platform === 'darwin' || trayIcon) return;
3129

3230
const toggleWin = function() {
3331
if (win.isVisible() && !win.isMinimized()) {
@@ -41,12 +39,10 @@ exports.create = function(win, config) {
4139

4240
const contextMenu = Menu.buildFromTemplate([
4341
{
44-
label: 'Show/Hide ' + config.brand,
42+
label: `Show/Hide ${config.brand}`,
4543
click: toggleWin,
4644
},
47-
{
48-
type: 'separator',
49-
},
45+
{ type: 'separator' },
5046
{
5147
label: 'Quit',
5248
click: function() {

0 commit comments

Comments
 (0)