nexfep 0.4.3 → 0.5.0

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.
@@ -1,7 +1,7 @@
1
1
  import { Logger } from "./Logger.js";
2
- import os from 'os';
3
- import path from 'path';
4
- import fs from 'fs';
2
+ import os from "os";
3
+ import path from "path";
4
+ import fs from "fs";
5
5
  class Window {
6
6
  window;
7
7
  webview;
@@ -41,6 +41,28 @@ class Window {
41
41
  unMinimize() {
42
42
  this.window.setMinimized(false);
43
43
  }
44
+ isMaximized() {
45
+ return this.window.isMaximized();
46
+ }
47
+ isMinimized() {
48
+ return this.window.isMinimized();
49
+ }
50
+ toggleMaximize() {
51
+ if (this.isMaximized()) {
52
+ this.unMaximize();
53
+ }
54
+ else {
55
+ this.maximize();
56
+ }
57
+ }
58
+ toggleMinimize() {
59
+ if (this.isMinimized()) {
60
+ this.unMinimize();
61
+ }
62
+ else {
63
+ this.minimize();
64
+ }
65
+ }
44
66
  openDevTools() {
45
67
  this.webview.openDevtools();
46
68
  }
@@ -50,6 +72,22 @@ class Window {
50
72
  setSize(width, height) {
51
73
  this.window.setSize(width, height);
52
74
  }
75
+ getSize() {
76
+ return this.window.getInnerSize();
77
+ }
78
+ setPosition(x, y) {
79
+ this.window.setPosition(x, y);
80
+ }
81
+ getPosition() {
82
+ return this.window.getPosition();
83
+ }
84
+ isFocused() {
85
+ return this.window.isFocused();
86
+ }
87
+ focus() {
88
+ this.window.setMinimized(false);
89
+ this.window.focus();
90
+ }
53
91
  hide() {
54
92
  this.window.hide();
55
93
  this.isShow = false;
@@ -81,16 +119,16 @@ class WindowPool {
81
119
  freeWindowCount;
82
120
  global;
83
121
  constructor(app, options) {
84
- if (os.platform() === 'win32') {
85
- // 设置 WebView2 用户数据目录(仅 Windows 需要此配置)
86
- const userDataDir = options.WindowsWebview2UserDataFolder || path.join(process.env.LOCALAPPDATA || os.homedir(), 'NexfepDevelopment.webview2-data');
122
+ if (os.platform() === "win32") {
123
+ const userDataDir = options.WindowsWebview2UserDataFolder ||
124
+ path.join(process.env.LOCALAPPDATA || os.homedir(), "NexfepDevelopment.webview2-data");
87
125
  if (!fs.existsSync(userDataDir)) {
88
126
  fs.mkdirSync(userDataDir, { recursive: true });
89
127
  }
90
128
  process.env.WEBVIEW2_USER_DATA_FOLDER = userDataDir;
91
129
  }
92
130
  this.onCustomMessage = (window, data) => {
93
- console.log('Get Custom Message:', data, "from window", window.id);
131
+ console.log("Get Custom Message:", data, "from window", window.id);
94
132
  };
95
133
  this.handlers = new Map();
96
134
  this.app = app;
@@ -139,13 +177,11 @@ class WindowPool {
139
177
  }
140
178
  `;
141
179
  const INJECT_CODE = `
142
- // 事件监听
143
180
  window.addEventListener("beforeunload", () => {
144
181
  const MessageBody = { type: 'NexfepBeforeUnload' }
145
182
  window.ipc.postMessage(JSON.stringify(MessageBody));
146
183
  });
147
184
 
148
- // 窗口控制函数
149
185
  window.close = () => {
150
186
  const MessageBody = { type: 'NexfepCloseWindow' }
151
187
  window.ipc.postMessage(JSON.stringify(MessageBody));
@@ -270,24 +306,24 @@ class WindowPool {
270
306
  window.__originConsoleDebug(...args);
271
307
  }
272
308
 
273
- // 注入 CSS 样式
274
309
  const style = document.createElement('style');
275
310
  style.textContent = \`${INJECT_CSS}\`;
276
311
  document.head.appendChild(style);
277
312
 
278
- // 标记加载完成
279
313
  window.isNexfepLoadDone = true;
280
314
 
281
- // 触发加载完成事件
282
315
  window.dispatchEvent(new CustomEvent('nexfep-load-done'));
283
316
  `;
284
317
  await this.__injectCode(windowObj, INJECT_CODE);
285
318
  }
286
319
  async __createNewWindowObj() {
287
- return await new Promise((resolve, reject) => {
320
+ return await new Promise((resolve) => {
288
321
  setImmediate(() => {
289
- const window = this.app.createBrowserWindow({ title: "Nexfep" });
290
- window.hide();
322
+ const window = this.app.createBrowserWindow({
323
+ title: "Nexfep",
324
+ visible: false,
325
+ focused: false,
326
+ });
291
327
  this.freeWindowCount++;
292
328
  const webview = window.createWebview();
293
329
  const windowObj = new Window(window, webview, ++this.windowCount, this);
@@ -295,7 +331,7 @@ class WindowPool {
295
331
  webview.onIpcMessage((data) => {
296
332
  const dataText = data.body.toString();
297
333
  const dataObj = JSON.parse(dataText);
298
- if (dataObj.type == 'NexfepBeforeUnload') {
334
+ if (dataObj.type == "NexfepBeforeUnload") {
299
335
  webview.evaluateScript(`if(window?.isNexfepLoadDone){
300
336
  const MessageBody = { type: 'NexfepBeforeUnload' }
301
337
  window.ipc.postMessage(JSON.stringify(MessageBody));
@@ -304,37 +340,37 @@ class WindowPool {
304
340
  window.ipc.postMessage(JSON.stringify(MessageBody));
305
341
  }`);
306
342
  }
307
- else if (dataObj.type == 'NexfepLoadFalse') {
343
+ else if (dataObj.type == "NexfepLoadFalse") {
308
344
  this.__injectControlFunctions(windowObj);
309
345
  }
310
- else if (dataObj.type == 'NexfepCloseWindow') {
346
+ else if (dataObj.type == "NexfepCloseWindow") {
311
347
  this.closeWindow(windowObj);
312
348
  }
313
- else if (dataObj.type == 'NexfepMinimizeWindow') {
349
+ else if (dataObj.type == "NexfepMinimizeWindow") {
314
350
  window.setMinimized(true);
315
351
  }
316
- else if (dataObj.type == 'NexfepUnMinimizeWindow') {
352
+ else if (dataObj.type == "NexfepUnMinimizeWindow") {
317
353
  window.setMinimized(false);
318
354
  }
319
- else if (dataObj.type == 'NexfepMaximizeWindow') {
355
+ else if (dataObj.type == "NexfepMaximizeWindow") {
320
356
  window.setMaximized(true);
321
357
  }
322
- else if (dataObj.type == 'NexfepUnMaximizeWindow') {
358
+ else if (dataObj.type == "NexfepUnMaximizeWindow") {
323
359
  window.setMaximized(false);
324
360
  }
325
- else if (dataObj.type == 'NexfepSetTitle') {
361
+ else if (dataObj.type == "NexfepSetTitle") {
326
362
  window.setTitle(dataObj.title);
327
363
  }
328
- else if (dataObj.type == 'NexfepOpenDevTools') {
364
+ else if (dataObj.type == "NexfepOpenDevTools") {
329
365
  webview.openDevtools();
330
366
  }
331
- else if (dataObj.type == 'NexfepCloseDevTools') {
367
+ else if (dataObj.type == "NexfepCloseDevTools") {
332
368
  webview.closeDevtools();
333
369
  }
334
- else if (dataObj.type == 'CustomMessage') {
370
+ else if (dataObj.type == "CustomMessage") {
335
371
  this.onCustomMessage(windowObj, dataObj.data);
336
372
  }
337
- else if (dataObj.type == 'NexfepInvoke') {
373
+ else if (dataObj.type == "NexfepInvoke") {
338
374
  const handlers = this.handlers.get(dataObj.event) || [];
339
375
  handlers.forEach(async (handler) => {
340
376
  const result = await handler(dataObj.data);
@@ -350,16 +386,16 @@ class WindowPool {
350
386
  }
351
387
  });
352
388
  }
353
- else if (dataObj.type == 'NexfepSetGlobal') {
389
+ else if (dataObj.type == "NexfepSetGlobal") {
354
390
  this.global.set(dataObj.name, dataObj.value);
355
391
  }
356
- else if (dataObj.type == 'NexfepGetGlobal') {
392
+ else if (dataObj.type == "NexfepGetGlobal") {
357
393
  const value = this.global.get(dataObj.name);
358
394
  webview.evaluateScript(`
359
395
  window.dispatchEvent(new CustomEvent('nexfep-get-global-result-${dataObj.eventId[0]}-${dataObj.eventId[1]}', { detail: ${JSON.stringify(value)} }));
360
396
  `);
361
397
  }
362
- else if (dataObj.type == 'NexfepBroadcast') {
398
+ else if (dataObj.type == "NexfepBroadcast") {
363
399
  this.windows.forEach(async (w) => {
364
400
  if (w != windowObj && w.isOpen) {
365
401
  w.webview.evaluateScript(`
@@ -368,7 +404,7 @@ class WindowPool {
368
404
  }
369
405
  });
370
406
  }
371
- else if (dataObj.type == 'NexfepTell') {
407
+ else if (dataObj.type == "NexfepTell") {
372
408
  this.windows.forEach(async (w) => {
373
409
  if (w.id == dataObj.to) {
374
410
  w.webview.evaluateScript(`
@@ -377,20 +413,20 @@ class WindowPool {
377
413
  }
378
414
  });
379
415
  }
380
- else if (dataObj.type == 'NexfepConsoleLog') {
381
- this.logger.__printLog(windowObj.id, 'Log', dataObj.message);
416
+ else if (dataObj.type == "NexfepConsoleLog") {
417
+ this.logger.__printLog(windowObj.id, "Log", dataObj.message);
382
418
  }
383
- else if (dataObj.type == 'NexfepConsoleError') {
384
- this.logger.__printLog(windowObj.id, 'Error', dataObj.message, '\x1b[31m', '\x1b[0m');
419
+ else if (dataObj.type == "NexfepConsoleError") {
420
+ this.logger.__printLog(windowObj.id, "Error", dataObj.message, "\x1b[31m", "\x1b[0m");
385
421
  }
386
- else if (dataObj.type == 'NexfepConsoleInfo') {
387
- this.logger.__printLog(windowObj.id, 'Info', dataObj.message, '\x1b[34m', '\x1b[0m');
422
+ else if (dataObj.type == "NexfepConsoleInfo") {
423
+ this.logger.__printLog(windowObj.id, "Info", dataObj.message, "\x1b[34m", "\x1b[0m");
388
424
  }
389
- else if (dataObj.type == 'NexfepConsoleWarn') {
390
- this.logger.__printLog(windowObj.id, 'Warn', dataObj.message, '\x1b[33m', '\x1b[0m');
425
+ else if (dataObj.type == "NexfepConsoleWarn") {
426
+ this.logger.__printLog(windowObj.id, "Warn", dataObj.message, "\x1b[33m", "\x1b[0m");
391
427
  }
392
- else if (dataObj.type == 'NexfepConsoleDebug') {
393
- this.logger.__printLog(windowObj.id, 'Debug', dataObj.message, '\x1b[90m', '\x1b[0m');
428
+ else if (dataObj.type == "NexfepConsoleDebug") {
429
+ this.logger.__printLog(windowObj.id, "Debug", dataObj.message, "\x1b[90m", "\x1b[0m");
394
430
  }
395
431
  });
396
432
  resolve(windowObj);
@@ -398,7 +434,7 @@ class WindowPool {
398
434
  });
399
435
  }
400
436
  async createWindow(isShow = true, isDecorated = true) {
401
- const window = this.windows.find(w => w.isOpen === false) || await this.__createNewWindowObj();
437
+ const window = this.windows.find((w) => w.isOpen === false) || (await this.__createNewWindowObj());
402
438
  this.freeWindowCount--;
403
439
  if (window) {
404
440
  window.isOpen = true;
@@ -427,10 +463,10 @@ class WindowPool {
427
463
  }
428
464
  }
429
465
  async handle(event, callback) {
430
- this.handlers.set(event, [...this.handlers.get(event) || [], callback]);
466
+ this.handlers.set(event, [...(this.handlers.get(event) || []), callback]);
431
467
  }
432
468
  async unhandle(event, callback) {
433
- this.handlers.set(event, (this.handlers.get(event) || []).filter(c => c !== callback));
469
+ this.handlers.set(event, (this.handlers.get(event) || []).filter((c) => c !== callback));
434
470
  }
435
471
  }
436
472
  export { WindowPool, Window };