Skip to content

Commit 4f18521

Browse files
committed
Set up Electron integration and builds
1 parent 602727b commit 4f18521

9 files changed

Lines changed: 174 additions & 7 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
/node_modules
66
/packages/
77
/vector/bundle.*
8+
/vector/electron-main.*
89
/vector/components.css
910
/vector/emojione/
11+
/vector/electron/
1012
/vector/config.json
1113
/vector/olm.js
1214
.DS_Store

‎package.json‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"name": "vector-web",
3+
"productName": "Riot",
4+
"main": "vector/electron-main.js",
35
"version": "0.8.1",
46
"description": "Vector webapp",
57
"author": "matrix.org",
@@ -16,15 +18,16 @@
1618
"build:modernizr": "modernizr -c .modernizr.json -d src/vector/modernizr.js",
1719
"build:css": "catw \"src/skins/vector/css/**/*.css\" -o vector/components.css --no-watch",
1820
"build:compile": "babel --source-maps -d lib src",
19-
"build:bundle": "NODE_ENV=production webpack -p lib/vector/index.js vector/bundle.js",
21+
"build:bundle": "NODE_ENV=production webpack",
2022
"build:bundle:dev": "webpack --optimize-occurence-order lib/vector/index.js vector/bundle.js",
2123
"build:staticfiles": "cpx -v node_modules/olm/olm.js vector/",
24+
"build:electron": "build -lwm",
2225
"build": "npm run build:staticfiles && npm run build:emojione && npm run build:css && npm run build:compile && npm run build:bundle",
2326
"build:dev": "npm run build:staticfiles && npm run build:emojione && npm run build:css && npm run build:compile && npm run build:bundle:dev",
2427
"package": "scripts/package.sh",
2528
"start:emojione": "cpx \"node_modules/emojione/assets/svg/*\" vector/emojione/svg/ -w",
26-
"start:js": "webpack -w src/vector/index.js vector/bundle.js",
27-
"start:js:prod": "NODE_ENV=production webpack -w src/vector/index.js vector/bundle.js",
29+
"start:js": "webpack -w",
30+
"start:js:prod": "NODE_ENV=production webpack -w",
2831
"start:skins:css": "catw \"src/skins/vector/css/**/*.css\" -o vector/components.css",
2932
"start:staticfiles": "cpx -Lwv node_modules/olm/olm.js vector/",
3033
"//cache": "Note the -c 1 below due to https://code.google.com/p/chromium/issues/detail?id=508270",
@@ -41,6 +44,7 @@
4144
"classnames": "^2.1.2",
4245
"draft-js": "^0.8.1",
4346
"extract-text-webpack-plugin": "^0.9.1",
47+
"favico.js": "^0.3.10",
4448
"filesize": "^3.1.2",
4549
"flux": "~2.0.3",
4650
"gemini-scrollbar": "matrix-org/gemini-scrollbar#b302279",
@@ -67,6 +71,7 @@
6771
"catw": "^1.0.1",
6872
"cpx": "^1.3.2",
6973
"css-raw-loader": "^0.1.1",
74+
"electron-builder": "^7.10.2",
7075
"emojione": "^2.2.3",
7176
"expect": "^1.16.0",
7277
"fs-extra": "^0.30.0",
@@ -91,5 +96,22 @@
9196
},
9297
"optionalDependencies": {
9398
"olm": "https://matrix.org/packages/npm/olm/olm-1.3.0.tgz"
99+
},
100+
"build": {
101+
"appId": "im.riot.app",
102+
"category": "Network",
103+
"electronVersion": "1.4.2",
104+
"//asar=false": "https://github.com/electron-userland/electron-builder/issues/675",
105+
"asar": false,
106+
"dereference": true,
107+
"//files": "We bundle everything, so we only need to include vector/",
108+
"files": [
109+
"!**/*",
110+
"vector/**",
111+
"package.json"
112+
]
113+
},
114+
"directories": {
115+
"output": "vector/electron"
94116
}
95117
}

‎src/electron-main.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @flow
2+
const {app, BrowserWindow} = require('electron');
3+
4+
let window = null;
5+
6+
app.on('ready', () => {
7+
window = new BrowserWindow({
8+
icon: `${__dirname}/img/logo.png`,
9+
});
10+
window.loadURL(`file://${__dirname}/index.html`);
11+
window.webContents.openDevTools();
12+
window.on('closed', () => {
13+
window = null;
14+
})
15+
});
16+
17+
app.on('window-all-closed', () => {
18+
app.quit();
19+
});

‎src/vector/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ require('gfm.css/gfm.css');
2828
require('highlight.js/styles/github.css');
2929
require('draft-js/dist/Draft.css');
3030

31-
3231
// add React and ReactPerf to the global namespace, to make them easier to
3332
// access via the console
3433
global.React = require("react");
@@ -49,6 +48,7 @@ import UAParser from 'ua-parser-js';
4948
import url from 'url';
5049

5150
import {parseQs, parseQsFromFragment} from './url_utils';
51+
import IntegrationManager from './integration';
5252

5353
var lastLocationHashSet = null;
5454

@@ -201,6 +201,7 @@ function onLoadCompleted() {
201201
async function loadApp() {
202202
const fragparts = parseQsFromFragment(window.location);
203203
const params = parseQs(window.location);
204+
const integrationManager = new IntegrationManager();
204205

205206
// don't try to redirect to the native apps if we're
206207
// verifying a 3pid
@@ -254,6 +255,7 @@ async function loadApp() {
254255
enableGuest={true}
255256
onLoadCompleted={onLoadCompleted}
256257
defaultDeviceDisplayName={getDefaultDeviceDisplayName()}
258+
integrationManager={integrationManager}
257259
/>,
258260
document.getElementById('matrixchat')
259261
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @flow
2+
3+
export default class BaseIntegrationManager {
4+
constructor() {
5+
this.notificationCount = 0;
6+
this.errorDidOccur = false;
7+
}
8+
9+
setNotificationCount(count: number) {
10+
this.notificationCount = count;
11+
}
12+
13+
setErrorStatus(errorDidOccur: boolean) {
14+
this.errorDidOccur = errorDidOccur;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @flow
2+
import BaseIntegrationManager from './BaseIntegrationManager';
3+
4+
// index.js imports us unconditionally, so we need this check here as well
5+
let electron = null, remote = null;
6+
if (window && window.process && window.process && window.process.type === 'renderer') {
7+
electron = require('electron');
8+
remote = electron.remote;
9+
}
10+
11+
export default class ElectronIntegrationManager extends BaseIntegrationManager {
12+
setNotificationCount(count: number) {
13+
super.setNotificationCount(count);
14+
remote.app.setBadgeCount(count);
15+
}
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @flow
2+
import BaseIntegrationManager from './BaseIntegrationManager';
3+
import Favico from 'favico.js';
4+
5+
export default class WebIntegrationManager extends BaseIntegrationManager {
6+
constructor() {
7+
super();
8+
this.favicon = new Favico({animation: 'popFade'});
9+
this.updateFavicon();
10+
}
11+
12+
updateFavicon() {
13+
try {
14+
// This needs to be in in a try block as it will throw
15+
// if there are more than 100 badge count changes in
16+
// its internal queue
17+
let bgColor = "#d00",
18+
notif = this.notificationCount;
19+
20+
if (this.errorDidOccur) {
21+
notif = notif || "×";
22+
bgColor = "#f00";
23+
}
24+
25+
this.favicon.badge(notif, {
26+
bgColor: bgColor
27+
});
28+
} catch (e) {
29+
console.warn(`Failed to set badge count: ${e.message}`);
30+
}
31+
}
32+
33+
setNotificationCount(count: number) {
34+
super.setNotificationCount(count);
35+
this.updateFavicon();
36+
}
37+
38+
setErrorStatus(errorDidOccur: boolean) {
39+
super.setErrorStatus(errorDidOccur);
40+
this.updateFavicon();
41+
}
42+
}

‎src/vector/integration/index.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @flow
2+
3+
import ElectronIntegrationManager from './ElectronIntegrationManager';
4+
import WebIntegrationManager from './WebIntegrationManager';
5+
6+
let IntegrationManager = null;
7+
8+
if (window && window.process && window.process && window.process.type === 'renderer') {
9+
// we're running inside electron
10+
IntegrationManager = ElectronIntegrationManager;
11+
} else {
12+
IntegrationManager = WebIntegrationManager;
13+
}
14+
15+
export default IntegrationManager;

‎webpack.config.js‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var path = require('path');
22
var webpack = require('webpack');
33
var ExtractTextPlugin = require("extract-text-webpack-plugin");
44

5-
module.exports = {
5+
module.exports = [{
6+
entry: './src/vector/index.js',
67
module: {
78
preLoaders: [
89
{ test: /\.js$/, loader: "source-map-loader" }
@@ -32,7 +33,9 @@ module.exports = {
3233
// various levels of '.' and '..'
3334
// Also, sometimes the resource path is absolute.
3435
return path.relative(process.cwd(), info.resourcePath).replace(/^[\/\.]*/, '');
35-
}
36+
},
37+
path: './vector/',
38+
filename: 'bundle.js'
3639
},
3740
resolve: {
3841
alias: {
@@ -52,6 +55,7 @@ module.exports = {
5255
// loads it into the browser global `Olm`), and reference it as an
5356
// external here.
5457
"olm": "Olm",
58+
"electron": 'commonjs electron'
5559
},
5660
plugins: [
5761
new webpack.DefinePlugin({
@@ -65,4 +69,33 @@ module.exports = {
6569
}),
6670
],
6771
devtool: 'source-map'
68-
};
72+
}, {
73+
entry: './src/electron-main.js',
74+
target: "electron",
75+
output: {
76+
devtoolModuleFilenameTemplate: function(info) {
77+
// Reading input source maps gives only relative paths here for
78+
// everything. Until I figure out how to fix this, this is a
79+
// workaround.
80+
// We use the relative resource path with any '../'s on the front
81+
// removed which gives a tree with matrix-react-sdk and vector
82+
// trees smashed together, but this fixes everything being under
83+
// various levels of '.' and '..'
84+
// Also, sometimes the resource path is absolute.
85+
return path.relative(process.cwd(), info.resourcePath).replace(/^[\/\.]*/, '');
86+
},
87+
filename: 'vector/electron-main.js'
88+
},
89+
node: {
90+
__dirname: false,
91+
__filename: false
92+
},
93+
plugins: [
94+
new webpack.DefinePlugin({
95+
'process.env': {
96+
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
97+
}
98+
}),
99+
],
100+
devtool: 'source-map',
101+
}];

0 commit comments

Comments
 (0)