nexfep 0.1.7 → 0.2.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.
package/README-CN.md CHANGED
@@ -33,29 +33,117 @@ pnpm add nexfep
33
33
  ## 快速开始
34
34
 
35
35
  ```typescript
36
- import { WindowPool } from 'nexfep';
36
+ import { Application } from 'nexfep';
37
37
 
38
- const pool = new WindowPool();
38
+ const app = new Application();
39
39
 
40
- const window = await pool.createWindow(true, false);
40
+ const window = await app.windows.createWindow(true, false);
41
41
 
42
42
  await window.loadHTML('<h1 nexfep-area-drag>Hello Nexfep!</h1>');
43
-
44
- pool.mainloop();
45
43
  ```
46
44
 
47
45
  ## 使用指南
48
46
 
47
+ ### Application
48
+
49
+ `Application` 是框架的主入口,负责管理应用生命周期,提供窗口和系统托盘的访问。
50
+
51
+ ```typescript
52
+ import { Application } from 'nexfep';
53
+
54
+ const app = new Application();
55
+ // 或指定自定义 WebView2 用户数据目录(仅 Windows)
56
+ const app = new Application('C:\\custom\\webview2-data');
57
+ ```
58
+
59
+ **属性**
60
+
61
+ - `windows` — `WindowPool` 实例,用于管理浏览器窗口
62
+ - `utils` — 工具方法(如桌面通知)
63
+
64
+ **方法**
65
+
66
+ - `createTray(options)` — 创建系统托盘图标和右键菜单
67
+ - `exit()` — 退出应用
68
+
69
+ ### 托盘图标
70
+
71
+ 通过 `app.createTray()` 创建和管理系统托盘图标及右键菜单。
72
+
73
+ ```typescript
74
+ import { readFileSync } from 'fs';
75
+
76
+ const tray = app.createTray({
77
+ id: 'my-tray',
78
+ tooltip: '我的应用',
79
+ icon: {
80
+ data: readFileSync('./icon.png'),
81
+ width: 32,
82
+ height: 32,
83
+ },
84
+ menuItems: [
85
+ { id: 'show', label: '显示窗口' },
86
+ { id: 'quit', label: '退出' },
87
+ ],
88
+ });
89
+ ```
90
+
91
+ `icon` 字段接受 `TrayIconImage` 对象:
92
+
93
+ ```typescript
94
+ interface TrayIconImage {
95
+ data: Buffer; // 图片二进制数据
96
+ width?: number; // 可选宽度
97
+ height?: number; // 可选高度
98
+ }
99
+ ```
100
+
101
+ **方法**
102
+
103
+ | 方法 | 描述 |
104
+ |---------------------------------------------|--------------------|
105
+ | `addMenuItem(item)` | 添加菜单项 |
106
+ | `removeMenuItem(id)` | 按 ID 删除菜单项 |
107
+ | `setMenuItems(items)` | 替换所有菜单项 |
108
+ | `setIcon(icon, width?, height?)` | 更改托盘图标(原始像素数据 `Uint8Array` / `number[]`) |
109
+ | `setTooltip(tooltip)` | 更改悬停提示文本 |
110
+ | `on(event, callback)` | 监听托盘事件(如 `'click'`) |
111
+ | `show()` | 显示托盘图标 |
112
+ | `hide()` | 隐藏托盘图标 |
113
+ | `destroy()` | 销毁托盘图标 |
114
+
115
+ ```typescript
116
+ tray.on('click', () => {
117
+ console.log('托盘被点击');
118
+ });
119
+ tray.addMenuItem({ id: 'about', label: '关于' });
120
+ tray.setTooltip('Nexfep 应用');
121
+ ```
122
+
123
+ ### 桌面通知
124
+
125
+ 通过 `app.utils.notify()` 发送桌面通知。
126
+
127
+ ```typescript
128
+ const notification = app.utils.notify('标题', '通知内容');
129
+ ```
130
+
131
+ **参数**
132
+
133
+ - `title` — 通知标题
134
+ - `body`(可选)— 通知正文
135
+
49
136
  ### 窗口池
50
137
 
51
138
  `WindowPool` 是框架的核心管理类,负责窗口的创建和回收。
52
139
 
53
140
  ```typescript
54
- const pool = new WindowPool();
141
+ const pool = app.windows;
55
142
  ```
56
143
 
57
144
  **构造函数参数**
58
145
 
146
+ - `app` — `Application` 实例
59
147
  - `WindowsWebview2UserDataFolder`(可选)— WebView2 用户数据目录,默认为 `%LOCALAPPDATA%\NexfepDevelopment.webview2-data`
60
148
 
61
149
  ### 窗口创建
@@ -348,8 +436,6 @@ if (window.isNexfepLoadDone) {
348
436
  | `unhandle(event, callback)` | `event`: string, `callback`: (data: string) => void | 无 | 取消监听指定事件回调中的指定函数 |
349
437
  | `global` | / | Map\<string, any> | 全局变量Map,类型为 `Map<string, any>` |
350
438
  | `closeWindow(window)` | `window`: Window | Promise\<void> | 关闭指定窗口并回收至池中 |
351
- | `closePool()` | 无 | Promise\<void> | 关闭窗口池,退出应用 |
352
- | `mainloop()` | 无 | void | 启动应用主循环,阻塞直到应用退出 |
353
439
  | `onCustomMessage` | `(window: Window, data: string) => void` | 无 | 自定义消息回调函数,当收到页面发来的自定义消息时触发 |
354
440
 
355
441
  ### Window
@@ -368,6 +454,7 @@ if (window.isNexfepLoadDone) {
368
454
  | `setTitle(title)` | `title`: string — 窗口标题 | void | 设置窗口标题 |
369
455
  | `setDecorated(isDecorated)` | `isDecorated`: boolean — 是否使用系统装饰 | void | 设置窗口是否带边框和标题栏 |
370
456
  | `resizable(resizable)` | `resizable`: boolean — 是否可调整大小 | void | 设置窗口是否可调整大小 |
457
+ | `setSize(width, height)` | `width`: number, `height`: number | void | 设置窗口尺寸(像素) |
371
458
  | `openDevTools()` | 无 | void | 打开开发者工具 |
372
459
  | `closeDevTools()` | 无 | void | 关闭开发者工具 |
373
460
  | `id` | 无 | number | 窗口唯一标识,自增编号 |
package/README.md CHANGED
@@ -33,29 +33,117 @@ pnpm add nexfep
33
33
  ## Quick Start
34
34
 
35
35
  ```typescript
36
- import { WindowPool } from 'nexfep';
36
+ import { Application } from 'nexfep';
37
37
 
38
- const pool = new WindowPool();
38
+ const app = new Application();
39
39
 
40
- const window = await pool.createWindow(true, false);
40
+ const window = await app.windows.createWindow(true, false);
41
41
 
42
42
  await window.loadHTML('<h1 nexfep-area-drag>Hello Nexfep!</h1>');
43
-
44
- pool.mainloop();
45
43
  ```
46
44
 
47
45
  ## Usage Guide
48
46
 
47
+ ### Application
48
+
49
+ `Application` is the main entry point of the framework, responsible for managing the application lifecycle and providing access to windows and system tray.
50
+
51
+ ```typescript
52
+ import { Application } from 'nexfep';
53
+
54
+ const app = new Application();
55
+ // or with custom WebView2 user data directory (Windows only)
56
+ const app = new Application('C:\\custom\\webview2-data');
57
+ ```
58
+
59
+ **Properties**
60
+
61
+ - `windows` — The `WindowPool` instance for managing browser windows
62
+ - `utils` — Utility methods (e.g., desktop notifications)
63
+
64
+ **Methods**
65
+
66
+ - `createTray(options)` — Create a system tray icon with context menu
67
+ - `exit()` — Exit the application
68
+
69
+ ### Tray
70
+
71
+ Create and manage system tray icons with context menus via `app.createTray()`.
72
+
73
+ ```typescript
74
+ import { readFileSync } from 'fs';
75
+
76
+ const tray = app.createTray({
77
+ id: 'my-tray',
78
+ tooltip: 'My App',
79
+ icon: {
80
+ data: readFileSync('./icon.png'),
81
+ width: 32,
82
+ height: 32,
83
+ },
84
+ menuItems: [
85
+ { id: 'show', label: 'Show Window' },
86
+ { id: 'quit', label: 'Quit' },
87
+ ],
88
+ });
89
+ ```
90
+
91
+ The `icon` field accepts a `TrayIconImage` object:
92
+
93
+ ```typescript
94
+ interface TrayIconImage {
95
+ data: Buffer; // Image binary data
96
+ width?: number; // Optional width
97
+ height?: number; // Optional height
98
+ }
99
+ ```
100
+
101
+ **Methods**
102
+
103
+ | Method | Description |
104
+ |---------------------------------------------|-------------------------------------|
105
+ | `addMenuItem(item)` | Add a menu item |
106
+ | `removeMenuItem(id)` | Remove a menu item by ID |
107
+ | `setMenuItems(items)` | Replace all menu items |
108
+ | `setIcon(icon, width?, height?)` | Change the tray icon (raw pixel data as `Uint8Array` / `number[]`) |
109
+ | `setTooltip(tooltip)` | Change the tooltip text |
110
+ | `on(event, callback)` | Listen for tray events (e.g. `'click'`) |
111
+ | `show()` | Show the tray icon |
112
+ | `hide()` | Hide the tray icon |
113
+ | `destroy()` | Destroy the tray icon |
114
+
115
+ ```typescript
116
+ tray.on('click', () => {
117
+ console.log('Tray clicked');
118
+ });
119
+ tray.addMenuItem({ id: 'about', label: 'About' });
120
+ tray.setTooltip('Nexfep App');
121
+ ```
122
+
123
+ ### Notifications
124
+
125
+ Send desktop notifications via `app.utils.notify()`.
126
+
127
+ ```typescript
128
+ const notification = app.utils.notify('Title', 'Notification body');
129
+ ```
130
+
131
+ **Parameters**
132
+
133
+ - `title` — Notification title
134
+ - `body` (optional) — Notification body text
135
+
49
136
  ### Window Pool
50
137
 
51
138
  `WindowPool` is the core management class of the framework, responsible for window creation and recycling.
52
139
 
53
140
  ```typescript
54
- const pool = new WindowPool();
141
+ const pool = app.windows;
55
142
  ```
56
143
 
57
144
  **Constructor Parameters**
58
145
 
146
+ - `app` — The `Application` instance
59
147
  - `WindowsWebview2UserDataFolder` (optional) — WebView2 user data directory, defaults to `%LOCALAPPDATA%\NexfepDevelopment.webview2-data`
60
148
 
61
149
  ### Window Creation
@@ -348,8 +436,6 @@ if (window.isNexfepLoadDone) {
348
436
  | `unhandle(event, callback)` | `event`: string, `callback`: (data: string) => void | None | Removes the specified callback from the event listener |
349
437
  | `global` | / | Map\<string, any> | A global variable map of type: `Map<string, any>` |
350
438
  | `closeWindow(window)` | `window`: Window | Promise\<void> | Closes the specified window and returns it to the pool |
351
- | `closePool()` | None | Promise\<void> | Closes the window pool and exits the application |
352
- | `mainloop()` | None | void | Starts the application main loop, blocking until the application exits |
353
439
  | `onCustomMessage` | `(window: Window, data: string) => void` | None | Custom message callback, triggered when receiving custom messages from pages |
354
440
 
355
441
  ### Window
@@ -368,6 +454,7 @@ if (window.isNexfepLoadDone) {
368
454
  | `setTitle(title)` | `title`: string — Window title | void | Sets the window title |
369
455
  | `setDecorated(isDecorated)` | `isDecorated`: boolean — Use system decorations | void | Sets whether the window has borders and title bar |
370
456
  | `resizable(resizable)` | `resizable`: boolean — Resizable | void | Sets whether the window is resizable |
457
+ | `setSize(width, height)` | `width`: number, `height`: number | void | Sets the window size in pixels |
371
458
  | `openDevTools()` | None | void | Opens developer tools |
372
459
  | `closeDevTools()` | None | void | Closes developer tools |
373
460
  | `id` | None | number | Unique window identifier, auto-incrementing |
package/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './src/WindowManager.js';
1
+ export * from './src/Application.js';
package/index.js CHANGED
@@ -1 +1 @@
1
- export * from './src/WindowManager.js';
1
+ export * from './src/Application.js';
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "webview",
15
15
  "typescript"
16
16
  ],
17
- "version": "0.1.7",
17
+ "version": "0.2.0",
18
18
  "type": "module",
19
19
  "main": "./index.js",
20
20
  "scripts": {
@@ -33,10 +33,14 @@
33
33
  "LICENSE",
34
34
  "package.json",
35
35
  "src/WindowManager.js",
36
- "src/WindowManager.d.ts"
36
+ "src/WindowManager.d.ts",
37
+ "src/Application.js",
38
+ "src/Application.d.ts",
39
+ "src/Tray.js",
40
+ "src/Tray.d.ts"
37
41
  ],
38
42
  "dependencies": {
39
- "@webviewjs/webview": "0.1.4"
43
+ "@webviewjs/webview": "0.4.0"
40
44
  },
41
45
  "devDependencies": {
42
46
  "@types/node": "^22.0.0",
@@ -0,0 +1,25 @@
1
+ import { Application as WebviewApplication, TrayIconImage, Notification } from '@webviewjs/webview';
2
+ import { WindowPool } from './WindowManager.js';
3
+ import { Tray } from './Tray.js';
4
+ declare class __Utils {
5
+ app: WebviewApplication;
6
+ constructor(app: WebviewApplication);
7
+ notify(title: string, body?: string): Notification;
8
+ }
9
+ declare class Application {
10
+ app: WebviewApplication;
11
+ windows: WindowPool;
12
+ utils: __Utils;
13
+ constructor(WindowsWebview2UserDataFolder?: string);
14
+ createTray(options: {
15
+ id: string;
16
+ tooltip: string;
17
+ icon: TrayIconImage | undefined;
18
+ menuItems: Array<{
19
+ id: string;
20
+ label: string;
21
+ }>;
22
+ }): Tray;
23
+ exit(): void;
24
+ }
25
+ export { Application };
@@ -0,0 +1,33 @@
1
+ import { Application as WebviewApplication, Notification } from '@webviewjs/webview';
2
+ import { WindowPool } from './WindowManager.js';
3
+ import { Tray } from './Tray.js';
4
+ import path from 'path';
5
+ import os from 'os';
6
+ class __Utils {
7
+ app;
8
+ constructor(app) {
9
+ this.app = app;
10
+ }
11
+ notify(title, body) {
12
+ const notification = new Notification(title, { body });
13
+ return notification;
14
+ }
15
+ }
16
+ class Application {
17
+ app;
18
+ windows;
19
+ utils;
20
+ constructor(WindowsWebview2UserDataFolder = path.join(process.env.LOCALAPPDATA || os.homedir(), 'NexfepDevelopment.webview2-data')) {
21
+ this.app = new WebviewApplication();
22
+ this.utils = new __Utils(this.app);
23
+ this.windows = new WindowPool(this.app, WindowsWebview2UserDataFolder);
24
+ this.app.whenReady();
25
+ }
26
+ createTray(options) {
27
+ return new Tray(this.app, options);
28
+ }
29
+ exit() {
30
+ this.app.exit();
31
+ }
32
+ }
33
+ export { Application };
package/src/Tray.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { Application, TrayIcon, TrayIconImage } from '@webviewjs/webview';
2
+ declare class Tray {
3
+ app: Application;
4
+ tray: TrayIcon;
5
+ menuItems: Array<{
6
+ id: string;
7
+ label: string;
8
+ }>;
9
+ destroyed: boolean;
10
+ constructor(app: Application, options: {
11
+ id: string;
12
+ tooltip: string;
13
+ icon: TrayIconImage | undefined;
14
+ menuItems: Array<{
15
+ id: string;
16
+ label: string;
17
+ }>;
18
+ });
19
+ __ErrorWhenDestroyed(): void;
20
+ addMenuItem(item: {
21
+ id: string;
22
+ label: string;
23
+ }): void;
24
+ removeMenuItem(id: string): void;
25
+ setMenuItems(items: Array<{
26
+ id: string;
27
+ label: string;
28
+ }>): void;
29
+ setIcon(icon: Uint8Array<ArrayBuffer | SharedArrayBuffer> | number[], width?: number | null | undefined, height?: number | null | undefined): void;
30
+ on(event: string, callback: (event: any) => void): void;
31
+ setTooltip(tooltip: string): void;
32
+ hide(): void;
33
+ show(): void;
34
+ destroy(): void;
35
+ }
36
+ export { Tray };
package/src/Tray.js ADDED
@@ -0,0 +1,65 @@
1
+ class Tray {
2
+ app;
3
+ tray;
4
+ menuItems;
5
+ destroyed;
6
+ constructor(app, options) {
7
+ this.menuItems = options.menuItems;
8
+ this.destroyed = false;
9
+ this.app = app;
10
+ this.tray = app.createTrayIcon({
11
+ id: options.id,
12
+ tooltip: options.tooltip,
13
+ icon: options.icon,
14
+ menu: {
15
+ items: this.menuItems
16
+ }
17
+ });
18
+ }
19
+ __ErrorWhenDestroyed() {
20
+ if (this.destroyed) {
21
+ throw new Error('Tray has been destroyed');
22
+ }
23
+ }
24
+ addMenuItem(item) {
25
+ this.__ErrorWhenDestroyed();
26
+ this.menuItems.push(item);
27
+ this.tray.setMenu({ items: this.menuItems });
28
+ }
29
+ removeMenuItem(id) {
30
+ this.__ErrorWhenDestroyed();
31
+ this.menuItems = this.menuItems.filter(item => item.id !== id);
32
+ this.tray.setMenu({ items: this.menuItems });
33
+ }
34
+ setMenuItems(items) {
35
+ this.__ErrorWhenDestroyed();
36
+ this.menuItems = items;
37
+ this.tray.setMenu({ items: this.menuItems });
38
+ }
39
+ setIcon(icon, width, height) {
40
+ this.__ErrorWhenDestroyed();
41
+ this.tray.setIcon(icon, width, height);
42
+ }
43
+ on(event, callback) {
44
+ this.__ErrorWhenDestroyed();
45
+ this.tray.on(event, callback);
46
+ }
47
+ setTooltip(tooltip) {
48
+ this.__ErrorWhenDestroyed();
49
+ this.tray.setTooltip(tooltip);
50
+ }
51
+ hide() {
52
+ this.__ErrorWhenDestroyed();
53
+ this.tray.setVisible(false);
54
+ }
55
+ show() {
56
+ this.__ErrorWhenDestroyed();
57
+ this.tray.setVisible(true);
58
+ }
59
+ destroy() {
60
+ this.__ErrorWhenDestroyed();
61
+ this.tray.dispose();
62
+ this.destroyed = true;
63
+ }
64
+ }
65
+ export { Tray };
@@ -1,4 +1,4 @@
1
- import { BrowserWindow, Webview } from "@webviewjs/webview";
1
+ import { Application, BrowserWindow, Webview } from "@webviewjs/webview";
2
2
  declare class Window {
3
3
  window: BrowserWindow;
4
4
  webview: Webview;
@@ -17,6 +17,7 @@ declare class Window {
17
17
  unMinimize(): void;
18
18
  openDevTools(): void;
19
19
  closeDevTools(): void;
20
+ setSize(width: number, height: number): void;
20
21
  hide(): void;
21
22
  show(): void;
22
23
  close(): void;
@@ -25,22 +26,20 @@ declare class Window {
25
26
  }
26
27
  declare class WindowPool {
27
28
  onCustomMessage: (window: Window, data: string) => void;
28
- private app;
29
+ app: Application;
29
30
  private windows;
30
31
  private handlers;
31
32
  private injectCount;
32
33
  private windowCount;
33
34
  private freeWindowCount;
34
35
  global: Map<string, any>;
35
- constructor(WindowsWebview2UserDataFolder?: string);
36
+ constructor(app: Application, WindowsWebview2UserDataFolder?: string);
36
37
  __injectCode(window: Window, code: string): Promise<void>;
37
38
  __injectControlFunctions(windowObj: Window): Promise<void>;
38
39
  __createNewWindowObj(): Promise<Window>;
39
40
  createWindow(isShow?: boolean, isDecorated?: boolean): Promise<Window>;
40
41
  closeWindow(window: Window): Promise<void>;
41
- closePool(): Promise<void>;
42
42
  handle(event: string, callback: (data: any) => any): Promise<void>;
43
43
  unhandle(event: string, callback: (data: any) => any): Promise<void>;
44
- mainloop(): void;
45
44
  }
46
45
  export { WindowPool, Window };
@@ -1,4 +1,3 @@
1
- import { Application } from "@webviewjs/webview";
2
1
  import os from 'os';
3
2
  import path from 'path';
4
3
  import fs from 'fs';
@@ -47,6 +46,9 @@ class Window {
47
46
  closeDevTools() {
48
47
  this.webview.closeDevtools();
49
48
  }
49
+ setSize(width, height) {
50
+ this.window.setSize(width, height);
51
+ }
50
52
  hide() {
51
53
  this.window.hide();
52
54
  this.isShow = false;
@@ -76,7 +78,7 @@ class WindowPool {
76
78
  windowCount;
77
79
  freeWindowCount;
78
80
  global;
79
- constructor(WindowsWebview2UserDataFolder = path.join(process.env.LOCALAPPDATA || os.homedir(), 'NexfepDevelopment.webview2-data')) {
81
+ constructor(app, WindowsWebview2UserDataFolder = path.join(process.env.LOCALAPPDATA || os.homedir(), 'NexfepDevelopment.webview2-data')) {
80
82
  if (os.platform() === 'win32') {
81
83
  // 设置 WebView2 用户数据目录(仅 Windows 需要此配置)
82
84
  const userDataDir = WindowsWebview2UserDataFolder;
@@ -89,7 +91,7 @@ class WindowPool {
89
91
  console.log('Get Custom Message:', data, "from window", window.id);
90
92
  };
91
93
  this.handlers = new Map();
92
- this.app = new Application();
94
+ this.app = app;
93
95
  this.injectCount = 0;
94
96
  this.windows = [];
95
97
  this.windowCount = 0;
@@ -371,17 +373,11 @@ class WindowPool {
371
373
  throw new Error("Window is not open");
372
374
  }
373
375
  }
374
- async closePool() {
375
- this.app.exit();
376
- }
377
376
  async handle(event, callback) {
378
377
  this.handlers.set(event, [...this.handlers.get(event) || [], callback]);
379
378
  }
380
379
  async unhandle(event, callback) {
381
380
  this.handlers.set(event, (this.handlers.get(event) || []).filter(c => c !== callback));
382
381
  }
383
- mainloop() {
384
- this.app.run();
385
- }
386
382
  }
387
383
  export { WindowPool, Window };