Skip to content

Commit 448c107

Browse files
authored
Merge pull request element-hq#4012 from vector-im/t3chguy/electron_settings
auto-launch support [Electron]
2 parents 4493994 + 0e96968 commit 448c107

4 files changed

Lines changed: 50 additions & 42 deletions

File tree

‎electron_app/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"description": "A feature-rich client for Matrix.org",
77
"author": "Vector Creations Ltd.",
88
"dependencies": {
9+
"auto-launch": "^5.0.1",
910
"electron-window-state": "^4.1.0"
1011
}
1112
}

‎electron_app/src/electron-main.js‎

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const checkSquirrelHooks = require('./squirrelhooks');
2222
if (checkSquirrelHooks()) return;
2323

2424
const electron = require('electron');
25+
const AutoLaunch = require('auto-launch');
2526

2627
const tray = require('./tray');
2728
const vectorMenu = require('./vectormenu');
@@ -45,47 +46,6 @@ const INITIAL_UPDATE_DELAY_MS = 30 * 1000;
4546
let mainWindow = null;
4647
let appQuitting = false;
4748

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-
8949
function installUpdate() {
9050
// for some reason, quitAndInstall does not fire the
9151
// before-quit event, so we need to set the flag here.
@@ -209,6 +169,47 @@ if (shouldQuit) {
209169
electron.app.quit();
210170
}
211171

172+
173+
const launcher = new AutoLaunch({
174+
name: vectorConfig.brand || 'Riot',
175+
isHidden: true,
176+
mac: {
177+
useLaunchAgent: true,
178+
},
179+
});
180+
181+
const settings = {
182+
'auto-launch': {
183+
get: launcher.isEnabled,
184+
set: function(bool) {
185+
if (bool) {
186+
return launcher.enable();
187+
} else {
188+
return launcher.disable();
189+
}
190+
},
191+
},
192+
};
193+
194+
electron.ipcMain.on('settings_get', async function(ev) {
195+
const data = {};
196+
197+
try {
198+
await Promise.all(Object.keys(settings).map(async function (setting) {
199+
data[setting] = await settings[setting].get();
200+
}));
201+
202+
ev.sender.send('settings', data);
203+
} catch(e) { console.error(e); }
204+
});
205+
206+
electron.ipcMain.on('settings_set', function(ev, key, value) {
207+
console.log(key, value);
208+
if (settings[key] && settings[key].set) {
209+
settings[key].set(value);
210+
}
211+
});
212+
212213
electron.app.on('ready', () => {
213214
if (vectorConfig.update_base_url) {
214215
console.log(`Starting auto update with base URL: ${vectorConfig.update_base_url}`);
@@ -238,6 +239,10 @@ electron.app.on('ready', () => {
238239
mainWindow.loadURL(`file://${__dirname}/../../webapp/index.html`);
239240
electron.Menu.setApplicationMenu(vectorMenu);
240241

242+
// explicitly hide because setApplicationMenu on Linux otherwise shows...
243+
// https://github.com/electron/electron/issues/9621
244+
mainWindow.hide();
245+
241246
// Create trayIcon icon
242247
tray.create(mainWindow, {
243248
icon_path: iconPath,

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"build:compile": "npm run reskindex && babel --source-maps -d lib src",
3434
"build:bundle": "cross-env NODE_ENV=production webpack -p --progress",
3535
"build:bundle:dev": "webpack --optimize-occurence-order --progress",
36-
"build:electron": "npm run clean && npm run build && build -wml --ia32 --x64",
36+
"build:electron": "npm run clean && npm run build && npm run install:electron && build -wml --ia32 --x64",
3737
"build": "npm run reskindex && npm run build:res && npm run build:bundle",
3838
"build:dev": "npm run reskindex && npm run build:res && npm run build:bundle:dev",
3939
"dist": "scripts/package.sh",

‎src/vector/platform/ElectronPlatform.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ export default class ElectronPlatform extends VectorBasePlatform {
153153
return null;
154154
}
155155

156+
isElectron(): boolean { return true; }
157+
156158
requestNotificationPermission(): Promise<string> {
157159
return q('granted');
158160
}

0 commit comments

Comments
 (0)