-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
93 lines (76 loc) · 1.82 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
const {app, ipcMain, BrowserWindow} = require('electron')
const path = require('path');
let background;
let windows = [];
let nextWindowIndex = 0;
let windowCount = 0;
const createBackground = () => {
background = new BrowserWindow({
width: 400,
height: 300,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
background.loadFile('background.html');
}
const createWindow = () => {
const window = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: false,
preload: path.join(app.getAppPath(), 'window-preload.js'),
}
});
const windowIndex = nextWindowIndex++;
windowCount++;
windows[windowIndex] = window;
window.loadFile('window.html');
window.on('closed', () => {
windows[windowIndex] = null;
windowCount--;
if (windowCount === 0) {
background.close();
}
});
};
app.on('ready', () => {
createBackground();
createWindow();
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', function () {
if (windowCount === 0) {
createWindow()
}
});
ipcMain.on('poc/increment', () => {
forwardToBackground('poc/increment');
});
ipcMain.on('poc/get-counter', () => {
forwardToBackground('poc/get-counter');
});
ipcMain.on('poc/update-counter', (event, arg) => {
forwardToWindows('poc/update-counter', {counter: arg});
});
ipcMain.on('poc/new-window', () => {
createWindow();
});
const forwardToBackground = (channel, arg) => {
background.webContents.send(channel, arg);
};
const forwardToWindows = (channel, arg) => {
for (let i = 0; i < nextWindowIndex; i++) {
if (windows[i] !== null) {
windows[i].webContents.send(channel, arg);
}
}
};