@lynker-desktop/electron-window-manager 0.0.9-alpha.0 → 0.0.9-alpha.10

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.
@@ -14,6 +14,8 @@ export declare class WindowsManager {
14
14
  preloadedBV: BVItem | null;
15
15
  preloading: boolean;
16
16
  webviewDomainWhiteList: string[];
17
+ private createQueue;
18
+ private isCreating;
17
19
  /**
18
20
  * webview 域名白名单
19
21
  * 传入格式示例:
@@ -31,17 +33,41 @@ export declare class WindowsManager {
31
33
  * - 'localhost'、'127.0.0.1'、'::1' 以及局域网 IP(如 192.168.x.x、10.x.x.x、172.16.x.x~172.31.x.x)都视为本地白名单。
32
34
  */
33
35
  constructor(preload?: string, loadingViewUrl?: string, errorViewUrl?: string, preloadWebContentsUrl?: string, webviewDomainWhiteList?: string[]);
36
+ /**
37
+ * 设置预加载的webContents的url
38
+ * @param preloadWebContentsUrl 预加载的webContents的url
39
+ */
34
40
  setPreloadWebContentsUrl(preloadWebContentsUrl: string): void;
41
+ /**
42
+ * 预加载实例
43
+ */
35
44
  private _preloadInstances;
45
+ /**
46
+ * 创建预加载的窗口
47
+ * @param options 窗口选项
48
+ * @returns 预加载的窗口
49
+ */
36
50
  private _createPreloadBW;
51
+ /**
52
+ * 创建预加载的浏览器视图
53
+ * @returns 预加载的浏览器视图
54
+ */
37
55
  private _createPreloadBV;
38
56
  create(options: Omit<ElectronWindowsManagerOptions, 'type'> & {
39
57
  type?: 'BW';
40
- }): BWItem;
58
+ }): Promise<BWItem>;
41
59
  create(options: ElectronWindowsManagerOptions & {
42
60
  type: 'BV';
43
- }): BVItem;
44
- create(options: ElectronWindowsManagerOptions): BWItem | BVItem;
61
+ }): Promise<BVItem>;
62
+ create(options: ElectronWindowsManagerOptions): Promise<BVItem | BWItem>;
63
+ /**
64
+ * 处理创建队列
65
+ */
66
+ private processCreateQueue;
67
+ /**
68
+ * 实际的窗口创建逻辑
69
+ */
70
+ private _createWindow;
45
71
  _setLoadingView(window: BWItem, createOptions: ElectronWindowsManagerOptions): void;
46
72
  get(idOrName: string | number): BWItem | BVItem | undefined;
47
73
  getAll(type: 'BW'): Map<number, BWItem>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/main/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,WAAW,EAAkE,MAAM,UAAU,CAAA;AAIvI,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAE/E,eAAO,MAAM,gBAAgB,wBAQzB,CAAC;AAUL,eAAO,MAAM,MAAM,QAAS,WAAW,SAEtC,CAAA;AAoBD,qBAAa,cAAc;IACzB,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IACtC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAQ;IAClC,gCAAgC,EAAE,MAAM,GAAG,IAAI,CAAQ;IACvD,8BAA8B,EAAE,MAAM,GAAG,IAAI,CAAQ;IACrD,WAAW,EAAE,MAAM,GAAG,IAAI,CAAQ;IAClC,UAAU,EAAE,OAAO,CAAS;IAC5B,sBAAsB,EAAE,MAAM,EAAE,CAAM;IACtC;;;;;;;;;;;;;;;OAeG;gBACS,OAAO,CAAC,EAAE,MAAM,EAAE,cAAc,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,EAAE,qBAAqB,CAAC,EAAE,MAAM,EAAE,sBAAsB,CAAC,EAAE,MAAM,EAAE;IAe/I,wBAAwB,CAAC,qBAAqB,EAAE,MAAM;YAgBxC,iBAAiB;IAwB/B,OAAO,CAAC,gBAAgB;IA2CxB,OAAO,CAAC,gB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,EAAE,KAAG,cA4QhK,CAAA"}
package/esm/main/index.js CHANGED
@@ -57,12 +57,19 @@ class WindowsManager {
57
57
  * - 'localhost'、'127.0.0.1'、'::1' 以及局域网 IP(如 192.168.x.x、10.x.x.x、172.16.x.x~172.31.x.x)都视为本地白名单。
58
58
  */
59
59
  constructor(preload, loadingViewUrl, errorViewUrl, preloadWebContentsUrl, webviewDomainWhiteList) {
60
+ // 预加载的窗口
60
61
  this.preloadedBW = null;
62
+ // 预加载的窗口(无边框,有按钮)
61
63
  this.preloadedBW_FramelessWithButtons = null;
64
+ // 预加载的窗口(无边框,无按钮)
62
65
  this.preloadedBW_FramelessNoButtons = null;
66
+ // 预加载的浏览器视图
63
67
  this.preloadedBV = null;
64
68
  this.preloading = false;
65
69
  this.webviewDomainWhiteList = [];
70
+ // 创建队列相关属性
71
+ this.createQueue = [];
72
+ this.isCreating = false;
66
73
  this.preload = preload;
67
74
  this.windows = new Map();
68
75
  this.loadingViewUrl = `${loadingViewUrl ?? ''}`;
@@ -72,10 +79,16 @@ class WindowsManager {
72
79
  log('log', 'preloadWebContentsUrl: ', this.preloadWebContentsUrl);
73
80
  if (this.preloadWebContentsUrl) {
74
81
  app.on('ready', () => {
75
- this._preloadInstances();
82
+ if (this.preloadWebContentsUrl) {
83
+ this.setPreloadWebContentsUrl(this.preloadWebContentsUrl);
84
+ }
76
85
  });
77
86
  }
78
87
  }
88
+ /**
89
+ * 设置预加载的webContents的url
90
+ * @param preloadWebContentsUrl 预加载的webContents的url
91
+ */
79
92
  setPreloadWebContentsUrl(preloadWebContentsUrl) {
80
93
  try {
81
94
  this.preloadWebContentsUrl = preloadWebContentsUrl;
@@ -93,23 +106,30 @@ class WindowsManager {
93
106
  log('error', 'setPreloadWebContentsUrl error:', error);
94
107
  }
95
108
  }
109
+ /**
110
+ * 预加载实例
111
+ */
96
112
  async _preloadInstances() {
97
113
  if (this.preloading)
98
114
  return;
99
115
  this.preloading = true;
100
116
  try {
101
117
  if (this.preloadWebContentsUrl) {
118
+ // 预加载的窗口
102
119
  this.preloadedBW = this.preloadedBW || await this._createPreloadBW({});
120
+ // 预加载的窗口(无边框,有按钮)
103
121
  this.preloadedBW_FramelessWithButtons = this.preloadedBW_FramelessWithButtons || await this._createPreloadBW({
104
122
  frame: false,
105
123
  transparent: true,
106
124
  titleBarStyle: 'hidden',
107
125
  });
126
+ // 预加载的窗口(无边框,无按钮)
108
127
  this.preloadedBW_FramelessNoButtons = this.preloadedBW_FramelessNoButtons || await this._createPreloadBW({
109
128
  frame: false,
110
129
  transparent: true,
111
130
  titleBarStyle: 'customButtonsOnHover',
112
131
  });
132
+ // 预加载的BV
113
133
  this.preloadedBV = this.preloadedBV || await this._createPreloadBV();
114
134
  }
115
135
  }
@@ -118,23 +138,30 @@ class WindowsManager {
118
138
  }
119
139
  this.preloading = false;
120
140
  }
141
+ /**
142
+ * 创建预加载的窗口
143
+ * @param options 窗口选项
144
+ * @returns 预加载的窗口
145
+ */
121
146
  _createPreloadBW(options = {}) {
122
147
  return new Promise((resolve) => {
123
148
  const preload = this.preload;
124
149
  const url = this.preloadWebContentsUrl;
125
150
  if (this.preloadWebContentsUrl) {
151
+ const webPreferences = (options.webPreferences || {});
126
152
  const instance = new BrowserWindow({
127
- show: false,
128
153
  ...options,
154
+ show: false,
155
+ backgroundColor: '#ffffff',
129
156
  webPreferences: {
157
+ ...webPreferences,
130
158
  webviewTag: true,
131
159
  plugins: true,
132
160
  nodeIntegration: true,
133
161
  contextIsolation: false,
134
162
  backgroundThrottling: false,
135
163
  webSecurity: false,
136
- preload: preload,
137
- ...(options.webPreferences || {}),
164
+ preload: webPreferences.preload || preload,
138
165
  }
139
166
  });
140
167
  try {
@@ -152,30 +179,37 @@ class WindowsManager {
152
179
  }
153
180
  // @ts-ignore
154
181
  log('log', '创建预BW: ', instance._id, this.preloadWebContentsUrl);
155
- instance.webContents.once('did-finish-load', () => {
156
- resolve(instance);
157
- });
158
- instance.webContents.once('did-fail-load', () => {
159
- resolve(instance);
160
- });
161
- instance.loadURL(url || 'about:blank');
182
+ // instance.webContents.once('did-finish-load', () => {
183
+ // resolve(instance as BWItem);
184
+ // });
185
+ // instance.webContents.once('did-fail-load', () => {
186
+ // resolve(instance as BWItem);
187
+ // });
188
+ instance.loadURL(url ? `${url}` : 'about:blank');
189
+ resolve(instance);
162
190
  }
163
191
  });
164
192
  }
165
- _createPreloadBV() {
193
+ /**
194
+ * 创建预加载的浏览器视图
195
+ * @returns 预加载的浏览器视图
196
+ */
197
+ _createPreloadBV(options = {}) {
166
198
  return new Promise((resolve) => {
167
199
  const preload = this.preload;
168
200
  const url = this.preloadWebContentsUrl;
169
201
  if (this.preloadWebContentsUrl) {
202
+ const webPreferences = (options.webPreferences || {});
170
203
  const instance = new BrowserView({
171
204
  webPreferences: {
205
+ ...webPreferences,
172
206
  webviewTag: true,
173
207
  plugins: true,
174
208
  nodeIntegration: true,
175
209
  contextIsolation: false,
176
210
  // backgroundThrottling: false,
177
211
  webSecurity: false,
178
- preload: preload,
212
+ preload: webPreferences.preload || preload,
179
213
  }
180
214
  });
181
215
  try {
@@ -193,17 +227,52 @@ class WindowsManager {
193
227
  }
194
228
  // @ts-ignore
195
229
  log('log', '创建预BV: ', instance._id, this.preloadWebContentsUrl);
196
- instance.webContents.once('did-finish-load', () => {
197
- resolve(instance);
198
- });
199
- instance.webContents.once('did-fail-load', () => {
200
- resolve(instance);
201
- });
230
+ // instance.webContents.once('did-finish-load', () => {
231
+ // resolve(instance as BVItem);
232
+ // });
233
+ // instance.webContents.once('did-fail-load', () => {
234
+ // resolve(instance as BVItem);
235
+ // });
202
236
  instance.webContents.loadURL(url || 'about:blank');
237
+ resolve(instance);
203
238
  }
204
239
  });
205
240
  }
206
241
  create(options) {
242
+ return new Promise((resolve, reject) => {
243
+ // 将创建请求添加到队列
244
+ this.createQueue.push({ options, resolve, reject });
245
+ // 如果当前没有在创建,则开始处理队列
246
+ if (!this.isCreating) {
247
+ this.processCreateQueue();
248
+ }
249
+ });
250
+ }
251
+ /**
252
+ * 处理创建队列
253
+ */
254
+ async processCreateQueue() {
255
+ if (this.isCreating || this.createQueue.length === 0) {
256
+ return;
257
+ }
258
+ this.isCreating = true;
259
+ while (this.createQueue.length > 0) {
260
+ const { options, resolve, reject } = this.createQueue.shift();
261
+ try {
262
+ const window = await this._createWindow(options);
263
+ resolve(window);
264
+ }
265
+ catch (error) {
266
+ log('error', 'create window failed:', error);
267
+ reject(error);
268
+ }
269
+ }
270
+ this.isCreating = false;
271
+ }
272
+ /**
273
+ * 实际的窗口创建逻辑
274
+ */
275
+ async _createWindow(options) {
207
276
  let window;
208
277
  const { usePreload = true, type = 'BW', name = 'anonymous', url, loadingView = { url: undefined }, errorView = { url: undefined }, browserWindow: browserWindowOptions, openDevTools = false, preventOriginClose = false, } = options;
209
278
  options.type = type;
@@ -211,37 +280,60 @@ class WindowsManager {
211
280
  let preloadWin = null;
212
281
  if (type === 'BW' && usePreload && this.preloadWebContentsUrl) {
213
282
  const bwOptions = browserWindowOptions || {};
214
- if (bwOptions.frame === false && bwOptions.titleBarStyle === 'hidden') {
215
- preloadWin = this.preloadedBW_FramelessWithButtons;
216
- this.preloadedBW_FramelessWithButtons = null;
217
- setTimeout(() => this._createPreloadBW({ frame: false, transparent: true, titleBarStyle: 'hidden' }), 0);
218
- }
219
- else if (bwOptions.frame === false && bwOptions.titleBarStyle === 'customButtonsOnHover') {
220
- preloadWin = this.preloadedBW_FramelessNoButtons;
221
- this.preloadedBW_FramelessNoButtons = null;
222
- setTimeout(() => this._createPreloadBW({ frame: false, transparent: true, titleBarStyle: 'customButtonsOnHover' }), 0);
283
+ if (bwOptions.frame === false) {
284
+ if (bwOptions.titleBarStyle === 'customButtonsOnHover') {
285
+ preloadWin = this.preloadedBW_FramelessNoButtons;
286
+ this.preloadedBW_FramelessNoButtons = await this._createPreloadBW({
287
+ frame: false,
288
+ transparent: true,
289
+ titleBarStyle: 'customButtonsOnHover',
290
+ webPreferences: {
291
+ preload: bwOptions?.webPreferences?.preload || this.preload,
292
+ }
293
+ });
294
+ }
295
+ else {
296
+ preloadWin = this.preloadedBW_FramelessWithButtons;
297
+ this.preloadedBW_FramelessWithButtons = await this._createPreloadBW({
298
+ frame: false,
299
+ transparent: true,
300
+ titleBarStyle: 'hidden',
301
+ webPreferences: {
302
+ preload: this.preload,
303
+ }
304
+ });
305
+ }
223
306
  }
224
307
  else {
225
308
  preloadWin = this.preloadedBW;
226
- this.preloadedBW = null;
227
- setTimeout(() => this._createPreloadBW({}), 0);
309
+ this.preloadedBW = await this._createPreloadBW({
310
+ webPreferences: {
311
+ preload: bwOptions?.webPreferences?.preload || this.preload,
312
+ }
313
+ });
228
314
  }
229
315
  }
230
316
  if (type === 'BV' && usePreload && this.preloadWebContentsUrl) {
231
- preloadWin = this.preloadedBV;
232
- this.preloadedBV = null;
233
- setTimeout(() => this._createPreloadBV(), 0);
317
+ const bvOptions = browserWindowOptions || {};
318
+ if (this.preloadedBV) {
319
+ preloadWin = this.preloadedBV;
320
+ this.preloadedBV = await this._createPreloadBV({
321
+ webPreferences: {
322
+ preload: bvOptions?.webPreferences?.preload || this.preload,
323
+ }
324
+ });
325
+ }
234
326
  }
235
327
  if (preloadWin) {
236
328
  const win = preloadWin;
237
- log('log', `${name} 使用预加载窗口`, win._id);
329
+ log('log', `${name} 使用预加载窗口(${type})`, win._id);
238
330
  win._type = 'BW';
239
331
  win._name = options.name || 'anonymous';
240
332
  win._extraData = `${options?.extraData || ''}`;
241
333
  win._initUrl = `${options?.url || ''}`;
242
334
  // @ts-ignore
243
- win?.removeAllListeners && win?.removeAllListeners?.();
244
- win.webContents.removeAllListeners && win.webContents.removeAllListeners();
335
+ // win?.removeAllListeners && win?.removeAllListeners?.();
336
+ // win.webContents.removeAllListeners && win.webContents.removeAllListeners();
245
337
  if (type === 'BW') {
246
338
  // @ts-ignore
247
339
  this._applyBrowserWindowOptions(win, options);
@@ -323,6 +415,8 @@ class WindowsManager {
323
415
  log('error', 'enable: ', error);
324
416
  }
325
417
  }
418
+ // 停止加载
419
+ window.webContents?.stop?.();
326
420
  // @ts-ignore
327
421
  try {
328
422
  window.id = Number(`${window.id || window.webContents.id}`);
@@ -351,7 +445,7 @@ class WindowsManager {
351
445
  if (errorView?.url) {
352
446
  if (type === 'BW') {
353
447
  const showErrorView = lodash.debounce(() => {
354
- const _url = window?.webContents?.getURL?.() || url;
448
+ const _url = window._initUrl;
355
449
  /**
356
450
  * 判断是否是错误视图
357
451
  */
@@ -365,16 +459,37 @@ class WindowsManager {
365
459
  sessionStorage.setItem(key, "${_url}");
366
460
  `);
367
461
  }
368
- }, 300);
462
+ }, 1000);
369
463
  // @ts-ignore
370
464
  window.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL, isMainFrame) => {
371
465
  if (isMainFrame) {
372
466
  showErrorView();
373
467
  }
374
468
  });
469
+ // 当开始加载时取消错误视图
375
470
  window.webContents.on('did-start-loading', () => {
376
471
  showErrorView.cancel();
377
472
  });
473
+ // 当导航开始时取消错误视图
474
+ window.webContents.on('did-start-navigation', () => {
475
+ showErrorView.cancel();
476
+ });
477
+ // 当页面重新加载时取消错误视图
478
+ window.webContents.on('did-navigate', () => {
479
+ showErrorView.cancel();
480
+ });
481
+ // 当页面完成加载时取消错误视图
482
+ window.webContents.on('did-finish-load', () => {
483
+ showErrorView.cancel();
484
+ });
485
+ // 当窗口关闭时取消错误视图
486
+ window.webContents.on('close', () => {
487
+ showErrorView.cancel();
488
+ });
489
+ // 当窗口销毁时取消错误视图
490
+ window.webContents.on('destroyed', () => {
491
+ showErrorView.cancel();
492
+ });
378
493
  }
379
494
  }
380
495
  window.webContents.on('did-attach-webview', (_event, webContents) => {
@@ -456,10 +571,6 @@ class WindowsManager {
456
571
  parentWin?.addBrowserView(window);
457
572
  log('log', 'create - addBrowserView');
458
573
  }
459
- // @ts-ignore
460
- window.loadURL ? window.loadURL(options.url) : window.webContents.loadURL(options.url);
461
- // @ts-ignore
462
- window.focus ? window.focus() : window.webContents.focus();
463
574
  this.windows.set(window.id || window._id || window.webContents.id, window);
464
575
  log('log', 'create', this.windows.keys());
465
576
  // 初始化值
@@ -552,6 +663,12 @@ class WindowsManager {
552
663
  log('error', 'focus', error);
553
664
  }
554
665
  }
666
+ if (options.url) {
667
+ // @ts-ignore
668
+ window.loadURL ? window.loadURL(options.url) : window.webContents.loadURL(options.url);
669
+ // @ts-ignore
670
+ window.focus ? window.focus() : window.webContents.focus();
671
+ }
555
672
  }
556
673
  catch (error) {
557
674
  log('error', 'create', error);
@@ -712,7 +829,7 @@ class WindowsManager {
712
829
  });
713
830
  }
714
831
  // @ts-ignore
715
- win?.webContent?.close?.();
832
+ win?.webContents?.destroy?.();
716
833
  // @ts-ignore
717
834
  win?.close?.();
718
835
  // @ts-ignore
@@ -735,7 +852,7 @@ class WindowsManager {
735
852
  });
736
853
  }
737
854
  // @ts-ignore
738
- win?.webContent?.close?.();
855
+ win?.webContents?.destroy?.();
739
856
  // @ts-ignore
740
857
  win?.close?.();
741
858
  // @ts-ignore
@@ -815,6 +932,11 @@ class WindowsManager {
815
932
  if (typeof browserWindowOptions.alwaysOnTop === 'boolean') {
816
933
  win.setAlwaysOnTop(browserWindowOptions.alwaysOnTop);
817
934
  }
935
+ // 设置背景颜色
936
+ if (typeof browserWindowOptions.backgroundColor === 'string') {
937
+ win.setBackgroundColor(browserWindowOptions.backgroundColor);
938
+ }
939
+ win.center();
818
940
  // 可继续扩展其他动态属性
819
941
  }
820
942
  _applyBrowserViewOptions(view, options) {
@@ -835,7 +957,7 @@ class WindowsManager {
835
957
  // 生成一个bv 做为预加载资源窗口,加载完成后销毁
836
958
  async createPreloadWebContents(url) {
837
959
  return new Promise(async (resolve, reject) => {
838
- let bv = this.create({
960
+ let bv = await this.create({
839
961
  type: 'BV',
840
962
  url,
841
963
  name: `preload-web-contents-${md5(url)}`,
@@ -890,7 +1012,7 @@ const initialize = (preload, loadingViewUrl, errorViewUrl, preloadWebContentsUrl
890
1012
  winInitUrl: `${findWin?._initUrl || ''}`,
891
1013
  };
892
1014
  }
893
- const res = wm.create(opt.data);
1015
+ const res = await wm.create(opt.data);
894
1016
  return {
895
1017
  winId: Number(`${res.id || res._id || -1}`),
896
1018
  winName: `${res?._name || ''}`,