@lynker-desktop/electron-window-manager 0.0.7-alpha.8 → 0.0.7

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.
@@ -13,16 +13,11 @@ export declare class WindowsManager {
13
13
  preloadedBW_FramelessNoButtons: BWItem | null;
14
14
  preloadedBV: BVItem | null;
15
15
  preloading: boolean;
16
- constructor(preload?: string, loadingViewUrl?: string, errorViewUrl?: string, preloadUrl?: string);
16
+ constructor(preload?: string, loadingViewUrl?: string, errorViewUrl?: string, preloadWebContentsUrl?: string);
17
17
  setPreloadWebContentsUrl(preloadWebContentsUrl: string): void;
18
18
  private _preloadInstances;
19
19
  private _createPreloadBW;
20
20
  private _createPreloadBV;
21
- /**
22
- * 预加载webContents
23
- * @param url
24
- */
25
- preloadWebContents(url: string): void;
26
21
  create(options: Omit<ElectronWindowsManagerOptions, 'type'> & {
27
22
  type?: 'BW';
28
23
  }): BWItem;
@@ -37,6 +32,7 @@ export declare class WindowsManager {
37
32
  getAll(): Map<number, BWItem | BVItem>;
38
33
  close(idOrName: string | number): boolean;
39
34
  closeAll(): void;
35
+ rename(idOrName: string | number, newName: string): BWItem | BVItem | undefined;
40
36
  getPreload(): string | undefined;
41
37
  private _applyBrowserWindowOptions;
42
38
  private _applyBrowserViewOptions;
@@ -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;AAED,qBAAa,cAAc;IACzB,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,OAA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js CHANGED
@@ -22,8 +22,24 @@ if (!remote.isInitialized()) {
22
22
  const enable = (win) => {
23
23
  remote.enable(win);
24
24
  };
25
+ const initWebContentsVal = (win, preload) => {
26
+ win.webContents.executeJavaScript(`
27
+ try {
28
+ const data = {
29
+ __ELECTRON_WINDOW_MANAGER_WEB_CONTENTS_ID__: ${JSON.stringify(win.webContents.id)},
30
+ __ELECTRON_WINDOW_MANAGER_TYPE__: ${JSON.stringify(win._type)},
31
+ __ELECTRON_WINDOW_MANAGER_NAME__: ${JSON.stringify(win._name)},
32
+ __ELECTRON_WINDOW_MANAGER_EXTRA_DATA__: ${JSON.stringify(win._extraData || '')},
33
+ __ELECTRON_WINDOW_MANAGER_PRELOAD__: ${JSON.stringify(preload)}
34
+ };
35
+ Object.entries(data).forEach(([key, value]) => {
36
+ window[key] = value;
37
+ });
38
+ } catch (error) {}
39
+ `);
40
+ };
25
41
  class WindowsManager {
26
- constructor(preload, loadingViewUrl, errorViewUrl, preloadUrl) {
42
+ constructor(preload, loadingViewUrl, errorViewUrl, preloadWebContentsUrl) {
27
43
  this.preloadedBW = null;
28
44
  this.preloadedBW_FramelessWithButtons = null;
29
45
  this.preloadedBW_FramelessNoButtons = null;
@@ -33,7 +49,8 @@ class WindowsManager {
33
49
  this.windows = new Map();
34
50
  this.loadingViewUrl = `${loadingViewUrl ?? ''}`;
35
51
  this.errorViewUrl = `${errorViewUrl ?? ''}`;
36
- this.preloadWebContentsUrl = `${preloadUrl ?? ''}`;
52
+ this.preloadWebContentsUrl = `${preloadWebContentsUrl ?? ''}`;
53
+ log('log', 'preloadWebContentsUrl: ', this.preloadWebContentsUrl);
37
54
  if (this.preloadWebContentsUrl) {
38
55
  app.on('ready', () => {
39
56
  this._preloadInstances();
@@ -86,97 +103,94 @@ class WindowsManager {
86
103
  return new Promise((resolve) => {
87
104
  const preload = this.preload;
88
105
  const url = this.preloadWebContentsUrl;
89
- const instance = new BrowserWindow({
90
- show: false,
91
- ...options,
92
- webPreferences: {
93
- webviewTag: true,
94
- plugins: true,
95
- nodeIntegration: true,
96
- contextIsolation: false,
97
- backgroundThrottling: false,
98
- webSecurity: false,
99
- preload: preload,
100
- ...(options.webPreferences || {}),
106
+ if (this.preloadWebContentsUrl) {
107
+ const instance = new BrowserWindow({
108
+ show: false,
109
+ ...options,
110
+ webPreferences: {
111
+ webviewTag: true,
112
+ plugins: true,
113
+ nodeIntegration: true,
114
+ contextIsolation: false,
115
+ backgroundThrottling: false,
116
+ webSecurity: false,
117
+ preload: preload,
118
+ ...(options.webPreferences || {}),
119
+ }
120
+ });
121
+ try {
122
+ remote.enable(instance.webContents);
123
+ }
124
+ catch (error) {
125
+ log('error', '预加载 BW 设置 remote 失败', error);
126
+ }
127
+ try {
128
+ // @ts-ignore
129
+ instance._id = instance.webContents.id;
130
+ }
131
+ catch (error) {
132
+ log('error', '预加载 BW 设置 _id 失败', error);
101
133
  }
102
- });
103
- try {
104
- remote.enable(instance.webContents);
105
- }
106
- catch (error) {
107
- log('error', '预加载 BW 设置 remote 失败', error);
108
- }
109
- try {
110
134
  // @ts-ignore
111
- instance._id = instance.webContents.id;
112
- }
113
- catch (error) {
114
- log('error', '预加载 BW 设置 _id 失败', error);
135
+ log('log', '创建预BW: ', instance._id, this.preloadWebContentsUrl);
136
+ instance.webContents.once('did-finish-load', () => {
137
+ resolve(instance);
138
+ });
139
+ instance.webContents.once('did-fail-load', () => {
140
+ resolve(instance);
141
+ });
142
+ instance.loadURL(url || 'about:blank');
115
143
  }
116
- // @ts-ignore
117
- log('log', '创建预BW: ', instance._id);
118
- instance.webContents.once('did-finish-load', () => {
119
- resolve(instance);
120
- });
121
- instance.webContents.once('did-fail-load', () => {
122
- resolve(instance);
123
- });
124
- instance.loadURL(url || 'about:blank');
125
144
  });
126
145
  }
127
146
  _createPreloadBV() {
128
147
  return new Promise((resolve) => {
129
148
  const preload = this.preload;
130
149
  const url = this.preloadWebContentsUrl;
131
- const instance = new BrowserView({
132
- webPreferences: {
133
- webviewTag: true,
134
- plugins: true,
135
- nodeIntegration: true,
136
- contextIsolation: false,
137
- // backgroundThrottling: false,
138
- webSecurity: false,
139
- preload: preload,
150
+ if (this.preloadWebContentsUrl) {
151
+ const instance = new BrowserView({
152
+ webPreferences: {
153
+ webviewTag: true,
154
+ plugins: true,
155
+ nodeIntegration: true,
156
+ contextIsolation: false,
157
+ // backgroundThrottling: false,
158
+ webSecurity: false,
159
+ preload: preload,
160
+ }
161
+ });
162
+ try {
163
+ remote.enable(instance.webContents);
164
+ }
165
+ catch (error) {
166
+ log('error', '预加载 BV 设置 remote 失败', error);
167
+ }
168
+ try {
169
+ // @ts-ignore
170
+ instance._id = instance.webContents.id;
171
+ }
172
+ catch (error) {
173
+ log('error', '预加载 BV 设置 _id 失败', error);
140
174
  }
141
- });
142
- try {
143
- remote.enable(instance.webContents);
144
- }
145
- catch (error) {
146
- log('error', '预加载 BV 设置 remote 失败', error);
147
- }
148
- try {
149
175
  // @ts-ignore
150
- instance._id = instance.webContents.id;
151
- }
152
- catch (error) {
153
- log('error', '预加载 BV 设置 _id 失败', error);
176
+ log('log', '创建预BV: ', instance._id, this.preloadWebContentsUrl);
177
+ instance.webContents.once('did-finish-load', () => {
178
+ resolve(instance);
179
+ });
180
+ instance.webContents.once('did-fail-load', () => {
181
+ resolve(instance);
182
+ });
183
+ instance.webContents.loadURL(url || 'about:blank');
154
184
  }
155
- // @ts-ignore
156
- log('log', '创建预BV: ', instance._id);
157
- instance.webContents.once('did-finish-load', () => {
158
- resolve(instance);
159
- });
160
- instance.webContents.once('did-fail-load', () => {
161
- resolve(instance);
162
- });
163
- instance.webContents.loadURL(url || 'about:blank');
164
185
  });
165
186
  }
166
- /**
167
- * 预加载webContents
168
- * @param url
169
- */
170
- preloadWebContents(url) {
171
- this.preloadWebContentsUrl = url;
172
- }
173
187
  create(options) {
174
188
  let window;
175
189
  const { usePreload = true, type = 'BW', name = 'anonymous', url, loadingView = { url: undefined }, errorView = { url: undefined }, browserWindow: browserWindowOptions, openDevTools = false, preventOriginClose = false, } = options;
176
190
  options.type = type;
177
191
  // 优先复用预创建实例
178
192
  let preloadWin = null;
179
- if (type === 'BW' && usePreload) {
193
+ if (type === 'BW' && usePreload && this.preloadWebContentsUrl) {
180
194
  const bwOptions = browserWindowOptions || {};
181
195
  if (bwOptions.frame === false && bwOptions.titleBarStyle === 'hidden') {
182
196
  preloadWin = this.preloadedBW_FramelessWithButtons;
@@ -194,7 +208,7 @@ class WindowsManager {
194
208
  setTimeout(() => this._createPreloadBW({}), 0);
195
209
  }
196
210
  }
197
- if (type === 'BV' && usePreload) {
211
+ if (type === 'BV' && usePreload && this.preloadWebContentsUrl) {
198
212
  preloadWin = this.preloadedBV;
199
213
  this.preloadedBV = null;
200
214
  setTimeout(() => this._createPreloadBV(), 0);
@@ -374,30 +388,10 @@ class WindowsManager {
374
388
  window.focus ? window.focus() : window.webContents.focus();
375
389
  this.windows.set(window.id || window._id || window.webContents.id, window);
376
390
  log('log', 'create', this.windows.keys());
377
- const initVal = () => {
378
- window.webContents.executeJavaScript(`
379
- try {
380
- const data = {
381
- __ELECTRON_WINDOW_MANAGER_WEB_CONTENTS_ID__: ${JSON.stringify(window.webContents.id)},
382
- __ELECTRON_WINDOW_MANAGER_TYPE__: ${JSON.stringify(window._type)},
383
- __ELECTRON_WINDOW_MANAGER_NAME__: ${JSON.stringify(window._name)},
384
- __ELECTRON_WINDOW_MANAGER_EXTRA_DATA__: ${JSON.stringify(window._extraData || '')},
385
- __ELECTRON_WINDOW_MANAGER_PRELOAD__: ${JSON.stringify(preload)}
386
- };
387
- Object.entries(data).forEach(([key, value]) => {
388
- Object.defineProperty(window, key, {
389
- value,
390
- writable: false,
391
- configurable: false,
392
- enumerable: true
393
- });
394
- });
395
- } catch (error) {}
396
- `);
397
- };
398
391
  // 初始化值
399
392
  window.webContents.on('did-finish-load', () => {
400
- initVal();
393
+ console.error('did-finish-load', window.webContents.id);
394
+ initWebContentsVal(window, `${preload || ''}`);
401
395
  });
402
396
  if (type === 'BW') {
403
397
  const handleBrowserViewFocus = (view) => {
@@ -472,10 +466,8 @@ class WindowsManager {
472
466
  for (const view of views) {
473
467
  handleBrowserViewBlur(view);
474
468
  }
475
- setTimeout(() => {
476
- _setBrowserView.call(window, view);
477
- handleBrowserViewFocus(view);
478
- }, 0);
469
+ _setBrowserView.call(window, view);
470
+ handleBrowserViewFocus(view);
479
471
  };
480
472
  }
481
473
  catch (error) {
@@ -675,6 +667,40 @@ class WindowsManager {
675
667
  }
676
668
  });
677
669
  }
670
+ rename(idOrName, newName) {
671
+ log('log', 'rename', idOrName, newName);
672
+ let win = undefined;
673
+ // 先查找目标窗口
674
+ if (typeof idOrName === 'number') {
675
+ win = this.get(idOrName);
676
+ }
677
+ else if (typeof idOrName === 'string') {
678
+ this.windows.forEach(i => {
679
+ if (i?._name === idOrName) {
680
+ win = i;
681
+ }
682
+ });
683
+ }
684
+ if (!win) {
685
+ // 没有找到目标窗口
686
+ return undefined;
687
+ }
688
+ // 检查新名字是否已存在
689
+ let nameExists = false;
690
+ this.windows.forEach(i => {
691
+ if (i !== win && i?._name === newName) {
692
+ nameExists = true;
693
+ }
694
+ });
695
+ if (nameExists) {
696
+ // 新名字已被占用
697
+ return undefined;
698
+ }
699
+ // 修改名字并同步 webContents
700
+ win._name = newName;
701
+ initWebContentsVal(win, `${this.preload || ''}`);
702
+ return win;
703
+ }
678
704
  getPreload() {
679
705
  log('log', 'getPreload');
680
706
  return this.preload;
@@ -811,6 +837,19 @@ const initialize = (preload, loadingViewUrl, errorViewUrl, preloadWebContentsUrl
811
837
  const res = wm.closeAll();
812
838
  return res;
813
839
  }
840
+ if (data?.type === 'rename') {
841
+ const opt = data;
842
+ const res = wm.rename(opt.data.idOrName, opt.data.newName);
843
+ if (res) {
844
+ return {
845
+ winId: Number(`${res?.id || res?._id || -1}`),
846
+ winName: `${res?._name || ''}`,
847
+ winType: `${res?._type || ''}`,
848
+ winExtraData: `${res?._extraData || ''}`,
849
+ };
850
+ }
851
+ return undefined;
852
+ }
814
853
  if (data?.type === 'getWindowForWebContentId') {
815
854
  const opt = data;
816
855
  const targetWebContents = webContents.fromId(opt.data);