Skip to content

Commit 80bae05

Browse files
authored
Merge pull request element-hq#2535 from vector-im/dbkr/electron_3
Electron app (take 3)
2 parents bc092d5 + 9a00ec1 commit 80bae05

9 files changed

Lines changed: 481 additions & 4 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
/vector/olm.*
1414
.DS_Store
1515
npm-debug.log
16+
electron/dist

‎electron/build/icon.icns‎

177 KB
Binary file not shown.

‎electron/build/icon.ico‎

21.1 KB
Binary file not shown.

‎electron/src/electron-main.js‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// @flow
2+
3+
/*
4+
Copyright 2016 Aviral Dasgupta
5+
Copyright 2016 OpenMarket Ltd
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
const electron = require('electron');
21+
const url = require('url');
22+
23+
const VectorMenu = require('./vectormenu');
24+
25+
const PERMITTED_URL_SCHEMES = [
26+
'http:',
27+
'https:',
28+
'mailto:',
29+
];
30+
31+
const UPDATE_POLL_INTERVAL_MS = 60 * 60 * 1000;
32+
33+
let mainWindow = null;
34+
let appQuitting = false;
35+
36+
function safeOpenURL(target) {
37+
// openExternal passes the target to open/start/xdg-open,
38+
// so put fairly stringent limits on what can be opened
39+
// (for instance, open /bin/sh does indeed open a terminal
40+
// with a shell, albeit with no arguments)
41+
const parsed_url = url.parse(target);
42+
if (PERMITTED_URL_SCHEMES.indexOf(parsed_url.protocol) > -1) {
43+
// explicitly use the URL re-assembled by the url library,
44+
// so we know the url parser has understood all the parts
45+
// of the input string
46+
const new_target = url.format(parsed_url);
47+
electron.shell.openExternal(new_target);
48+
}
49+
}
50+
51+
function onWindowOrNavigate(ev, target) {
52+
// always prevent the default: if something goes wrong,
53+
// we don't want to end up opening it in the electron
54+
// app, as we could end up opening any sort of random
55+
// url in a window that has node scripting access.
56+
ev.preventDefault();
57+
safeOpenURL(target);
58+
}
59+
60+
function onLinkContextMenu(ev, params) {
61+
const popup_menu = new electron.Menu();
62+
popup_menu.append(new electron.MenuItem({
63+
label: params.linkURL,
64+
click() {
65+
safeOpenURL(params.linkURL);
66+
},
67+
}));
68+
popup_menu.popup();
69+
ev.preventDefault();
70+
}
71+
72+
function installUpdate() {
73+
// for some reason, quitAndInstall does not fire the
74+
// before-quit event, so we need to set the flag here.
75+
appQuitting = true;
76+
electron.autoUpdater.quitAndInstall();
77+
}
78+
79+
function pollForUpdates() {
80+
try {
81+
electron.autoUpdater.checkForUpdates();
82+
} catch (e) {
83+
console.log("Couldn't check for update", e);
84+
}
85+
}
86+
87+
electron.ipcMain.on('install_update', installUpdate);
88+
89+
electron.app.on('ready', () => {
90+
try {
91+
// For reasons best known to Squirrel, the way it checks for updates
92+
// is completely different between macOS and windows. On macOS, it
93+
// hits a URL that either gives it a 200 with some json or
94+
// 204 No Content. On windows it takes a base path and looks for
95+
// files under that path.
96+
if (process.platform == 'darwin') {
97+
electron.autoUpdater.setFeedURL("https://riot.im/autoupdate/desktop/");
98+
} else if (process.platform == 'win32') {
99+
electron.autoUpdater.setFeedURL("https://riot.im/download/desktop/win32/");
100+
} else {
101+
// Squirrel / electron only supports auto-update on these two platforms.
102+
// I'm not even going to try to guess which feed style they'd use if they
103+
// implemented it on Linux, or if it would be different again.
104+
console.log("Auto update not supported on this platform");
105+
}
106+
// We check for updates ourselves rather than using 'updater' because we need to
107+
// do it in the main process (and we don't really need to check every 10 minutes:
108+
// every hour should be just fine for a desktop app)
109+
// However, we still let the main window listen for the update events.
110+
pollForUpdates();
111+
setInterval(pollForUpdates, UPDATE_POLL_INTERVAL_MS);
112+
} catch (err) {
113+
// will fail if running in debug mode
114+
console.log("Couldn't enable update checking", err);
115+
}
116+
117+
mainWindow = new electron.BrowserWindow({
118+
icon: `${__dirname}/../../vector/img/logo.png`,
119+
width: 1024, height: 768,
120+
});
121+
mainWindow.loadURL(`file://${__dirname}/../../vector/index.html`);
122+
electron.Menu.setApplicationMenu(VectorMenu);
123+
124+
mainWindow.on('closed', () => {
125+
mainWindow = null;
126+
});
127+
mainWindow.on('close', (e) => {
128+
if (process.platform == 'darwin' && !appQuitting) {
129+
// On Mac, closing the window just hides it
130+
// (this is generally how single-window Mac apps
131+
// behave, eg. Mail.app)
132+
e.preventDefault();
133+
mainWindow.hide();
134+
return false;
135+
}
136+
});
137+
138+
mainWindow.webContents.on('new-window', onWindowOrNavigate);
139+
mainWindow.webContents.on('will-navigate', onWindowOrNavigate);
140+
141+
mainWindow.webContents.on('context-menu', function(ev, params) {
142+
if (params.linkURL) {
143+
onLinkContextMenu(ev, params);
144+
}
145+
});
146+
});
147+
148+
electron.app.on('window-all-closed', () => {
149+
electron.app.quit();
150+
});
151+
152+
electron.app.on('activate', () => {
153+
mainWindow.show();
154+
});
155+
156+
electron.app.on('before-quit', () => {
157+
appQuitting = true;
158+
});

‎electron/src/vectormenu.js‎

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
Copyright 2016 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
const electron = require('electron');
18+
19+
// Menu template from http://electron.atom.io/docs/api/menu/, edited
20+
const template = [
21+
{
22+
label: 'Edit',
23+
submenu: [
24+
{
25+
role: 'undo'
26+
},
27+
{
28+
role: 'redo'
29+
},
30+
{
31+
type: 'separator'
32+
},
33+
{
34+
role: 'cut'
35+
},
36+
{
37+
role: 'copy'
38+
},
39+
{
40+
role: 'paste'
41+
},
42+
{
43+
role: 'pasteandmatchstyle'
44+
},
45+
{
46+
role: 'delete'
47+
},
48+
{
49+
role: 'selectall'
50+
}
51+
]
52+
},
53+
{
54+
label: 'View',
55+
submenu: [
56+
{
57+
type: 'separator'
58+
},
59+
{
60+
role: 'resetzoom'
61+
},
62+
{
63+
role: 'zoomin'
64+
},
65+
{
66+
role: 'zoomout'
67+
},
68+
{
69+
type: 'separator'
70+
},
71+
{
72+
role: 'togglefullscreen'
73+
},
74+
{
75+
label: 'Toggle Developer Tools',
76+
accelerator: process.platform == 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
77+
click: function(item, focusedWindow) {
78+
if (focusedWindow) focusedWindow.toggleDevTools();
79+
}
80+
}
81+
]
82+
},
83+
{
84+
role: 'window',
85+
submenu: [
86+
{
87+
role: 'minimize'
88+
},
89+
{
90+
role: 'close'
91+
}
92+
]
93+
},
94+
{
95+
role: 'help',
96+
submenu: [
97+
{
98+
label: 'riot.im',
99+
click () { electron.shell.openExternal('https://riot.im/') }
100+
}
101+
]
102+
}
103+
];
104+
105+
// macOS has specific menu conventions...
106+
if (process.platform === 'darwin') {
107+
// first macOS menu is the name of the app
108+
const name = electron.app.getName()
109+
template.unshift({
110+
label: name,
111+
submenu: [
112+
{
113+
role: 'about'
114+
},
115+
{
116+
type: 'separator'
117+
},
118+
{
119+
role: 'services',
120+
submenu: []
121+
},
122+
{
123+
type: 'separator'
124+
},
125+
{
126+
role: 'hide'
127+
},
128+
{
129+
role: 'hideothers'
130+
},
131+
{
132+
role: 'unhide'
133+
},
134+
{
135+
type: 'separator'
136+
},
137+
{
138+
role: 'quit'
139+
}
140+
]
141+
})
142+
// Edit menu.
143+
// This has a 'speech' section on macOS
144+
template[1].submenu.push(
145+
{
146+
type: 'separator'
147+
},
148+
{
149+
label: 'Speech',
150+
submenu: [
151+
{
152+
role: 'startspeaking'
153+
},
154+
{
155+
role: 'stopspeaking'
156+
}
157+
]
158+
}
159+
)
160+
// Window menu.
161+
// This also has specific functionality on macOS
162+
template[3].submenu = [
163+
{
164+
label: 'Close',
165+
accelerator: 'CmdOrCtrl+W',
166+
role: 'close'
167+
},
168+
{
169+
label: 'Minimize',
170+
accelerator: 'CmdOrCtrl+M',
171+
role: 'minimize'
172+
},
173+
{
174+
label: 'Zoom',
175+
role: 'zoom'
176+
},
177+
{
178+
type: 'separator'
179+
},
180+
{
181+
label: 'Bring All to Front',
182+
role: 'front'
183+
}
184+
]
185+
};
186+
187+
module.exports = electron.Menu.buildFromTemplate(template)
188+

0 commit comments

Comments
 (0)