@thirty-coder/electron-base-kit 0.3.0 → 0.4.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.md +26 -1
- package/dist/index.d.ts +19 -3
- package/dist/index.js +53 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ import { join } from 'node:path'
|
|
|
37
37
|
import config from '../app.config.json'
|
|
38
38
|
|
|
39
39
|
app.whenReady().then(() => {
|
|
40
|
-
bootstrap(config)
|
|
40
|
+
if (!bootstrap(config)) return
|
|
41
41
|
|
|
42
42
|
createWindow({
|
|
43
43
|
preload: join(__dirname, '../preload/preload.js'),
|
|
@@ -48,6 +48,8 @@ app.whenReady().then(() => {
|
|
|
48
48
|
|
|
49
49
|
框架不读配置文件,config 由项目传入。`preload`、`renderer` 必须是业务传的绝对路径,因为本包装在 `node_modules` 里拿不到业务的目录结构。
|
|
50
50
|
|
|
51
|
+
`bootstrap` 返回 `false` 表示已有实例在跑,本进程正在退出,直接 `return` 别建窗口。
|
|
52
|
+
|
|
51
53
|
## 窗口
|
|
52
54
|
|
|
53
55
|
`createWindow` 接管了这些事:
|
|
@@ -65,6 +67,29 @@ createWindow({ id: 'settings', preload, renderer, width: 640, height: 480, remem
|
|
|
65
67
|
|
|
66
68
|
不传 `id` 就是 `main`。窗口尺寸和开关默认读 `app.config.json` 的 `window`,只有需要和主窗口不同的窗口才在参数里覆盖。
|
|
67
69
|
|
|
70
|
+
## 单实例
|
|
71
|
+
|
|
72
|
+
`bootstrap` 内部拿锁。二次启动时新进程直接退出,主进程收到 `second-instance` 后唤起 `main` 窗口:最小化就还原、隐藏就显示、然后聚焦。
|
|
73
|
+
|
|
74
|
+
窗口引用和唤起:
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { getWindow, focusWindow } from '@thirty-coder/electron-base-kit'
|
|
78
|
+
|
|
79
|
+
getWindow('main') // BrowserWindow | undefined
|
|
80
|
+
focusWindow('main') // 还原 + 显示 + 聚焦,返回是否找到
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
托盘、自动更新这些能力用 `getWindow` 拿主窗口。窗口已经关掉时返回 `undefined`,要重建就自己监听:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
app.on('second-instance', () => {
|
|
87
|
+
if (!getWindow('main')) createWindow({ preload, renderer })
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
锁以 `userData` 为界,改 `userData` 就能跑多个独立实例。macOS 上双击图标是系统保证单实例,验证本能力要用 `open -n`。
|
|
92
|
+
|
|
68
93
|
## 配置
|
|
69
94
|
|
|
70
95
|
`app.config.json` 由业务项目维护,结构对应 `AppConfig` 类型:
|
package/dist/index.d.ts
CHANGED
|
@@ -86,6 +86,21 @@ interface SettingsApi {
|
|
|
86
86
|
}
|
|
87
87
|
declare const settings: SettingsApi;
|
|
88
88
|
|
|
89
|
+
interface SingleInstanceOptions {
|
|
90
|
+
/** 二次启动时唤起的窗口 id,默认 main。传 false 不自动唤起 */
|
|
91
|
+
windowId?: string | false;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 拿单实例锁。拿不到时本进程会被 quit,返回 false。
|
|
95
|
+
*
|
|
96
|
+
* 重复调用返回第一次的结果。必须在创建任何窗口之前调用。
|
|
97
|
+
*/
|
|
98
|
+
declare function acquireSingleInstance(options?: SingleInstanceOptions): boolean;
|
|
99
|
+
|
|
100
|
+
/** 取窗口引用。已销毁的当作不存在。 */
|
|
101
|
+
declare function getWindow(id?: string): BrowserWindow | undefined;
|
|
102
|
+
/** 还原、显示、聚焦。窗口不存在时返回 false,不抛错。 */
|
|
103
|
+
declare function focusWindow(id?: string): boolean;
|
|
89
104
|
interface CreateWindowOptions {
|
|
90
105
|
/** preload 脚本绝对路径 */
|
|
91
106
|
preload: string;
|
|
@@ -110,9 +125,10 @@ declare function createWindow(options: CreateWindowOptions): BrowserWindow;
|
|
|
110
125
|
/**
|
|
111
126
|
* 初始化框架。
|
|
112
127
|
*
|
|
128
|
+
* 返回 false 表示已有实例持锁,本进程正在退出,业务不要再建窗口。
|
|
113
129
|
* 常开能力初始化失败直接抛;可选能力失败只记错误,继续启动。
|
|
114
130
|
*/
|
|
115
|
-
declare function bootstrap(config: AppConfig):
|
|
131
|
+
declare function bootstrap(config: AppConfig): boolean;
|
|
116
132
|
|
|
117
|
-
export { bootstrap, createWindow, logger, settings };
|
|
118
|
-
export type { AppConfig, CreateWindowOptions, FeatureFlags, LicenseConfig, LogConfig, LogLevel, LoggerApi, SettingsApi, UpdateConfig, UpdateProvider, WindowConfig };
|
|
133
|
+
export { acquireSingleInstance, bootstrap, createWindow, focusWindow, getWindow, logger, settings };
|
|
134
|
+
export type { AppConfig, CreateWindowOptions, FeatureFlags, LicenseConfig, LogConfig, LogLevel, LoggerApi, SettingsApi, SingleInstanceOptions, UpdateConfig, UpdateProvider, WindowConfig };
|
package/dist/index.js
CHANGED
|
@@ -20,8 +20,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
acquireSingleInstance: () => acquireSingleInstance,
|
|
23
24
|
bootstrap: () => bootstrap,
|
|
24
25
|
createWindow: () => createWindow,
|
|
26
|
+
focusWindow: () => focusWindow,
|
|
27
|
+
getWindow: () => getWindow,
|
|
25
28
|
logger: () => logger,
|
|
26
29
|
settings: () => settings
|
|
27
30
|
});
|
|
@@ -317,15 +320,36 @@ var settings = {
|
|
|
317
320
|
}
|
|
318
321
|
};
|
|
319
322
|
|
|
323
|
+
// src/single-instance.ts
|
|
324
|
+
var import_electron4 = require("electron");
|
|
325
|
+
|
|
320
326
|
// src/window.ts
|
|
321
327
|
var import_electron3 = require("electron");
|
|
322
328
|
var FALLBACK = { width: 1200, height: 800, minWidth: 900, minHeight: 600 };
|
|
323
329
|
var VISIBLE_RATIO = 0.3;
|
|
324
330
|
var SAVE_DELAY = 300;
|
|
325
331
|
var defaults = {};
|
|
332
|
+
var registry = /* @__PURE__ */ new Map();
|
|
326
333
|
function setWindowDefaults(config) {
|
|
327
334
|
defaults = config ?? {};
|
|
328
335
|
}
|
|
336
|
+
function getWindow(id = "main") {
|
|
337
|
+
const win = registry.get(id);
|
|
338
|
+
if (!win) return void 0;
|
|
339
|
+
if (win.isDestroyed()) {
|
|
340
|
+
registry.delete(id);
|
|
341
|
+
return void 0;
|
|
342
|
+
}
|
|
343
|
+
return win;
|
|
344
|
+
}
|
|
345
|
+
function focusWindow(id = "main") {
|
|
346
|
+
const win = getWindow(id);
|
|
347
|
+
if (!win) return false;
|
|
348
|
+
if (win.isMinimized()) win.restore();
|
|
349
|
+
win.show();
|
|
350
|
+
win.focus();
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
329
353
|
function clamp(value, min, max) {
|
|
330
354
|
return Math.min(Math.max(value, min), max);
|
|
331
355
|
}
|
|
@@ -489,9 +513,33 @@ function createWindow(options) {
|
|
|
489
513
|
}
|
|
490
514
|
if (remember) attachPersistence(win, key);
|
|
491
515
|
if (ensureVisible) attachDisplayGuard(win);
|
|
516
|
+
registry.set(id, win);
|
|
517
|
+
win.once("closed", () => {
|
|
518
|
+
if (registry.get(id) === win) registry.delete(id);
|
|
519
|
+
});
|
|
492
520
|
return win;
|
|
493
521
|
}
|
|
494
522
|
|
|
523
|
+
// src/single-instance.ts
|
|
524
|
+
var log2 = logger.child("single-instance");
|
|
525
|
+
var locked = null;
|
|
526
|
+
function acquireSingleInstance(options = {}) {
|
|
527
|
+
if (locked !== null) return locked;
|
|
528
|
+
const windowId = options.windowId ?? "main";
|
|
529
|
+
locked = import_electron4.app.requestSingleInstanceLock();
|
|
530
|
+
if (!locked) {
|
|
531
|
+
log2.info("\u5DF2\u6709\u5B9E\u4F8B\u5728\u8FD0\u884C\uFF0C\u672C\u8FDB\u7A0B\u9000\u51FA");
|
|
532
|
+
import_electron4.app.quit();
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
import_electron4.app.on("second-instance", (_event, argv, workingDirectory) => {
|
|
536
|
+
log2.info("\u68C0\u6D4B\u5230\u4E8C\u6B21\u542F\u52A8", { cwd: workingDirectory });
|
|
537
|
+
log2.debug("\u4E8C\u6B21\u542F\u52A8\u53C2\u6570", { argv: argv.slice(1) });
|
|
538
|
+
if (windowId) focusWindow(windowId);
|
|
539
|
+
});
|
|
540
|
+
return true;
|
|
541
|
+
}
|
|
542
|
+
|
|
495
543
|
// src/bootstrap.ts
|
|
496
544
|
function assertConfig(config) {
|
|
497
545
|
if (!config || typeof config !== "object") {
|
|
@@ -507,6 +555,7 @@ function assertConfig(config) {
|
|
|
507
555
|
function bootstrap(config) {
|
|
508
556
|
assertConfig(config);
|
|
509
557
|
initLogger(config.log);
|
|
558
|
+
if (!acquireSingleInstance()) return false;
|
|
510
559
|
initSettings();
|
|
511
560
|
setWindowDefaults(config.window);
|
|
512
561
|
if (config.features?.updater) {
|
|
@@ -527,11 +576,15 @@ function bootstrap(config) {
|
|
|
527
576
|
}
|
|
528
577
|
if (config.features?.deeplink) {
|
|
529
578
|
}
|
|
579
|
+
return true;
|
|
530
580
|
}
|
|
531
581
|
// Annotate the CommonJS export names for ESM import in node:
|
|
532
582
|
0 && (module.exports = {
|
|
583
|
+
acquireSingleInstance,
|
|
533
584
|
bootstrap,
|
|
534
585
|
createWindow,
|
|
586
|
+
focusWindow,
|
|
587
|
+
getWindow,
|
|
535
588
|
logger,
|
|
536
589
|
settings
|
|
537
590
|
});
|