electron-multi-app-kit 0.1.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/LICENSE +21 -0
- package/README.md +611 -0
- package/dist/core/context.d.ts +15 -0
- package/dist/core/eventBus.d.ts +2 -0
- package/dist/core/lifecycle.d.ts +9 -0
- package/dist/create-app.d.ts +30 -0
- package/dist/index-CtN4cHF_.mjs +4 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +1 -0
- package/dist/modules/autoLaunch.d.ts +47 -0
- package/dist/modules/batchDownloader.d.ts +89 -0
- package/dist/modules/downloader.d.ts +38 -0
- package/dist/modules/downloader.optimized.d.ts +45 -0
- package/dist/modules/instance.d.ts +8 -0
- package/dist/modules/ipc/builtins/dialog.d.ts +6 -0
- package/dist/modules/ipc/builtins/downloader.d.ts +6 -0
- package/dist/modules/ipc/builtins/fs-api.d.ts +4 -0
- package/dist/modules/ipc/builtins/notification.d.ts +5 -0
- package/dist/modules/ipc/builtins/shell.d.ts +5 -0
- package/dist/modules/ipc/builtins/window-control.d.ts +5 -0
- package/dist/modules/ipc/builtins/window-move.d.ts +7 -0
- package/dist/modules/ipc/index.d.ts +15 -0
- package/dist/modules/session.d.ts +6 -0
- package/dist/modules/tray.d.ts +7 -0
- package/dist/modules/updater.d.ts +5 -0
- package/dist/modules/window/manager.d.ts +103 -0
- package/dist/modules/window/useFloatWin.d.ts +6 -0
- package/dist/modules/window/useWinDrag.d.ts +8 -0
- package/dist/preload/base.d.ts +34 -0
- package/dist/preload/events.d.ts +6 -0
- package/dist/preload/index.d.ts +1 -0
- package/dist/preload/index.js +1 -0
- package/dist/renderer/composable/useIpcRenderer.d.ts +28 -0
- package/dist/renderer/composable/useWindowMove.d.ts +23 -0
- package/dist/renderer/index.d.ts +4 -0
- package/dist/renderer/index.js +1 -0
- package/dist/renderer/style.css +1 -0
- package/dist/types.d.ts +127 -0
- package/dist/utils/clone.d.ts +4 -0
- package/dist/utils/debounce.d.ts +4 -0
- package/dist/utils/merge.d.ts +7 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dsir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
# electron-multi-app-kit
|
|
2
|
+
|
|
3
|
+
> Electron 主进程生命周期 & 模块库 — setup 风格,Hook 驱动,按需配置。
|
|
4
|
+
|
|
5
|
+
为 Electron 应用的主进程提供可复用的脚手架:窗口管理、IPC 事件、系统托盘、自动更新、下载管理、HTTP/WS 服务等模块,全部通过配置驱动,无需重复编写样板代码。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 快速开始
|
|
10
|
+
|
|
11
|
+
### 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install electron-multi-app-kit
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### 最小示例
|
|
18
|
+
|
|
19
|
+
用 `createElectronApp` 替换你的主进程入口:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// electron/main/index.ts
|
|
23
|
+
import { createElectronApp } from 'electron-multi-app-kit'
|
|
24
|
+
import path from 'node:path'
|
|
25
|
+
|
|
26
|
+
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
|
27
|
+
|
|
28
|
+
createElectronApp({
|
|
29
|
+
// 路径配置(必填)
|
|
30
|
+
paths: {
|
|
31
|
+
rendererDist: path.join(__dirname, '../../dist'),
|
|
32
|
+
mainDist: path.join(__dirname, '../../dist-electron'),
|
|
33
|
+
publicDir: path.join(__dirname, '../../public'),
|
|
34
|
+
preloadScript: path.join(__dirname, '../preload/index.js'),
|
|
35
|
+
iconPath: path.join(__dirname, '../../public/icons/icon.ico'),
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
// 窗口定义(必填)
|
|
39
|
+
windows: [
|
|
40
|
+
{
|
|
41
|
+
type: 'main',
|
|
42
|
+
options: {
|
|
43
|
+
width: 1200,
|
|
44
|
+
height: 800,
|
|
45
|
+
frame: false,
|
|
46
|
+
webPreferences: { webviewTag: true },
|
|
47
|
+
},
|
|
48
|
+
onCreate(win) {
|
|
49
|
+
// 加载页面 —— 适合用 Vite dev server 或本地文件
|
|
50
|
+
if (process.env.VITE_DEV_SERVER_URL) {
|
|
51
|
+
win.loadURL(process.env.VITE_DEV_SERVER_URL)
|
|
52
|
+
} else {
|
|
53
|
+
win.loadFile(path.join(__dirname, '../../dist/index.html'))
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
|
|
59
|
+
mainWindowType: 'main',
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## 配置结构
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
interface ElectronCliConfig {
|
|
69
|
+
paths: ElectronCliPaths // 必填 - 路径配置
|
|
70
|
+
windows: WindowDefinition[] // 必填 - 窗口定义
|
|
71
|
+
mainWindowType: string // 必填 - 主窗口标识
|
|
72
|
+
|
|
73
|
+
isDev?: boolean // 开发模式
|
|
74
|
+
defaultWindowOptions?: BrowserWindowConstructorOptions // 默认窗口选项
|
|
75
|
+
|
|
76
|
+
lifecycle?: LifecycleHooks // 生命周期钩子
|
|
77
|
+
ipc?: IpcOptions // IPC 配置
|
|
78
|
+
session?: SessionConfig // Session 拦截
|
|
79
|
+
tray?: TrayConfig // 系统托盘
|
|
80
|
+
instance?: InstanceConfig // 单实例锁
|
|
81
|
+
updater?: UpdaterConfig // 自动更新
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## 核心功能
|
|
88
|
+
|
|
89
|
+
### 1. 路径配置 `paths`
|
|
90
|
+
|
|
91
|
+
解耦构建工具(Vite/Webpack)的路径依赖,所有模块通过此配置获取路径。
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
paths: {
|
|
95
|
+
rendererDist: string // 渲染进程构建产物目录
|
|
96
|
+
mainDist: string // 主进程构建产物目录
|
|
97
|
+
publicDir: string // 静态资源目录
|
|
98
|
+
preloadScript: string // preload 脚本绝对路径
|
|
99
|
+
iconPath: string // 应用图标路径
|
|
100
|
+
userAgent?: string // 自定义 UA(未注入 node 时使用)
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 2. 窗口定义 `windows`
|
|
105
|
+
|
|
106
|
+
用声明式配置替代硬编码的 `WindowType` 枚举和 switch/case。
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
windows: [
|
|
110
|
+
{
|
|
111
|
+
type: 'main', // 窗口类型标识
|
|
112
|
+
options: { // BrowserWindow 构造选项
|
|
113
|
+
width: 1200,
|
|
114
|
+
height: 800,
|
|
115
|
+
frame: false,
|
|
116
|
+
webPreferences: {
|
|
117
|
+
preload: '...', // 可选,默认使用 paths.preloadScript
|
|
118
|
+
webviewTag: true,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
onCreate(win) {
|
|
122
|
+
// 窗口创建后立即执行 —— 加载 URL、设置位置等
|
|
123
|
+
win.loadURL('https://example.com')
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
]
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`WindowManager` 会自动:
|
|
130
|
+
- 合并 `defaultWindowOptions` + 各窗口的 `options`
|
|
131
|
+
- 设置菜单隐藏、`ready-to-show`、外部链接处理
|
|
132
|
+
- 转发 `fullscreen-change` / `maximize` / `minimize` 等状态到渲染进程
|
|
133
|
+
|
|
134
|
+
### 3. IPC 事件 `ipc`
|
|
135
|
+
|
|
136
|
+
内置 7 类 IPC 模块,同时支持三种方式自定义:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
ipc: {
|
|
140
|
+
// 方式一:结构化定义
|
|
141
|
+
extraHandlers: [
|
|
142
|
+
{ channel: 'dev-test', type: 'handle', handler: (e, ...args) => { /* ... */ } },
|
|
143
|
+
{ channel: 'log', type: 'on', handler: (e, msg) => console.log(msg) },
|
|
144
|
+
],
|
|
145
|
+
|
|
146
|
+
// 方式二:覆盖特定 channel
|
|
147
|
+
overrides: {
|
|
148
|
+
'window-control': { channel: 'window-control', type: 'handle', handler: myHandler },
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
// 方式三:完全自由的 ipcMain 配置
|
|
152
|
+
setup(ipcMain) {
|
|
153
|
+
ipcMain.handle('get-ipconfig', async () => await getLocalIP())
|
|
154
|
+
ipcMain.on('custom-event', (event, data) => { /* ... */ })
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
// 禁用不需要的内置模块
|
|
158
|
+
disabledBuiltins: ['downloader', 'notification'],
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**注册时序**:内置模块 → extraHandlers → overrides → `setup(ipcMain)`
|
|
163
|
+
|
|
164
|
+
`setup` 最后调用,可以覆盖任何之前注册的 handler。
|
|
165
|
+
|
|
166
|
+
#### 内置 IPC 模块
|
|
167
|
+
|
|
168
|
+
| 模块 | Channel | 功能 |
|
|
169
|
+
|------|---------|------|
|
|
170
|
+
| `window-control` | `window-control` | BrowserWindow 控制(最小化/最大化/全屏/置顶/属性查询) |
|
|
171
|
+
| `fs-api` | `fs-api` | fs 模块 IPC 桥接(readFile/writeFile/readFileAsBase64 等) |
|
|
172
|
+
| `dialog` | `open-file-dialog` `show-message-box` | 原生对话框 |
|
|
173
|
+
| `shell` | `open-file` `open-external` `show-file` | 文件/链接打开 |
|
|
174
|
+
| `downloader` | `download-file` `download-controler` `get-download-tasks` | 下载管理 |
|
|
175
|
+
| `notification` | `show-notification` | 原生通知 |
|
|
176
|
+
| `window-move` | `window-move` `window-move-start` `window-move-end` | 无边框窗口拖拽 |
|
|
177
|
+
|
|
178
|
+
**框架级 handler**(不可单独禁用):
|
|
179
|
+
|
|
180
|
+
| Channel | 功能 |
|
|
181
|
+
|---------|------|
|
|
182
|
+
| `get-preload` | 获取 preload 路径 |
|
|
183
|
+
| `get-renderer-dist` | 获取渲染进程 dist 路径 |
|
|
184
|
+
| `get-main-dist` | 获取主进程 dist 路径 |
|
|
185
|
+
| `open-win` | 打开新窗口 |
|
|
186
|
+
| `open-main-window` | 打开主窗口 |
|
|
187
|
+
| `open-window-page` | 打开子窗口/模态窗口 |
|
|
188
|
+
| `read-file` | 读取文件 |
|
|
189
|
+
| `set-process` | 设置窗口进度条 |
|
|
190
|
+
| `send-to-parent` | 向父窗口发送消息 |
|
|
191
|
+
| `send-to-child` | 向子窗口发送消息 |
|
|
192
|
+
| `set-ignore-mouse-events` | 设置鼠标穿透 |
|
|
193
|
+
| `window-manager-api` | 窗口管理器 API 调用 |
|
|
194
|
+
|
|
195
|
+
### 4. 生命周期钩子 `lifecycle`
|
|
196
|
+
|
|
197
|
+
在 app 启动的各个阶段注入自定义逻辑:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
lifecycle: {
|
|
201
|
+
async onReady() {
|
|
202
|
+
// app.whenReady() 内,窗口创建前
|
|
203
|
+
await initDatabase()
|
|
204
|
+
},
|
|
205
|
+
async afterReady() {
|
|
206
|
+
// app.whenReady() 内,窗口创建后
|
|
207
|
+
startBackgroundTasks()
|
|
208
|
+
},
|
|
209
|
+
onAllWindowsClosed() {
|
|
210
|
+
// 覆盖默认行为(非 macOS 默认 quit)
|
|
211
|
+
// 返回 void 表示使用自定义处理
|
|
212
|
+
},
|
|
213
|
+
onActivate() {
|
|
214
|
+
// macOS dock 点击,默认重新创建主窗口
|
|
215
|
+
},
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**内部启动流程**:
|
|
220
|
+
|
|
221
|
+
```
|
|
222
|
+
createElectronApp(config)
|
|
223
|
+
├─ GPU 配置(Win7 兼容)
|
|
224
|
+
├─ Windows app model ID
|
|
225
|
+
├─ 单实例锁
|
|
226
|
+
├─ IPC 事件注册(内置 + 自定义)
|
|
227
|
+
└─ useLifeCycle()
|
|
228
|
+
├─ protocol.registerSchemesAsPrivileged('local:')
|
|
229
|
+
├─ app.on('window-all-closed')
|
|
230
|
+
├─ app.on('activate')
|
|
231
|
+
└─ app.whenReady()
|
|
232
|
+
├─ lifecycle.onReady()
|
|
233
|
+
├─ protocol.registerFileProtocol('local:', ...)
|
|
234
|
+
├─ Session 配置
|
|
235
|
+
├─ 创建主窗口
|
|
236
|
+
├─ 系统托盘(可选)
|
|
237
|
+
├─ HTTP/WS 服务(可选)
|
|
238
|
+
├─ 自动更新(可选)
|
|
239
|
+
└─ lifecycle.afterReady()
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### 5. 系统托盘 `tray`
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
tray: {
|
|
246
|
+
enabled: true,
|
|
247
|
+
tooltip: 'My App',
|
|
248
|
+
menuItems: [
|
|
249
|
+
{ label: '显示主窗口', click: () => windowManager.openMainWindow() },
|
|
250
|
+
{ label: '退出', click: () => app.quit() },
|
|
251
|
+
],
|
|
252
|
+
onDoubleClick: () => { /* 自定义双击行为 */ },
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### 6. 单实例锁 `instance`
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
instance: {
|
|
260
|
+
enabled: true,
|
|
261
|
+
onSecondInstance() {
|
|
262
|
+
// 第二个实例启动时的行为(默认打开主窗口)
|
|
263
|
+
},
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### 7. 自动更新 `updater`
|
|
268
|
+
|
|
269
|
+
基于 `electron-updater`,使用 generic provider:
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
updater: {
|
|
273
|
+
enabled: true,
|
|
274
|
+
feedUrl: 'https://update.example.com/releases',
|
|
275
|
+
channel: 'latest',
|
|
276
|
+
onUpdateAvailable(info) {
|
|
277
|
+
mainWindow.webContents.send('update-available', info)
|
|
278
|
+
},
|
|
279
|
+
onUpdateError(error) {
|
|
280
|
+
console.error('Update error:', error)
|
|
281
|
+
},
|
|
282
|
+
onUpdateDownloaded() {
|
|
283
|
+
// 默认弹窗询问是否重启;返回 false 阻止默认行为
|
|
284
|
+
},
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## 高级用法
|
|
291
|
+
|
|
292
|
+
### 独立使用模块
|
|
293
|
+
|
|
294
|
+
除了 `createElectronApp` 一键启动,每个模块也可以独立导入使用:
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
import {
|
|
298
|
+
WindowManager, // 窗口管理器类
|
|
299
|
+
DownloadManager, // 下载管理器类
|
|
300
|
+
useIpcEvents, // IPC 事件注册
|
|
301
|
+
useLifeCycle, // 生命周期管理
|
|
302
|
+
useSession, // Session 配置
|
|
303
|
+
useSingleInstance, // 单实例锁
|
|
304
|
+
useTray, // 系统托盘
|
|
305
|
+
useUpdater, // 自动更新
|
|
306
|
+
useWinDrag, // 透明窗口拖拽
|
|
307
|
+
} from 'electron-multi-app-kit'
|
|
308
|
+
|
|
309
|
+
// 单独使用 WindowManager
|
|
310
|
+
const wm = new WindowManager({
|
|
311
|
+
windows: [...],
|
|
312
|
+
mainWindowType: 'main',
|
|
313
|
+
paths: { ... },
|
|
314
|
+
})
|
|
315
|
+
wm.openMainWindow()
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### 独立使用 IPC 内置模块
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
import {
|
|
322
|
+
useWindowMove,
|
|
323
|
+
useWindowControl,
|
|
324
|
+
useFsApi,
|
|
325
|
+
useDialogApi,
|
|
326
|
+
useShellApi,
|
|
327
|
+
useDownloaderApi,
|
|
328
|
+
useNotificationApi,
|
|
329
|
+
} from 'electron-multi-app-kit'
|
|
330
|
+
|
|
331
|
+
// 在自定义 setup 中按需注册
|
|
332
|
+
useWindowMove(windowManager)
|
|
333
|
+
useFsApi()
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### 工具函数
|
|
337
|
+
|
|
338
|
+
```ts
|
|
339
|
+
import { merge, debounce, cloneJSON } from 'electron-multi-app-kit'
|
|
340
|
+
|
|
341
|
+
// 深度合并对象
|
|
342
|
+
const opts = merge(defaults, userOptions, { deepMerge: true })
|
|
343
|
+
|
|
344
|
+
// 防抖
|
|
345
|
+
const save = debounce(() => persist(), 1000)
|
|
346
|
+
|
|
347
|
+
// JSON 深克隆
|
|
348
|
+
const copy = cloneJSON(original)
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
### WindowManager API
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
const wm = ctx.windowManager // 从 createElectronApp 返回值获取
|
|
355
|
+
|
|
356
|
+
wm.create('settings') // 创建窗口
|
|
357
|
+
wm.openWindow('settings') // 打开(自动 show/restore/maximize)
|
|
358
|
+
wm.openMainWindow() // 打开主窗口
|
|
359
|
+
wm.openWindowPage('/hash', { modal: true, parent: mainWin })
|
|
360
|
+
wm.openUrl('/some-hash')
|
|
361
|
+
|
|
362
|
+
wm.getMain() // 获取主窗口
|
|
363
|
+
wm.getWindow('settings') // 获取指定窗口
|
|
364
|
+
wm.getWindowByWebContents(wc) // 从 webContents 反查窗口
|
|
365
|
+
wm.getAllWindows() // 所有窗口
|
|
366
|
+
wm.getWindowConfig(win) // 窗口的合并配置
|
|
367
|
+
|
|
368
|
+
wm.closeWindow('settings') // 关闭窗口
|
|
369
|
+
wm.closeAllWindows() // 关闭所有
|
|
370
|
+
|
|
371
|
+
wm.confirmExit(win) // 弹出退出确认对话框
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
## Preload 桥接模块
|
|
377
|
+
|
|
378
|
+
`electron-multi-app-kit/preload` 提供一个开箱即用的 IPC 桥接层,通过 `contextBridge` 将 `ipcRenderer` 安全地暴露给渲染进程。
|
|
379
|
+
|
|
380
|
+
### 使用方法
|
|
381
|
+
|
|
382
|
+
在你的 preload 入口文件中导入(副作用导入即可):
|
|
383
|
+
|
|
384
|
+
```ts
|
|
385
|
+
// electron/preload/index.ts
|
|
386
|
+
import 'electron-multi-app-kit/preload' // 自动注册 window.ipcRenderer
|
|
387
|
+
|
|
388
|
+
// 基于 ipcBridge 构建业务 API
|
|
389
|
+
import { ipcBridge } from 'electron-multi-app-kit/preload'
|
|
390
|
+
import { contextBridge } from 'electron'
|
|
391
|
+
|
|
392
|
+
contextBridge.exposeInMainWorld('MyAPI', {
|
|
393
|
+
async getVersion() {
|
|
394
|
+
return ipcBridge.invoke<string>('get-app-version')
|
|
395
|
+
},
|
|
396
|
+
onMessage(callback: (data: any) => void) {
|
|
397
|
+
ipcBridge.on('main-message', (_event, data) => callback(data))
|
|
398
|
+
},
|
|
399
|
+
})
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### 渲染进程中使用
|
|
403
|
+
|
|
404
|
+
```ts
|
|
405
|
+
// 直接调用
|
|
406
|
+
const version = await window.ipcRenderer.invoke<string>('get-app-version')
|
|
407
|
+
|
|
408
|
+
// 监听事件
|
|
409
|
+
window.ipcRenderer.on('main-message', (_event, data) => {
|
|
410
|
+
console.log(data)
|
|
411
|
+
})
|
|
412
|
+
|
|
413
|
+
// 单向发送
|
|
414
|
+
window.ipcRenderer.send('log', 'hello from renderer')
|
|
415
|
+
|
|
416
|
+
// 向父窗口发送
|
|
417
|
+
window.ipcRenderer.sendToParent('child-ready')
|
|
418
|
+
|
|
419
|
+
// 向子窗口广播
|
|
420
|
+
window.ipcRenderer.sendToChild('parent-data', payload)
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### window.ipcRenderer API
|
|
424
|
+
|
|
425
|
+
| 方法 | 类型 | 说明 |
|
|
426
|
+
| ------ | ------ | ------ |
|
|
427
|
+
| `invoke<T>(channel, ...args)` | `Promise<T>` | 请求-响应 |
|
|
428
|
+
| `send(channel, ...args)` | `void` | 单向发送 |
|
|
429
|
+
| `on(channel, listener)` | `void` | 监听事件 |
|
|
430
|
+
| `once(channel, listener)` | `void` | 一次性监听 |
|
|
431
|
+
| `off(channel, listener)` | `void` | 移除监听 |
|
|
432
|
+
| `sendToParent(channel, ...args)` | `void` | 向父窗口发送 |
|
|
433
|
+
| `sendToChild(channel, ...args)` | `void` | 向子窗口广播 |
|
|
434
|
+
|
|
435
|
+
### 类型声明
|
|
436
|
+
|
|
437
|
+
如需在渲染进程中为 `window.ipcRenderer` 添加类型:
|
|
438
|
+
|
|
439
|
+
```ts
|
|
440
|
+
// src/vite-env.d.ts 或全局声明文件
|
|
441
|
+
import type { IpcBridge } from 'electron-multi-app-kit/preload'
|
|
442
|
+
|
|
443
|
+
declare global {
|
|
444
|
+
interface Window {
|
|
445
|
+
ipcRenderer: IpcBridge
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
---
|
|
451
|
+
|
|
452
|
+
## Renderer 模块
|
|
453
|
+
|
|
454
|
+
`electron-multi-app-kit/renderer` 提供 Vue 渲染进程可直接使用的组合式函数和组件。
|
|
455
|
+
|
|
456
|
+
### useIpcRenderer
|
|
457
|
+
|
|
458
|
+
内存安全的 IPC 封装,自动在组件卸载时清理监听器。
|
|
459
|
+
|
|
460
|
+
```vue
|
|
461
|
+
<script setup>
|
|
462
|
+
import { useIpcRenderer } from 'electron-multi-app-kit/renderer'
|
|
463
|
+
|
|
464
|
+
const ipc = useIpcRenderer()
|
|
465
|
+
|
|
466
|
+
// 监听主进程消息(自动清理)
|
|
467
|
+
ipc.on('main-message', (event, data) => {
|
|
468
|
+
console.log(data)
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
// 请求-响应
|
|
472
|
+
const version = await ipc.invoke<string>('get-app-version')
|
|
473
|
+
</script>
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### NavBar — 无边框窗口导航栏
|
|
477
|
+
|
|
478
|
+
开箱即用的自定义标题栏组件,专为无边框(`frame: false`)窗口设计。
|
|
479
|
+
|
|
480
|
+
```vue
|
|
481
|
+
<template>
|
|
482
|
+
<electron-nav-bar title="我的应用" icon="/icon.png">
|
|
483
|
+
<button @click="goBack">返回</button>
|
|
484
|
+
</electron-nav-bar>
|
|
485
|
+
</template>
|
|
486
|
+
|
|
487
|
+
<script setup>
|
|
488
|
+
import { NavBar } from 'electron-multi-app-kit/renderer'
|
|
489
|
+
</script>
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
**功能:**
|
|
493
|
+
|
|
494
|
+
- 自动检测是否为无边框窗口,标准窗口自动隐藏
|
|
495
|
+
- 窗口拖拽(`-webkit-app-region: drag`)
|
|
496
|
+
- 窗口控制按钮:置顶、最小化、最大化/还原、关闭
|
|
497
|
+
- 实时同步窗口状态(全屏、最大化、置顶等)
|
|
498
|
+
- 通过 CSS 变量自定义样式
|
|
499
|
+
|
|
500
|
+
**Props:**
|
|
501
|
+
|
|
502
|
+
| Prop | 类型 | 说明 |
|
|
503
|
+
| ------ | ------ | ------ |
|
|
504
|
+
| `title` | `string` | 自定义标题(不传则使用 BrowserWindow 的 title) |
|
|
505
|
+
| `icon` | `string` | 自定义图标 URL(不传则不显示图标) |
|
|
506
|
+
|
|
507
|
+
**Slot:** 默认插槽,内容显示在标题和窗口控制按钮之间。
|
|
508
|
+
|
|
509
|
+
**CSS 变量:**
|
|
510
|
+
|
|
511
|
+
| 变量 | 默认值 | 说明 |
|
|
512
|
+
| ------ | ------ | ------ |
|
|
513
|
+
| `--app-nav-bar-size` | `40px` | 导航栏高度 |
|
|
514
|
+
| `--app-color-f` | `#fff` | 背景色 |
|
|
515
|
+
| `--app-color-0` | `#333` | 文字颜色 |
|
|
516
|
+
| `--app-color-e` | `#e8e8e8` | 按钮 hover 背景 |
|
|
517
|
+
| `--app-color-c` | `#d0d0d0` | 按钮 active 背景 |
|
|
518
|
+
| `--app-color-red` | `#e81123` | 关闭按钮 hover 背景 |
|
|
519
|
+
| `--app-color-red-light` | `#f1707a` | 关闭按钮 active 背景 |
|
|
520
|
+
|
|
521
|
+
---
|
|
522
|
+
|
|
523
|
+
## 完整示例
|
|
524
|
+
|
|
525
|
+
以 Vite + Vue 项目为例,替换 `electron/main/index.ts`:
|
|
526
|
+
|
|
527
|
+
```ts
|
|
528
|
+
import { createElectronApp } from 'electron-multi-app-kit'
|
|
529
|
+
import path from 'node:path'
|
|
530
|
+
import { app, shell } from 'electron'
|
|
531
|
+
|
|
532
|
+
const __dirname = path.dirname(new URL(import.meta.url).pathname)
|
|
533
|
+
const isDev = process.env.NODE_ENV === 'development'
|
|
534
|
+
|
|
535
|
+
const appCtx = createElectronApp({
|
|
536
|
+
// ---- 路径 ----
|
|
537
|
+
paths: {
|
|
538
|
+
rendererDist: path.join(__dirname, '../../dist'),
|
|
539
|
+
mainDist: path.join(__dirname, '../../dist-electron'),
|
|
540
|
+
publicDir: isDev
|
|
541
|
+
? path.join(__dirname, '../../public')
|
|
542
|
+
: path.join(__dirname, '../../dist'),
|
|
543
|
+
preloadScript: path.join(__dirname, '../preload/index.js'),
|
|
544
|
+
iconPath: path.join(__dirname, '../../public/icons/icon.ico'),
|
|
545
|
+
userAgent: 'Mozilla/5.0 ...',
|
|
546
|
+
},
|
|
547
|
+
|
|
548
|
+
isDev,
|
|
549
|
+
|
|
550
|
+
// ---- 默认窗口选项 ----
|
|
551
|
+
defaultWindowOptions: {
|
|
552
|
+
icon: path.join(__dirname, '../../public/icons/icon.ico'),
|
|
553
|
+
webPreferences: {
|
|
554
|
+
nodeIntegration: false,
|
|
555
|
+
preload: path.join(__dirname, '../preload/index.js'),
|
|
556
|
+
},
|
|
557
|
+
},
|
|
558
|
+
|
|
559
|
+
// ---- 窗口定义 ----
|
|
560
|
+
windows: [
|
|
561
|
+
{
|
|
562
|
+
type: 'main',
|
|
563
|
+
options: {
|
|
564
|
+
width: 1200,
|
|
565
|
+
height: 800,
|
|
566
|
+
minWidth: 1200,
|
|
567
|
+
minHeight: 800,
|
|
568
|
+
frame: false,
|
|
569
|
+
webPreferences: { webviewTag: true, contextIsolation: true },
|
|
570
|
+
},
|
|
571
|
+
onCreate(win) {
|
|
572
|
+
if (process.env.VITE_DEV_SERVER_URL) {
|
|
573
|
+
win.loadURL(process.env.VITE_DEV_SERVER_URL)
|
|
574
|
+
} else {
|
|
575
|
+
win.loadFile(path.join(__dirname, '../../dist/index.html'))
|
|
576
|
+
}
|
|
577
|
+
},
|
|
578
|
+
},
|
|
579
|
+
],
|
|
580
|
+
|
|
581
|
+
mainWindowType: 'main',
|
|
582
|
+
|
|
583
|
+
// ---- IPC 自定义 ----
|
|
584
|
+
ipc: {
|
|
585
|
+
setup(ipcMain) {
|
|
586
|
+
ipcMain.handle('dev-test', () => shell.beep())
|
|
587
|
+
ipcMain.handle('get-app-version', () => app.getVersion())
|
|
588
|
+
},
|
|
589
|
+
},
|
|
590
|
+
|
|
591
|
+
// ---- 启用模块 ----
|
|
592
|
+
tray: { enabled: true },
|
|
593
|
+
instance: { enabled: true },
|
|
594
|
+
|
|
595
|
+
// ---- 生命周期 ----
|
|
596
|
+
lifecycle: {
|
|
597
|
+
async onReady() {
|
|
598
|
+
console.log('App is ready, creating window...')
|
|
599
|
+
},
|
|
600
|
+
},
|
|
601
|
+
})
|
|
602
|
+
|
|
603
|
+
// 返回的 context 可后续使用
|
|
604
|
+
export const { windowManager } = appCtx
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
---
|
|
608
|
+
|
|
609
|
+
## License
|
|
610
|
+
|
|
611
|
+
MIT
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ElectronCliConfig } from '../types';
|
|
2
|
+
import { WindowBaseType, WindowManager } from '../modules/window/manager';
|
|
3
|
+
export interface ElectronCliContext<W extends WindowBaseType = WindowBaseType> {
|
|
4
|
+
/** 用户原始配置 */
|
|
5
|
+
config: ElectronCliConfig<W>;
|
|
6
|
+
/** 窗口管理器实例 */
|
|
7
|
+
windowManager: WindowManager<W>;
|
|
8
|
+
/** 托盘是否已创建 */
|
|
9
|
+
trayCreated: boolean;
|
|
10
|
+
isDev: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 创建上下文实例
|
|
14
|
+
*/
|
|
15
|
+
export declare function createContext<W extends WindowBaseType>(config: ElectronCliConfig<W>): ElectronCliContext<W>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ElectronCliContext } from './context';
|
|
2
|
+
import { WindowBaseType } from '../modules/window/manager';
|
|
3
|
+
/**
|
|
4
|
+
* 应用生命周期管理 — 从 electron/main/events/lifeCycle.ts 移植
|
|
5
|
+
*
|
|
6
|
+
* 负责 protocol 注册、app 事件监听、app.whenReady() 内部编排。
|
|
7
|
+
* 用户可通过 config.lifecycle 钩子注入自定义逻辑。
|
|
8
|
+
*/
|
|
9
|
+
export declare function useLifeCycle<W extends WindowBaseType = WindowBaseType>(ctx: ElectronCliContext<W>): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ElectronCliConfig } from './types';
|
|
2
|
+
import { ElectronCliContext } from './core/context';
|
|
3
|
+
import { WindowBaseType } from './modules/window/manager';
|
|
4
|
+
/**
|
|
5
|
+
* createElectronApp — electron-multi-app-kit 的主入口函数
|
|
6
|
+
*
|
|
7
|
+
* 对应 electron/main/app/index.ts 的 appSetup() 函数,
|
|
8
|
+
* 将所有硬编码逻辑改为通过 ElectronCliConfig 配置驱动。
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { createElectronApp } from 'electron-multi-app-kit'
|
|
13
|
+
*
|
|
14
|
+
* createElectronApp({
|
|
15
|
+
* paths: { ... },
|
|
16
|
+
* windows: [ ... ],
|
|
17
|
+
* mainWindowType: 'main',
|
|
18
|
+
* isDev: process.env.NODE_ENV === 'development',
|
|
19
|
+
* ipc: {
|
|
20
|
+
* extraHandlers: [
|
|
21
|
+
* { channel: 'dev-test', type: 'handle', handler: (e, ...args) => { ... } }
|
|
22
|
+
* ]
|
|
23
|
+
* },
|
|
24
|
+
* tray: { enabled: true },
|
|
25
|
+
* instance: { enabled: true },
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function createElectronApp<W extends WindowBaseType = WindowBaseType>(config: ElectronCliConfig<W>): ElectronCliContext<W>;
|
|
30
|
+
export type { ElectronCliConfig } from './types';
|