-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectron.js
71 lines (64 loc) · 1.5 KB
/
electron.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
const path = require('path');
const url = require('url');
const promisify = require('util').promisify;
const EventEmitter = require('events');
class ElectronService extends EventEmitter {
constructor () {
super();
this._electron = require('electron');
this._app = this._electron.app;
this._BrowserWindow = this._electron.BrowserWindow;
this._windows = [];
}
async load (platform) {
if (typeof(this._electron) === 'string') {
throw new Error('This module can only be loaded by Electron');
}
if (!this._app.isReady()) {
await promisify(c => this._app.once('ready', c));
}
this._app.once('before-quit', e => {
e.preventDefault();
this.emit('before-quit', e);
});
}
async unload (platform) {
for (let window of this._windows) {
window.destroy();
}
}
createWindow (config, initialPath) {
if (!config) {
config = {};
}
if (!config.width) {
config.width = 800;
}
if (!config.height) {
config.height = 600;
}
if (!initialPath) {
initialPath = path.join(__dirname, 'index.html');
}
const window = new this._BrowserWindow(config);
window.loadURL(url.format({
pathname: initialPath,
protocol: 'file:',
slashes: true
}));
this._windows.push(window);
window.once('closed', () => {
this._windows.splice(this._windows.indexOf(window), 1);
return false;
});
this.emit('create-window', window);
return window;
}
getApp () {
return this._app;
}
getElectron () {
return this._electron;
}
}
module.exports = new ElectronService();