@orielweb/api 0.0.0-stage → 0.4.14

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/src/dialog.js ADDED
@@ -0,0 +1,193 @@
1
+ // 对标 `@tauri-apps/plugin-dialog`。
2
+ //
3
+ // **与 `window.Window.pickFile()` 的关系(一个刻意的决定)**:那条是既有的公开契约
4
+ // (只收标题与过滤器、返回单个路径),**冻结不动**;本模块是新入口,收 Tauri 形状的
5
+ // options(多选 / 过滤器 / 初始目录)并返回数组。两者共用底下的原生对话框,
6
+ // 所以不会出现"两份实现行为不一致"。
7
+ //
8
+ // 一处实现细节值得说:`message` 系列在 Tauri 那边返回**被点按钮的文案**,
9
+ // 而本库的命令层返回**语义**(`ok` / `cancel` / `yes` / `no`)。
10
+ // 原因是 Windows 的 `MessageBoxW` **只能显示系统文案**,若让命令返回"实际显示的文案",
11
+ // 页面拿它与期望标签比对时会**静默得到错误答案**(用户点了"是",显示的却是"确定")。
12
+ // 语义与文案分开之后,文案只是这一层的事,判断不受影响。
13
+
14
+ import { invoke } from './core.js';
15
+
16
+ /**
17
+ * `message` / `ask` / `confirm` 的选项。
18
+ *
19
+ * @typedef {object} DialogMessageOptions
20
+ * @property {string} [title] 对话框标题。
21
+ * @property {'info' | 'warning' | 'error'} [kind] 语气(决定图标)。
22
+ * @property {string | {ok?: string, cancel?: string, yes?: string, no?: string}} [buttons]
23
+ * 按钮集合:字符串(`'OkCancel'` / `'YesNo'` / `'YesNoCancel'` / `'Ok'`)或对象。
24
+ * @property {string} [okLabel] `ask` / `confirm` 的肯定按钮文案。
25
+ * @property {string} [cancelLabel] `ask` / `confirm` 的否定按钮文案。
26
+ */
27
+
28
+ /**
29
+ * 把"选项或标题字符串"归一成选项对象。
30
+ *
31
+ * @param {DialogMessageOptions | string | undefined} options
32
+ * @returns {DialogMessageOptions}
33
+ * @remarks Tauri 允许第二参直接给一个标题字符串,所以两种写法都要收。
34
+ */
35
+ function normalizeOptions(options) {
36
+ return typeof options === 'string' ? { title: options } : (options ?? {});
37
+ }
38
+
39
+ /**
40
+ * 打开文件(或目录)选择对话框。
41
+ *
42
+ * @param {object} [options]
43
+ * @param {string} [options.title] 对话框标题。
44
+ * @param {string} [options.defaultPath] 初始目录,或一个完整路径(会拆出目录部分)。
45
+ * @param {Array<{name: string, extensions: string[]}>} [options.filters] 文件类型过滤。
46
+ * 扩展名带不带点都行;`['*']` 表示"所有文件"。
47
+ * @param {boolean} [options.multiple] 允许多选(与 `directory` 不能同时为真)。
48
+ * @param {boolean} [options.directory] 选目录而不是文件(**只支持单选**)。
49
+ * @returns {Promise<string | string[] | null>} 多选时是数组;取消时是 `null`。
50
+ * @throws {Error} 传了 `canCreateDirectories` / `recursive`(未实现,会明确报错)。
51
+ * @remarks 形状与 Tauri 一致:单选给 `string`、多选给 `string[]`、取消给 `null`。
52
+ */
53
+ export async function open(options = {}) {
54
+ const selected = await invoke('dialog.open', { options });
55
+
56
+ // 命令层统一返回数组(返回值形状稳定),这一层按"是不是多选"摊平——
57
+ // 那样页面拿到的形状与 Tauri 一致,而命令层不需要页面去猜。
58
+ if (!Array.isArray(selected) || selected.length === 0) {
59
+ return null;
60
+ }
61
+
62
+ return options.multiple === true ? selected : selected[0];
63
+ }
64
+
65
+ /**
66
+ * 保存文件对话框。
67
+ *
68
+ * @param {object} [options] 同 {@link open} 的 `title` / `defaultPath` / `filters`。
69
+ * @returns {Promise<string | null>} 选中的路径;取消时是 `null`。
70
+ */
71
+ export function save(options = {}) {
72
+ return invoke('dialog.save', { options });
73
+ }
74
+
75
+ /**
76
+ * 弹一个消息对话框。
77
+ *
78
+ * @param {string} message 正文。
79
+ * @param {DialogMessageOptions | string} [options] 选项,或直接给标题字符串(Tauri 也接受)。
80
+ * @returns {Promise<string | null>} 用户点的那个按钮的**文案**;被外部关掉时是 `null`。
81
+ * @remarks
82
+ * 返回值是**文案**(与 Tauri 一致):所以 `ask` / `confirm` 能直接拿它跟
83
+ * 自己期望的标签比对,调用方也能用它区分"是/否/取消"三个按钮。
84
+ * 命令层返回的是语义,这里映射回文案。
85
+ */
86
+ export function message(message, options) {
87
+ return sendMessage(message, options);
88
+ }
89
+
90
+ /**
91
+ * 询问"是/否"。
92
+ *
93
+ * @param {string} message
94
+ * @param {DialogMessageOptions | string} [options] 同 {@link message}。
95
+ * @returns {Promise<boolean>} 用户是否点了肯定性的那个按钮。
96
+ */
97
+ export async function ask(message, options) {
98
+ const opts = normalizeOptions(options);
99
+ const okLabel = opts.okLabel ?? 'Yes';
100
+ const cancelLabel = opts.cancelLabel ?? 'No';
101
+
102
+ const clicked = await sendMessage(message, {
103
+ title: opts.title,
104
+ kind: opts.kind,
105
+ buttons: { yes: okLabel, no: cancelLabel },
106
+ });
107
+
108
+ return clicked === okLabel;
109
+ }
110
+
111
+ /**
112
+ * 询问"确定/取消"。
113
+ *
114
+ * @param {string} message
115
+ * @param {DialogMessageOptions | string} [options] 同 {@link message}。
116
+ * @returns {Promise<boolean>} 用户是否点了确定。
117
+ */
118
+ export async function confirm(message, options) {
119
+ const opts = normalizeOptions(options);
120
+ const okLabel = opts.okLabel ?? 'Ok';
121
+ const cancelLabel = opts.cancelLabel ?? 'Cancel';
122
+
123
+ const clicked = await sendMessage(message, {
124
+ title: opts.title,
125
+ kind: opts.kind,
126
+ buttons: { ok: okLabel, cancel: cancelLabel },
127
+ });
128
+
129
+ return clicked === okLabel;
130
+ }
131
+
132
+ /**
133
+ * 三个导出函数共用的实现。
134
+ *
135
+ * @param {string} text
136
+ * @param {DialogMessageOptions | string} [options]
137
+ * @returns {Promise<string | null>}
138
+ * @remarks
139
+ * 抽成内部函数而不是让 `ask` / `confirm` 调导出的 `message`:
140
+ * 那三个函数的**形参也叫 `message`**,而形参会遮蔽同名的导出函数——
141
+ * 写成 `message(...)` 会在运行时得到"message is not a function"。
142
+ * (Tauri 那边把它叫 `messageCommand` 正是躲这个。这里换个名字更直白。)
143
+ * TypeScript 在 `checkJs` 下能抓到,但那是"碰巧被抓到"——形参改名就不会了,
144
+ * 所以结构上避开它比依赖检查器更稳。
145
+ */
146
+ async function sendMessage(text, options) {
147
+ const opts = normalizeOptions(options);
148
+ const { buttons, labels } = normalizeButtons(opts.buttons);
149
+
150
+ const result = await invoke('dialog.message', {
151
+ message: text,
152
+ title: opts.title,
153
+ kind: opts.kind,
154
+ buttons,
155
+ });
156
+
157
+ return result === null || result === undefined ? null : labels[result] ?? null;
158
+ }
159
+
160
+ /**
161
+ * 把 Tauri 的按钮写法归一成"命令层要的形状 + 语义 → 文案的映射"。
162
+ *
163
+ * @param {string | {ok?: string, cancel?: string, yes?: string, no?: string}} [buttons]
164
+ * @returns {{buttons: string | Record<string, string>, labels: Record<string, string>}}
165
+ * @remarks
166
+ * Tauri 的写法有三种:省略、字符串(`'OkCancel'` 等)、对象(`{ok}` / `{ok,cancel}` /
167
+ * `{yes,no,cancel}`)。这里统一成"命令层收字符串或对象",同时给出
168
+ * **语义 → 文案**的映射(命令层回来的是语义)。
169
+ */
170
+ function normalizeButtons(buttons) {
171
+ if (buttons === undefined || buttons === null) {
172
+ return { buttons: 'Ok', labels: { ok: '确定', cancel: '取消', yes: '是', no: '否' } };
173
+ }
174
+
175
+ if (typeof buttons === 'string') {
176
+ return { buttons, labels: { ok: '确定', cancel: '取消', yes: '是', no: '否' } };
177
+ }
178
+
179
+ const ok = buttons.ok ?? '确定';
180
+ const cancel = buttons.cancel ?? '取消';
181
+ const yes = buttons.yes ?? '是';
182
+ const no = buttons.no ?? '否';
183
+
184
+ if (buttons.yes !== undefined || buttons.no !== undefined) {
185
+ return buttons.cancel !== undefined
186
+ ? { buttons: { yes, no, cancel }, labels: { yes, no, cancel } }
187
+ : { buttons: { yes, no }, labels: { yes, no } };
188
+ }
189
+
190
+ return buttons.cancel !== undefined
191
+ ? { buttons: { ok, cancel }, labels: { ok, cancel } }
192
+ : { buttons: { ok }, labels: { ok } };
193
+ }
package/src/dpi.js ADDED
@@ -0,0 +1,392 @@
1
+ // 对标 `@tauri-apps/api/dpi`。
2
+ //
3
+ // 纯形状层:只负责"多态尺寸/位置对象"的构造与归一,**不含任何 IPC**。
4
+ // 真正的换算发生在宿主侧(Windows 按窗口 DPI 折物理像素、macOS/Linux 用各自逻辑像素),
5
+ // 这里不重复一遍——两份换算必然漂移,而漂移的表现是"某个平台上差一圈"。
6
+ //
7
+ // 与 Tauri 的差别:
8
+ // * Tauri 的 `LogicalSize` 等是**类**(带 `type` 字段供 Rust 侧反序列化);
9
+ // 这里同样是类 + `type` 字段,形状一致。
10
+ // * 本库的宿主命令目前只吃 int 逻辑像素(`win.*`),`PhysicalSize` 会被
11
+ // `window.setSize` 换算成逻辑尺寸再发(见 window 模块,P0-5/E3)。
12
+
13
+ /**
14
+ * @typedef {'Logical' | 'Physical'} SizeType
15
+ * @typedef {{ type: SizeType, width: number, height: number }} SizeObject
16
+ * @typedef {{ type: SizeType, x: number, y: number }} PositionObject
17
+ */
18
+
19
+ /**
20
+ * 逻辑像素尺寸(对标 `LogicalSize`)。
21
+ */
22
+ export class LogicalSize {
23
+ /**
24
+ * @param {number} width
25
+ * @param {number} height
26
+ */
27
+ constructor(width, height) {
28
+ /** @type {SizeType} */
29
+ this.type = 'Logical';
30
+ this.width = width;
31
+ this.height = height;
32
+ }
33
+
34
+ /**
35
+ * 换算成物理像素尺寸(对标 Tauri 的 `LogicalSize.toPhysical(scaleFactor)`)。
36
+ *
37
+ * @param {number} scaleFactor
38
+ * @returns {PhysicalSize}
39
+ */
40
+ toPhysical(scaleFactor) {
41
+ return new PhysicalSize(logicalToPhysical(this.width, scaleFactor), logicalToPhysical(this.height, scaleFactor));
42
+ }
43
+
44
+ /** Tauri 2 线格式:外部标记 `{ Logical: { width, height } }`(B14)。 */
45
+ toJSON() {
46
+ return { Logical: { width: this.width, height: this.height } };
47
+ }
48
+ }
49
+
50
+ /**
51
+ * 物理像素尺寸(对标 `PhysicalSize`)。
52
+ */
53
+ export class PhysicalSize {
54
+ /**
55
+ * @param {number} width
56
+ * @param {number} height
57
+ */
58
+ constructor(width, height) {
59
+ /** @type {SizeType} */
60
+ this.type = 'Physical';
61
+ this.width = width;
62
+ this.height = height;
63
+ }
64
+
65
+ /**
66
+ * 换算成逻辑像素尺寸(对标 Tauri 的 `PhysicalSize.toLogical(scaleFactor)`)。
67
+ *
68
+ * @param {number} scaleFactor
69
+ * @returns {LogicalSize}
70
+ */
71
+ toLogical(scaleFactor) {
72
+ return new LogicalSize(physicalToLogical(this.width, scaleFactor), physicalToLogical(this.height, scaleFactor));
73
+ }
74
+
75
+ /** Tauri 2 线格式:外部标记 `{ Physical: { width, height } }`(B14)。 */
76
+ toJSON() {
77
+ return { Physical: { width: this.width, height: this.height } };
78
+ }
79
+ }
80
+
81
+ /**
82
+ * 逻辑坐标位置(对标 `LogicalPosition`)。
83
+ */
84
+ export class LogicalPosition {
85
+ /**
86
+ * @param {number} x
87
+ * @param {number} y
88
+ */
89
+ constructor(x, y) {
90
+ /** @type {SizeType} */
91
+ this.type = 'Logical';
92
+ this.x = x;
93
+ this.y = y;
94
+ }
95
+
96
+ /**
97
+ * 换算成物理坐标(对标 Tauri 的 `LogicalPosition.toPhysical(scaleFactor)`)。
98
+ *
99
+ * @param {number} scaleFactor
100
+ * @returns {PhysicalPosition}
101
+ */
102
+ toPhysical(scaleFactor) {
103
+ return new PhysicalPosition(logicalToPhysical(this.x, scaleFactor), logicalToPhysical(this.y, scaleFactor));
104
+ }
105
+
106
+ /** Tauri 2 线格式:外部标记 `{ Logical: { x, y } }`(B14)。 */
107
+ toJSON() {
108
+ return { Logical: { x: this.x, y: this.y } };
109
+ }
110
+ }
111
+
112
+ /**
113
+ * 物理坐标位置(对标 `PhysicalPosition`)。
114
+ */
115
+ export class PhysicalPosition {
116
+ /**
117
+ * @param {number} x
118
+ * @param {number} y
119
+ */
120
+ constructor(x, y) {
121
+ /** @type {SizeType} */
122
+ this.type = 'Physical';
123
+ this.x = x;
124
+ this.y = y;
125
+ }
126
+
127
+ /**
128
+ * 换算成逻辑坐标(对标 Tauri 的 `PhysicalPosition.toLogical(scaleFactor)`)。
129
+ *
130
+ * @param {number} scaleFactor
131
+ * @returns {LogicalPosition}
132
+ */
133
+ toLogical(scaleFactor) {
134
+ return new LogicalPosition(physicalToLogical(this.x, scaleFactor), physicalToLogical(this.y, scaleFactor));
135
+ }
136
+
137
+ /** Tauri 2 线格式:外部标记 `{ Physical: { x, y } }`(B14)。 */
138
+ toJSON() {
139
+ return { Physical: { x: this.x, y: this.y } };
140
+ }
141
+ }
142
+
143
+ /**
144
+ * 把任意"看起来像尺寸"的入参归一成尺寸对象。
145
+ *
146
+ * 接受四种形态,与 Tauri 的宽松度一致:
147
+ * `{width, height}`(缺 `type` 时按逻辑算)、`LogicalSize`/`PhysicalSize` 实例、以及裸数字对。
148
+ *
149
+ * @param {SizeObject | { width: number, height: number } | [number, number]} size
150
+ * @param {SizeType} [fallbackType] 入参缺 `type` 时用的类型,默认 `'Logical'`。
151
+ * @returns {SizeObject}
152
+ * @throws {Error} 入参不可解释时抛出(不静默当成 0,那会得到一个 0x0 的窗口)。
153
+ */
154
+ export function normalizeSize(size, fallbackType = 'Logical') {
155
+ if (Array.isArray(size)) {
156
+ const [width, height] = size;
157
+ return { type: fallbackType, width: requireFinite(width, 'width'), height: requireFinite(height, 'height') };
158
+ }
159
+
160
+ if (size && typeof size === 'object') {
161
+ // Tauri 2 外部标记形状 `{ Logical: { width, height } }`(B14 起的线格式);
162
+ // 旧的内联 tag 形状 `{ type: 'Logical', width, height }` 仍然接受(读兼容一个版本)。
163
+ const record = /** @type {Record<string, unknown>} */ (size);
164
+ if (typeof record.type !== 'string'
165
+ && (record.Logical !== undefined || record.Physical !== undefined)) {
166
+ const tag = record.Logical !== undefined ? 'Logical' : 'Physical';
167
+ const inner = /** @type {Record<string, unknown>} */ (record[tag]);
168
+ return {
169
+ type: tag,
170
+ width: requireFinite(inner?.width, 'width'),
171
+ height: requireFinite(inner?.height, 'height'),
172
+ };
173
+ }
174
+
175
+ const candidate = /** @type {Partial<SizeObject>} */ (size);
176
+ const type = candidate.type === 'Physical' ? 'Physical'
177
+ : candidate.type === 'Logical' ? 'Logical'
178
+ : fallbackType;
179
+ return {
180
+ type,
181
+ width: requireFinite(candidate.width, 'width'),
182
+ height: requireFinite(candidate.height, 'height'),
183
+ };
184
+ }
185
+
186
+ throw new Error('normalizeSize: 需要 {width, height} 形式的对象(或 [width, height])');
187
+ }
188
+
189
+ /**
190
+ * 把任意"看起来像位置"的入参归一成位置对象。
191
+ *
192
+ * @param {PositionObject | { x: number, y: number } | [number, number]} position
193
+ * @param {SizeType} [fallbackType] 入参缺 `type` 时用的类型,默认 `'Logical'`。
194
+ * @returns {PositionObject}
195
+ * @throws {Error} 入参不可解释时抛出。
196
+ */
197
+ export function normalizePosition(position, fallbackType = 'Logical') {
198
+ if (Array.isArray(position)) {
199
+ const [x, y] = position;
200
+ return { type: fallbackType, x: requireFinite(x, 'x'), y: requireFinite(y, 'y') };
201
+ }
202
+
203
+ if (position && typeof position === 'object') {
204
+ // 同 normalizeSize:Tauri 2 外部标记形状优先,旧内联 tag 兼容读取。
205
+ const record = /** @type {Record<string, unknown>} */ (position);
206
+ if (typeof record.type !== 'string'
207
+ && (record.Logical !== undefined || record.Physical !== undefined)) {
208
+ const tag = record.Logical !== undefined ? 'Logical' : 'Physical';
209
+ const inner = /** @type {Record<string, unknown>} */ (record[tag]);
210
+ return {
211
+ type: tag,
212
+ x: requireFinite(inner?.x, 'x'),
213
+ y: requireFinite(inner?.y, 'y'),
214
+ };
215
+ }
216
+
217
+ const candidate = /** @type {Partial<PositionObject>} */ (position);
218
+ const type = candidate.type === 'Physical' ? 'Physical'
219
+ : candidate.type === 'Logical' ? 'Logical'
220
+ : fallbackType;
221
+ return {
222
+ type,
223
+ x: requireFinite(candidate.x, 'x'),
224
+ y: requireFinite(candidate.y, 'y'),
225
+ };
226
+ }
227
+
228
+ throw new Error('normalizePosition: 需要 {x, y} 形式的对象(或 [x, y])');
229
+ }
230
+
231
+ /**
232
+ * 校验是有限数字。
233
+ *
234
+ * 为什么不允许 NaN/Infinity:它们传到宿主会被序列化成 `null`,
235
+ * 而宿主侧的"参数缺失"与"参数是 0"看起来一样——症状是窗口莫名变成 0x0 或跳到左上角。
236
+ *
237
+ * @param {unknown} value
238
+ * @param {string} field
239
+ * @returns {number}
240
+ */
241
+ function requireFinite(value, field) {
242
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
243
+ throw new Error(`尺寸/位置字段 '${field}' 必须是有限数字(收到 ${String(value)})`);
244
+ }
245
+
246
+ return value;
247
+ }
248
+
249
+ /**
250
+ * 逻辑尺寸 → 物理尺寸。
251
+ *
252
+ * @param {number} logical 逻辑像素。
253
+ * @param {number} scaleFactor 缩放因子(1 = 100%)。
254
+ * @returns {number} 物理像素(四舍五入到整像素,与宿主侧 LogicalPixels.ToPhysical 一致)。
255
+ */
256
+ export function logicalToPhysical(logical, scaleFactor) {
257
+ requireFinite(logical, 'logical');
258
+ requireFinite(scaleFactor, 'scaleFactor');
259
+ if (scaleFactor <= 0) {
260
+ throw new Error(`scaleFactor 必须是正数(收到 ${scaleFactor})`);
261
+ }
262
+
263
+ return roundToEven(logical * scaleFactor);
264
+ }
265
+
266
+ /**
267
+ * 物理尺寸 → 逻辑尺寸。
268
+ *
269
+ * @param {number} physical 物理像素。
270
+ * @param {number} scaleFactor 缩放因子(1 = 100%)。
271
+ * @returns {number} 逻辑像素(保留小数:除以缩放比通常不是整数,取整会逐次累积偏差)。
272
+ */
273
+ export function physicalToLogical(physical, scaleFactor) {
274
+ requireFinite(physical, 'physical');
275
+ requireFinite(scaleFactor, 'scaleFactor');
276
+ if (scaleFactor <= 0) {
277
+ throw new Error(`scaleFactor 必须是正数(收到 ${scaleFactor})`);
278
+ }
279
+
280
+ return physical / scaleFactor;
281
+ }
282
+
283
+ /**
284
+ * 四舍五入到整像素,**.5 时取偶数**(银行家舍入)。
285
+ *
286
+ * 为什么不直接用 `Math.round`:宿主侧是 .NET 的 `Math.Round(logical * dpi / 96)`,
287
+ * 它默认 `MidpointRounding.ToEven`——`Math.Round(4.5)` 给 **4**,而 JS 的
288
+ * `Math.round(4.5)` 给 **5**。两边不一致的后果是"同一份前端代码在 150% 屏上
289
+ * 比宿主算的尺寸大 1 像素",而这种偏差只在恰好落在 .5 的输入上出现,
290
+ * 靠人眼看不出、靠随机测试也撞不到。
291
+ *
292
+ * 复刻规则(与 .NET 一致):先取 `Math.floor`,若小数部分恰为 0.5,
293
+ * 则"取偶"——即向下取整的结果为偶数时保持,奇数时进一。
294
+ *
295
+ * @param {number} value
296
+ * @returns {number}
297
+ */
298
+ function roundToEven(value) {
299
+ const floored = Math.floor(value);
300
+ const fraction = value - floored;
301
+
302
+ if (fraction > 0.5) {
303
+ return floored + 1;
304
+ }
305
+
306
+ if (fraction < 0.5) {
307
+ return floored;
308
+ }
309
+
310
+ // 恰为 .5:取偶。floored 是偶数则留它,否则进一。
311
+ return floored % 2 === 0 ? floored : floored + 1;
312
+ }
313
+
314
+ /**
315
+ * 尺寸的联合包装(对标 Tauri 的 `Size`):`setSize` 一类接口收两种单位时用它。
316
+ *
317
+ * `toJSON()` 输出 Tauri 2 的外部标记形状 `{ Logical: { width, height } }`(B14 起)。
318
+ */
319
+ export class Size {
320
+ /**
321
+ * @param {LogicalSize | PhysicalSize} size
322
+ */
323
+ constructor(size) {
324
+ /** @type {LogicalSize | PhysicalSize} */
325
+ this.size = size;
326
+ }
327
+
328
+ /**
329
+ * 换算成逻辑尺寸(已是 `LogicalSize` 时原样返回)。
330
+ * @param {number} scaleFactor
331
+ * @returns {LogicalSize}
332
+ */
333
+ toLogical(scaleFactor) {
334
+ return this.size instanceof LogicalSize
335
+ ? this.size
336
+ : this.size.toLogical(scaleFactor);
337
+ }
338
+
339
+ /**
340
+ * 换算成物理尺寸(已是 `PhysicalSize` 时原样返回)。
341
+ * @param {number} scaleFactor
342
+ * @returns {PhysicalSize}
343
+ */
344
+ toPhysical(scaleFactor) {
345
+ return this.size instanceof PhysicalSize
346
+ ? this.size
347
+ : this.size.toPhysical(scaleFactor);
348
+ }
349
+
350
+ toJSON() {
351
+ return { [this.size.type]: { width: this.size.width, height: this.size.height } };
352
+ }
353
+ }
354
+
355
+ /**
356
+ * 位置的联合包装(对标 Tauri 的 `Position`)。线格式差别同 {@link Size}。
357
+ */
358
+ export class Position {
359
+ /**
360
+ * @param {LogicalPosition | PhysicalPosition} position
361
+ */
362
+ constructor(position) {
363
+ /** @type {LogicalPosition | PhysicalPosition} */
364
+ this.position = position;
365
+ }
366
+
367
+ /**
368
+ * 换算成逻辑位置。
369
+ * @param {number} scaleFactor
370
+ * @returns {LogicalPosition}
371
+ */
372
+ toLogical(scaleFactor) {
373
+ return this.position instanceof LogicalPosition
374
+ ? this.position
375
+ : this.position.toLogical(scaleFactor);
376
+ }
377
+
378
+ /**
379
+ * 换算成物理位置。
380
+ * @param {number} scaleFactor
381
+ * @returns {PhysicalPosition}
382
+ */
383
+ toPhysical(scaleFactor) {
384
+ return this.position instanceof PhysicalPosition
385
+ ? this.position
386
+ : this.position.toPhysical(scaleFactor);
387
+ }
388
+
389
+ toJSON() {
390
+ return { [this.position.type]: { x: this.position.x, y: this.position.y } };
391
+ }
392
+ }