nexfep 0.1.1 → 0.1.3

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.
Files changed (6) hide show
  1. package/LICENSE +20 -20
  2. package/README-CN.md +323 -285
  3. package/README.md +323 -285
  4. package/index.d.ts +46 -45
  5. package/index.js +152 -127
  6. package/package.json +35 -36
package/README.md CHANGED
@@ -1,285 +1,323 @@
1
- # Nexfep
2
-
3
- Language: English(now) | [简体中文](https://github.com/nexfteam/Nexfep/blob/main/README-CN.md)
4
-
5
- A desktop application framework based on @webviewjs/webview
6
-
7
- ## Project Status
8
-
9
- **🚧 Early Stage**
10
-
11
- This project is currently in early development, with many core capabilities for desktop application development still missing. The framework is being continuously iterated.
12
-
13
- ## Introduction
14
-
15
- Nexfep is a desktop application framework built on [@webviewjs/webview](https://github.com/webviewjs/webview), written in TypeScript. It aims to provide developers with a concise and efficient toolchain for building cross-platform desktop applications.
16
-
17
- The framework uses a window pool management mechanism, supporting multi-window application scenarios such as code editors, chat tools, dashboards, etc.
18
-
19
- ## Features
20
-
21
- - **Window Pool Management** — Built-in window pool mechanism for automatic reuse and recycling of window resources, avoiding the overhead of frequent creation and destruction
22
- - **IPC Communication** — Supports bidirectional message communication between the main process and WebView, via injected functions
23
- - **Window Control** — Provides complete window operation APIs including maximize, minimize, close, title setting, and developer tools
24
- - **Drag Regions** — Built-in HTML attribute support for defining window drag regions (`nexfep-area-drag`, etc.)
25
- - **TypeScript Support** — Complete type definitions for excellent development experience
26
-
27
- ## Installation
28
-
29
- ```bash
30
- pnpm add nexfep
31
- ```
32
-
33
- ## Quick Start
34
-
35
- ```typescript
36
- import { WindowPool } from 'nexfep';
37
-
38
- const pool = new WindowPool();
39
-
40
- const window = await pool.createWindow(true, false);
41
-
42
- await window.loadHTML('<h1 nexfep-area-drag>Hello Nexfep!</h1>');
43
-
44
- pool.mainloop();
45
- ```
46
-
47
- ## Usage Guide
48
-
49
- ### Window Pool
50
-
51
- `WindowPool` is the core management class of the framework, responsible for window creation and recycling.
52
-
53
- ```typescript
54
- const pool = new WindowPool();
55
- ```
56
-
57
- **Constructor Parameters**
58
-
59
- - `WindowsWebview2UserDataFolder` (optional) — WebView2 user data directory, defaults to `%LOCALAPPDATA%\NexfepDevelopment.webview2-data`
60
-
61
- ### Window Creation
62
-
63
- ```typescript
64
- const win = await pool.createWindow(true, false);
65
- ```
66
-
67
- **Parameters**
68
-
69
- - `isShow` (boolean, default `true`) — Whether to immediately show the window
70
- - `isDecorated` (boolean, default `true`) — Whether to use system window decorations. When set to `false`, the window has no border and requires a custom title bar
71
-
72
- ### Window Operations
73
-
74
- ```typescript
75
- window.show();
76
- window.hide();
77
- window.maximize();
78
- window.minimize();
79
- window.close();
80
- window.setTitle('New Title');
81
- window.openDevTools();
82
- ```
83
-
84
- ### IPC Communication
85
-
86
- The framework automatically injects a series of control functions after WebView page loading is complete. It is recommended to use these injected functions for IPC communication, rather than the native IPC wrapped by `@webviewjs/webview`.
87
-
88
- #### Send Custom Messages
89
-
90
- Send messages via `window.postMessage` in the page:
91
-
92
- ```javascript
93
- window.postMessage({ hello: 'world' });
94
- ```
95
-
96
- **Parameters**
97
-
98
- - `data` Any serializable data, will be serialized to JSON string before sending
99
-
100
- #### Listen for Messages
101
-
102
- Receive messages via `onCustomMessage` callback in the main process:
103
-
104
- ```typescript
105
- pool.onCustomMessage = (window, data) => {
106
- console.log(`Message from window ${window.id}:`, data);
107
- };
108
- ```
109
-
110
- **Callback Parameters**
111
-
112
- - `window` — The window object that sent the message
113
- - `data` — Message content, an object type (automatically converted from JSON string via `JSON.parse`)
114
-
115
- #### Invoke Custom Events
116
-
117
- Invoke events via `window.invoke` in the page:
118
-
119
- ```javascript
120
- window.invoke('hello', 'world');
121
- ```
122
-
123
- **Parameters**
124
-
125
- - `event` — Event name
126
- - `data` — Any serializable data, will be serialized to JSON string before sending
127
-
128
- #### Listen for Events
129
-
130
- Listen for events via `pool.handle` in the main process:
131
-
132
- ```typescript
133
- pool.handle('hello', (data) => {
134
- console.log('Received event hello:', data);
135
- });
136
- ```
137
-
138
- **Parameters**
139
-
140
- - `event` — Event name, must match the event name when invoking
141
- - `callback` — Event handler function, receives event data as `data` parameter. Can return any serializable data, which will be serialized to JSON string and sent back to the frontend as the return value of `window.invoke` (can also return nothing)
142
-
143
- #### Remove Event Listener
144
-
145
- Remove event listener via `pool.unhandle` in the main process:
146
-
147
- ```typescript
148
- pool.unhandle('hello', (data) => {
149
- console.log('Received event hello:', data);
150
- });
151
- ```
152
-
153
- **Parameters**
154
-
155
- - `event` — Event name, must match the event name when listening
156
- - `callback` — Event handler function, must match the callback function when listening
157
-
158
- #### Window Control Functions
159
-
160
- The following injected functions can be directly called in the page for window control:
161
-
162
- ```javascript
163
- window.close(); // Close window
164
- window.minimize(); // Minimize window
165
- window.unminimize(); // Restore minimized window
166
- window.maximize(); // Maximize window
167
- window.unmaximize(); // Restore maximized window
168
- window.setTitle('Title'); // Set window title
169
- window.openDevTools(); // Open developer tools
170
- window.closeDevTools(); // Close developer tools
171
- ```
172
-
173
- ### Drag Regions
174
-
175
- Define window drag regions via HTML attributes without writing additional JavaScript code. These attributes automatically apply `-webkit-app-region` and `app-region` CSS properties.
176
-
177
- #### `nexfep-area-drag`
178
-
179
- Makes the entire region and all its child elements draggable. Suitable for scenarios like custom title bars where the entire region needs to be draggable.
180
-
181
- ```html
182
- <div nexfep-area-drag>
183
- <h1>Title Bar</h1>
184
- <span>Subtitle</span>
185
- </div>
186
- ```
187
-
188
- #### `nexfep-element-drag`
189
-
190
- Makes only the specified element itself draggable; child elements are not affected. Suitable for scenarios requiring precise control over the drag region.
191
-
192
- ```html
193
- <div>
194
- <div nexfep-element-drag>Drag Handle</div>
195
- <p>This part is not draggable</p>
196
- </div>
197
- ```
198
-
199
- #### `nexfep-no-drag`
200
-
201
- Makes the specified region and all its child elements non-draggable. Highest priority, can override parent element's drag attributes. Suitable for interactive elements like buttons and input fields.
202
-
203
- ```html
204
- <div nexfep-area-drag>
205
- <h1>Title Bar</h1>
206
- <button nexfep-no-drag>Click Button</button>
207
- </div>
208
- ```
209
-
210
- #### `nexfep-auto-drag`
211
-
212
- Automatically determines drag regions: the entire region is draggable, but common interactive elements (`button`, `input`, `select`, `textarea`, `a`) are automatically set to non-draggable. Suitable for complex regions containing multiple interactive elements.
213
-
214
- ```html
215
- <div nexfep-auto-drag>
216
- <h1>Title Bar</h1>
217
- <button>Automatically non-draggable</button>
218
- <input placeholder="Automatically non-draggable" />
219
- <a href="#">Automatically non-draggable</a>
220
- </div>
221
- ```
222
-
223
- ### Load Complete Event
224
-
225
- The `nexfep-load-done` event is triggered after the WebView window finishes loading:
226
-
227
- ```javascript
228
- window.addEventListener('nexfep-load-done', () => {
229
- console.log('Nexfep window loaded');
230
- });
231
- ```
232
-
233
- It can also be checked via the `window.isNexfepLoadDone` property:
234
-
235
- ```javascript
236
- if (window.isNexfepLoadDone) {
237
- // Window is ready
238
- }
239
- ```
240
-
241
- ## API
242
-
243
- ### WindowPool
244
-
245
- | Method/Property | Parameters | Return Value | Description |
246
- | ------------------------------------- | -------------------------------------------------------------------------- | ---------------- | ------------------------------------------------------------------------ |
247
- | `constructor(userDataFolder?)` | `userDataFolder`: string (optional) | WindowPool | Creates a window pool, optionally specifying the WebView2 user data directory |
248
- | `createWindow(isShow?, isDecorated?)` | `isShow`: boolean (default true), `isDecorated`: boolean (default true) | Promise\<Window> | Creates and returns a window |
249
- | `handle(event, callback)` | `event`: string, `callback`: (data: string) => void | None | Listens for the specified event, triggers callback when event is received |
250
- | `unhandle(event, callback)` | `event`: string, `callback`: (data: string) => void | None | Removes the specified callback from the event listener |
251
- | `closeWindow(window)` | `window`: Window | Promise\<void> | Closes the specified window and returns it to the pool |
252
- | `closePool()` | None | Promise\<void> | Closes the window pool and exits the application |
253
- | `mainloop()` | None | void | Starts the application main loop, blocking until the application exits |
254
- | `onCustomMessage` | `(window: Window, data: string) => void` | None | Custom message callback, triggered when receiving custom messages from pages |
255
-
256
- ### Window
257
-
258
- | Method/Property | Parameters | Return Value | Description |
259
- | ------------------------------------- | ------------------------------------------- | ---------------- | ---------------------------------------- |
260
- | `loadURL(url)` | `url`: string — URL to load | Promise\<void> | Loads the specified URL |
261
- | `loadHTML(html)` | `html`: string — HTML string | Promise\<void> | Loads the specified HTML content |
262
- | `show()` | None | void | Shows the window |
263
- | `hide()` | None | void | Hides the window |
264
- | `maximize()` | None | void | Maximizes the window |
265
- | `unMaximize()` | None | void | Restores the window (cancels maximize) |
266
- | `minimize()` | None | void | Minimizes the window |
267
- | `unMinimize()` | None | void | Restores the window (cancels minimize) |
268
- | `close()` | None | void | Closes the window and returns to pool |
269
- | `setTitle(title)` | `title`: string — Window title | void | Sets the window title |
270
- | `setDecorated(isDecorated)` | `isDecorated`: boolean Use system decorations | void | Sets whether the window has borders and title bar |
271
- | `resizable(resizable)` | `resizable`: boolean — Resizable | void | Sets whether the window is resizable |
272
- | `openDevTools()` | None | void | Opens developer tools |
273
- | `closeDevTools()` | None | void | Closes developer tools |
274
- | `id` | None | number | Unique window identifier, auto-incrementing |
275
-
276
- ## Development
277
-
278
- ```bash
279
- pnpm install
280
- pnpm run compile
281
- ```
282
-
283
- ## License
284
-
285
- MIT License
1
+ # Nexfep
2
+
3
+ Language: English(now) | [简体中文](https://github.com/nexfteam/Nexfep/blob/main/README-CN.md)
4
+
5
+ A desktop application framework based on @webviewjs/webview
6
+
7
+ ## Project Status
8
+
9
+ **🚧 Early Stage**
10
+
11
+ This project is currently in early development, with many core capabilities for desktop application development still missing. The framework is being continuously iterated.
12
+
13
+ ## Introduction
14
+
15
+ Nexfep is a desktop application framework built on [@webviewjs/webview](https://github.com/webviewjs/webview), written in TypeScript. It aims to provide developers with a concise and efficient toolchain for building cross-platform desktop applications.
16
+
17
+ The framework uses a window pool management mechanism, supporting multi-window application scenarios such as code editors, chat tools, dashboards, etc.
18
+
19
+ ## Features
20
+
21
+ - **Window Pool Management** — Built-in window pool mechanism for automatic reuse and recycling of window resources, avoiding the overhead of frequent creation and destruction
22
+ - **IPC Communication** — Supports bidirectional message communication between the main process and WebView, via injected functions
23
+ - **Window Control** — Provides complete window operation APIs including maximize, minimize, close, title setting, and developer tools
24
+ - **Drag Regions** — Built-in HTML attribute support for defining window drag regions (`nexfep-area-drag`, etc.)
25
+ - **TypeScript Support** — Complete type definitions for excellent development experience
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ pnpm add nexfep
31
+ ```
32
+
33
+ ## Quick Start
34
+
35
+ ```typescript
36
+ import { WindowPool } from 'nexfep';
37
+
38
+ const pool = new WindowPool();
39
+
40
+ const window = await pool.createWindow(true, false);
41
+
42
+ await window.loadHTML('<h1 nexfep-area-drag>Hello Nexfep!</h1>');
43
+
44
+ pool.mainloop();
45
+ ```
46
+
47
+ ## Usage Guide
48
+
49
+ ### Window Pool
50
+
51
+ `WindowPool` is the core management class of the framework, responsible for window creation and recycling.
52
+
53
+ ```typescript
54
+ const pool = new WindowPool();
55
+ ```
56
+
57
+ **Constructor Parameters**
58
+
59
+ - `WindowsWebview2UserDataFolder` (optional) — WebView2 user data directory, defaults to `%LOCALAPPDATA%\NexfepDevelopment.webview2-data`
60
+
61
+ ### Window Creation
62
+
63
+ ```typescript
64
+ const win = await pool.createWindow(true, false);
65
+ ```
66
+
67
+ **Parameters**
68
+
69
+ - `isShow` (boolean, default `true`) — Whether to immediately show the window
70
+ - `isDecorated` (boolean, default `true`) — Whether to use system window decorations. When set to `false`, the window has no border and requires a custom title bar
71
+
72
+ ### Window Operations
73
+
74
+ ```javascript
75
+ window.show();
76
+ window.hide();
77
+ window.maximize();
78
+ window.minimize();
79
+ window.close();
80
+ window.setTitle('New Title');
81
+ window.openDevTools();
82
+ ```
83
+
84
+ ### Custom Messages
85
+
86
+ #### Send Messages
87
+
88
+ Send messages via `window.postMessage` in the page:
89
+
90
+ ```javascript
91
+ window.postMessage({ hello: 'world' });
92
+ ```
93
+
94
+ **Parameters**
95
+
96
+ - `data` — Any serializable data, will be serialized to JSON string before sending
97
+
98
+ #### Listen for Messages
99
+
100
+ Receive messages via `onCustomMessage` callback in the main process:
101
+
102
+ ```typescript
103
+ pool.onCustomMessage = (window, data) => {
104
+ console.log(`Message from window ${window.id}:`, data);
105
+ };
106
+ ```
107
+
108
+ **Callback Parameters**
109
+
110
+ - `window` — The window object that sent the message
111
+ - `data` — Message content, an object type (automatically converted from JSON string via `JSON.parse`)
112
+
113
+ ### Custom Events
114
+
115
+ #### Invoke Events
116
+
117
+ Invoke events via `window.invoke` in the page:
118
+
119
+ ```javascript
120
+ window.invoke('hello', 'world');
121
+ ```
122
+
123
+ **Parameters**
124
+
125
+ - `event` — Event name
126
+ - `data` — Any serializable data, will be serialized to JSON string before sending
127
+
128
+ #### Listen for Events
129
+
130
+ Listen for events via `pool.handle` in the main process:
131
+
132
+ ```typescript
133
+ pool.handle('hello', (data) => {
134
+ console.log('Received event hello:', data);
135
+ });
136
+ ```
137
+
138
+ **Parameters**
139
+
140
+ - `event` — Event name, must match the event name when invoking
141
+ - `callback` — Event handler function, receives event data as `data` parameter. Can return any serializable data, which will be serialized to JSON string and sent back to the frontend as the return value of `window.invoke` (can also return nothing)
142
+
143
+ #### Remove Event Listener
144
+
145
+ Remove event listener via `pool.unhandle` in the main process:
146
+
147
+ ```typescript
148
+ pool.unhandle('hello', (data) => {
149
+ console.log('Received event hello:', data);
150
+ });
151
+ ```
152
+
153
+ **Parameters**
154
+
155
+ - `event` — Event name, must match the event name when listening
156
+ - `callback` — Event handler function, must match the callback function when listening
157
+
158
+ ### Global Variables
159
+
160
+ #### Set Variables
161
+
162
+ Set a global variable via `window.setGlobal` in the page:
163
+
164
+ ```javascript
165
+ window.setGlobal('hello', 'world');
166
+ ```
167
+
168
+ **Parameters**
169
+
170
+ - `name` Global variable name
171
+ - `value` — Global variable value, any serializable data, will be serialized to JSON string before sending
172
+
173
+ #### Get Variables
174
+
175
+ Get a global variable via `window.getGlobal` in the page:
176
+
177
+ ```javascript
178
+ const value = await window.getGlobal('hello');
179
+ ```
180
+
181
+ **Parameters**
182
+
183
+ - `name` — Global variable name
184
+
185
+ #### Global Variable Map
186
+
187
+ Get a `Map<string, any>` containing all global variables via `WindowPool.global` in the main process, which supports operations like set and get:
188
+
189
+ ```typescript
190
+ const globals = pool.global;
191
+ globals.set('hello', 'world');
192
+ const value = globals.get('hello');
193
+ ```
194
+
195
+ ### Window Control Functions
196
+
197
+ The following injected functions can be directly called in the page for window control:
198
+
199
+ ```javascript
200
+ window.close(); // Close window
201
+ window.minimize(); // Minimize window
202
+ window.unminimize(); // Restore minimized window
203
+ window.maximize(); // Maximize window
204
+ window.unmaximize(); // Restore maximized window
205
+ window.setTitle('Title'); // Set window title
206
+ window.openDevTools(); // Open developer tools
207
+ window.closeDevTools(); // Close developer tools
208
+ ```
209
+
210
+ ### Drag Regions
211
+
212
+ Define window drag regions via HTML attributes without writing additional JavaScript code. These attributes automatically apply `-webkit-app-region` and `app-region` CSS properties.
213
+
214
+ #### `nexfep-area-drag`
215
+
216
+ Makes the entire region and all its child elements draggable. Suitable for scenarios like custom title bars where the entire region needs to be draggable.
217
+
218
+ ```html
219
+ <div nexfep-area-drag>
220
+ <h1>Title Bar</h1>
221
+ <span>Subtitle</span>
222
+ </div>
223
+ ```
224
+
225
+ #### `nexfep-element-drag`
226
+
227
+ Makes only the specified element itself draggable; child elements are not affected. Suitable for scenarios requiring precise control over the drag region.
228
+
229
+ ```html
230
+ <div>
231
+ <div nexfep-element-drag>Drag Handle</div>
232
+ <p>This part is not draggable</p>
233
+ </div>
234
+ ```
235
+
236
+ #### `nexfep-no-drag`
237
+
238
+ Makes the specified region and all its child elements non-draggable. Highest priority, can override parent element's drag attributes. Suitable for interactive elements like buttons and input fields.
239
+
240
+ ```html
241
+ <div nexfep-area-drag>
242
+ <h1>Title Bar</h1>
243
+ <button nexfep-no-drag>Click Button</button>
244
+ </div>
245
+ ```
246
+
247
+ #### `nexfep-auto-drag`
248
+
249
+ Automatically determines drag regions: the entire region is draggable, but common interactive elements (`button`, `input`, `select`, `textarea`, `a`) are automatically set to non-draggable. Suitable for complex regions containing multiple interactive elements.
250
+
251
+ ```html
252
+ <div nexfep-auto-drag>
253
+ <h1>Title Bar</h1>
254
+ <button>Automatically non-draggable</button>
255
+ <input placeholder="Automatically non-draggable" />
256
+ <a href="#">Automatically non-draggable</a>
257
+ </div>
258
+ ```
259
+
260
+ ### Load Complete Event
261
+
262
+ The `nexfep-load-done` event is triggered after the WebView window finishes loading:
263
+
264
+ ```javascript
265
+ window.addEventListener('nexfep-load-done', () => {
266
+ console.log('Nexfep window loaded');
267
+ });
268
+ ```
269
+
270
+ It can also be checked via the `window.isNexfepLoadDone` property:
271
+
272
+ ```javascript
273
+ if (window.isNexfepLoadDone) {
274
+ // Window is ready
275
+ }
276
+ ```
277
+
278
+ ## API
279
+
280
+ ### WindowPool
281
+
282
+ | Method/Property | Parameters | Return Value | Description |
283
+ | ------------------------------------- | -------------------------------------------------------------------------- | ---------------- | ------------------------------------------------------------------------ |
284
+ | `constructor(userDataFolder?)` | `userDataFolder`: string (optional) | WindowPool | Creates a window pool, optionally specifying the WebView2 user data directory |
285
+ | `createWindow(isShow?, isDecorated?)` | `isShow`: boolean (default true), `isDecorated`: boolean (default true) | Promise\<Window> | Creates and returns a window |
286
+ | `handle(event, callback)` | `event`: string, `callback`: (data: string) => void | None | Listens for the specified event, triggers callback when event is received |
287
+ | `unhandle(event, callback)` | `event`: string, `callback`: (data: string) => void | None | Removes the specified callback from the event listener |
288
+ | `global` | / | Map\<string, any> | A global variable map of type: `Map<string, any>` |
289
+ | `closeWindow(window)` | `window`: Window | Promise\<void> | Closes the specified window and returns it to the pool |
290
+ | `closePool()` | None | Promise\<void> | Closes the window pool and exits the application |
291
+ | `mainloop()` | None | void | Starts the application main loop, blocking until the application exits |
292
+ | `onCustomMessage` | `(window: Window, data: string) => void` | None | Custom message callback, triggered when receiving custom messages from pages |
293
+
294
+ ### Window
295
+
296
+ | Method/Property | Parameters | Return Value | Description |
297
+ | ------------------------------------- | ------------------------------------------- | ---------------- | ---------------------------------------- |
298
+ | `loadURL(url)` | `url`: string — URL to load | Promise\<void> | Loads the specified URL |
299
+ | `loadHTML(html)` | `html`: string — HTML string | Promise\<void> | Loads the specified HTML content |
300
+ | `show()` | None | void | Shows the window |
301
+ | `hide()` | None | void | Hides the window |
302
+ | `maximize()` | None | void | Maximizes the window |
303
+ | `unMaximize()` | None | void | Restores the window (cancels maximize) |
304
+ | `minimize()` | None | void | Minimizes the window |
305
+ | `unMinimize()` | None | void | Restores the window (cancels minimize) |
306
+ | `close()` | None | void | Closes the window and returns to pool |
307
+ | `setTitle(title)` | `title`: string — Window title | void | Sets the window title |
308
+ | `setDecorated(isDecorated)` | `isDecorated`: boolean — Use system decorations | void | Sets whether the window has borders and title bar |
309
+ | `resizable(resizable)` | `resizable`: boolean — Resizable | void | Sets whether the window is resizable |
310
+ | `openDevTools()` | None | void | Opens developer tools |
311
+ | `closeDevTools()` | None | void | Closes developer tools |
312
+ | `id` | None | number | Unique window identifier, auto-incrementing |
313
+
314
+ ## Development
315
+
316
+ ```bash
317
+ pnpm install
318
+ pnpm run compile
319
+ ```
320
+
321
+ ## License
322
+
323
+ MIT License