@lynker-desktop/electron-window-manager 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +69 -0
- package/common/index.d.ts +22 -0
- package/common/index.d.ts.map +1 -0
- package/esm/common/index.d.ts +22 -0
- package/esm/common/index.d.ts.map +1 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +5 -0
- package/esm/index.js.map +1 -0
- package/esm/main/index.d.ts +19 -0
- package/esm/main/index.d.ts.map +1 -0
- package/esm/main/index.js +254 -0
- package/esm/main/index.js.map +1 -0
- package/esm/package.json +1 -0
- package/esm/preload/index.js +27 -0
- package/esm/preload/index.js.map +1 -0
- package/esm/renderer/index.d.ts +15 -0
- package/esm/renderer/index.d.ts.map +1 -0
- package/esm/renderer/index.js +116 -0
- package/esm/renderer/index.js.map +1 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -0
- package/index.js +5 -0
- package/index.js.map +1 -0
- package/main/index.d.ts +19 -0
- package/main/index.d.ts.map +1 -0
- package/main/index.js +269 -0
- package/main/index.js.map +1 -0
- package/package.json +80 -0
- package/preload/index.js +27 -0
- package/preload/index.js.map +1 -0
- package/renderer/index.d.ts +15 -0
- package/renderer/index.d.ts.map +1 -0
- package/renderer/index.js +122 -0
- package/renderer/index.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 hui.liu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# @lynker-desktop/electron-window-manager
|
|
2
|
+
|
|
3
|
+
electron 窗口管理
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
`npm i @lynker-desktop/electron-window-manager`
|
|
8
|
+
|
|
9
|
+
## 使用
|
|
10
|
+
|
|
11
|
+
### 1. 在 [主进程](https://electronjs.org/docs/glossary#main-process) 实例化
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const {initialize} = require('@lynker-desktop/electron-window-manager/main');
|
|
15
|
+
const windowsManager = initialize({
|
|
16
|
+
preload: path.join(__dirname, '../preload/index.js')
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
win = windowsManager.create({
|
|
20
|
+
name: 'demo',
|
|
21
|
+
url: 'http://localhost:8000',
|
|
22
|
+
loadingView: {
|
|
23
|
+
url: 'http://localhost:8000/loading'
|
|
24
|
+
},
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 2. 在 [preload](https://www.electronjs.org/docs/latest/tutorial/tutorial-preload) 引入
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import '@lynker-desktop/electron-window-manager/preload'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 3. 在 [渲染进程](https://electronjs.org/docs/glossary#renderer-process) 实例化
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
import ewm from '@lynker-desktop/electron-window-manager/renderer'
|
|
39
|
+
ewm.create({
|
|
40
|
+
name: 'test',
|
|
41
|
+
url: 'https://baidu.com',
|
|
42
|
+
browserWindow: {
|
|
43
|
+
resizable: false,
|
|
44
|
+
alwaysOnTop: true,
|
|
45
|
+
focusable: false,
|
|
46
|
+
frame: false,
|
|
47
|
+
show: true,
|
|
48
|
+
hasShadow: false,
|
|
49
|
+
transparent: true,
|
|
50
|
+
width: 100,
|
|
51
|
+
height:100,
|
|
52
|
+
webPreferences: {
|
|
53
|
+
plugins: true,
|
|
54
|
+
nodeIntegration: true,
|
|
55
|
+
contextIsolation: false,
|
|
56
|
+
backgroundThrottling: false,
|
|
57
|
+
webSecurity: false,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
完整的控制接口实现参见[example](./example)。
|
|
64
|
+
|
|
65
|
+
### 运行 Example
|
|
66
|
+
|
|
67
|
+
- yarn install && cd example/renderer && yarn
|
|
68
|
+
- yarn dev
|
|
69
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BrowserWindow, BrowserWindowConstructorOptions } from 'electron';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
__ELECTRON_WINDOW_MANAGER__?: any;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export interface ElectronWindowsManagerOptions {
|
|
8
|
+
name: string;
|
|
9
|
+
url: string;
|
|
10
|
+
loadingView?: {
|
|
11
|
+
url: string;
|
|
12
|
+
};
|
|
13
|
+
browserWindow?: BrowserWindowConstructorOptions;
|
|
14
|
+
openDevTools?: boolean;
|
|
15
|
+
preventOriginClose?: boolean;
|
|
16
|
+
preventOriginNavigate?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface WindowItem extends BrowserWindow {
|
|
19
|
+
/** BW别名 */
|
|
20
|
+
_name: string;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/common/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,+BAA+B,EAAE,MAAM,UAAU,CAAC;AAG/E,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,2BAA2B,CAAC,EAAE,GAAG,CAAC;KACnC;CACF;AACD,MAAM,WAAW,6BAA6B;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE;QACZ,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IACF,aAAa,CAAC,EAAE,+BAA+B,CAAC;IAChD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACnC;AAED,MAAM,WAAW,UAAW,SAAQ,aAAa;IAC/C,WAAW;IACX,KAAK,EAAE,MAAM,CAAC;CACf"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BrowserWindow, BrowserWindowConstructorOptions } from 'electron';
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
__ELECTRON_WINDOW_MANAGER__?: any;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export interface ElectronWindowsManagerOptions {
|
|
8
|
+
name: string;
|
|
9
|
+
url: string;
|
|
10
|
+
loadingView?: {
|
|
11
|
+
url: string;
|
|
12
|
+
};
|
|
13
|
+
browserWindow?: BrowserWindowConstructorOptions;
|
|
14
|
+
openDevTools?: boolean;
|
|
15
|
+
preventOriginClose?: boolean;
|
|
16
|
+
preventOriginNavigate?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface WindowItem extends BrowserWindow {
|
|
19
|
+
/** BW别名 */
|
|
20
|
+
_name: string;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,+BAA+B,EAAE,MAAM,UAAU,CAAC;AAG/E,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,2BAA2B,CAAC,EAAE,GAAG,CAAC;KACnC;CACF;AACD,MAAM,WAAW,6BAA6B;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE;QACZ,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IACF,aAAa,CAAC,EAAE,+BAA+B,CAAC;IAChD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACnC;AAED,MAAM,WAAW,UAAW,SAAQ,aAAa;IAC/C,WAAW;IACX,KAAK,EAAE,MAAM,CAAC;CACf"}
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,CAAC"}
|
package/esm/index.js
ADDED
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["throw new Error(`electron 对主进程和渲染进程使用不同的代码: \n在electron主进程中, 你应该导入“@lynker-desktop/electron-window-manager/main” \n在electron渲染过程中, 你应该导入“@lynker-desktop/electron-window-manager/renderer”\n`);\n\nexport {};"],"names":[],"mappings":"AAAA,MAAM,IAAI,KAAK,CAAC,CAAA;;;AAGf,CAAA,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { WebContents } from 'electron';
|
|
2
|
+
import type { ElectronWindowsManagerOptions, WindowItem } from '../common';
|
|
3
|
+
export declare const enable: (win: WebContents) => void;
|
|
4
|
+
export declare class WindowsManager {
|
|
5
|
+
preload?: string;
|
|
6
|
+
windows: Map<number, WindowItem>;
|
|
7
|
+
constructor(preload?: string);
|
|
8
|
+
create(options: ElectronWindowsManagerOptions): WindowItem;
|
|
9
|
+
_setLoadingView(window: WindowItem, createOptions: ElectronWindowsManagerOptions): void;
|
|
10
|
+
get(name: string): WindowItem | undefined;
|
|
11
|
+
getById(id: number): WindowItem | undefined;
|
|
12
|
+
getAll(): Map<number, WindowItem>;
|
|
13
|
+
close(name: string): undefined;
|
|
14
|
+
closeById(id: number): undefined;
|
|
15
|
+
getPreload(): string | undefined;
|
|
16
|
+
}
|
|
17
|
+
export declare let isInitialized: boolean;
|
|
18
|
+
export declare const initialize: (preload?: string) => WindowsManager;
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/main/index.ts"],"names":[],"mappings":"AACA,OAAO,EAA8B,WAAW,EAAE,MAAM,UAAU,CAAA;AAGlE,OAAO,KAAK,EAAE,6BAA6B,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAM3E,eAAO,MAAM,MAAM,QAAS,WAAW,SAEtC,CAAA;AAED,qBAAa,cAAc;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;gBACrB,OAAO,CAAC,EAAE,MAAM;IAK5B,MAAM,CAAC,OAAO,EAAE,6BAA6B;IAsD7C,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,6BAA6B;IA+EhF,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS;IAUzC,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS;IAU3C,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC;IAIjC,KAAK,CAAC,IAAI,EAAE,MAAM;IAalB,SAAS,CAAC,EAAE,EAAE,MAAM;IAapB,UAAU;CAGX;AAID,eAAO,IAAI,aAAa,SAAQ,CAAC;AAEjC,eAAO,MAAM,UAAU,aAAc,MAAM,KAAG,cAoG7C,CAAA"}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { BrowserWindow, BrowserView } from 'electron';
|
|
3
|
+
import * as remote from '@electron/remote/main';
|
|
4
|
+
import eIpc from '@lynker-desktop/electron-ipc/main';
|
|
5
|
+
|
|
6
|
+
if (!remote.isInitialized()) {
|
|
7
|
+
remote.initialize();
|
|
8
|
+
}
|
|
9
|
+
const enable = (win) => {
|
|
10
|
+
remote.enable(win);
|
|
11
|
+
};
|
|
12
|
+
class WindowsManager {
|
|
13
|
+
constructor(preload) {
|
|
14
|
+
this.preload = preload;
|
|
15
|
+
this.windows = new Map();
|
|
16
|
+
}
|
|
17
|
+
create(options) {
|
|
18
|
+
const { name = 'anonymous', loadingView, browserWindow: browserWindowOptions = {}, openDevTools = false, preventOriginClose = false, } = options;
|
|
19
|
+
const window = new BrowserWindow(_.merge({
|
|
20
|
+
acceptFirstMouse: true,
|
|
21
|
+
}, browserWindowOptions, {
|
|
22
|
+
webPreferences: {
|
|
23
|
+
plugins: true,
|
|
24
|
+
nodeIntegration: true,
|
|
25
|
+
contextIsolation: false,
|
|
26
|
+
backgroundThrottling: false,
|
|
27
|
+
nativeWindowOpen: false,
|
|
28
|
+
webSecurity: false,
|
|
29
|
+
preload: browserWindowOptions?.webPreferences?.preload || this.preload,
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
remote.enable(window.webContents);
|
|
33
|
+
window._name = name;
|
|
34
|
+
if (loadingView?.url) {
|
|
35
|
+
this._setLoadingView(window, options);
|
|
36
|
+
}
|
|
37
|
+
window.on('close', (event) => {
|
|
38
|
+
if (preventOriginClose) {
|
|
39
|
+
event.preventDefault();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
this.windows.delete(window.id);
|
|
43
|
+
});
|
|
44
|
+
window.webContents.on('dom-ready', () => {
|
|
45
|
+
if (openDevTools) {
|
|
46
|
+
window.webContents.openDevTools();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
this.windows.set(window.id, window);
|
|
50
|
+
if (/^file/gi.test(options.url)) {
|
|
51
|
+
window.loadFile(options.url);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
window.loadURL(options.url);
|
|
55
|
+
}
|
|
56
|
+
window.focus();
|
|
57
|
+
return window;
|
|
58
|
+
}
|
|
59
|
+
_setLoadingView(window, createOptions) {
|
|
60
|
+
if (createOptions) {
|
|
61
|
+
const { loadingView, preventOriginNavigate = false, } = createOptions;
|
|
62
|
+
let _loadingView = new BrowserView({
|
|
63
|
+
webPreferences: {
|
|
64
|
+
contextIsolation: false,
|
|
65
|
+
nodeIntegration: true,
|
|
66
|
+
// 允许loadURL与文件路径在开发环境
|
|
67
|
+
webSecurity: false,
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
if (window.isDestroyed()) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const loadLoadingView = () => {
|
|
74
|
+
const [viewWidth, viewHeight] = window.getSize();
|
|
75
|
+
window.setBrowserView(_loadingView);
|
|
76
|
+
_loadingView.setBounds({
|
|
77
|
+
x: 0,
|
|
78
|
+
y: 0,
|
|
79
|
+
width: viewWidth || 10,
|
|
80
|
+
height: viewHeight || 10,
|
|
81
|
+
});
|
|
82
|
+
_loadingView.webContents.loadURL(loadingView?.url || '');
|
|
83
|
+
};
|
|
84
|
+
const onFailure = () => {
|
|
85
|
+
if (_loadingView.webContents && !_loadingView.webContents.isDestroyed()) {
|
|
86
|
+
_loadingView.webContents.close();
|
|
87
|
+
}
|
|
88
|
+
if (window.isDestroyed()) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (window) {
|
|
92
|
+
window.removeBrowserView(_loadingView);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
loadLoadingView();
|
|
96
|
+
window.on('resize', _.debounce(() => {
|
|
97
|
+
if (_loadingView.webContents && !_loadingView.webContents.isDestroyed()) {
|
|
98
|
+
if (window.isDestroyed()) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const [viewWidth, viewHeight] = window.getSize();
|
|
102
|
+
_loadingView.setBounds({ x: 0, y: 0, width: viewWidth || 10, height: viewHeight || 10 });
|
|
103
|
+
}
|
|
104
|
+
}, 500));
|
|
105
|
+
window.webContents.on('will-navigate', (e) => {
|
|
106
|
+
if (preventOriginNavigate) {
|
|
107
|
+
e.preventDefault();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (window.isDestroyed()) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (_loadingView.webContents && !_loadingView.webContents.isDestroyed()) {
|
|
114
|
+
window.setBrowserView(_loadingView);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
// if loadingView has been destroyed
|
|
118
|
+
_loadingView = new BrowserView();
|
|
119
|
+
loadLoadingView();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
window.webContents.on('dom-ready', onFailure);
|
|
123
|
+
window.webContents.on('crashed', onFailure);
|
|
124
|
+
window.webContents.on('unresponsive', onFailure);
|
|
125
|
+
window.webContents.on('did-fail-load', onFailure);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
get(name) {
|
|
129
|
+
let win = undefined;
|
|
130
|
+
this.windows.forEach((i) => {
|
|
131
|
+
if (i._name === name) {
|
|
132
|
+
win = i;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
return win;
|
|
136
|
+
}
|
|
137
|
+
getById(id) {
|
|
138
|
+
let win = undefined;
|
|
139
|
+
this.windows.forEach((i) => {
|
|
140
|
+
if (i.id === id) {
|
|
141
|
+
win = i;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
return win;
|
|
145
|
+
}
|
|
146
|
+
getAll() {
|
|
147
|
+
return this.windows;
|
|
148
|
+
}
|
|
149
|
+
close(name) {
|
|
150
|
+
let win = undefined;
|
|
151
|
+
this.windows.forEach((i) => {
|
|
152
|
+
if (i._name === name) {
|
|
153
|
+
win = i;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
// @ts-ignore
|
|
157
|
+
win && this.windows.delete(win?.id);
|
|
158
|
+
// @ts-ignore
|
|
159
|
+
return win && win?.destroy();
|
|
160
|
+
}
|
|
161
|
+
closeById(id) {
|
|
162
|
+
let win = undefined;
|
|
163
|
+
this.windows.forEach((i) => {
|
|
164
|
+
if (i.id === id) {
|
|
165
|
+
win = i;
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
win && this.windows.delete(win?.id);
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
return win && win?.destroy();
|
|
172
|
+
}
|
|
173
|
+
getPreload() {
|
|
174
|
+
return this.preload;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
global['__ELECTRON_WINDOWS_MANAGER__'] = undefined;
|
|
179
|
+
let isInitialized = false;
|
|
180
|
+
const initialize = (preload) => {
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
if (global['__ELECTRON_WINDOWS_MANAGER__']) {
|
|
183
|
+
// @ts-ignore
|
|
184
|
+
return global['__ELECTRON_WINDOWS_MANAGER__'];
|
|
185
|
+
}
|
|
186
|
+
isInitialized = true;
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
const wm = global['__ELECTRON_WINDOWS_MANAGER__'] = new WindowsManager(preload);
|
|
189
|
+
eIpc.mainIPC.handleRenderer('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', async (data) => {
|
|
190
|
+
if (data?.type === 'create') {
|
|
191
|
+
const opt = data;
|
|
192
|
+
const findWin = wm.get(opt.data.name);
|
|
193
|
+
if (findWin) {
|
|
194
|
+
findWin.focus();
|
|
195
|
+
return {
|
|
196
|
+
winId: findWin?.id,
|
|
197
|
+
winName: findWin?._name,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
const res = wm.create(opt.data);
|
|
201
|
+
return {
|
|
202
|
+
winId: Number(res.id),
|
|
203
|
+
winName: res._name,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
if (data?.type === 'get') {
|
|
207
|
+
const opt = data;
|
|
208
|
+
const res = wm.get(opt?.data);
|
|
209
|
+
return {
|
|
210
|
+
winId: res?.id ? Number(res?.id) : -1,
|
|
211
|
+
winName: res?._name ? res?._name : '',
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
if (data?.type === 'getById') {
|
|
215
|
+
const opt = data;
|
|
216
|
+
const res = wm.getById(opt?.data);
|
|
217
|
+
return {
|
|
218
|
+
winId: res?.id ? Number(res?.id) : -1,
|
|
219
|
+
winName: res?._name ? res?._name : '',
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (data?.type === 'getAll') {
|
|
223
|
+
const res = wm.getAll();
|
|
224
|
+
const obj = {};
|
|
225
|
+
res.forEach(i => {
|
|
226
|
+
// @ts-ignore
|
|
227
|
+
obj[i.id] = {
|
|
228
|
+
winId: i?.id ? Number(i?.id) : -1,
|
|
229
|
+
winName: i?._name ? i?._name : '',
|
|
230
|
+
};
|
|
231
|
+
});
|
|
232
|
+
return obj;
|
|
233
|
+
}
|
|
234
|
+
if (data?.type === 'close') {
|
|
235
|
+
const opt = data;
|
|
236
|
+
const res = wm.close(opt?.data);
|
|
237
|
+
return res;
|
|
238
|
+
}
|
|
239
|
+
if (data?.type === 'closeById') {
|
|
240
|
+
const opt = data;
|
|
241
|
+
const res = wm.closeById(opt?.data);
|
|
242
|
+
return res;
|
|
243
|
+
}
|
|
244
|
+
if (data?.type === 'getPreload') {
|
|
245
|
+
const res = wm.getPreload();
|
|
246
|
+
return res;
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
});
|
|
250
|
+
return wm;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
export { WindowsManager, enable, initialize, isInitialized };
|
|
254
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/main/index.ts"],"sourcesContent":["import _ from 'lodash'\nimport { BrowserWindow, BrowserView, WebContents } from 'electron'\nimport * as remote from '@electron/remote/main'\nimport eIpc from '@lynker-desktop/electron-ipc/main'\nimport type { ElectronWindowsManagerOptions, WindowItem } from '../common';\n\nif (!remote.isInitialized()) {\n remote.initialize();\n}\n\nexport const enable = (win: WebContents) => {\n remote.enable(win)\n}\n\nexport class WindowsManager {\n preload?: string;\n windows: Map<number, WindowItem>;\n constructor(preload?: string) {\n this.preload = preload;\n this.windows = new Map();\n }\n\n create(options: ElectronWindowsManagerOptions) {\n const {\n name = 'anonymous',\n loadingView,\n browserWindow: browserWindowOptions = {},\n openDevTools = false,\n preventOriginClose = false,\n } = options;\n const window = new BrowserWindow(\n _.merge(\n {\n acceptFirstMouse: true,\n },\n browserWindowOptions,\n {\n webPreferences: {\n plugins: true,\n nodeIntegration: true,\n contextIsolation: false,\n backgroundThrottling: false,\n nativeWindowOpen: false,\n webSecurity: false,\n preload: browserWindowOptions?.webPreferences?.preload || this.preload,\n }\n },\n )\n ) as WindowItem;\n remote.enable(window.webContents);\n window._name = name;\n if (loadingView?.url) {\n this._setLoadingView(window, options);\n }\n window.on('close', (event) => {\n if (preventOriginClose) {\n event.preventDefault();\n return;\n }\n this.windows.delete(window.id)\n });\n window.webContents.on('dom-ready', () => {\n if (openDevTools) {\n window.webContents.openDevTools();\n }\n });\n this.windows.set(window.id, window);\n if (/^file/gi.test(options.url)) {\n window.loadFile(options.url)\n } else {\n window.loadURL(options.url)\n }\n window.focus();\n return window;\n }\n\n _setLoadingView(window: WindowItem, createOptions: ElectronWindowsManagerOptions) {\n if (createOptions) {\n const {\n loadingView,\n preventOriginNavigate = false,\n } = createOptions;\n let _loadingView = new BrowserView({\n webPreferences: {\n contextIsolation: false,\n nodeIntegration: true,\n // 允许loadURL与文件路径在开发环境\n webSecurity: false,\n }\n });\n\n if (window.isDestroyed()) {\n return;\n }\n\n const loadLoadingView = () => {\n const [ viewWidth, viewHeight ] = window.getSize();\n window.setBrowserView(_loadingView);\n _loadingView.setBounds({\n x: 0,\n y: 0,\n width: viewWidth || 10,\n height: viewHeight || 10,\n });\n _loadingView.webContents.loadURL(loadingView?.url || '');\n };\n\n const onFailure = () => {\n if (_loadingView.webContents && !_loadingView.webContents.isDestroyed()) {\n _loadingView.webContents.close();\n }\n if (window.isDestroyed()) {\n return;\n }\n\n if (window) {\n window.removeBrowserView(_loadingView);\n }\n };\n\n loadLoadingView();\n\n window.on('resize', _.debounce(() => {\n if (_loadingView.webContents && !_loadingView.webContents.isDestroyed()) {\n if (window.isDestroyed()) {\n return;\n }\n const [ viewWidth, viewHeight ] = window.getSize();\n _loadingView.setBounds({ x: 0, y: 0, width: viewWidth || 10, height: viewHeight || 10 });\n }\n }, 500));\n\n window.webContents.on('will-navigate', (e) => {\n if (preventOriginNavigate) {\n e.preventDefault();\n return;\n }\n if (window.isDestroyed()) {\n return;\n }\n if (_loadingView.webContents && !_loadingView.webContents.isDestroyed()) {\n window.setBrowserView(_loadingView);\n } else {\n // if loadingView has been destroyed\n _loadingView = new BrowserView();\n loadLoadingView();\n }\n });\n window.webContents.on('dom-ready', onFailure);\n window.webContents.on('crashed', onFailure);\n window.webContents.on('unresponsive', onFailure);\n window.webContents.on('did-fail-load', onFailure);\n }\n }\n\n get(name: string): WindowItem | undefined {\n let win: WindowItem | undefined = undefined;\n this.windows.forEach((i: WindowItem) => {\n if (i._name === name) {\n win = i;\n }\n })\n return win;\n }\n\n getById(id: number): WindowItem | undefined {\n let win: WindowItem | undefined = undefined;\n this.windows.forEach((i: WindowItem) => {\n if (i.id === id) {\n win = i;\n }\n })\n return win;\n }\n\n getAll(): Map<number, WindowItem> {\n return this.windows;\n }\n\n close(name: string) {\n let win: WindowItem | undefined = undefined;\n this.windows.forEach((i: WindowItem) => {\n if (i._name === name) {\n win = i;\n }\n })\n // @ts-ignore\n win && this.windows.delete(win?.id)\n // @ts-ignore\n return win && win?.destroy();\n }\n\n closeById(id: number) {\n let win: WindowItem | undefined = undefined;\n this.windows.forEach((i: WindowItem) => {\n if (i.id === id) {\n win = i;\n }\n })\n // @ts-ignore\n win && this.windows.delete(win?.id)\n // @ts-ignore\n return win && win?.destroy();\n }\n\n getPreload() {\n return this.preload;\n }\n}\n// @ts-ignore\nglobal['__ELECTRON_WINDOWS_MANAGER__'] = undefined;\n\nexport let isInitialized = false;\n\nexport const initialize = (preload?: string): WindowsManager => {\n // @ts-ignore\n if (global['__ELECTRON_WINDOWS_MANAGER__']) {\n // @ts-ignore\n return global['__ELECTRON_WINDOWS_MANAGER__'];\n }\n isInitialized = true;\n // @ts-ignore\n const wm = global['__ELECTRON_WINDOWS_MANAGER__'] = new WindowsManager(preload)\n eIpc.mainIPC.handleRenderer('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', async (data: any) => {\n if (data?.type === 'create') {\n const opt = data as {\n type: 'create';\n data: ElectronWindowsManagerOptions\n };\n const findWin = wm.get(opt.data.name);\n if (findWin) {\n findWin.focus();\n return {\n winId: findWin?.id,\n winName: findWin?._name,\n };\n }\n const res = wm.create(opt.data)\n return {\n winId: Number(res.id),\n winName: res._name,\n };\n }\n if (data?.type === 'get') {\n const opt = data as {\n type: 'get';\n data: string;\n };\n const res = wm.get(opt?.data)\n return {\n winId: res?.id ? Number(res?.id) : -1,\n winName: res?._name ? res?._name : '',\n };\n }\n\n if (data?.type === 'getById') {\n const opt = data as {\n type: 'get';\n data: number;\n };\n const res = wm.getById(opt?.data)\n return {\n winId: res?.id ? Number(res?.id) : -1,\n winName: res?._name ? res?._name : '',\n };\n }\n\n if (data?.type === 'getAll') {\n const opt = data as {\n type: 'getAll';\n data: any;\n };\n const res = wm.getAll()\n const obj = {}\n res.forEach(i => {\n // @ts-ignore\n obj[i.id] = {\n winId: i?.id ? Number(i?.id) : -1,\n winName: i?._name ? i?._name : '',\n }\n })\n return obj;\n }\n\n if (data?.type === 'close') {\n const opt = data as {\n type: 'close';\n data: string;\n };\n const res = wm.close(opt?.data)\n return res;\n }\n\n if (data?.type === 'closeById') {\n const opt = data as {\n type: 'closeById';\n data: number;\n };\n const res = wm.closeById(opt?.data)\n return res;\n }\n\n if (data?.type === 'getPreload') {\n const opt = data as {\n type: 'getPreload';\n data: number;\n };\n const res = wm.getPreload()\n return res;\n }\n\n return undefined\n })\n return wm;\n}\n"],"names":[],"mappings":";;;;;AAMA,IAAI,CAAC,MAAM,CAAC,aAAa,EAAE,EAAE;IAC3B,MAAM,CAAC,UAAU,EAAE,CAAC;AACtB,CAAC;AAEY,MAAA,MAAM,GAAG,CAAC,GAAgB,KAAI;AACzC,IAAA,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACpB,EAAC;MAEY,cAAc,CAAA;AAGzB,IAAA,WAAA,CAAY,OAAgB,EAAA;AAC1B,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AACvB,QAAA,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;KAC1B;AAED,IAAA,MAAM,CAAC,OAAsC,EAAA;QAC3C,MAAM,EACJ,IAAI,GAAG,WAAW,EAClB,WAAW,EACX,aAAa,EAAE,oBAAoB,GAAG,EAAE,EACxC,YAAY,GAAG,KAAK,EACpB,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extBridge, ipcRenderer } from 'electron';
|
|
2
|
+
import remote from '@electron/remote';
|
|
3
|
+
import '@lynker-desktop/electron-ipc/preload';
|
|
4
|
+
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
if (window.__ELECTRON_WINDOW_MANAGER__) {
|
|
7
|
+
// eslint-disable-next-line no-console
|
|
8
|
+
console.log('electron-window-manager Electron preload has already been run');
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
const config = {
|
|
12
|
+
ipcRenderer,
|
|
13
|
+
remote,
|
|
14
|
+
};
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
window.__ELECTRON_WINDOW_MANAGER__ = config;
|
|
17
|
+
if (contextBridge) {
|
|
18
|
+
// This will fail if contextIsolation is not enabled
|
|
19
|
+
try {
|
|
20
|
+
contextBridge.exposeInMainWorld('__ELECTRON_WINDOW_MANAGER__', config);
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
// console.error(e)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/src/preload/index.ts"],"sourcesContent":["import { ipcRenderer, contextBridge } from 'electron';\nimport remote from '@electron/remote';\nimport '@lynker-desktop/electron-ipc/preload'\n\n// @ts-ignore\nif (window.__ELECTRON_WINDOW_MANAGER__) {\n // eslint-disable-next-line no-console\n console.log('electron-window-manager Electron preload has already been run');\n} else {\n const config = {\n ipcRenderer,\n remote,\n }\n // @ts-ignore\n window.__ELECTRON_WINDOW_MANAGER__ = config;\n if (contextBridge) {\n // This will fail if contextIsolation is not enabled\n try {\n contextBridge.exposeInMainWorld('__ELECTRON_WINDOW_MANAGER__', config);\n } catch (e) {\n // console.error(e)\n }\n }\n}\n"],"names":[],"mappings":";;;;AAIA;AACA,IAAI,MAAM,CAAC,2BAA2B,EAAE;;AAEtC,IAAA,OAAO,CAAC,GAAG,CAAC,+DAA+D,CAAC,CAAC;AAC/E,CAAC;KAAM;AACL,IAAA,MAAM,MAAM,GAAG;QACb,WAAW;QACX,MAAM;KACP,CAAA;;AAED,IAAA,MAAM,CAAC,2BAA2B,GAAG,MAAM,CAAC;IAC5C,IAAI,aAAa,EAAE;;AAEjB,QAAA,IAAI;AACF,YAAA,aAAa,CAAC,iBAAiB,CAAC,6BAA6B,EAAE,MAAM,CAAC,CAAC;SACxE;QAAC,OAAO,CAAC,EAAE;;SAEX;KACF;AACH"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Used in Renderer process */
|
|
2
|
+
import type { BrowserWindow } from 'electron';
|
|
3
|
+
import { ElectronWindowsManagerOptions } from '../common';
|
|
4
|
+
export declare const create: (options: ElectronWindowsManagerOptions) => Promise<BrowserWindow>;
|
|
5
|
+
export declare const get: (name: string) => Promise<BrowserWindow | undefined>;
|
|
6
|
+
export declare const getById: (id: number) => Promise<BrowserWindow | undefined>;
|
|
7
|
+
export declare const getAll: () => Promise<Map<number, BrowserWindow>>;
|
|
8
|
+
export declare const close: (name: string) => Promise<any>;
|
|
9
|
+
export declare const closeById: (id: number) => Promise<any>;
|
|
10
|
+
/**
|
|
11
|
+
* 获取 preload
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export declare const getPreload: () => Promise<string | undefined>;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/renderer/index.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,KAAK,EAAC,aAAa,EAAc,MAAM,UAAU,CAAA;AACxD,OAAO,EAAE,6BAA6B,EAAE,MAAM,WAAW,CAAC;AAyB1D,eAAO,MAAM,MAAM,YAAmB,6BAA6B,KAAG,OAAO,CAAC,aAAa,CAU1F,CAAA;AAED,eAAO,MAAM,GAAG,SAAgB,MAAM,KAAG,OAAO,CAAC,aAAa,GAAG,SAAS,CAWzE,CAAA;AAED,eAAO,MAAM,OAAO,OAAc,MAAM,KAAG,OAAO,CAAC,aAAa,GAAG,SAAS,CAU3E,CAAA;AAED,eAAO,MAAM,MAAM,QAAa,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAiBjE,CAAA;AAED,eAAO,MAAM,KAAK,SAAgB,MAAM,KAAG,OAAO,CAAC,GAAG,CAGrD,CAAA;AAED,eAAO,MAAM,SAAS,OAAc,MAAM,KAAG,OAAO,CAAC,GAAG,CAGvD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,UAAU,QAEH,OAAO,CAAC,MAAM,GAAG,SAAS,CAc1C,CAAA"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import eIpc from '@lynker-desktop/electron-ipc/renderer';
|
|
2
|
+
|
|
3
|
+
const getIpc = () => {
|
|
4
|
+
try {
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
return window?.__ELECTRON_WINDOW_MANAGER__?.ipcRenderer || window?.require('electron').ipcRenderer;
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
console.error('getIpc error: ', error);
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const getRemote = () => {
|
|
14
|
+
try {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
return window?.__ELECTRON_WINDOW_MANAGER__?.remote || window?.require('@electron/remote');
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
console.error('getIpc error: ', error);
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const create = async (options) => {
|
|
24
|
+
const remote = getRemote();
|
|
25
|
+
const data = await eIpc.RendererIPC.invokeMain('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', { type: 'create', data: options });
|
|
26
|
+
const allW = remote.BrowserWindow.getAllWindows();
|
|
27
|
+
const win = allW.find(i => i.id === data.winId);
|
|
28
|
+
if (win) {
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
win && (win['_name'] = data.winName);
|
|
31
|
+
}
|
|
32
|
+
return win;
|
|
33
|
+
};
|
|
34
|
+
const get = async (name) => {
|
|
35
|
+
const remote = getRemote();
|
|
36
|
+
const data = await getIpc().invoke('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', { type: 'get', data: name });
|
|
37
|
+
const allW = remote.BrowserWindow.getAllWindows();
|
|
38
|
+
const win = allW.find(i => i.id === data.winId);
|
|
39
|
+
console.error(allW, data, win);
|
|
40
|
+
if (win) {
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
win && (win['_name'] = data.winName);
|
|
43
|
+
}
|
|
44
|
+
return win;
|
|
45
|
+
};
|
|
46
|
+
const getById = async (id) => {
|
|
47
|
+
const remote = getRemote();
|
|
48
|
+
const data = await getIpc().invoke('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', { type: 'getById', data: name });
|
|
49
|
+
const allW = remote.BrowserWindow.getAllWindows();
|
|
50
|
+
const win = allW.find(i => i.id === data.winId);
|
|
51
|
+
if (win) {
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
win && (win['_name'] = data.winName);
|
|
54
|
+
}
|
|
55
|
+
return win;
|
|
56
|
+
};
|
|
57
|
+
const getAll = async () => {
|
|
58
|
+
const remote = getRemote();
|
|
59
|
+
const data = await getIpc().invoke('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', { type: 'getAll', data: undefined });
|
|
60
|
+
const wins = new Map();
|
|
61
|
+
const allW = remote.BrowserWindow.getAllWindows();
|
|
62
|
+
for (const key in data) {
|
|
63
|
+
const element = data[key];
|
|
64
|
+
const win = allW.find(i => {
|
|
65
|
+
return `${i.id}` === `${element.winId}`;
|
|
66
|
+
});
|
|
67
|
+
if (win) {
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
win && (win['_name'] = element.winName);
|
|
70
|
+
wins.set(key, win);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return wins;
|
|
74
|
+
};
|
|
75
|
+
const close = async (name) => {
|
|
76
|
+
const data = await getIpc().invoke('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', { type: 'close', data: name });
|
|
77
|
+
return data;
|
|
78
|
+
};
|
|
79
|
+
const closeById = async (id) => {
|
|
80
|
+
const data = await getIpc().invoke('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', { type: 'closeById', data: id });
|
|
81
|
+
return data;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* 获取 preload
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
const getPreload = (() => {
|
|
88
|
+
let cachePreload = undefined;
|
|
89
|
+
return async () => {
|
|
90
|
+
if (cachePreload) {
|
|
91
|
+
return cachePreload;
|
|
92
|
+
}
|
|
93
|
+
const data = await getIpc().invoke('__ELECTRON_WINDOW_MANAGER_IPC_CHANNEL__', {
|
|
94
|
+
type: 'getPreload',
|
|
95
|
+
data: undefined,
|
|
96
|
+
});
|
|
97
|
+
cachePreload = data;
|
|
98
|
+
return data;
|
|
99
|
+
};
|
|
100
|
+
})();
|
|
101
|
+
getPreload();
|
|
102
|
+
// const handleOpenDevTools = (e: HTMLElement, ev: KeyboardEvent): any => {
|
|
103
|
+
// const remote = getRemote()
|
|
104
|
+
// const webContents = remote.getCurrentWebContents()
|
|
105
|
+
// webContents.openDevTools({
|
|
106
|
+
// mode: 'detach'
|
|
107
|
+
// })
|
|
108
|
+
// return '';
|
|
109
|
+
// }
|
|
110
|
+
// export const registerDevTools = () => {
|
|
111
|
+
// document.body.removeEventListener('keydown', handleOpenDevTools)
|
|
112
|
+
// document.body.addEventListener('keydown', handleOpenDevTools)
|
|
113
|
+
// }
|
|
114
|
+
|
|
115
|
+
export { close, closeById, create, get, getAll, getById, getPreload };
|
|
116
|
+
//# sourceMappingURL=index.js.map
|