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
| const { BrowserWindow } = require('electron');
|
| /**
| * 窗口插件
| * @class
| */
| class WinAddon {
|
| constructor(app) {
| this.app = app;
| this.windowContentsIdMap = {};
| }
|
| /**
| * create window
| *
| * @function
| * @since 1.0.0
| */
| create(name, opt) {
|
| // todo 判断name是否唯一
| // if (this.windowContentsIdMap.hasOwnProperty(name)) {
| // throw new Error(`[addon] [window] Name: ${name} already exists!`);
| // }
|
| // [todo] 使用 extend, 避免多维对象被覆盖
| const options = Object.assign({
| x: 10,
| y: 10,
| width: 980,
| height: 650,
| webPreferences: {
| contextIsolation: false,
| nodeIntegration: true,
| },
| }, opt);
| const win = new BrowserWindow(options)
|
| const winContentsId = win.webContents.id;
| win.webContents.on('did-finish-load', () => {
| this.registerWCid(name, winContentsId);
| })
|
| win.webContents.on('destroyed', () => {
| this.removeWCid(name);
| })
|
| win.webContents.on('render-process-gone', (event, details) => {
| this.removeWCid(name);
| })
|
| return win;
| }
|
| /**
| * 获取窗口Contents id
| *
| * @function
| * @since 1.0.0
| */
| getWCid(name) {
| const id = this.windowContentsIdMap[name] || null;
| return id;
| }
|
| /**
| * 获取主窗口Contents id
| *
| * @function
| * @since 1.0.0
| */
| getMWCid() {
| const id = this.windowContentsIdMap['main'] || null;
| return id;
| }
|
| /**
| * 注册窗口Contents id
| *
| * @function
| * @since 1.0.0
| */
| registerWCid(name, id) {
| this.windowContentsIdMap[name] = id;
| }
|
| /**
| * 销毁窗口Contents id
| *
| * @function
| * @since 1.0.0
| */
| removeWCid(name) {
| delete this.windowContentsIdMap[name];
| }
| }
|
| module.exports = WinAddon;
|
|