@easy-electron/core 1.0.1-alpha.1

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.
@@ -0,0 +1,528 @@
1
+ import { BrowserWindowConstructorOptions, BrowserWindow, IpcMainInvokeEvent, App } from 'electron';
2
+ import { ILogger } from '@easy-electron/logger';
3
+
4
+ /**
5
+ * 窗口事件类型数组
6
+ */
7
+ declare const WindowEventTypes: readonly ["created", "closed", "focus", "blur", "ready"];
8
+
9
+ /**
10
+ * 窗口事件类型
11
+ */
12
+ type WindowEventType = typeof WindowEventTypes[number];
13
+ /**
14
+ * 窗口事件监听器
15
+ */
16
+ type WindowEventListener = (instance: WindowInstance) => void;
17
+ /**
18
+ * 窗口配置接口
19
+ */
20
+ interface EasyWindowConfig {
21
+ id?: string;
22
+ /**
23
+ * 窗口宽度,默认 800
24
+ */
25
+ width?: number;
26
+ /**
27
+ * 窗口高度,默认 600
28
+ */
29
+ height?: number;
30
+ /**
31
+ * 窗口最小宽度,默认 0
32
+ */
33
+ minWidth?: number;
34
+ /**
35
+ * 窗口最小高度,默认 0
36
+ */
37
+ minHeight?: number;
38
+ /**
39
+ * 窗口相对于屏幕左侧的偏移量,默认值为将窗口居中
40
+ */
41
+ x?: number;
42
+ /**
43
+ * 窗口相对于屏幕顶端的偏移量,默认值为将窗口居中
44
+ */
45
+ y?: number;
46
+ /**
47
+ * 窗口是否可被调整大小,默认 true
48
+ */
49
+ resizable?: boolean;
50
+ /**
51
+ * 窗口是否可被移动,默认 true
52
+ */
53
+ movable?: boolean;
54
+ /**
55
+ * 窗口是否可被最小化,默认 true
56
+ */
57
+ minimizable?: boolean;
58
+ /**
59
+ * 窗口是否可被最大化,默认 true
60
+ */
61
+ maximizable?: boolean;
62
+ /**
63
+ * 窗口是否可被关闭,默认 true
64
+ */
65
+ closable?: boolean;
66
+ /**
67
+ * 窗口是否始终显示在其他窗口之上,默认 false
68
+ */
69
+ alwaysOnTop?: boolean;
70
+ /**
71
+ * 窗口标题
72
+ */
73
+ title?: string;
74
+ /**
75
+ * 窗口图标
76
+ */
77
+ icon?: string;
78
+ /**
79
+ * 是否有边框,默认 true
80
+ */
81
+ frame?: boolean;
82
+ /**
83
+ * 使得窗口透明,默认 false
84
+ */
85
+ transparent?: boolean;
86
+ /**
87
+ * 在线地址
88
+ */
89
+ url?: string;
90
+ /**
91
+ * html 路径
92
+ */
93
+ htmlPath?: string;
94
+ webPreferences?: {
95
+ /**
96
+ * 预加载脚本
97
+ */
98
+ preload?: string;
99
+ /**
100
+ * 是否启用 Node integration(渲染进程是否能够直接访问 Node.js API),风险项!建议不要开启,默认 false
101
+ */
102
+ nodeIntegration?: boolean;
103
+ /**
104
+ * 是否启用上下文隔离(预加载脚本和 Electron 的内部逻辑是否在独立的上下文中运行),风险项!建议开启,默认 true
105
+ */
106
+ contextIsolation?: boolean;
107
+ };
108
+ raw?: BrowserWindowConstructorOptions;
109
+ }
110
+ /**
111
+ * 窗口实例接口
112
+ */
113
+ interface WindowInstance {
114
+ /**
115
+ * 窗口ID
116
+ */
117
+ id: string;
118
+ /**
119
+ * 窗口
120
+ */
121
+ window: BrowserWindow;
122
+ /**
123
+ * 窗口配置
124
+ */
125
+ config: EasyWindowConfig;
126
+ }
127
+
128
+ /**
129
+ * 开发者工具模式
130
+ * - left 左侧
131
+ * - right 右侧
132
+ * - bottom 下方
133
+ * - undocked 拆离,单独一个窗口,但可以恢复为停靠状态
134
+ * - detach 拆离,单独一个窗口
135
+ */
136
+ type DevToolsMode = 'left' | 'right' | 'bottom' | 'undocked' | 'detach';
137
+ /**
138
+ * EasyElectron 配置接口
139
+ */
140
+ interface EasyElectronConfig {
141
+ app?: {
142
+ name?: string;
143
+ /**
144
+ * 是否使用单例模式(只会有一个程序启动)
145
+ */
146
+ singleInstance?: boolean;
147
+ };
148
+ window?: EasyWindowConfig;
149
+ dev?: {
150
+ serverUrl?: string;
151
+ /**
152
+ * 打开开发者工具,默认 right,设置为 null 或 undefined 则不打开
153
+ */
154
+ openDevTools?: DevToolsMode;
155
+ };
156
+ prod?: {
157
+ /**
158
+ * html 页面路径
159
+ */
160
+ htmlPath?: string;
161
+ };
162
+ plugins?: EasyElectronPlugin[];
163
+ }
164
+
165
+ /**
166
+ * 窗口管理器类
167
+ * 负责创建、管理和销毁应用程序窗口
168
+ */
169
+ declare class WindowManager {
170
+ private readonly windows;
171
+ private readonly defaultConfig;
172
+ private eventListeners;
173
+ private readonly logger;
174
+ /** 开发模式下的页面 URL(可选,替代默认的 VITE_DEV_SERVER_URL 检测) */
175
+ private readonly devUrl?;
176
+ /** 生产模式下的 HTML 路径(可选) */
177
+ private readonly prodHtmlPath?;
178
+ constructor(defaultConfig?: EasyWindowConfig, devUrl?: string, prodHtmlPath?: string);
179
+ /**
180
+ * 创建窗口
181
+ * @param config 窗口配置
182
+ * @returns 窗口实例
183
+ */
184
+ create(config?: EasyWindowConfig): Promise<WindowInstance>;
185
+ /**
186
+ * 加载窗口内容
187
+ */
188
+ private loadContent;
189
+ /**
190
+ * 设置窗口事件监听器
191
+ */
192
+ private setupWindowListeners;
193
+ /**
194
+ * 获取窗口实例
195
+ * @param id 窗口 ID
196
+ * @returns 窗口实例或 undefined
197
+ */
198
+ get(id: string): WindowInstance | undefined;
199
+ /**
200
+ * 获取所有窗口实例
201
+ * @returns 窗口实例数组
202
+ */
203
+ getAll(): WindowInstance[];
204
+ /**
205
+ * 检查窗口是否存在
206
+ * @param id 窗口 ID
207
+ * @returns 是否存在
208
+ */
209
+ has(id: string): boolean;
210
+ /**
211
+ * 获取窗口数量
212
+ * @returns 窗口数量
213
+ */
214
+ count(): number;
215
+ /**
216
+ * 关闭窗口
217
+ * @param id 窗口 ID
218
+ */
219
+ close(id: string): void;
220
+ /**
221
+ * 关闭所有窗口
222
+ */
223
+ closeAll(): void;
224
+ /**
225
+ * 注册窗口事件监听器
226
+ * @param event 事件类型
227
+ * @param listener 监听器函数
228
+ */
229
+ on(event: WindowEventType, listener: WindowEventListener): void;
230
+ /**
231
+ * 移除窗口事件监听器
232
+ * @param event 事件类型
233
+ * @param listener 监听器函数
234
+ */
235
+ off(event: WindowEventType, listener: WindowEventListener): void;
236
+ /**
237
+ * 触发窗口事件
238
+ * @param event 事件类型
239
+ * @param instance 窗口实例
240
+ */
241
+ protected emit(event: WindowEventType, instance: WindowInstance): void;
242
+ /**
243
+ * 获取默认配置
244
+ */
245
+ getDefaultConfig(): EasyWindowConfig;
246
+ /**
247
+ * 构建降级错误页面(当窗口内容加载失败时显示)
248
+ * @param windowId 窗口 ID
249
+ * @param errorMessage 错误信息
250
+ * @returns HTML 字符串
251
+ */
252
+ private buildErrorPage;
253
+ clear(): void;
254
+ }
255
+
256
+ /**
257
+ * IPC 处理器类型
258
+ */
259
+ type IPCHandler<T = any, R = any> = (event: IpcMainInvokeEvent, ...args: T[]) => R | Promise<R>;
260
+ /**
261
+ * IPC 处理器选项
262
+ */
263
+ interface IPCHandlerOptions {
264
+ sync?: boolean;
265
+ }
266
+
267
+ /**
268
+ * IPC 管理器类
269
+ * 简化主进程和渲染进程之间的 IPC 通信
270
+ */
271
+ declare class IpcManager {
272
+ private readonly handlers;
273
+ private readonly windowManager;
274
+ private readonly logger;
275
+ constructor(windowManager: WindowManager);
276
+ /**
277
+ * 注册 IPC 处理器
278
+ * @param channel IPC 通道名称
279
+ * @param handler 处理器函数
280
+ * @param options 处理器选项
281
+ */
282
+ handle<T = any, R = any>(channel: string, handler: IPCHandler<T, R>, options?: IPCHandlerOptions): void;
283
+ /**
284
+ * 批量注册 defineIpc 返回的处理器映射
285
+ *
286
+ * 自动识别函数形式(handle)和对象形式({type, handler}),
287
+ * 根据 moduleName 前缀拼接 IPC 通道名。
288
+ *
289
+ * @param handlers defineIpc 返回的处理器映射对象
290
+ * @param moduleName 可选的模块名称(用于拼接通道前缀,如 `module:method`)
291
+ *
292
+ * @example
293
+ * ```ts
294
+ * import ipcHandlers from './electron/ipc/handlers'
295
+ * app.ipc.registerHandlers(ipcHandlers)
296
+ * ```
297
+ */
298
+ registerHandlers(handlers: Record<string, unknown>, moduleName?: string): void;
299
+ /**
300
+ * 移除 IPC 处理器
301
+ * @param channel IPC 通道名称
302
+ */
303
+ removeHandler(channel: string): void;
304
+ /**
305
+ * 移除所有 IPC 处理器
306
+ */
307
+ removeAllHandlers(): void;
308
+ /**
309
+ * 获取已注册的处理器数量
310
+ */
311
+ count(): number;
312
+ /**
313
+ * 检查处理器是否已注册
314
+ */
315
+ has(channel: string): boolean;
316
+ /**
317
+ * 向主窗口发送消息
318
+ * @param channel IPC 通道名称
319
+ * @param args 消息参数
320
+ */
321
+ send(channel: string, ...args: any[]): void;
322
+ /**
323
+ * 向指定窗口发送消息
324
+ * @param windowId 窗口 ID
325
+ * @param channel IPC 通道名称
326
+ * @param args 消息参数
327
+ */
328
+ sendTo(windowId: string, channel: string, ...args: any[]): void;
329
+ /**
330
+ * 向所有窗口广播消息
331
+ * @param channel IPC 通道名称
332
+ * @param args 消息参数
333
+ */
334
+ broadcast(channel: string, ...args: any[]): void;
335
+ }
336
+
337
+ /**
338
+ * 插件上下文接口
339
+ */
340
+ interface PluginContext {
341
+ /** 窗口管理器 */
342
+ windows: WindowManager;
343
+ /** IPC 管理器 */
344
+ ipc: IpcManager;
345
+ /** Electron app 实例 */
346
+ app: App;
347
+ /** 应用配置 */
348
+ config: EasyElectronConfig;
349
+ }
350
+ /**
351
+ * 插件接口
352
+ */
353
+ interface EasyElectronPlugin {
354
+ name: string;
355
+ version?: string;
356
+ install?(context: PluginContext): void | Promise<void>;
357
+ onReady?(context: PluginContext): void | Promise<void>;
358
+ onBeforeQuit?(context: PluginContext): void | Promise<void>;
359
+ }
360
+
361
+ /**
362
+ * EasyElectron 核心类
363
+ * 应用程序的主入口点
364
+ */
365
+ declare class EasyElectron {
366
+ private readonly windowManager;
367
+ private readonly ipcManager;
368
+ private lifecycleManager;
369
+ private readonly pluginManager;
370
+ private readonly config;
371
+ constructor(config?: EasyElectronConfig);
372
+ /**
373
+ * 获取窗口管理器
374
+ */
375
+ get windows(): WindowManager;
376
+ /**
377
+ * 获取 IPC 管理器
378
+ */
379
+ get ipc(): IpcManager;
380
+ /**
381
+ * 初始化应用程序
382
+ */
383
+ init(): Promise<void>;
384
+ /**
385
+ * 注册插件
386
+ */
387
+ use(plugin: EasyElectronPlugin): this;
388
+ /**
389
+ * 注册 ready 钩子
390
+ */
391
+ onReady(callback: () => void | Promise<void>): this;
392
+ /**
393
+ * 注册 activate 钩子
394
+ */
395
+ onActivate(callback: () => void | Promise<void>): this;
396
+ /**
397
+ * 注册 beforeQuit 钩子
398
+ */
399
+ onBeforeQuit(callback: () => void | Promise<void>): this;
400
+ /**
401
+ * 设置 IPC 处理器
402
+ * @param setup IPC 处理器设置函数
403
+ */
404
+ setupIpc(setup: (ipc: IpcManager) => void): this;
405
+ /**
406
+ * 批量注册 IPC 处理器
407
+ *
408
+ * 接收 defineIpc 返回的定义对象,自动识别模块名并注册所有处理器。
409
+ *
410
+ * @param handlers defineIpc 返回的处理器映射
411
+ *
412
+ * @example
413
+ * ```ts
414
+ * import ipcHandlers from './electron/ipc/handlers'
415
+ * app.registerIpc(ipcHandlers)
416
+ * ```
417
+ */
418
+ registerIpc(handlers: Record<string, unknown>): this;
419
+ /**
420
+ * 修改日志实现
421
+ * @param logger 日志实现
422
+ */
423
+ setLogger(logger: ILogger): this;
424
+ }
425
+
426
+ /**
427
+ * 生命周期钩子类型
428
+ */
429
+ type LifecycleHook = () => void | Promise<void>;
430
+
431
+ /**
432
+ * 生命周期管理器
433
+ *
434
+ * 管理 Electron 应用的生命周期事件,提供钩子注册机制。
435
+ *
436
+ * 事件处理策略:
437
+ * - ready / activate → 直接执行异步钩子
438
+ * - will-quit → 阻止退出 → 执行清理钩子 → 强制退出(避免 event.preventDefault + app.quit 循环)
439
+ */
440
+ declare class LifecycleManager {
441
+ private app;
442
+ private readonly windowManager;
443
+ private readonly readyHooks;
444
+ private readonly activateHooks;
445
+ private readonly beforeQuitHooks;
446
+ constructor(app: App, windowManager: WindowManager);
447
+ /**
448
+ * 注册 ready 钩子
449
+ * @param hook 钩子函数
450
+ */
451
+ onReady(hook: LifecycleHook): void;
452
+ /**
453
+ * 注册 activate 钩子(macOS)
454
+ * @param hook 钩子函数
455
+ */
456
+ onActivate(hook: LifecycleHook): void;
457
+ /**
458
+ * 注册退出前的清理钩子
459
+ * @param hook 钩子函数
460
+ */
461
+ onBeforeQuit(hook: LifecycleHook): void;
462
+ /**
463
+ * 初始化生命周期监听
464
+ */
465
+ init(): void;
466
+ /**
467
+ * 执行钩子集合
468
+ * @param hooks 钩子函数集合
469
+ */
470
+ private executeHooks;
471
+ }
472
+
473
+ /**
474
+ * 插件管理器类
475
+ * 管理插件的注册和生命周期
476
+ */
477
+ declare class PluginManager {
478
+ private plugins;
479
+ private readonly context;
480
+ constructor(context: PluginContext);
481
+ /**
482
+ * 注册插件
483
+ * @param plugin 插件对象
484
+ */
485
+ register(plugin: EasyElectronPlugin): void;
486
+ /**
487
+ * 获取插件
488
+ * @param name 插件名称
489
+ * @returns 插件对象或 undefined
490
+ */
491
+ get(name: string): EasyElectronPlugin | undefined;
492
+ /**
493
+ * 检查插件是否已注册
494
+ * @param name 插件名称
495
+ * @returns 是否已注册
496
+ */
497
+ has(name: string): boolean;
498
+ /**
499
+ * 调用插件钩子
500
+ * @param hookName 钩子名称
501
+ */
502
+ callHook(hookName: keyof EasyElectronPlugin): Promise<void>;
503
+ /**
504
+ * 获取已注册的插件数量
505
+ */
506
+ count(): number;
507
+ /**
508
+ * 获取所有插件
509
+ */
510
+ getAll(): EasyElectronPlugin[];
511
+ }
512
+
513
+ /**
514
+ * EasyElectron 自定义错误类
515
+ */
516
+ declare class EasyElectronError extends Error {
517
+ constructor(message: string);
518
+ }
519
+
520
+ /**
521
+ * 创建 EasyElectron 应用实例
522
+ * @param config 应用配置
523
+ * @returns EasyElectron 实例
524
+ */
525
+ declare function createElectronApp(config?: EasyElectronConfig): EasyElectron;
526
+
527
+ export { EasyElectron, EasyElectronError, IpcManager, LifecycleManager, PluginManager, WindowManager, createElectronApp };
528
+ export type { DevToolsMode, EasyElectronConfig, EasyElectronPlugin, EasyWindowConfig, IPCHandler, IPCHandlerOptions, LifecycleHook, PluginContext, WindowEventListener, WindowEventType, WindowInstance };