Merge pull request #4012 from vector-im/t3chguy/electron_settings

auto-launch support [Electron]
pull/4120/head
Matthew Hodgson 2017-05-30 23:59:16 +01:00 committed by GitHub
commit 448c10736a
4 changed files with 50 additions and 42 deletions

View File

@ -6,6 +6,7 @@
"description": "A feature-rich client for Matrix.org", "description": "A feature-rich client for Matrix.org",
"author": "Vector Creations Ltd.", "author": "Vector Creations Ltd.",
"dependencies": { "dependencies": {
"auto-launch": "^5.0.1",
"electron-window-state": "^4.1.0" "electron-window-state": "^4.1.0"
} }
} }

View File

@ -22,6 +22,7 @@ const checkSquirrelHooks = require('./squirrelhooks');
if (checkSquirrelHooks()) return; if (checkSquirrelHooks()) return;
const electron = require('electron'); const electron = require('electron');
const AutoLaunch = require('auto-launch');
const tray = require('./tray'); const tray = require('./tray');
const vectorMenu = require('./vectormenu'); const vectorMenu = require('./vectormenu');
@ -45,47 +46,6 @@ const INITIAL_UPDATE_DELAY_MS = 30 * 1000;
let mainWindow = null; let mainWindow = null;
let appQuitting = false; let appQuitting = false;
function safeOpenURL(target) {
// openExternal passes the target to open/start/xdg-open,
// so put fairly stringent limits on what can be opened
// (for instance, open /bin/sh does indeed open a terminal
// with a shell, albeit with no arguments)
const parsedUrl = url.parse(target);
if (PERMITTED_URL_SCHEMES.indexOf(parsedUrl.protocol) > -1) {
// explicitly use the URL re-assembled by the url library,
// so we know the url parser has understood all the parts
// of the input string
const newTarget = url.format(parsedUrl);
electron.shell.openExternal(newTarget);
}
}
function onWindowOrNavigate(ev, target) {
// always prevent the default: if something goes wrong,
// we don't want to end up opening it in the electron
// app, as we could end up opening any sort of random
// url in a window that has node scripting access.
ev.preventDefault();
safeOpenURL(target);
}
function onLinkContextMenu(ev, params) {
const popupMenu = new electron.Menu();
popupMenu.append(new electron.MenuItem({
label: params.linkURL,
click() { safeOpenURL(params.linkURL); },
}));
popupMenu.append(new electron.MenuItem({
label: 'Copy Link Address',
click() { electron.clipboard.writeText(params.linkURL); },
}));
popupMenu.popup();
ev.preventDefault();
}
function installUpdate() { function installUpdate() {
// for some reason, quitAndInstall does not fire the // for some reason, quitAndInstall does not fire the
// before-quit event, so we need to set the flag here. // before-quit event, so we need to set the flag here.
@ -209,6 +169,47 @@ if (shouldQuit) {
electron.app.quit(); electron.app.quit();
} }
const launcher = new AutoLaunch({
name: vectorConfig.brand || 'Riot',
isHidden: true,
mac: {
useLaunchAgent: true,
},
});
const settings = {
'auto-launch': {
get: launcher.isEnabled,
set: function(bool) {
if (bool) {
return launcher.enable();
} else {
return launcher.disable();
}
},
},
};
electron.ipcMain.on('settings_get', async function(ev) {
const data = {};
try {
await Promise.all(Object.keys(settings).map(async function (setting) {
data[setting] = await settings[setting].get();
}));
ev.sender.send('settings', data);
} catch(e) { console.error(e); }
});
electron.ipcMain.on('settings_set', function(ev, key, value) {
console.log(key, value);
if (settings[key] && settings[key].set) {
settings[key].set(value);
}
});
electron.app.on('ready', () => { electron.app.on('ready', () => {
if (vectorConfig.update_base_url) { if (vectorConfig.update_base_url) {
console.log(`Starting auto update with base URL: ${vectorConfig.update_base_url}`); console.log(`Starting auto update with base URL: ${vectorConfig.update_base_url}`);
@ -238,6 +239,10 @@ electron.app.on('ready', () => {
mainWindow.loadURL(`file://${__dirname}/../../webapp/index.html`); mainWindow.loadURL(`file://${__dirname}/../../webapp/index.html`);
electron.Menu.setApplicationMenu(vectorMenu); electron.Menu.setApplicationMenu(vectorMenu);
// explicitly hide because setApplicationMenu on Linux otherwise shows...
// https://github.com/electron/electron/issues/9621
mainWindow.hide();
// Create trayIcon icon // Create trayIcon icon
tray.create(mainWindow, { tray.create(mainWindow, {
icon_path: iconPath, icon_path: iconPath,

View File

@ -33,7 +33,7 @@
"build:compile": "npm run reskindex && babel --source-maps -d lib src", "build:compile": "npm run reskindex && babel --source-maps -d lib src",
"build:bundle": "cross-env NODE_ENV=production webpack -p --progress", "build:bundle": "cross-env NODE_ENV=production webpack -p --progress",
"build:bundle:dev": "webpack --optimize-occurence-order --progress", "build:bundle:dev": "webpack --optimize-occurence-order --progress",
"build:electron": "npm run clean && npm run build && build -wml --ia32 --x64", "build:electron": "npm run clean && npm run build && npm run install:electron && build -wml --ia32 --x64",
"build": "npm run reskindex && npm run build:res && npm run build:bundle", "build": "npm run reskindex && npm run build:res && npm run build:bundle",
"build:dev": "npm run reskindex && npm run build:res && npm run build:bundle:dev", "build:dev": "npm run reskindex && npm run build:res && npm run build:bundle:dev",
"dist": "scripts/package.sh", "dist": "scripts/package.sh",

View File

@ -153,6 +153,8 @@ export default class ElectronPlatform extends VectorBasePlatform {
return null; return null;
} }
isElectron(): boolean { return true; }
requestNotificationPermission(): Promise<string> { requestNotificationPermission(): Promise<string> {
return q('granted'); return q('granted');
} }