@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.
package/main/index.js CHANGED
@@ -5,14 +5,14 @@ const eIpc = require('@lynker-desktop/electron-ipc/main');
5
5
  const md5 = require('md5');
6
6
 
7
7
  function _interopNamespaceDefault(e) {
8
- const n = Object.create(null);
9
- if (e) {
10
- for (const k in e) {
11
- n[k] = e[k];
12
- }
8
+ const n = Object.create(null);
9
+ if (e) {
10
+ for (const k in e) {
11
+ n[k] = e[k];
13
12
  }
14
- n.default = e;
15
- return n;
13
+ }
14
+ n.default = e;
15
+ return n;
16
16
  }
17
17
 
18
18
  const remote__namespace = /*#__PURE__*/_interopNamespaceDefault(remote);
@@ -35,8 +35,24 @@ if (!remote__namespace.isInitialized()) {
35
35
  const enable = (win) => {
36
36
  remote__namespace.enable(win);
37
37
  };
38
+ const initWebContentsVal = (win, preload) => {
39
+ win.webContents.executeJavaScript(`
40
+ try {
41
+ const data = {
42
+ __ELECTRON_WINDOW_MANAGER_WEB_CONTENTS_ID__: ${JSON.stringify(win.webContents.id)},
43
+ __ELECTRON_WINDOW_MANAGER_TYPE__: ${JSON.stringify(win._type)},
44
+ __ELECTRON_WINDOW_MANAGER_NAME__: ${JSON.stringify(win._name)},
45
+ __ELECTRON_WINDOW_MANAGER_EXTRA_DATA__: ${JSON.stringify(win._extraData || '')},
46
+ __ELECTRON_WINDOW_MANAGER_PRELOAD__: ${JSON.stringify(preload)}
47
+ };
48
+ Object.entries(data).forEach(([key, value]) => {
49
+ window[key] = value;
50
+ });
51
+ } catch (error) {}
52
+ `);
53
+ };
38
54
  class WindowsManager {
39
- constructor(preload, loadingViewUrl, errorViewUrl, preloadUrl) {
55
+ constructor(preload, loadingViewUrl, errorViewUrl, preloadWebContentsUrl) {
40
56
  this.preloadedBW = null;
41
57
  this.preloadedBW_FramelessWithButtons = null;
42
58
  this.preloadedBW_FramelessNoButtons = null;
@@ -46,7 +62,8 @@ class WindowsManager {
46
62
  this.windows = new Map();
47
63
  this.loadingViewUrl = `${loadingViewUrl ?? ''}`;
48
64
  this.errorViewUrl = `${errorViewUrl ?? ''}`;
49
- this.preloadWebContentsUrl = `${preloadUrl ?? ''}`;
65
+ this.preloadWebContentsUrl = `${preloadWebContentsUrl ?? ''}`;
66
+ log('log', 'preloadWebContentsUrl: ', this.preloadWebContentsUrl);
50
67
  if (this.preloadWebContentsUrl) {
51
68
  electron.app.on('ready', () => {
52
69
  this._preloadInstances();
@@ -99,97 +116,94 @@ class WindowsManager {
99
116
  return new Promise((resolve) => {
100
117
  const preload = this.preload;
101
118
  const url = this.preloadWebContentsUrl;
102
- const instance = new electron.BrowserWindow({
103
- show: false,
104
- ...options,
105
- webPreferences: {
106
- webviewTag: true,
107
- plugins: true,
108
- nodeIntegration: true,
109
- contextIsolation: false,
110
- backgroundThrottling: false,
111
- webSecurity: false,
112
- preload: preload,
113
- ...(options.webPreferences || {}),
119
+ if (this.preloadWebContentsUrl) {
120
+ const instance = new electron.BrowserWindow({
121
+ show: false,
122
+ ...options,
123
+ webPreferences: {
124
+ webviewTag: true,
125
+ plugins: true,
126
+ nodeIntegration: true,
127
+ contextIsolation: false,
128
+ backgroundThrottling: false,
129
+ webSecurity: false,
130
+ preload: preload,
131
+ ...(options.webPreferences || {}),
132
+ }
133
+ });
134
+ try {
135
+ remote__namespace.enable(instance.webContents);
136
+ }
137
+ catch (error) {
138
+ log('error', '预加载 BW 设置 remote 失败', error);
139
+ }
140
+ try {
141
+ // @ts-ignore
142
+ instance._id = instance.webContents.id;
143
+ }
144
+ catch (error) {
145
+ log('error', '预加载 BW 设置 _id 失败', error);
114
146
  }
115
- });
116
- try {
117
- remote__namespace.enable(instance.webContents);
118
- }
119
- catch (error) {
120
- log('error', '预加载 BW 设置 remote 失败', error);
121
- }
122
- try {
123
147
  // @ts-ignore
124
- instance._id = instance.webContents.id;
125
- }
126
- catch (error) {
127
- log('error', '预加载 BW 设置 _id 失败', error);
148
+ log('log', '创建预BW: ', instance._id, this.preloadWebContentsUrl);
149
+ instance.webContents.once('did-finish-load', () => {
150
+ resolve(instance);
151
+ });
152
+ instance.webContents.once('did-fail-load', () => {
153
+ resolve(instance);
154
+ });
155
+ instance.loadURL(url || 'about:blank');
128
156
  }
129
- // @ts-ignore
130
- log('log', '创建预BW: ', instance._id);
131
- instance.webContents.once('did-finish-load', () => {
132
- resolve(instance);
133
- });
134
- instance.webContents.once('did-fail-load', () => {
135
- resolve(instance);
136
- });
137
- instance.loadURL(url || 'about:blank');
138
157
  });
139
158
  }
140
159
  _createPreloadBV() {
141
160
  return new Promise((resolve) => {
142
161
  const preload = this.preload;
143
162
  const url = this.preloadWebContentsUrl;
144
- const instance = new electron.BrowserView({
145
- webPreferences: {
146
- webviewTag: true,
147
- plugins: true,
148
- nodeIntegration: true,
149
- contextIsolation: false,
150
- // backgroundThrottling: false,
151
- webSecurity: false,
152
- preload: preload,
163
+ if (this.preloadWebContentsUrl) {
164
+ const instance = new electron.BrowserView({
165
+ webPreferences: {
166
+ webviewTag: true,
167
+ plugins: true,
168
+ nodeIntegration: true,
169
+ contextIsolation: false,
170
+ // backgroundThrottling: false,
171
+ webSecurity: false,
172
+ preload: preload,
173
+ }
174
+ });
175
+ try {
176
+ remote__namespace.enable(instance.webContents);
177
+ }
178
+ catch (error) {
179
+ log('error', '预加载 BV 设置 remote 失败', error);
180
+ }
181
+ try {
182
+ // @ts-ignore
183
+ instance._id = instance.webContents.id;
184
+ }
185
+ catch (error) {
186
+ log('error', '预加载 BV 设置 _id 失败', error);
153
187
  }
154
- });
155
- try {
156
- remote__namespace.enable(instance.webContents);
157
- }
158
- catch (error) {
159
- log('error', '预加载 BV 设置 remote 失败', error);
160
- }
161
- try {
162
188
  // @ts-ignore
163
- instance._id = instance.webContents.id;
164
- }
165
- catch (error) {
166
- log('error', '预加载 BV 设置 _id 失败', error);
189
+ log('log', '创建预BV: ', instance._id, this.preloadWebContentsUrl);
190
+ instance.webContents.once('did-finish-load', () => {
191
+ resolve(instance);
192
+ });
193
+ instance.webContents.once('did-fail-load', () => {
194
+ resolve(instance);
195
+ });
196
+ instance.webContents.loadURL(url || 'about:blank');
167
197
  }
168
- // @ts-ignore
169
- log('log', '创建预BV: ', instance._id);
170
- instance.webContents.once('did-finish-load', () => {
171
- resolve(instance);
172
- });
173
- instance.webContents.once('did-fail-load', () => {
174
- resolve(instance);
175
- });
176
- instance.webContents.loadURL(url || 'about:blank');
177
198
  });
178
199
  }
179
- /**
180
- * 预加载webContents
181
- * @param url
182
- */
183
- preloadWebContents(url) {
184
- this.preloadWebContentsUrl = url;
185
- }
186
200
  create(options) {
187
201
  let window;
188
202
  const { usePreload = true, type = 'BW', name = 'anonymous', url, loadingView = { url: undefined }, errorView = { url: undefined }, browserWindow: browserWindowOptions, openDevTools = false, preventOriginClose = false, } = options;
189
203
  options.type = type;
190
204
  // 优先复用预创建实例
191
205
  let preloadWin = null;
192
- if (type === 'BW' && usePreload) {
206
+ if (type === 'BW' && usePreload && this.preloadWebContentsUrl) {
193
207
  const bwOptions = browserWindowOptions || {};
194
208
  if (bwOptions.frame === false && bwOptions.titleBarStyle === 'hidden') {
195
209
  preloadWin = this.preloadedBW_FramelessWithButtons;
@@ -207,7 +221,7 @@ class WindowsManager {
207
221
  setTimeout(() => this._createPreloadBW({}), 0);
208
222
  }
209
223
  }
210
- if (type === 'BV' && usePreload) {
224
+ if (type === 'BV' && usePreload && this.preloadWebContentsUrl) {
211
225
  preloadWin = this.preloadedBV;
212
226
  this.preloadedBV = null;
213
227
  setTimeout(() => this._createPreloadBV(), 0);
@@ -387,30 +401,10 @@ class WindowsManager {
387
401
  window.focus ? window.focus() : window.webContents.focus();
388
402
  this.windows.set(window.id || window._id || window.webContents.id, window);
389
403
  log('log', 'create', this.windows.keys());
390
- const initVal = () => {
391
- window.webContents.executeJavaScript(`
392
- try {
393
- const data = {
394
- __ELECTRON_WINDOW_MANAGER_WEB_CONTENTS_ID__: ${JSON.stringify(window.webContents.id)},
395
- __ELECTRON_WINDOW_MANAGER_TYPE__: ${JSON.stringify(window._type)},
396
- __ELECTRON_WINDOW_MANAGER_NAME__: ${JSON.stringify(window._name)},
397
- __ELECTRON_WINDOW_MANAGER_EXTRA_DATA__: ${JSON.stringify(window._extraData || '')},
398
- __ELECTRON_WINDOW_MANAGER_PRELOAD__: ${JSON.stringify(preload)}
399
- };
400
- Object.entries(data).forEach(([key, value]) => {
401
- Object.defineProperty(window, key, {
402
- value,
403
- writable: false,
404
- configurable: false,
405
- enumerable: true
406
- });
407
- });
408
- } catch (error) {}
409
- `);
410
- };
411
404
  // 初始化值
412
405
  window.webContents.on('did-finish-load', () => {
413
- initVal();
406
+ console.error('did-finish-load', window.webContents.id);
407
+ initWebContentsVal(window, `${preload || ''}`);
414
408
  });
415
409
  if (type === 'BW') {
416
410
  const handleBrowserViewFocus = (view) => {
@@ -485,10 +479,8 @@ class WindowsManager {
485
479
  for (const view of views) {
486
480
  handleBrowserViewBlur(view);
487
481
  }
488
- setTimeout(() => {
489
- _setBrowserView.call(window, view);
490
- handleBrowserViewFocus(view);
491
- }, 0);
482
+ _setBrowserView.call(window, view);
483
+ handleBrowserViewFocus(view);
492
484
  };
493
485
  }
494
486
  catch (error) {
@@ -688,6 +680,40 @@ class WindowsManager {
688
680
  }
689
681
  });
690
682
  }
683
+ rename(idOrName, newName) {
684
+ log('log', 'rename', idOrName, newName);
685
+ let win = undefined;
686
+ // 先查找目标窗口
687
+ if (typeof idOrName === 'number') {
688
+ win = this.get(idOrName);
689
+ }
690
+ else if (typeof idOrName === 'string') {
691
+ this.windows.forEach(i => {
692
+ if (i?._name === idOrName) {
693
+ win = i;
694
+ }
695
+ });
696
+ }
697
+ if (!win) {
698
+ // 没有找到目标窗口
699
+ return undefined;
700
+ }
701
+ // 检查新名字是否已存在
702
+ let nameExists = false;
703
+ this.windows.forEach(i => {
704
+ if (i !== win && i?._name === newName) {
705
+ nameExists = true;
706
+ }
707
+ });
708
+ if (nameExists) {
709
+ // 新名字已被占用
710
+ return undefined;
711
+ }
712
+ // 修改名字并同步 webContents
713
+ win._name = newName;
714
+ initWebContentsVal(win, `${this.preload || ''}`);
715
+ return win;
716
+ }
691
717
  getPreload() {
692
718
  log('log', 'getPreload');
693
719
  return this.preload;
@@ -824,6 +850,19 @@ const initialize = (preload, loadingViewUrl, errorViewUrl, preloadWebContentsUrl
824
850
  const res = wm.closeAll();
825
851
  return res;
826
852
  }
853
+ if (data?.type === 'rename') {
854
+ const opt = data;
855
+ const res = wm.rename(opt.data.idOrName, opt.data.newName);
856
+ if (res) {
857
+ return {
858
+ winId: Number(`${res?.id || res?._id || -1}`),
859
+ winName: `${res?._name || ''}`,
860
+ winType: `${res?._type || ''}`,
861
+ winExtraData: `${res?._extraData || ''}`,
862
+ };
863
+ }
864
+ return undefined;
865
+ }
827
866
  if (data?.type === 'getWindowForWebContentId') {
828
867
  const opt = data;
829
868
  const targetWebContents = electron.webContents.fromId(opt.data);