-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhost.js
92 lines (76 loc) · 2.6 KB
/
host.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
const path = require('path');
const EventEmitter = require('events');
const { IPCChannel } = require('./global.js');
const events = require("./enums");
class Host extends EventEmitter {
constructor(mod, file, options, createWindow = true, rootFolder = null) {
super();
this.setMaxListeners(0);
this.mod = mod;
this.file = file;
this.options = options;
this.rootFolder = rootFolder;
this.ipc = null;
this.window = null;
if (createWindow)
this.show();
}
destructor() {
this.close();
this.mod = null;
}
show(file = null, options = null) {
if (this.window !== null) {
this.window.show();
return;
}
const { BrowserWindow } = require('electron');
this.ipc = require('electron').ipcMain;
//hotfix for electron deprecation message
let opt = options || this.options;
opt = {...opt};
if(!opt.webPreferences || !opt.webPreferences.contextIsolation || !opt.webPreferences.nodeIntegration ) {
if(!opt.webPreferences) opt.webPreferences = {};
opt.webPreferences.contextIsolation = false;
opt.webPreferences.nodeIntegration = true;
}
this.window = new BrowserWindow(opt);
this.window.loadFile(path.join(this.rootFolder || this.mod.info.path, file || this.file));
this.window.on('closed', () => { this._onClosed(); this.window = null; });
this._handleEvent = this._handleEvent.bind(this);
this.ipc.on(IPCChannel, this._handleEvent);
}
hide() {
if (this.window !== null)
this.window.hide();
}
_onClosed() {
if (this.ipc !== null) {
this.ipc.removeListener(IPCChannel, this._handleEvent);
this.ipc = null;
}
}
close() {
if (this.window !== null) {
this._onClosed();
this.window.close();
this.window = null;
}
}
_handleEvent(event, name, ...args) {
if (this.window && event.sender.id === this.window.id) {
switch(name) {
case(events.CLOSE_EVENT): this.close(); break;
case(events.MINIMIZE_EVENT): this.window.minimize(); break;
case(events.MAXIMIZE_EVENT): this.window.maximize(); break;
default: this.emit(name, ...args);
}
}
}
send(name, ...args) {
if (this.window !== null)
return this.window.webContents.send(IPCChannel, name, ...args);
return false;
}
}
module.exports = Host;