-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.js
131 lines (111 loc) · 3.3 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const path = require('path');
const menubar = require('menubar')
const electron = require('electron');
const debug = require('debug')('lsapp:main');
const backend = require('./backend');
const Model = require('./lib/model');
const appModelController = require('./lib/controller/app-model');
const connect = require('./lib/client');
const autoUpdate = require('./lib/autoupdate');
const pkg = require('./package.json');
if (require('electron-squirrel-startup')) {
return;
}
const ipc = electron.ipcMain;
const BrowserWindow = electron.BrowserWindow;
const startAutoupdateTimeout = 20 * 1000; // when to start auto-update polling
// XXX init
require('electron-debug')();
var appModel = new Model();
var app = menubar({
width: 400,
height: 360,
resizable: false,
'always-on-top': process.argv.indexOf('--on-top') !== -1,
icon: path.resolve(__dirname, `assets/${process.platform === 'win32' ? 'menu-icon.ico' : 'menu-iconTemplate.png'}`)
})
.on('ready', function() {
connect(pkg.config.websocketUrl, (err, client) => {
if (err) {
return error(err);
}
info('Client connected');
// supress 'error' event since in Node.js, in most cases it means unhandled exception
client.on('error', err => console.error(err));
var controller = appModelController(appModel, client);
backend(client);
updateMainWindow(appModel);
setupAppEvents(app, controller);
initialWindowDisplay(app);
setupAutoUpdate(appModel);
});
})
.on('show', () => updateMainWindow(appModel))
.on('after-create-window', () => {
app.window.webContents.on('did-finish-load', () => updateMainWindow(appModel));
});
appModel.on('change', function() {
debug('model update %o', this.attributes);
updateMainWindow(this);
});
////////////////////////////////////
function setupAppEvents(app, controller) {
ipc.on('install-plugin', function(event, id) {
log(`install plugin ${id}`);
controller.install(id).catch(error);
})
.on('install-update', () => electron.autoUpdater.quitAndInstall())
.on('rv-close-session', (event, key) => backend.closeRvSession(key))
.on('quit', () => app && app.app && app.app.quit());
}
function setupAutoUpdate(model) {
setTimeout(() => {
autoUpdate(pkg)
.on('update-downloaded', () => model.set('updateAvailable', true))
.on('update-not-available', () => model.set('updateAvailable', false))
.on('error', error);
}, startAutoupdateTimeout).unref();
}
function toArray(obj) {
return Array.prototype.slice.call(obj, 0);
}
function updateMainWindow(model) {
if (model) {
_send('model', model.toJSON());
}
}
/**
* Initial window display when app starts: do not quit app when window requested
* it for the first time
* @param {App} app
* @param {BrowserWindow} wnd
*/
function initialWindowDisplay(menuApp) {
var handled = false;
ipc.on('will-quit', evt => {
if (!handled) {
evt.returnValue = handled = true;
menuApp.hideWindow();
} else {
evt.returnValue = false;
}
});
menuApp.once('after-hide', () => handled = true).showWindow();
}
function log() {
debug.apply(null, toArray(arguments));
_send('log', toArray(arguments));
}
function warn() {
_send('warn', toArray(arguments));
}
function info() {
_send('info', toArray(arguments));
}
function error() {
_send('error', toArray(arguments));
}
function _send(name, args) {
app.window && app.window.webContents.send(name, args);
}