@morlay/zephyr-desktop-shell 0.0.0-stage → 0.2.0-alpha.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 +135 -2
- package/dist/app-BCTAe0Ae.mjs +949 -0
- package/dist/deployment-line-Dd1AIkCI.mjs +402 -0
- package/dist/failure-Dapchl7c.mjs +40 -0
- package/dist/index.d.mts +396 -0
- package/dist/index.mjs +1 -0
- package/dist/ipc-9pZ8Xo6_.mjs +27 -0
- package/dist/main.d.mts +1 -0
- package/dist/main.mjs +14 -0
- package/dist/preload.d.mts +1 -0
- package/dist/preload.mjs +94 -0
- package/dist/storage-backend.d.mts +38 -0
- package/dist/storage-backend.mjs +22 -0
- package/package.json +63 -3
- package/src/app.ts +655 -0
- package/src/browser-pane-bridge.ts +57 -0
- package/src/browser-pane-view.ts +214 -0
- package/src/browser-pane.ts +174 -0
- package/src/browser-url.ts +20 -0
- package/src/debug.ts +57 -0
- package/src/deployment-line.ts +36 -0
- package/src/dev-mode.ts +97 -0
- package/src/dev-tools.ts +125 -0
- package/src/document-marks.ts +26 -0
- package/src/electron-like.ts +115 -0
- package/src/failure.ts +55 -0
- package/src/index.ts +24 -0
- package/src/inspector.ts +145 -0
- package/src/ipc-owner.ts +23 -0
- package/src/ipc.ts +35 -0
- package/src/main.ts +31 -0
- package/src/menu.ts +179 -0
- package/src/navigation.ts +42 -0
- package/src/preload.ts +57 -0
- package/src/shell-env.ts +81 -0
- package/src/shortcuts.ts +122 -0
- package/src/single-instance.ts +28 -0
- package/src/storage-backend.ts +49 -0
- package/src/storage-bridge.ts +52 -0
- package/src/storage.ts +225 -0
- package/src/window-state.ts +145 -0
- package/src/window.ts +120 -0
package/src/storage.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// 桌面壳的**主进程存储**(桥):页面侧 `localStorage` / `sessionStorage` 代理的落盘后端。
|
|
2
|
+
//
|
|
3
|
+
// **为什么真源在主进程**:页面的 `localStorage` 按 **origin** 隔离。桌面壳里页面可能换端口(dev server
|
|
4
|
+
// 重启、多窗口不同 origin),同一份偏好就读不到——它记着,另一个 origin 看不见。数据搬到主进程、按
|
|
5
|
+
// `appId`(配置里的 `desktop.id`,打包用的那个)分桶之后,键里**不含 origin**,所以从哪个端口进页面
|
|
6
|
+
// 读到的都是同一份。
|
|
7
|
+
//
|
|
8
|
+
// **判据边界**:本文件只证主进程这一侧自洽(同 appId 往返 / 不同 appId 隔离 / write 同步返回 / 落盘后
|
|
9
|
+
// 新实例能预载)。跨端口本身要两个真 origin(真机才验得出)——但**④ 通过就满足了它的数据前提**:
|
|
10
|
+
// 数据不按 origin 分、重启能读回来,剩下的只是装配(第 4 步:桌面入口把 `desktopStorageBackend()` 递给
|
|
11
|
+
// `installStorageShim`)。
|
|
12
|
+
//
|
|
13
|
+
// **同步语义与 `attachPersistence` 同契约**:内存表是唯一真源,`storageWrite` 立即改内存后就返回,
|
|
14
|
+
// 落盘去抖合并(缺省 100ms);**写文件失败只 `console.error`、不回滚内存**——回滚会让「刚写进去的值消失」,
|
|
15
|
+
// 比丢盘更难查。
|
|
16
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { DESKTOP_IPC, type StorageStore } from "./ipc.ts";
|
|
19
|
+
import type { ElectronLike } from "./electron-like.ts";
|
|
20
|
+
|
|
21
|
+
/** 落盘文件的名字(在 `userData` 下,与 `shortcuts.json` / `window-state.json` 同一处)。 */
|
|
22
|
+
export const STORAGE_FILE = "storage.json";
|
|
23
|
+
|
|
24
|
+
/** 去抖缺省值(毫秒):页面一次操作常连着几条 `setItem`,合并成一次写盘。 */
|
|
25
|
+
const DEFAULT_DEBOUNCE_MS = 100;
|
|
26
|
+
|
|
27
|
+
/** 一个 app 实例的两张表(`local` / `session` 各一张;值都是字符串,`Storage` 面只存字符串)。 */
|
|
28
|
+
type StoreTables = Map<StorageStore, Map<string, string>>;
|
|
29
|
+
|
|
30
|
+
/** 装存储桥需要的部署值。 */
|
|
31
|
+
export interface StorageHostOptions {
|
|
32
|
+
/** electron 面(只用 `ipcMain`:存储不碰窗口)。 */
|
|
33
|
+
readonly electron: Pick<ElectronLike, "ipcMain">;
|
|
34
|
+
/** `userData`(`storage.json` 落在这里)。 */
|
|
35
|
+
readonly userData: string;
|
|
36
|
+
/**
|
|
37
|
+
* 数据按它分桶:`desktop.id`(打包用的那个,`appId`)。
|
|
38
|
+
*
|
|
39
|
+
* **不能换成 windowId / origin**:换端口时 origin 变了,而同一个 app 实例的偏好必须还是那一份。
|
|
40
|
+
*/
|
|
41
|
+
readonly appId: string;
|
|
42
|
+
/** 落盘去抖(毫秒);缺省 100。 */
|
|
43
|
+
readonly debounceMs?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 桥的收尾操作。 */
|
|
47
|
+
export interface StorageHost {
|
|
48
|
+
/** 把当前全部数据立刻写盘(去抖没到期也写);退出与测试用。 */
|
|
49
|
+
flush(): Promise<void>;
|
|
50
|
+
/** 清定时器并做最后一次落盘尝试。 */
|
|
51
|
+
dispose(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 装上存储桥:预载文件 → 注册两个 handler。
|
|
56
|
+
*
|
|
57
|
+
* 预载在**开窗之前**完成(页面一上来就会 `read`):所以本函数是 async,调用方 await 它再开窗。
|
|
58
|
+
* @param options - electron 面、userData、appId 与去抖。
|
|
59
|
+
* @returns 收尾操作。
|
|
60
|
+
*/
|
|
61
|
+
export async function installStorage(options: StorageHostOptions): Promise<StorageHost> {
|
|
62
|
+
const file = join(options.userData, STORAGE_FILE);
|
|
63
|
+
/** 内存真源:appId → 两张表。 */
|
|
64
|
+
const byApp = await loadBuckets(file);
|
|
65
|
+
const debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;
|
|
66
|
+
/** 去抖定时器(存在就说明「还有没写盘的变化」)。 */
|
|
67
|
+
let timer: NodeJS.Timeout | undefined;
|
|
68
|
+
/** 写盘串行队列:同一份文件两次 `writeFile` 并发会互相截断。 */
|
|
69
|
+
let writing: Promise<void> = Promise.resolve();
|
|
70
|
+
|
|
71
|
+
const flush = (): Promise<void> => {
|
|
72
|
+
if (timer !== undefined) {
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
timer = undefined;
|
|
75
|
+
}
|
|
76
|
+
// 先拍快照:写盘期间的新变化留给下一次(它自己会再排一次 flush)。
|
|
77
|
+
// 整份覆写:一个进程只有一个 `appId`(单实例锁 + userData 按 `desktop.id` 分目录),所以不会丢别人的桶。
|
|
78
|
+
// 退化形态(两个 app 共用同一份 userData 并同时写):后写的那份按自己启动时读到的快照覆盖对方的桶——
|
|
79
|
+
// 这是「一份文件一个真源」的代价,要共享就把 appId 也共享。
|
|
80
|
+
const snapshot = snapshotOf(byApp);
|
|
81
|
+
writing = writing.then(async () => {
|
|
82
|
+
try {
|
|
83
|
+
await writeFile(file, `${JSON.stringify(snapshot, undefined, 2)}\n`, "utf8");
|
|
84
|
+
} catch (error) {
|
|
85
|
+
// 只报错、不回滚:内存仍是真源,下一次写会把这份也带上。
|
|
86
|
+
console.error(`[desktop-shell] ${STORAGE_FILE} 落盘失败(内存真源保留):`, error);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return writing;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const schedule = (): void => {
|
|
93
|
+
// 已经在等:合并进那一次(一次开窗里几十条 setItem 只写一次盘)。
|
|
94
|
+
if (timer !== undefined) return;
|
|
95
|
+
timer = setTimeout(() => {
|
|
96
|
+
timer = undefined;
|
|
97
|
+
void flush();
|
|
98
|
+
}, debounceMs);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const { ipcMain } = options.electron;
|
|
102
|
+
ipcMain.handle(DESKTOP_IPC.storageRead, async (_event, store: unknown) => {
|
|
103
|
+
// 过桥用**普通对象**(键值表):结构简单、与落盘文件同一形状,也省掉「Map 过 contextBridge 行不行」的疑问。
|
|
104
|
+
return Object.fromEntries(tableOf(byApp, options.appId, storeNameOf(store)));
|
|
105
|
+
});
|
|
106
|
+
ipcMain.handle(DESKTOP_IPC.storageWrite, async (_event, store, key, value) => {
|
|
107
|
+
const table = tableOf(byApp, options.appId, storeNameOf(store));
|
|
108
|
+
// 降级形态:键不是非空字符串、值既不是字符串也不是 null 时**抛**(渲染侧 `void invoke` 收到拒绝,
|
|
109
|
+
// 内存不变)。宁可报错,也不静默当「删除」或写进 `[object Object]`。
|
|
110
|
+
if (typeof key !== "string" || key.length === 0) throw new Error("storage: 键必须是非空字符串");
|
|
111
|
+
if (value !== null && typeof value !== "string")
|
|
112
|
+
throw new Error("storage: 值必须是字符串或 null(null 表示删除)");
|
|
113
|
+
if (value === null) table.delete(key);
|
|
114
|
+
else table.set(key, value);
|
|
115
|
+
// **同步语义**:内存已改,这里就返回。渲染侧的 `setItem` 是同步 API,绝不能等落盘。
|
|
116
|
+
schedule();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return { flush, dispose: () => flush() };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 取(必要时建)某个 app 的某张表。
|
|
124
|
+
* @param byApp - 内存真源。
|
|
125
|
+
* @param appId - app 实例的身份。
|
|
126
|
+
* @param store - store 名。
|
|
127
|
+
* @returns 那张表(可写)。
|
|
128
|
+
*/
|
|
129
|
+
function tableOf(
|
|
130
|
+
byApp: Map<string, StoreTables>,
|
|
131
|
+
appId: string,
|
|
132
|
+
store: StorageStore,
|
|
133
|
+
): Map<string, string> {
|
|
134
|
+
let tables = byApp.get(appId);
|
|
135
|
+
if (tables === undefined) {
|
|
136
|
+
// 两张表一起建:落盘形状固定(`{ local, session }`),不随「先写哪个 store」变。
|
|
137
|
+
tables = new Map([
|
|
138
|
+
["local", new Map()],
|
|
139
|
+
["session", new Map()],
|
|
140
|
+
]);
|
|
141
|
+
byApp.set(appId, tables);
|
|
142
|
+
}
|
|
143
|
+
const table = tables.get(store);
|
|
144
|
+
if (table === undefined) throw new Error(`storage: 内部表缺失:${appId}/${store}`);
|
|
145
|
+
return table;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 过桥的 store 名 → 联合类型。
|
|
150
|
+
* @param value - 渲染进程给的任意值。
|
|
151
|
+
* @returns store 名。
|
|
152
|
+
* @throws Error 不是 `local` / `session`:名字错了是编程错误,不静默落到某一张表上。
|
|
153
|
+
*/
|
|
154
|
+
function storeNameOf(value: unknown): StorageStore {
|
|
155
|
+
if (value === "local" || value === "session") return value;
|
|
156
|
+
throw new Error(`storage: 未知的 store(只有 local / session):${String(value)}`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 内存真源 → 落盘形状:`{ [appId]: { local: {...}, session: {...} } }`(人能读,也能手改)。
|
|
161
|
+
* @param byApp - 内存真源。
|
|
162
|
+
* @returns 可 `JSON.stringify` 的普通对象。
|
|
163
|
+
*/
|
|
164
|
+
function snapshotOf(
|
|
165
|
+
byApp: Map<string, StoreTables>,
|
|
166
|
+
): Record<string, Record<string, Record<string, string>>> {
|
|
167
|
+
return Object.fromEntries(
|
|
168
|
+
[...byApp].map(([appId, tables]) => [
|
|
169
|
+
appId,
|
|
170
|
+
Object.fromEntries([...tables].map(([store, table]) => [store, Object.fromEntries(table)])),
|
|
171
|
+
]),
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 读盘上的整份表。**文件是外部输入**:坏了一层就丢一层,全坏就当空——不因为一份坏文件让应用起不来。
|
|
177
|
+
* @param file - 落盘文件的绝对路径。
|
|
178
|
+
* @returns 内存真源的初值。
|
|
179
|
+
*/
|
|
180
|
+
async function loadBuckets(file: string): Promise<Map<string, StoreTables>> {
|
|
181
|
+
const byApp = new Map<string, StoreTables>();
|
|
182
|
+
const raw = await readFile(file, "utf8").catch(() => undefined);
|
|
183
|
+
if (raw === undefined) return byApp;
|
|
184
|
+
let parsed: unknown;
|
|
185
|
+
try {
|
|
186
|
+
parsed = JSON.parse(raw);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
console.error(`[desktop-shell] ${STORAGE_FILE} 不是合法 JSON(按空处理):`, error);
|
|
189
|
+
return byApp;
|
|
190
|
+
}
|
|
191
|
+
if (!isRecord(parsed)) return byApp;
|
|
192
|
+
for (const [appId, value] of Object.entries(parsed)) {
|
|
193
|
+
if (!isRecord(value)) continue;
|
|
194
|
+
byApp.set(
|
|
195
|
+
appId,
|
|
196
|
+
new Map([
|
|
197
|
+
["local", stringEntriesOf(value["local"])],
|
|
198
|
+
["session", stringEntriesOf(value["session"])],
|
|
199
|
+
]),
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return byApp;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 一层键值表(非字符串的值丢掉)。
|
|
207
|
+
* @param value - 盘上的那一层(不可信)。
|
|
208
|
+
* @returns 键值表(`Map`:键来自外部输入,`__proto__` 之类的键在普通对象上会改原型)。
|
|
209
|
+
*/
|
|
210
|
+
function stringEntriesOf(value: unknown): Map<string, string> {
|
|
211
|
+
const out = new Map<string, string>();
|
|
212
|
+
if (!isRecord(value)) return out;
|
|
213
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
214
|
+
if (typeof entry === "string") out.set(key, entry);
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @param value - 任意值。
|
|
221
|
+
* @returns 是不是普通对象(数组与 `null` 不算)。
|
|
222
|
+
*/
|
|
223
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
224
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
225
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// 窗口状态:位置与尺寸是**用户数据**(`userData/window-state.json`),不是配置。
|
|
2
|
+
//
|
|
3
|
+
// 配置只回答「第一次打开时多大」,之后的位置与尺寸由用户拖动决定。两者混在一处,`desktop.json`
|
|
4
|
+
// 就会在运行期被改写——部署数据变成可变文件。
|
|
5
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
/** 窗口矩形(Electron `Rectangle` 的子集)。 */
|
|
9
|
+
export interface WindowBounds {
|
|
10
|
+
readonly x: number;
|
|
11
|
+
readonly y: number;
|
|
12
|
+
readonly width: number;
|
|
13
|
+
readonly height: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 显示器可用区域(Electron 的 `workArea`:已扣掉菜单栏 / 任务栏)。 */
|
|
17
|
+
export type WorkArea = WindowBounds;
|
|
18
|
+
|
|
19
|
+
/** 一次启动要恢复的状态。 */
|
|
20
|
+
export interface WindowState {
|
|
21
|
+
readonly bounds: WindowBounds;
|
|
22
|
+
readonly maximized: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 状态文件名(在 `userData` 下)。 */
|
|
26
|
+
export const WINDOW_STATE_FILE = "window-state.json";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 从存档与当前显示器算出这次要用的窗口状态。
|
|
30
|
+
*
|
|
31
|
+
* 存档是**外部输入**(上一个版本的进程写的文件),形状不可信:坏掉就当没有存档。
|
|
32
|
+
* @param saved - 存档内容(`loadWindowState` 的原样返回,或 `undefined`)。
|
|
33
|
+
* @param workArea - 主显示器的可用区域。
|
|
34
|
+
* @param fallback - 配置给的初始尺寸(第一次启动用)。
|
|
35
|
+
* @returns 已夹进工作区的状态。
|
|
36
|
+
*/
|
|
37
|
+
export function windowStateOf(
|
|
38
|
+
saved: unknown,
|
|
39
|
+
workArea: WorkArea,
|
|
40
|
+
fallback: { readonly width: number; readonly height: number },
|
|
41
|
+
): WindowState {
|
|
42
|
+
const bounds = boundsOf(saved);
|
|
43
|
+
if (bounds === undefined) {
|
|
44
|
+
return { bounds: centered(fallback, workArea), maximized: false };
|
|
45
|
+
}
|
|
46
|
+
return { bounds: clampInto(bounds, workArea), maximized: maximizedOf(saved) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 读 `userData` 里的存档(原样返回:形状由 {@link windowStateOf} 判)。
|
|
51
|
+
* @param userData - `userData` 目录。
|
|
52
|
+
* @returns 解析后的 JSON,或 `undefined`(没有文件 / 不是 JSON)。
|
|
53
|
+
*/
|
|
54
|
+
export async function loadWindowState(userData: string): Promise<unknown> {
|
|
55
|
+
const raw = await readFile(join(userData, WINDOW_STATE_FILE), "utf8").catch(() => undefined);
|
|
56
|
+
if (raw === undefined) return undefined;
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(raw) as unknown;
|
|
59
|
+
} catch {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 写回存档(写不了就算了:下次启动回到配置尺寸,不是功能故障)。
|
|
66
|
+
* @param userData - `userData` 目录。
|
|
67
|
+
* @param state - 关窗时的状态。
|
|
68
|
+
* @returns 写完成。
|
|
69
|
+
*/
|
|
70
|
+
export async function saveWindowState(userData: string, state: WindowState): Promise<void> {
|
|
71
|
+
await writeFile(
|
|
72
|
+
join(userData, WINDOW_STATE_FILE),
|
|
73
|
+
`${JSON.stringify(state, null, 2)}\n`,
|
|
74
|
+
"utf8",
|
|
75
|
+
).catch(() => undefined);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 取存档里的矩形(四项都得是有限数且宽高为正)。
|
|
80
|
+
* @param saved - 存档内容。
|
|
81
|
+
* @returns 矩形,或 `undefined`(形状不成立)。
|
|
82
|
+
*/
|
|
83
|
+
function boundsOf(saved: unknown): WindowBounds | undefined {
|
|
84
|
+
if (typeof saved !== "object" || saved === null) return undefined;
|
|
85
|
+
const bounds = (saved as { readonly bounds?: unknown }).bounds;
|
|
86
|
+
if (typeof bounds !== "object" || bounds === null) return undefined;
|
|
87
|
+
const { x, y, width, height } = bounds as Record<string, unknown>;
|
|
88
|
+
if (!isNumber(x) || !isNumber(y) || !isNumber(width) || !isNumber(height)) return undefined;
|
|
89
|
+
if (width <= 0 || height <= 0) return undefined;
|
|
90
|
+
return { x, y, width, height };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @param saved - 存档内容。
|
|
95
|
+
* @returns 存档里的最大化标记(缺省 `false`)。
|
|
96
|
+
*/
|
|
97
|
+
function maximizedOf(saved: unknown): boolean {
|
|
98
|
+
if (typeof saved !== "object" || saved === null) return false;
|
|
99
|
+
return (saved as { readonly maximized?: unknown }).maximized === true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 把矩形夹进工作区:窗口整个落在可见区域内(换显示器 / 拔掉外接屏之后都还在眼前)。
|
|
104
|
+
* @param bounds - 存档里的矩形。
|
|
105
|
+
* @param area - 主显示器的可用区域。
|
|
106
|
+
* @returns 夹好的矩形。
|
|
107
|
+
*/
|
|
108
|
+
function clampInto(bounds: WindowBounds, area: WorkArea): WindowBounds {
|
|
109
|
+
const width = Math.min(bounds.width, area.width);
|
|
110
|
+
const height = Math.min(bounds.height, area.height);
|
|
111
|
+
return {
|
|
112
|
+
x: Math.min(Math.max(bounds.x, area.x), area.x + area.width - width),
|
|
113
|
+
y: Math.min(Math.max(bounds.y, area.y), area.y + area.height - height),
|
|
114
|
+
width,
|
|
115
|
+
height,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 第一次启动:用配置尺寸,在工作区里居中(与不给 `x/y` 时 Electron 自己的居中一致)。
|
|
121
|
+
* @param size - 配置给的尺寸。
|
|
122
|
+
* @param area - 主显示器的可用区域。
|
|
123
|
+
* @returns 居中后的矩形。
|
|
124
|
+
*/
|
|
125
|
+
function centered(
|
|
126
|
+
size: { readonly width: number; readonly height: number },
|
|
127
|
+
area: WorkArea,
|
|
128
|
+
): WindowBounds {
|
|
129
|
+
const width = Math.min(size.width, area.width);
|
|
130
|
+
const height = Math.min(size.height, area.height);
|
|
131
|
+
return {
|
|
132
|
+
x: Math.round(area.x + (area.width - width) / 2),
|
|
133
|
+
y: Math.round(area.y + (area.height - height) / 2),
|
|
134
|
+
width,
|
|
135
|
+
height,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param value - 待判的值。
|
|
141
|
+
* @returns 是不是有限数。
|
|
142
|
+
*/
|
|
143
|
+
function isNumber(value: unknown): value is number {
|
|
144
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
145
|
+
}
|
package/src/window.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// 窗口选项的推导:**纯函数**,三处默认值(尺寸、外观、webPreferences)。
|
|
2
|
+
import type { ZephyrDesktopConfig } from "@morlay/zephyr-cli";
|
|
3
|
+
import type { WindowState } from "./window-state.ts";
|
|
4
|
+
|
|
5
|
+
/** 窗口外观(与平台相关的那几项)。 */
|
|
6
|
+
export interface WindowFrame {
|
|
7
|
+
/** macOS:无标题栏但保留红绿灯按钮。 */
|
|
8
|
+
readonly titleBarStyle?: "hidden" | "hiddenInset" | "default";
|
|
9
|
+
readonly trafficLightPosition?: { readonly x: number; readonly y: number };
|
|
10
|
+
readonly vibrancy?: "sidebar" | "under-window";
|
|
11
|
+
readonly visualEffectState?: "active" | "followWindow";
|
|
12
|
+
/** Windows / Linux:去掉系统边框,用 overlay 留出系统按钮的位置。 */
|
|
13
|
+
readonly frame?: boolean;
|
|
14
|
+
readonly titleBarOverlay?: {
|
|
15
|
+
readonly color: string;
|
|
16
|
+
readonly symbolColor: string;
|
|
17
|
+
readonly height: number;
|
|
18
|
+
};
|
|
19
|
+
readonly backgroundColor: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 窗口选项(Electron `BrowserWindow` 用的那个子集)。 */
|
|
23
|
+
export interface WindowOptions extends WindowFrame {
|
|
24
|
+
/** 位置只在「上次关窗的位置」存在时给(否则让 Electron 自己居中)。 */
|
|
25
|
+
readonly x?: number;
|
|
26
|
+
readonly y?: number;
|
|
27
|
+
readonly width: number;
|
|
28
|
+
readonly height: number;
|
|
29
|
+
readonly minWidth: number;
|
|
30
|
+
readonly minHeight: number;
|
|
31
|
+
readonly show: boolean;
|
|
32
|
+
readonly webPreferences: {
|
|
33
|
+
readonly contextIsolation: boolean;
|
|
34
|
+
readonly nodeIntegration: boolean;
|
|
35
|
+
readonly sandbox: boolean;
|
|
36
|
+
/** 只有桌面形态才有的桥(目录选择器、平台标记)。 */
|
|
37
|
+
readonly preload?: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 缺省尺寸:够放下侧栏与一栏对话(也是第一次启动的尺寸)。 */
|
|
42
|
+
export const DEFAULT_WINDOW_SIZE = { width: 1280, height: 800 } as const;
|
|
43
|
+
|
|
44
|
+
/** 缺省最小尺寸。 */
|
|
45
|
+
const DEFAULT_MIN_SIZE = { minWidth: 800, minHeight: 600 } as const;
|
|
46
|
+
|
|
47
|
+
/** 开窗时传进来的那两样(都不是配置:一个来自构建产物,一个来自上次关窗)。 */
|
|
48
|
+
export interface WindowSizing {
|
|
49
|
+
/** preload 脚本绝对路径(桌面桥)。 */
|
|
50
|
+
readonly preload?: string;
|
|
51
|
+
/** 上次关窗时的状态(位置、尺寸、最大化)。 */
|
|
52
|
+
readonly state?: WindowState;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 按平台给窗口外观:**无标题栏**,窗口内容顶到顶边。
|
|
57
|
+
*
|
|
58
|
+
* macOS 保留红绿灯(`hiddenInset` + 指定内缩),左侧栏因此要给顶部留白(页面侧按
|
|
59
|
+
* `document.documentElement.dataset.titlebar` 折叠);Windows 用 `titleBarOverlay`。
|
|
60
|
+
* @param platform - 平台(注入以便测三个平台)。
|
|
61
|
+
* @returns 外观选项。
|
|
62
|
+
*/
|
|
63
|
+
export function windowFrameOf(platform: NodeJS.Platform): WindowFrame {
|
|
64
|
+
if (platform === "darwin") {
|
|
65
|
+
return {
|
|
66
|
+
titleBarStyle: "hiddenInset",
|
|
67
|
+
trafficLightPosition: { x: 16, y: 18 },
|
|
68
|
+
vibrancy: "sidebar",
|
|
69
|
+
// 'active' 让失焦时的材质稳定;'followWindow' 会把侧边栏洗白。
|
|
70
|
+
visualEffectState: "active",
|
|
71
|
+
backgroundColor: "#00000000",
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (platform === "win32") {
|
|
75
|
+
return {
|
|
76
|
+
frame: false,
|
|
77
|
+
titleBarOverlay: { color: "#00000000", symbolColor: "#e5e5e5", height: 40 },
|
|
78
|
+
backgroundColor: "#111111",
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return { frame: false, backgroundColor: "#111111" };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 从配置与上次关窗的状态推导窗口选项。
|
|
86
|
+
*
|
|
87
|
+
* `nodeIntegration: false` + `contextIsolation: true` 不是保守,是**必须**:页面来自 dev server
|
|
88
|
+
* (打包形态也走它),内容里跑的是客户端的整棵 React 树,给渲染进程 node 权限等于把宿主的文件系统交出去。
|
|
89
|
+
* @param desktop - 配置里的 `desktop` 段(可能没有 `window`)。
|
|
90
|
+
* @param platform - 平台(注入以便测三个平台)。
|
|
91
|
+
* @param sizing - preload 路径与上次关窗的状态。
|
|
92
|
+
* @returns 窗口选项。
|
|
93
|
+
*/
|
|
94
|
+
export function windowOptionsOf(
|
|
95
|
+
desktop: ZephyrDesktopConfig | undefined,
|
|
96
|
+
platform: NodeJS.Platform = process.platform,
|
|
97
|
+
sizing: WindowSizing = {},
|
|
98
|
+
): WindowOptions {
|
|
99
|
+
const window = desktop?.window;
|
|
100
|
+
// 位置与尺寸只有一个来源:上次关窗的状态(`window-state.ts` 已经夹进工作区,也补过配置尺寸)。
|
|
101
|
+
const bounds = sizing.state?.bounds;
|
|
102
|
+
return {
|
|
103
|
+
...windowFrameOf(platform),
|
|
104
|
+
...(bounds === undefined ? {} : { x: bounds.x, y: bounds.y }),
|
|
105
|
+
width: bounds?.width ?? window?.width ?? DEFAULT_WINDOW_SIZE.width,
|
|
106
|
+
height: bounds?.height ?? window?.height ?? DEFAULT_WINDOW_SIZE.height,
|
|
107
|
+
minWidth: window?.minWidth ?? DEFAULT_MIN_SIZE.minWidth,
|
|
108
|
+
minHeight: window?.minHeight ?? DEFAULT_MIN_SIZE.minHeight,
|
|
109
|
+
show: false,
|
|
110
|
+
webPreferences: {
|
|
111
|
+
contextIsolation: true,
|
|
112
|
+
nodeIntegration: false,
|
|
113
|
+
// **sandbox 必须关**:Electron 的 sandboxed preload 只认 CJS,而我们的 preload 是 ESM
|
|
114
|
+
// (与 main.mjs 同一条构建链)。开了 sandbox,preload 会加载失败(报「Unable to load preload」)。
|
|
115
|
+
// 隔离仍由 contextIsolation + nodeIntegration:false 保证,页面只加载本地 dev server。
|
|
116
|
+
sandbox: false,
|
|
117
|
+
...(sizing.preload === undefined ? {} : { preload: sizing.preload }),
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|