@orielweb/api 0.5.0 → 0.6.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.
package/src/dpi.js CHANGED
@@ -1,392 +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
- }
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
+ }