@margoengine/ui 0.0.0-stage → 0.1.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.
Files changed (61) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +708 -3
  4. package/dist/basic.d.ts +87 -0
  5. package/dist/collections.d.ts +180 -0
  6. package/dist/color.d.ts +24 -0
  7. package/dist/controls.d.ts +130 -0
  8. package/dist/curved.d.ts +41 -0
  9. package/dist/curved.js +105 -0
  10. package/dist/dom-CWJhpotq.js +340 -0
  11. package/dist/dom-host.d.ts +67 -0
  12. package/dist/dom-host.js +563 -0
  13. package/dist/dom-spec.d.ts +21 -0
  14. package/dist/dom.d.ts +63 -0
  15. package/dist/draw.d.ts +107 -0
  16. package/dist/editor.d.ts +83 -0
  17. package/dist/editor.js +323 -0
  18. package/dist/errors-CDgOgzCd.js +7 -0
  19. package/dist/focus.d.ts +12 -0
  20. package/dist/forms.d.ts +114 -0
  21. package/dist/forms.js +354 -0
  22. package/dist/gl-bb768Mje.js +165 -0
  23. package/dist/gl-load-wwKmojhb.js +5 -0
  24. package/dist/gl-load.d.ts +3 -0
  25. package/dist/gl.d.ts +30 -0
  26. package/dist/i18n.d.ts +69 -0
  27. package/dist/i18n.js +117 -0
  28. package/dist/image.d.ts +74 -0
  29. package/dist/image.js +295 -0
  30. package/dist/index.d.ts +36 -0
  31. package/dist/index.js +4 -0
  32. package/dist/input-dom.d.ts +14 -0
  33. package/dist/input.d.ts +92 -0
  34. package/dist/input.js +315 -0
  35. package/dist/interaction.d.ts +106 -0
  36. package/dist/keyboard.d.ts +40 -0
  37. package/dist/keyboard.js +132 -0
  38. package/dist/layout.d.ts +116 -0
  39. package/dist/markup.d.ts +89 -0
  40. package/dist/markup.js +369 -0
  41. package/dist/meters-yaZRtwsX.js +1401 -0
  42. package/dist/meters.d.ts +72 -0
  43. package/dist/mirror.d.ts +30 -0
  44. package/dist/overlays.d.ts +89 -0
  45. package/dist/painter.d.ts +115 -0
  46. package/dist/pass-CAn29pqc.js +15 -0
  47. package/dist/react.d.ts +99 -0
  48. package/dist/react.js +246 -0
  49. package/dist/renderer-Bdhc_0Cu.js +1653 -0
  50. package/dist/renderer.d.ts +92 -0
  51. package/dist/rsc/react.js +3 -0
  52. package/dist/shader.d.ts +6 -0
  53. package/dist/theme.d.ts +104 -0
  54. package/dist/ui.d.ts +328 -0
  55. package/dist/virtual.d.ts +81 -0
  56. package/dist/widget.d.ts +196 -0
  57. package/dist/world-gl.d.ts +32 -0
  58. package/dist/world-gl.js +195 -0
  59. package/dist/world.d.ts +216 -0
  60. package/dist/world.js +561 -0
  61. package/package.json +119 -6
@@ -0,0 +1,1653 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { n as e, r as t, t as n } from "./errors-CDgOgzCd.js";
5
+ import { S as r, _ as i, a, b as o, c as s, i as c, l, m as u, n as d, r as f, s as p, v as m, x as h, y as g } from "./meters-yaZRtwsX.js";
6
+ import { t as _ } from "./pass-CAn29pqc.js";
7
+ import { c as v, i as y, t as b } from "./dom-CWJhpotq.js";
8
+ import { Font as x, bidiClass as S, graphemes as C, layoutText as w } from "@margoengine/text";
9
+ //#region src/draw.ts
10
+ var T = [
11
+ 0,
12
+ 0,
13
+ 0,
14
+ 0
15
+ ];
16
+ function E(t, ...n) {
17
+ for (let r of n) if (r !== void 0 && !Number.isFinite(r)) throw e(`DrawList ${t} must be finite; pass finite UI units, not ${r}`, "ui");
18
+ }
19
+ var D = class {
20
+ constructor() {
21
+ this.commands = [], this.scale = 1, this.alpha = 1, this.version = 0, this.clips = [];
22
+ }
23
+ clear(t) {
24
+ if (!(t > 0) || !Number.isFinite(t)) throw e(`DrawList scale must be finite and above 0; pass device pixels per UI unit, not ${t}`, "ui");
25
+ this.commands.length = 0, this.clips.length = 0, this.alpha = 1, this.scale = t, this.version++;
26
+ }
27
+ fade(e, t) {
28
+ return t === 1 ? e : [
29
+ e[0],
30
+ e[1],
31
+ e[2],
32
+ e[3] * t
33
+ ];
34
+ }
35
+ rect(e, t, n, r, i) {
36
+ E("rect", e, t, n, r, i.borderWidth, i.radius, i.blur);
37
+ let a = this.scale, o = Math.round(e * a), s = Math.round(t * a), c = Math.round((e + n) * a), l = Math.round((t + r) * a), u = (i.blur ?? 0) * a;
38
+ if (c <= o || l <= s) return;
39
+ let d = this.alpha, f = this.fade(i.fill ?? T, d), p = this.fade(i.border ?? T, d);
40
+ f[3] <= 0 && (p[3] <= 0 || !i.borderWidth) || this.commands.push({
41
+ type: "rect",
42
+ x: o,
43
+ y: s,
44
+ width: c - o,
45
+ height: l - s,
46
+ radius: Math.min((i.radius ?? 0) * a, (c - o) / 2, (l - s) / 2),
47
+ fill: f,
48
+ border: p,
49
+ borderWidth: i.borderWidth ? Math.max(1, Math.round(i.borderWidth * a)) : 0,
50
+ blur: u
51
+ });
52
+ }
53
+ text(e, t, n, r, i, a) {
54
+ E("text position", e, t), !(!n.glyphs.length || i[3] * this.alpha <= 0) && this.commands.push({
55
+ type: "text",
56
+ x: Math.round(e * this.scale),
57
+ y: Math.round(t * this.scale),
58
+ layout: n,
59
+ scale: this.scale,
60
+ font: r,
61
+ color: this.fade(i, this.alpha),
62
+ outlineWidth: a ? a.width * this.scale : 0,
63
+ outlineColor: this.fade(a?.color ?? T, this.alpha)
64
+ });
65
+ }
66
+ pushClip(e, t, n, r) {
67
+ E("clip", e, t, n, r);
68
+ let i = this.scale, a = this.clips.at(-1), o = Math.max(Math.round(e * i), a?.x0 ?? -Infinity), s = Math.max(Math.round(t * i), a?.y0 ?? -Infinity), c = Math.min(Math.round((e + n) * i), a?.x1 ?? Infinity), l = Math.min(Math.round((t + r) * i), a?.y1 ?? Infinity), u = {
69
+ x0: o,
70
+ y0: s,
71
+ x1: Math.max(o, c),
72
+ y1: Math.max(s, l)
73
+ };
74
+ this.clips.push(u), this.commands.push({
75
+ type: "clip",
76
+ x: u.x0,
77
+ y: u.y0,
78
+ width: u.x1 - u.x0,
79
+ height: u.y1 - u.y0
80
+ });
81
+ }
82
+ popClip() {
83
+ if (!this.clips.pop()) throw e("DrawList popClip has no open clip; pass each pushClip one popClip", "ui");
84
+ this.commands.push({ type: "unclip" });
85
+ }
86
+ }, O = (e) => [e.x + e.width / 2, e.y + e.height / 2];
87
+ function k(e, t, n, r = !1) {
88
+ let i = n === "left" || n === "right", a = n === "right" || n === "down" ? 1 : -1, [o, s] = O(e), c = -1, l = Infinity, u = (t, n) => {
89
+ let [r, c] = O(t), l = i ? (r - o) * a : (c - s) * a, u = Math.abs(i ? c - s : r - o), d = i ? Math.min(t.y + t.height, e.y + e.height) - Math.max(t.y, e.y) > 0 : Math.min(t.x + t.width, e.x + e.width) - Math.max(t.x, e.x) > 0, f = i ? Math.max(0, a > 0 ? t.x - (e.x + e.width) : e.x - (t.x + t.width)) : Math.max(0, a > 0 ? t.y - (e.y + e.height) : e.y - (t.y + t.height));
90
+ return (n ? f + l * .001 : l) + u * 2 + (d ? 0 : 1e5);
91
+ };
92
+ for (let e = 0; e < t.length; e++) {
93
+ let n = t[e], [r, d] = O(n);
94
+ if ((i ? (r - o) * a : (d - s) * a) <= 1e-6) continue;
95
+ let f = u(n, !0);
96
+ f < l && (l = f, c = e);
97
+ }
98
+ if (c >= 0 || !r) return c;
99
+ for (let e = 0; e < t.length; e++) {
100
+ let n = t[e], [r, d] = O(n);
101
+ if ((i ? (r - o) * a : (d - s) * a) >= -1e-6) continue;
102
+ let f = u(n, !1);
103
+ f < l && (l = f, c = e);
104
+ }
105
+ return c;
106
+ }
107
+ //#endregion
108
+ //#region src/overlays.ts
109
+ var A = class extends m {
110
+ constructor(e = {}) {
111
+ let { title: t = "", message: n = "", buttons: r = [{
112
+ label: "OK",
113
+ value: !0
114
+ }], cancelValue: a, dismissible: o = !0, width: s = 440, signal: c } = e;
115
+ super({
116
+ background: "surface",
117
+ border: !0,
118
+ shadow: !0,
119
+ padding: 24,
120
+ gap: 14,
121
+ width: s,
122
+ anchor: "center",
123
+ pointerEvents: "auto"
124
+ }), this.open = !0, this.role = "dialog", this.label = t || n, this.description = t ? n : void 0, this.cancelValue = a, this.dismissible = o, this.result = new Promise((e, t) => {
125
+ this.resolve = e, this.reject = t;
126
+ }), this.result.catch(() => {}), t && this.add(new i({
127
+ text: t,
128
+ size: "large",
129
+ id: `${this.id}-title`
130
+ })), n && this.add(new i({
131
+ text: n,
132
+ color: "textMuted",
133
+ id: `${this.id}-message`
134
+ }));
135
+ let l = new m({
136
+ direction: "row",
137
+ gap: 10,
138
+ justify: "end",
139
+ margin: [
140
+ 8,
141
+ 0,
142
+ 0,
143
+ 0
144
+ ]
145
+ });
146
+ if (r.forEach((e, t) => {
147
+ let n = new p({
148
+ text: e.label,
149
+ variant: e.variant ?? (t === 0 ? "primary" : "secondary"),
150
+ minWidth: 96
151
+ });
152
+ n.on("click", () => this.close("value" in e ? e.value : e.label)), l.add(n);
153
+ }), this.add(l), c) {
154
+ if (c.aborted) queueMicrotask(() => this.abort(c.reason));
155
+ else {
156
+ let e = () => this.abort(c.reason);
157
+ c.addEventListener("abort", e, { once: !0 }), this.removeAbort = () => c.removeEventListener("abort", e);
158
+ }
159
+ }
160
+ }
161
+ get firstButton() {
162
+ return this.children.at(-1)?.children[0];
163
+ }
164
+ close(e) {
165
+ if (!this.open) return;
166
+ this.open = !1, this.removeAbort?.();
167
+ let t = this.ui;
168
+ this.removeFromParent(), t?.dialogClosed(this), this.resolve(e);
169
+ }
170
+ abort(e) {
171
+ if (!this.open) return;
172
+ this.open = !1, this.removeAbort?.();
173
+ let t = this.ui;
174
+ this.removeFromParent(), t?.dialogClosed(this), this.reject(e ?? new DOMException("The dialog was aborted", "AbortError"));
175
+ }
176
+ command(e) {
177
+ return e === "cancel" && this.dismissible ? (this.close(this.cancelValue), !0) : !1;
178
+ }
179
+ }, j = {
180
+ info: "i",
181
+ success: "✓",
182
+ warning: "!",
183
+ danger: "×"
184
+ }, M = {
185
+ info: "Info",
186
+ success: "Done",
187
+ warning: "Warning",
188
+ danger: "Error"
189
+ }, N = class extends m {
190
+ constructor(e, t = {}) {
191
+ let n = t.kind ?? "info";
192
+ if (super({
193
+ direction: "row",
194
+ background: "raised",
195
+ border: !0,
196
+ shadow: !0,
197
+ padding: [
198
+ 8,
199
+ 14,
200
+ 8,
201
+ 8
202
+ ],
203
+ gap: 12,
204
+ align: "center",
205
+ maxWidth: 520,
206
+ pointerEvents: "auto"
207
+ }), this.fade = 0, this.closing = !1, this.kind = n, this.message = e, this.remaining = t.duration === 0 ? Infinity : t.duration ?? Math.max(4, 2 + e.length * .06), this.role = n === "danger" ? "alert" : "status", this.label = `${M[n]}: ${e}`, this.add(new i({
208
+ text: j[n],
209
+ align: "center",
210
+ size: "body",
211
+ width: 28,
212
+ height: 28,
213
+ color: n === "warning" ? "onWarning" : "onPrimary",
214
+ id: `${this.id}-badge`
215
+ })), this.add(new i({
216
+ text: e,
217
+ grow: 1,
218
+ shrink: 1
219
+ })), t.action) {
220
+ let e = t.action, n = new p({
221
+ text: e.label,
222
+ variant: "ghost",
223
+ minHeight: 36
224
+ });
225
+ n.on("click", () => {
226
+ e.onClick(), this.dismiss();
227
+ }), this.add(n);
228
+ }
229
+ }
230
+ dismiss() {
231
+ this.remaining = 0, this.closing = !0;
232
+ }
233
+ get dismissed() {
234
+ return this.closing;
235
+ }
236
+ tick(e) {
237
+ let t = super.tick(e);
238
+ !(this.hovered || this.focused || this.children.some((e) => e.focused)) && this.remaining !== Infinity && !this.closing && (this.remaining -= e, this.remaining <= 0 && (this.closing = !0));
239
+ let n = +!this.closing, r = this.ui && this.ui.motion ? e * 5 : 1;
240
+ return this.fade !== n && (this.fade = n > this.fade ? Math.min(1, this.fade + r) : Math.max(0, this.fade - r), t = !0), this.closing && this.fade <= 0 && this.removeFromParent(), t;
241
+ }
242
+ draw(e) {
243
+ super.draw(e);
244
+ let { list: t, ui: n } = e, r = this.kind === "danger" ? "danger" : this.kind === "success" ? "success" : this.kind === "warning" ? "warning" : "info", i = this.children[0].rect;
245
+ t.rect(i.x, i.y, i.width, i.height, {
246
+ fill: n.color(r),
247
+ radius: 6
248
+ });
249
+ }
250
+ get opacity() {
251
+ return this.fade;
252
+ }
253
+ }, P = class extends m {
254
+ constructor() {
255
+ super({
256
+ background: "raised",
257
+ border: !0,
258
+ shadow: !0,
259
+ padding: [8, 12],
260
+ maxWidth: 320,
261
+ anchor: "top-left",
262
+ visible: !1
263
+ }), this.role = "tooltip", this.body = new i({
264
+ text: "",
265
+ size: "small"
266
+ }), this.add(this.body), this.pointerEvents = "none";
267
+ }
268
+ get text() {
269
+ return this.body.text;
270
+ }
271
+ set text(e) {
272
+ this.body.text = e;
273
+ }
274
+ }, F = {
275
+ up: "ui.up",
276
+ down: "ui.down",
277
+ left: "ui.left",
278
+ right: "ui.right",
279
+ accept: "ui.accept",
280
+ cancel: "ui.cancel",
281
+ next: "ui.next",
282
+ previous: "ui.previous"
283
+ };
284
+ function I({ keyboard: e = !1, gamepad: t = !0, pad: n = 0 } = {}) {
285
+ let r = (e) => ({
286
+ type: "gamepad-button",
287
+ index: e,
288
+ gamepad: n
289
+ }), i = (e, t) => ({
290
+ type: "gamepad-axis",
291
+ index: e,
292
+ gamepad: n,
293
+ scale: t
294
+ }), a = (e) => ({
295
+ type: "key",
296
+ code: e
297
+ }), o = (n, r) => [...e ? n.map(a) : [], ...t ? r : []], s = {
298
+ [F.up]: o(["ArrowUp"], [r(12), i(1, -1)]),
299
+ [F.down]: o(["ArrowDown"], [r(13), i(1, 1)]),
300
+ [F.left]: o(["ArrowLeft"], [r(14), i(0, -1)]),
301
+ [F.right]: o(["ArrowRight"], [r(15), i(0, 1)]),
302
+ [F.accept]: o(["Enter", "Space"], [r(0)]),
303
+ [F.cancel]: o(["Escape"], [r(1)]),
304
+ [F.next]: o(["Tab"], [r(5)]),
305
+ [F.previous]: o([], [r(4)])
306
+ };
307
+ for (let e of Object.keys(s)) s[e].length || delete s[e];
308
+ return s;
309
+ }
310
+ var ee = .4, te = .1, ne = .5, re = .12, L = [
311
+ "up",
312
+ "down",
313
+ "left",
314
+ "right"
315
+ ], R = class {
316
+ constructor(e) {
317
+ this.ui = e, this.focused = null, this.inputMode = "pointer", this.captureKeys = !1, this.wrapFocus = !0, this.hoverChain = [], this.pressed = null, this.captured = null, this.pointer = -1, this.flash = null, this.restore = null, this.tipTarget = null, this.tipTime = 0, this.tipDismissed = !1, this.tipShown = !1;
318
+ }
319
+ scope() {
320
+ let e = this.ui.modalLayer.children;
321
+ for (let t = e.length - 1; t >= 0; t--) {
322
+ let n = e[t];
323
+ if (n instanceof A && n.open) return [n];
324
+ }
325
+ return [this.ui.root, this.ui.toastLayer];
326
+ }
327
+ focusables() {
328
+ this.ui.layout();
329
+ let e = [], t = (n) => {
330
+ if (n.visible && n.enabled) {
331
+ n.focusable && n.rect.width > 0 && n.rect.height > 0 && e.push(n);
332
+ for (let e of n.children) t(e);
333
+ }
334
+ };
335
+ for (let e of this.scope()) t(e);
336
+ return e;
337
+ }
338
+ contains(e, t) {
339
+ for (let n = t; n; n = n.parent) if (n === e) return !0;
340
+ return !1;
341
+ }
342
+ setMode(e) {
343
+ e !== this.inputMode && (this.inputMode = e, this.focused && (this.focused.focusVisible = e === "keyboard" || e === "gamepad"), this.ui.invalidate(!1));
344
+ }
345
+ focus(e, t = this.inputMode) {
346
+ if (e && (!e.focusable || !e.shown || !e.active || e.ui !== this.ui)) return !1;
347
+ let n = this.focused;
348
+ return this.inputMode = t, n === e ? (e && (e.focusVisible = t === "keyboard" || t === "gamepad"), !0) : (n && (n.focused = !1, n.focusVisible = !1, n.emit("blur")), this.focused = e, e && (e.focused = !0, e.focusVisible = t === "keyboard" || t === "gamepad", e.scrollIntoView(), e.emit("focus")), this.ui.invalidate(!1), this.ui.focusChanged(), !0);
349
+ }
350
+ first(e = "start") {
351
+ let t = this.focusables();
352
+ return (e === "start" ? t[0] : t.at(-1)) ?? null;
353
+ }
354
+ navigate(e, t = "keyboard") {
355
+ this.setMode(t);
356
+ let n = this.focused;
357
+ if (!n || !this.focusables().includes(n)) {
358
+ let n = this.first(e === "up" || e === "left" ? "end" : "start");
359
+ return n ? this.focus(n, t) : !1;
360
+ }
361
+ if (n.focusVisible = !0, n.command(e)) return !0;
362
+ let r = this.focusables().filter((e) => e !== n), i = k(n.rect, r.map((e) => e.rect), e, this.wrapFocus);
363
+ return i >= 0 && this.focus(r[i], t);
364
+ }
365
+ step(e, t = "keyboard") {
366
+ this.setMode(t);
367
+ let n = this.focusables();
368
+ if (!n.length) return !1;
369
+ let r = this.focused ? n.indexOf(this.focused) : -1;
370
+ if (r < 0) return this.focus(e > 0 ? n[0] : n[n.length - 1], t);
371
+ let i = r + e;
372
+ return i >= 0 && i < n.length ? this.focus(n[i], t) : this.scope()[0] === this.ui.root ? (this.focus(null, t), !1) : this.focus(n[(i + n.length) % n.length], t);
373
+ }
374
+ accept(e = "keyboard") {
375
+ this.setMode(e);
376
+ let t = this.focused;
377
+ if (!t || !this.focusables().includes(t)) {
378
+ let t = this.first();
379
+ return t ? this.focus(t, e) : !1;
380
+ }
381
+ return t.pressed = !0, this.flash = {
382
+ widget: t,
383
+ left: re
384
+ }, t.activate(), this.ui.invalidate(!1), !0;
385
+ }
386
+ cancel(e = "keyboard") {
387
+ if (this.setMode(e), this.tipShown && !this.tipDismissed) return this.tipDismissed = !0, this.hideTip(), !0;
388
+ if (this.focused?.command("cancel")) return !0;
389
+ let t = this.scope()[0];
390
+ return t instanceof A && t.command("cancel");
391
+ }
392
+ execute(e, t = "keyboard") {
393
+ switch (e) {
394
+ case "up":
395
+ case "down":
396
+ case "left":
397
+ case "right": return this.navigate(e, t);
398
+ case "accept": return this.accept(t);
399
+ case "cancel": return this.cancel(t);
400
+ case "next": return this.step(1, t);
401
+ case "previous": return this.step(-1, t);
402
+ }
403
+ }
404
+ wantsKeys() {
405
+ return this.captureKeys || this.focused !== null || this.scope()[0] !== this.ui.root;
406
+ }
407
+ hitTest(e, t) {
408
+ let n = this.ui;
409
+ return n.toastLayer.hitTest(e, t) || (n.modalLayer.children.length ? n.modalLayer.hitTest(e, t) : n.root.hitTest(e, t));
410
+ }
411
+ setHover(e) {
412
+ let t = [];
413
+ for (let n = e; n; n = n.parent) t.push(n);
414
+ for (let e of this.hoverChain) t.includes(e) || (e.hovered = !1, e.emit("hover", { value: !1 }));
415
+ for (let e of t) this.hoverChain.includes(e) || (e.hovered = !0, e.emit("hover", { value: !0 }));
416
+ (t.length !== this.hoverChain.length || t.some((e, t) => e !== this.hoverChain[t])) && this.ui.invalidate(!1), this.hoverChain = t;
417
+ }
418
+ toUnits(e, t) {
419
+ let n = this.ui.cssScale;
420
+ return [e / n, t / n];
421
+ }
422
+ pointerMove(e, t, n = {}) {
423
+ let [r, i] = this.toUnits(e, t), a = n.type === "touch";
424
+ if (this.captured) return (n.id ?? 0) === this.pointer && this.captured.pointerMove(r, i), !0;
425
+ if (a) return !1;
426
+ this.setMode("pointer");
427
+ let o = this.hitTest(r, i);
428
+ return this.setHover(o), o !== null;
429
+ }
430
+ pointerDown(e, t, n = {}) {
431
+ if (this.captured || this.pressed) return !0;
432
+ let [r, i] = this.toUnits(e, t), a = n.type === "touch";
433
+ this.setMode(a ? "touch" : "pointer");
434
+ let o = this.hitTest(r, i);
435
+ if (a || this.setHover(o), !o) return this.focus(null, this.inputMode), !1;
436
+ if (o === this.ui.modalLayer) {
437
+ let e = this.scope()[0];
438
+ return e instanceof A && e.dismissible && (n.button ?? 0) === 0 && e.command("cancel"), !0;
439
+ }
440
+ if ((n.button ?? 0) !== 0) return !0;
441
+ this.pointer = n.id ?? 0;
442
+ let s = o;
443
+ for (; s && !s.focusable;) s = s.parent;
444
+ return this.focus(s && s.active ? s : null, this.inputMode), o.active && (o.pressed = !0, this.pressed = o, o.pointerDown(r, i) && (this.captured = o), this.ui.invalidate(!1)), !0;
445
+ }
446
+ pointerUp(e, t, n = {}) {
447
+ if ((n.id ?? 0) !== this.pointer || !this.pressed && !this.captured) return !1;
448
+ let [r, i] = this.toUnits(e, t), a = this.captured, o = this.pressed;
449
+ return this.captured = this.pressed = null, this.pointer = -1, o && (o.pressed = !1), a ? a.pointerUp(r, i) : o && o.active && this.hitTest(r, i) === o && o.activate(), n.type === "touch" && this.setHover(null), this.ui.invalidate(!1), !0;
450
+ }
451
+ pointerCancel() {
452
+ let e = this.captured, t = this.pressed;
453
+ this.captured = this.pressed = null, this.pointer = -1, t && (t.pressed = !1), e?.pointerUp(-1e9, -1e9), this.setHover(null), this.ui.invalidate(!1);
454
+ }
455
+ leave() {
456
+ this.captured || this.setHover(null);
457
+ }
458
+ wheel(e, t, n) {
459
+ let [r, i] = this.toUnits(e, t);
460
+ for (let e = this.hitTest(r, i); e; e = e.parent) if (e.wheel(n / this.ui.cssScale)) return !0;
461
+ return !1;
462
+ }
463
+ wantsPointer(e, t) {
464
+ if (this.captured || this.pressed) return !0;
465
+ let [n, r] = this.toUnits(e, t);
466
+ return this.hitTest(n, r) !== null;
467
+ }
468
+ widgetGone(e) {
469
+ this.focused && this.contains(e, this.focused) && this.focus(null), this.restore && this.contains(e, this.restore) && (this.restore = null), this.pressed && this.contains(e, this.pressed) && (this.pressed = null), this.captured && this.contains(e, this.captured) && (this.captured = null, this.pointer = -1), this.hoverChain.some((t) => this.contains(e, t)) && this.setHover(null), this.tipTarget && this.contains(e, this.tipTarget) && (this.tipTarget = null), this.flash && this.contains(e, this.flash.widget) && (this.flash = null);
470
+ }
471
+ dialogOpened(e) {
472
+ this.restore = this.focused;
473
+ let t = e.firstButton;
474
+ t && this.focus(t, this.inputMode);
475
+ }
476
+ dialogClosed() {
477
+ let e = this.restore;
478
+ this.restore = null, e && e.shown && e.active && e.ui === this.ui && this.focus(e);
479
+ }
480
+ bindInput(e) {
481
+ if (!e || typeof e.state != "function") throw t("bindInput takes an Input from @margoengine/input made with uiActions()", "ui");
482
+ let n = [];
483
+ for (let r of Object.keys(F)) try {
484
+ e.state(F[r]), n.push(r);
485
+ } catch {
486
+ if (r === "up" || r === "down" || r === "left" || r === "right" || r === "accept") throw t(`bindInput: the Input has no '${F[r]}' action; create it with new Input(uiActions())`, "ui");
487
+ }
488
+ this.binding = {
489
+ input: e,
490
+ commands: n,
491
+ held: /* @__PURE__ */ new Map()
492
+ };
493
+ }
494
+ unbindInput() {
495
+ this.binding = void 0;
496
+ }
497
+ poll(e) {
498
+ let t = this.binding;
499
+ if (t) for (let n of t.commands) {
500
+ let r = t.input.state(F[n]).value > .5, i = t.held.get(n);
501
+ if (!r) t.held.delete(n);
502
+ else if (i === void 0) t.held.set(n, 0), this.execute(n, "gamepad");
503
+ else if (L.includes(n)) {
504
+ let r = i + e;
505
+ r >= ee ? (t.held.set(n, r - te), this.execute(n, "gamepad")) : t.held.set(n, r);
506
+ }
507
+ }
508
+ }
509
+ tick(e) {
510
+ this.poll(e), this.flash && (this.flash.left -= e, this.flash.left <= 0 && (this.flash.widget.pressed = this.flash.widget === this.pressed, this.flash = null, this.ui.invalidate(!1))), this.tickTip(e);
511
+ }
512
+ hideTip() {
513
+ this.tipShown = !1;
514
+ let e = this.ui.tooltipWidget;
515
+ e.visible &&= !1;
516
+ }
517
+ tipSource() {
518
+ let e = this.focused;
519
+ if (e && e.focusVisible && e.tooltip) return {
520
+ widget: e,
521
+ delay: .15
522
+ };
523
+ if (this.captured) return null;
524
+ for (let e of [...this.hoverChain].reverse()) if (e.tooltip) return {
525
+ widget: e,
526
+ delay: ne
527
+ };
528
+ return null;
529
+ }
530
+ tickTip(e) {
531
+ let t = this.tipSource();
532
+ if (t?.widget !== this.tipTarget && (this.tipTarget = t?.widget ?? null, this.tipTime = 0, this.tipDismissed = !1, this.hideTip()), !t || this.tipDismissed) return;
533
+ this.tipTime += e;
534
+ let n = t.widget.tooltip;
535
+ if (this.tipTime >= t.delay) {
536
+ let e = this.ui.tooltipWidget;
537
+ e.text !== n && (e.text = n), e.visible ||= !0, this.tipShown = !0;
538
+ }
539
+ }
540
+ cancelTip() {
541
+ this.tipDismissed = !0, this.hideTip();
542
+ }
543
+ placeTip() {
544
+ let e = this.ui.tooltipWidget, t = this.tipTarget;
545
+ if (!e.visible || !t) return;
546
+ let [n, i, a, o] = this.ui.safeArea, s = this.ui.width, c = this.ui.height, l = e.rect, u = t.rect, d = Math.max(o, Math.min(u.x, s - i - l.width)), f = u.y + u.height + 8, p = f + l.height <= c - a ? f : Math.max(n, u.y - l.height - 8);
547
+ r(e, d - l.x, p - l.y);
548
+ }
549
+ }, z = Object.freeze({
550
+ dark: B({
551
+ name: "dark",
552
+ colors: {
553
+ background: "#0b0e13",
554
+ surface: "#1a2029",
555
+ surfaceRaised: "#2a3340",
556
+ surfaceSunken: "#10151c",
557
+ border: "#7f8ea3",
558
+ text: "#f1f4f8",
559
+ textMuted: "#b4bfce",
560
+ primary: "#56b4e9",
561
+ onPrimary: "#04141f",
562
+ accent: "#e69f00",
563
+ onAccent: "#1c1200",
564
+ success: "#009e73",
565
+ onSuccess: "#03120d",
566
+ warning: "#f0e442",
567
+ onWarning: "#1a1700",
568
+ danger: "#d55e00",
569
+ onDanger: "#150700",
570
+ info: "#0072b2",
571
+ onInfo: "#ffffff",
572
+ focus: "#f0e442",
573
+ disabled: "#222a35",
574
+ onDisabled: "#7d8999",
575
+ scrim: "#000000b8",
576
+ shadow: "#00000099"
577
+ },
578
+ radius: 8,
579
+ borderWidth: 1,
580
+ focusWidth: 3,
581
+ spacing: 8,
582
+ fontSize: {
583
+ small: 14,
584
+ body: 18,
585
+ large: 22,
586
+ title: 32,
587
+ display: 48
588
+ },
589
+ shadowBlur: 16,
590
+ transition: .12
591
+ }),
592
+ light: B({
593
+ name: "light",
594
+ colors: {
595
+ background: "#e8ecf1",
596
+ surface: "#ffffff",
597
+ surfaceRaised: "#e9eef4",
598
+ surfaceSunken: "#d7dee7",
599
+ border: "#677588",
600
+ text: "#11151b",
601
+ textMuted: "#465163",
602
+ primary: "#0072b2",
603
+ onPrimary: "#ffffff",
604
+ accent: "#9b3d78",
605
+ onAccent: "#ffffff",
606
+ success: "#007a59",
607
+ onSuccess: "#ffffff",
608
+ warning: "#f0e442",
609
+ onWarning: "#1a1700",
610
+ danger: "#b34a00",
611
+ onDanger: "#ffffff",
612
+ info: "#005a8e",
613
+ onInfo: "#ffffff",
614
+ focus: "#7a2bd9",
615
+ disabled: "#dde3ea",
616
+ onDisabled: "#6c7889",
617
+ scrim: "#0b0e13a6",
618
+ shadow: "#0b0e1340"
619
+ },
620
+ radius: 8,
621
+ borderWidth: 1,
622
+ focusWidth: 3,
623
+ spacing: 8,
624
+ fontSize: {
625
+ small: 14,
626
+ body: 18,
627
+ large: 22,
628
+ title: 32,
629
+ display: 48
630
+ },
631
+ shadowBlur: 16,
632
+ transition: .12
633
+ }),
634
+ highContrast: B({
635
+ name: "high-contrast",
636
+ colors: {
637
+ background: "#000000",
638
+ surface: "#000000",
639
+ surfaceRaised: "#000000",
640
+ surfaceSunken: "#000000",
641
+ border: "#ffffff",
642
+ text: "#ffffff",
643
+ textMuted: "#ffffff",
644
+ primary: "#ffff00",
645
+ onPrimary: "#000000",
646
+ accent: "#56b4e9",
647
+ onAccent: "#000000",
648
+ success: "#4de0b4",
649
+ onSuccess: "#000000",
650
+ warning: "#ffff00",
651
+ onWarning: "#000000",
652
+ danger: "#ff9a5c",
653
+ onDanger: "#000000",
654
+ info: "#7cc8f2",
655
+ onInfo: "#000000",
656
+ focus: "#00ffff",
657
+ disabled: "#000000",
658
+ onDisabled: "#bfbfbf",
659
+ scrim: "#000000e6",
660
+ shadow: "#00000000"
661
+ },
662
+ radius: 4,
663
+ borderWidth: 2,
664
+ focusWidth: 4,
665
+ spacing: 8,
666
+ fontSize: {
667
+ small: 16,
668
+ body: 20,
669
+ large: 24,
670
+ title: 34,
671
+ display: 50
672
+ },
673
+ shadowBlur: 0,
674
+ transition: 0
675
+ })
676
+ });
677
+ function B(e) {
678
+ for (let t of Object.values(e)) t && typeof t == "object" && B(t);
679
+ return Object.freeze(e);
680
+ }
681
+ var V = [
682
+ "radius",
683
+ "borderWidth",
684
+ "focusWidth",
685
+ "spacing",
686
+ "shadowBlur",
687
+ "transition"
688
+ ];
689
+ function ie(n = {}, r = z.dark) {
690
+ let i = /* @__PURE__ */ new Set([
691
+ "name",
692
+ "colors",
693
+ "fontSize",
694
+ ...V
695
+ ]);
696
+ for (let e of Object.keys(n)) if (!i.has(e)) throw t(`Unknown theme field '${e}'`, "ui");
697
+ let a = {
698
+ ...r.colors,
699
+ ...n.colors
700
+ };
701
+ for (let [e, n] of Object.entries(a)) {
702
+ if (!(e in r.colors)) throw t(`Unknown theme colour '${e}'`, "ui");
703
+ u(n);
704
+ }
705
+ let o = {
706
+ ...r.fontSize,
707
+ ...n.fontSize
708
+ };
709
+ for (let [n, i] of Object.entries(o)) {
710
+ if (!(n in r.fontSize)) throw t(`Unknown theme font size '${n}'`, "ui");
711
+ if (typeof i != "number" || !Number.isFinite(i) || i <= 0) throw e(`Theme font size '${n}' must be a number above 0`, "ui");
712
+ }
713
+ let s = {
714
+ ...r,
715
+ name: n.name ?? r.name,
716
+ colors: a,
717
+ fontSize: o
718
+ };
719
+ for (let t of V) {
720
+ let i = n[t] ?? r[t];
721
+ if (typeof i != "number" || !Number.isFinite(i) || i < 0) throw e(`Theme ${t} must be a number of at least 0`, "ui");
722
+ s[t] = i;
723
+ }
724
+ return B(s);
725
+ }
726
+ function H() {
727
+ let e = (e) => typeof matchMedia == "function" && matchMedia(e).matches;
728
+ return e("(prefers-contrast: more)") || e("(forced-colors: active)") ? z.highContrast : e("(prefers-color-scheme: light)") ? z.light : z.dark;
729
+ }
730
+ //#endregion
731
+ //#region src/ui.ts
732
+ function U(t, n, r = 1) {
733
+ if (typeof r == "number") {
734
+ if (!Number.isFinite(r) || r <= 0) throw e("scale must be a number above 0", "ui");
735
+ return r;
736
+ }
737
+ let [i, a] = r.reference ?? [], o = r.match ?? .5;
738
+ if (!(i > 0) || !(a > 0) || !Number.isFinite(i) || !Number.isFinite(a)) throw e("scale.reference is [width, height] above 0", "ui");
739
+ if (!(o >= 0 && o <= 1)) throw e("scale.match is between 0 and 1", "ui");
740
+ if (!(t > 0) || !(n > 0)) return 1;
741
+ let s = (t / i) ** (1 - o) * (n / a) ** o;
742
+ return Math.max(r.min ?? .5, Math.min(r.max ?? 4, s));
743
+ }
744
+ function W(e = globalThis.document) {
745
+ let t = e?.documentElement, n = e?.defaultView;
746
+ if (!t || !n) return 1;
747
+ let r = Number.parseFloat(n.getComputedStyle(t).fontSize);
748
+ return Number.isFinite(r) && r > 0 ? Math.max(.5, Math.min(3, r / 16)) : 1;
749
+ }
750
+ function G(e = globalThis.document) {
751
+ if (!e?.body) return [
752
+ 0,
753
+ 0,
754
+ 0,
755
+ 0
756
+ ];
757
+ let t = e.createElement("div");
758
+ t.style.cssText = "position:fixed;visibility:hidden;pointer-events:none;padding:env(safe-area-inset-top,0px) env(safe-area-inset-right,0px) env(safe-area-inset-bottom,0px) env(safe-area-inset-left,0px)", e.body.append(t);
759
+ let n = e.defaultView.getComputedStyle(t), r = (e) => Number.parseFloat(e) || 0, i = [
760
+ r(n.paddingTop),
761
+ r(n.paddingRight),
762
+ r(n.paddingBottom),
763
+ r(n.paddingLeft)
764
+ ];
765
+ return t.remove(), i;
766
+ }
767
+ var ae = [
768
+ "auto",
769
+ "ltr",
770
+ "rtl"
771
+ ], oe = 5, K = 1.25, se = {
772
+ start: "left",
773
+ center: "center",
774
+ end: "right"
775
+ }, ce = {
776
+ start: "right",
777
+ center: "center",
778
+ end: "left"
779
+ }, le = class {
780
+ constructor(e = {}) {
781
+ this.list = new D(), this.cache = /* @__PURE__ */ new Map(), this.colors = /* @__PURE__ */ new Map(), this._textScale = 1, this._ligatures = !1, this._direction = "auto", this.resolved = 1, this.layoutDirty = !0, this.drawDirty = !0, this.fontRevision = 0, this.disposed = !1, this.changeListeners = /* @__PURE__ */ new Set(), this.focusListeners = /* @__PURE__ */ new Set(), this.announcers = /* @__PURE__ */ new Set(), this.mirrorRevision = 0;
782
+ let { width: n = 1280, height: r = 720, pixelRatio: i = 1, scale: a = 1, textScale: o = 1, safeArea: s = "auto" } = e;
783
+ if (this._theme = e.theme ?? H(), !this._theme || typeof this._theme != "object" || !this._theme.colors) throw t("theme must come from createTheme() or themes", "ui");
784
+ if (e.measurer && (typeof e.measurer.advance != "function" || typeof e.measurer.metrics != "function")) throw t("measurer needs advance(), kerning() and metrics()", "ui");
785
+ this.font = e.font instanceof x ? e.font : new x({
786
+ family: "system-ui, sans-serif",
787
+ weight: 500,
788
+ ...e.font
789
+ }), this.measurer = e.measurer ?? this.font, this._motion = !(e.reducedMotion ?? (typeof matchMedia == "function" && matchMedia("(prefers-reduced-motion: reduce)").matches)), this._scale = a, U(1, 1, a), this._width = this._height = 0, this._pixelRatio = 1, this._safeArea = s, this.checkSize(n, r, i), this._width = n, this._height = r, this._pixelRatio = i, this.setTextScale(o === "auto" ? W() : o), this.setShaping(e.ligatures ?? !1, e.direction ?? "auto"), this.root = new m({
790
+ id: "ui-root",
791
+ direction: "stack",
792
+ radius: 0
793
+ }), this.modalLayer = new m({
794
+ id: "ui-modal",
795
+ direction: "stack",
796
+ radius: 0
797
+ }), this.toastLayer = new m({
798
+ id: "ui-toasts",
799
+ direction: "stack",
800
+ radius: 0
801
+ }), this.tooltipLayer = new m({
802
+ id: "ui-tooltip",
803
+ direction: "stack",
804
+ radius: 0
805
+ }), this.toastStack = new m({
806
+ id: "ui-toast-stack",
807
+ anchor: "bottom",
808
+ gap: 8,
809
+ margin: 24,
810
+ align: "center"
811
+ }), this.tooltip = new P(), this.toastLayer.add(this.toastStack), this.tooltipLayer.add(this.tooltip), this.layers = [
812
+ this.root,
813
+ this.modalLayer,
814
+ this.toastLayer,
815
+ this.tooltipLayer
816
+ ];
817
+ for (let e of this.layers) e.attachTo(this);
818
+ this.interaction = new R(this), this.context = {
819
+ list: this.list,
820
+ ui: this,
821
+ theme: this._theme
822
+ }, this.resolved = U(n, r, a), this.ready = this.measurer === this.font ? this.font.load() : Promise.resolve();
823
+ }
824
+ checkSize(t, n, r) {
825
+ if (![t, n].every((e) => Number.isFinite(e) && e > 0 && e <= 65536)) throw e("Ui width and height must be numbers from 0 to 65536", "ui");
826
+ if (!Number.isFinite(r) || r <= 0 || r > 16) throw e("pixelRatio must be a number above 0 and up to 16", "ui");
827
+ }
828
+ get theme() {
829
+ return this._theme;
830
+ }
831
+ set theme(e) {
832
+ if (!e || typeof e != "object" || !e.colors) throw t("theme must come from createTheme() or themes", "ui");
833
+ this._theme = e, this.context.theme = e, this.colors.clear(), this.invalidate(!0);
834
+ }
835
+ get textScale() {
836
+ return this._textScale;
837
+ }
838
+ set textScale(e) {
839
+ this.setTextScale(e), this.invalidate(!0);
840
+ }
841
+ setTextScale(t) {
842
+ if (!Number.isFinite(t) || t < .5 || t > 3) throw e("textScale must be a number from 0.5 to 3", "ui");
843
+ this._textScale = t, this.cache.clear();
844
+ }
845
+ get ligatures() {
846
+ return this._ligatures;
847
+ }
848
+ set ligatures(e) {
849
+ this.setShaping(e, this._direction), this.invalidate(!0);
850
+ }
851
+ get direction() {
852
+ return this._direction;
853
+ }
854
+ set direction(e) {
855
+ this.setShaping(this._ligatures, e), this.invalidate(!0);
856
+ }
857
+ setShaping(e, n) {
858
+ if (typeof e != "boolean") throw t("ligatures must be true or false", "ui");
859
+ if (!ae.includes(n)) throw t("direction must be 'auto', 'ltr' or 'rtl'", "ui");
860
+ this._ligatures = e, this._direction = n, this.cache.clear();
861
+ }
862
+ textDirection(e) {
863
+ if (this._direction !== "auto") return this._direction;
864
+ for (let { cluster: t } of C(e)) {
865
+ let e = S(t);
866
+ if (e !== "N") return e === "R" ? "rtl" : "ltr";
867
+ }
868
+ return "ltr";
869
+ }
870
+ get motion() {
871
+ return this._motion;
872
+ }
873
+ set motion(e) {
874
+ this._motion = !!e;
875
+ }
876
+ get width() {
877
+ return this._width / this.resolved;
878
+ }
879
+ get height() {
880
+ return this._height / this.resolved;
881
+ }
882
+ get scale() {
883
+ return this._pixelRatio * this.resolved;
884
+ }
885
+ get cssScale() {
886
+ return this.resolved;
887
+ }
888
+ get pixelRatio() {
889
+ return this._pixelRatio;
890
+ }
891
+ get safeArea() {
892
+ return (this._safeArea === "auto" ? G() : this._safeArea).map((e) => e / this.resolved);
893
+ }
894
+ setSafeArea(e) {
895
+ e !== "auto" && h(e), this._safeArea = e, this.invalidate(!0);
896
+ }
897
+ resize(e, t, n = this._pixelRatio) {
898
+ this.checkSize(e, t, n), (e !== this._width || t !== this._height || n !== this._pixelRatio) && (this._width = e, this._height = t, this._pixelRatio = n, this.resolved = U(e, t, this._scale), this.invalidate(!0));
899
+ }
900
+ setScale(e) {
901
+ U(1, 1, e), this._scale = e, this.resolved = U(this._width, this._height, e), this.invalidate(!0);
902
+ }
903
+ add(...e) {
904
+ return this.root.add(...e), this;
905
+ }
906
+ remove(e) {
907
+ return this.root.remove(e), this;
908
+ }
909
+ find(e) {
910
+ for (let t of this.layers) {
911
+ let n = t.find(e);
912
+ if (n) return n;
913
+ }
914
+ }
915
+ walk(e) {
916
+ for (let t of this.layers) t.walk(e);
917
+ }
918
+ get focused() {
919
+ return this.interaction.focused;
920
+ }
921
+ get inputMode() {
922
+ return this.interaction.inputMode;
923
+ }
924
+ get captureKeys() {
925
+ return this.interaction.captureKeys;
926
+ }
927
+ set captureKeys(e) {
928
+ this.interaction.captureKeys = !!e;
929
+ }
930
+ get wrapFocus() {
931
+ return this.interaction.wrapFocus;
932
+ }
933
+ set wrapFocus(e) {
934
+ this.interaction.wrapFocus = !!e;
935
+ }
936
+ focusables() {
937
+ return this.layout(), this.interaction.focusables();
938
+ }
939
+ focus(e, t = this.inputMode) {
940
+ return this.live(), this.interaction.focus(e, t);
941
+ }
942
+ navigate(e, t = "keyboard") {
943
+ return this.live(), this.layout(), this.interaction.navigate(e, t);
944
+ }
945
+ tab(e = !0, t = "keyboard") {
946
+ return this.live(), this.layout(), this.interaction.step(e ? 1 : -1, t);
947
+ }
948
+ accept(e = "keyboard") {
949
+ return this.live(), this.layout(), this.interaction.accept(e);
950
+ }
951
+ cancel(e = "keyboard") {
952
+ return this.live(), this.interaction.cancel(e);
953
+ }
954
+ execute(e, t = "keyboard") {
955
+ return this.live(), this.layout(), this.interaction.execute(e, t);
956
+ }
957
+ wantsKeys() {
958
+ return this.interaction.wantsKeys();
959
+ }
960
+ pointerMove(e, t, n) {
961
+ return this.live(), this.layout(), this.interaction.pointerMove(e, t, n);
962
+ }
963
+ pointerDown(e, t, n) {
964
+ return this.live(), this.layout(), this.interaction.pointerDown(e, t, n);
965
+ }
966
+ pointerUp(e, t, n) {
967
+ return this.live(), this.interaction.pointerUp(e, t, n);
968
+ }
969
+ pointerCancel() {
970
+ this.interaction.pointerCancel();
971
+ }
972
+ pointerLeave() {
973
+ this.interaction.leave();
974
+ }
975
+ wheel(e, t, n) {
976
+ return this.live(), this.layout(), this.interaction.wheel(e, t, n);
977
+ }
978
+ wantsPointer(e, t) {
979
+ return this.layout(), this.interaction.wantsPointer(e, t);
980
+ }
981
+ hitTest(e, t) {
982
+ return this.layout(), this.interaction.hitTest(e / this.cssScale, t / this.cssScale);
983
+ }
984
+ bindInput(e) {
985
+ return this.live(), this.interaction.bindInput(e), this;
986
+ }
987
+ unbindInput() {
988
+ return this.interaction.unbindInput(), this;
989
+ }
990
+ color(e) {
991
+ let t = this._theme.colors[e] ?? e, n = this.colors.get(t);
992
+ return n || this.colors.set(t, n = u(t)), n;
993
+ }
994
+ fontSize(t) {
995
+ let n = typeof t == "number" ? t : this._theme.fontSize[t];
996
+ if (!(n > 0)) throw e(`Unknown text size '${String(t)}'; use small, body, large, title, display or a number`, "ui");
997
+ return n * this._textScale;
998
+ }
999
+ textLayout(e, t, n = Infinity, r = K, i = "start") {
1000
+ let a = this.fontSize(t);
1001
+ this.font.revision !== this.fontRevision && (this.fontRevision = this.font.revision, this.cache.clear());
1002
+ let o = `${a}|${n}|${r}|${i}|${e}`, s = this.cache.get(o);
1003
+ return s || (this.cache.size > 4e3 && this.cache.clear(), s = w(e, {
1004
+ fontSize: a,
1005
+ lineHeight: r,
1006
+ maxWidth: Math.max(0, n),
1007
+ textAlign: (this.textDirection(e) === "rtl" ? ce : se)[i],
1008
+ direction: this._direction,
1009
+ ligatures: this._ligatures,
1010
+ whiteSpace: "normal",
1011
+ anchorX: "left",
1012
+ anchorY: "top"
1013
+ }, this.measurer), this.cache.set(o, s)), s;
1014
+ }
1015
+ measureText(e, t, n = Infinity, r = K) {
1016
+ let i = this.textLayout(e, t, n, r);
1017
+ return {
1018
+ width: i.bounds.right - i.bounds.left,
1019
+ height: i.lineHeight * i.lines.length
1020
+ };
1021
+ }
1022
+ invalidate(e = !0) {
1023
+ e && (this.layoutDirty = !0), this.drawDirty = !0, this.mirrorRevision++;
1024
+ for (let e of this.changeListeners) e();
1025
+ }
1026
+ touchMirror() {
1027
+ this.mirrorRevision++, this.drawDirty = !0;
1028
+ for (let e of this.changeListeners) e();
1029
+ }
1030
+ onChange(e) {
1031
+ return this.changeListeners.add(e), () => this.changeListeners.delete(e);
1032
+ }
1033
+ onFocus(e) {
1034
+ return this.focusListeners.add(e), () => this.focusListeners.delete(e);
1035
+ }
1036
+ focusChanged() {
1037
+ for (let e of this.focusListeners) e();
1038
+ }
1039
+ announce(e, n = "polite") {
1040
+ if (typeof e != "string" || !e) throw t("announce(message) takes a non-empty string", "ui");
1041
+ for (let t of this.announcers) t(e, n);
1042
+ }
1043
+ onAnnounce(e) {
1044
+ return this.announcers.add(e), () => this.announcers.delete(e);
1045
+ }
1046
+ widgetAdded(e) {}
1047
+ widgetRemoved(e) {
1048
+ this.interaction.widgetGone(e);
1049
+ }
1050
+ widgetHidden(e) {
1051
+ this.interaction.widgetGone(e);
1052
+ }
1053
+ cancelTooltip() {
1054
+ this.interaction.cancelTip();
1055
+ }
1056
+ dialogClosed(e) {
1057
+ let t = this.modalLayer.children.length > 0;
1058
+ this.modalLayer.background = t ? "scrim" : "none", this.modalLayer.pointerEvents = t ? "auto" : "none", this.interaction.dialogClosed();
1059
+ }
1060
+ layout() {
1061
+ if (!this.layoutDirty) return;
1062
+ this.layoutDirty = !1;
1063
+ let e = this.width, t = this.height, n = this.safeArea;
1064
+ for (let r of this.layers) r.setStyleQuiet({ padding: n }), o(r, {
1065
+ x: 0,
1066
+ y: 0,
1067
+ width: e,
1068
+ height: t
1069
+ });
1070
+ this.interaction.placeTip(), this.drawDirty = !0;
1071
+ }
1072
+ update(t = 0) {
1073
+ if (this.disposed) throw n("disposed", "Ui is disposed; create a new Ui", "ui");
1074
+ if (!Number.isFinite(t) || t < 0) throw e("dt must be a finite number of seconds, 0 or more", "ui");
1075
+ this.font.revision !== this.fontRevision && (this.fontRevision = this.font.revision, this.cache.clear(), this.layoutDirty = !0), t = Math.min(t, .1), this.interaction.tick(t), this.layout();
1076
+ let r = !1;
1077
+ for (let e of this.layers) e.walk((e) => r = e.tick(t) || r);
1078
+ r && (this.drawDirty = !0), this.layoutDirty && this.layout();
1079
+ }
1080
+ draw() {
1081
+ if (this.layout(), this.drawDirty || this.list.scale !== this.scale) {
1082
+ this.drawDirty = !1, this.list.clear(this.scale);
1083
+ for (let e of this.layers) this.drawWidget(e);
1084
+ }
1085
+ return this.list;
1086
+ }
1087
+ drawWidget(e) {
1088
+ if (!e.visible) return;
1089
+ let t = this.list, n = t.alpha;
1090
+ t.alpha = n * e.opacity, e.draw(this.context);
1091
+ let r = e.style.overflow && e.style.overflow !== "visible";
1092
+ r && t.pushClip(e.rect.x, e.rect.y, e.rect.width, e.rect.height);
1093
+ for (let t of e.children) this.drawWidget(t);
1094
+ r && t.popClip(), e.drawAfter(this.context), t.alpha = n;
1095
+ }
1096
+ dialog(e = {}) {
1097
+ this.live();
1098
+ let t = new A(e);
1099
+ return this.modalLayer.add(t), this.dialogOpened(t), t.result;
1100
+ }
1101
+ dialogOpened(e) {
1102
+ this.modalLayer.background = "scrim", this.modalLayer.pointerEvents = "auto", this.interaction.dialogOpened(e);
1103
+ }
1104
+ async confirm(e, t = {}) {
1105
+ return await this.dialog({
1106
+ title: t.title,
1107
+ message: e,
1108
+ signal: t.signal,
1109
+ cancelValue: !1,
1110
+ buttons: [{
1111
+ label: t.confirmLabel ?? "OK",
1112
+ value: !0
1113
+ }, {
1114
+ label: t.cancelLabel ?? "Cancel",
1115
+ value: !1
1116
+ }]
1117
+ }) === !0;
1118
+ }
1119
+ async alert(e, t = {}) {
1120
+ await this.dialog({
1121
+ title: t.title,
1122
+ message: e,
1123
+ signal: t.signal,
1124
+ cancelValue: void 0
1125
+ });
1126
+ }
1127
+ toast(e, n = {}) {
1128
+ if (this.live(), typeof e != "string" || !e) throw t("toast(message) takes a non-empty string", "ui");
1129
+ let r = new N(e, n);
1130
+ this.toastStack.add(r), this.announce(r.label, r.kind === "danger" ? "assertive" : "polite");
1131
+ let i = this.toastStack.children.filter((e) => e instanceof N && !e.dismissed);
1132
+ for (let e of i.slice(0, Math.max(0, i.length - oe))) e.dismiss();
1133
+ return r;
1134
+ }
1135
+ get tooltipWidget() {
1136
+ return this.tooltip;
1137
+ }
1138
+ live() {
1139
+ if (this.disposed) throw n("disposed", "Ui is disposed; create a new Ui", "ui");
1140
+ }
1141
+ dispose() {
1142
+ if (!this.disposed) {
1143
+ this.disposed = !0;
1144
+ for (let e of this.layers) e.dispose();
1145
+ this.changeListeners.clear(), this.focusListeners.clear(), this.announcers.clear();
1146
+ }
1147
+ }
1148
+ [Symbol.dispose ?? Symbol.for("Symbol.dispose")]() {
1149
+ this.dispose();
1150
+ }
1151
+ }, q = class extends a {
1152
+ constructor(e, t, n, r) {
1153
+ super(e, t, n), this.owner = e, this.setStyleQuiet({
1154
+ height: r,
1155
+ minHeight: 0,
1156
+ shrink: 0
1157
+ });
1158
+ }
1159
+ command(e) {
1160
+ if (e !== "down" && e !== "up") return !1;
1161
+ let t = e === "down" ? this.index + 1 : this.index - 1;
1162
+ return t >= 0 && t < this.owner.count ? this.owner.focusIndex(t) : this.owner.leave(this, e);
1163
+ }
1164
+ }, J = class extends m {
1165
+ constructor(n = {}) {
1166
+ let { count: r = 0, rowHeight: i = 44, row: a = (e) => `Row ${e + 1}`, overscan: o = 4, selected: s = -1, onSelect: c, ...l } = n;
1167
+ if (super({
1168
+ background: "surface",
1169
+ border: !0,
1170
+ padding: 4,
1171
+ gap: 2,
1172
+ overflow: "scroll",
1173
+ ...l
1174
+ }), this._count = 0, this._selected = -1, this.built = /* @__PURE__ */ new Map(), this.top = new g({
1175
+ minHeight: 0,
1176
+ shrink: 0
1177
+ }), this.bottom = new g({
1178
+ minHeight: 0,
1179
+ shrink: 0
1180
+ }), this.first = 0, this.last = 0, this.synced = "", this.leaving = !1, typeof a != "function") throw t("row is a function from an index to a label or a { label } entry", "ui");
1181
+ if (!(Number.isFinite(i) && i > 0)) throw e("rowHeight must be a number above 0", "ui");
1182
+ if (!Number.isInteger(o) || o < 0) throw e("overscan must be a whole number, 0 or more", "ui");
1183
+ this.role = "listbox", this.source = a, this._rowHeight = i, this.overscan = o, this.count = r, this.select(s), c && this.on("select", c);
1184
+ }
1185
+ get count() {
1186
+ return this._count;
1187
+ }
1188
+ set count(t) {
1189
+ if (!Number.isInteger(t) || t < 0) throw e("count must be a whole number, 0 or more", "ui");
1190
+ this._count = t, this._selected >= t && (this._selected = -1), this.refresh();
1191
+ }
1192
+ get rowHeight() {
1193
+ return this._rowHeight;
1194
+ }
1195
+ get selected() {
1196
+ return this._selected;
1197
+ }
1198
+ get range() {
1199
+ return [this.first, this.last];
1200
+ }
1201
+ refresh() {
1202
+ for (let e of this.built.values()) e.dispose();
1203
+ this.built.clear(), this.synced = "", this.sync();
1204
+ }
1205
+ select(t, n = !1) {
1206
+ if (!Number.isInteger(t) || t < -1 || t >= this._count) {
1207
+ if (t === -1) return;
1208
+ throw e(`VirtualList index ${t} is outside 0 to ${this._count - 1}`, "ui");
1209
+ }
1210
+ this._selected = t;
1211
+ for (let e of this.built.values()) e.selected = e.index === t;
1212
+ if (this.invalidate(!1), this.ui?.touchMirror(), n && t >= 0) {
1213
+ let e = this.built.get(t)?.entry ?? Y(this.source(t));
1214
+ this.emit("select", {
1215
+ index: t,
1216
+ item: e.value ?? e
1217
+ });
1218
+ }
1219
+ }
1220
+ scrollToIndex(t) {
1221
+ if (!Number.isInteger(t) || t < 0 || t >= this._count) throw e(`VirtualList index ${t} is outside 0 to ${this._count - 1}`, "ui");
1222
+ let n = t * this.step(), r = this.view();
1223
+ n < this.scrollY ? this.scrollY = n : n + this._rowHeight > this.scrollY + r && (this.scrollY = n + this._rowHeight - r), this.invalidate(), this.sync();
1224
+ }
1225
+ focusIndex(e, t) {
1226
+ this.scrollToIndex(e);
1227
+ let n = this.ui;
1228
+ if (!n) return !1;
1229
+ n.layout();
1230
+ let r = this.built.get(e);
1231
+ return !!r && n.focus(r, t);
1232
+ }
1233
+ leave(e, t) {
1234
+ let n = this.ui;
1235
+ if (!n || this.leaving) return !1;
1236
+ this.leaving = !0;
1237
+ try {
1238
+ n.navigate(t, n.inputMode);
1239
+ } finally {
1240
+ this.leaving = !1;
1241
+ }
1242
+ let r = n.focused;
1243
+ return r !== e && r instanceof q && r.owner === this ? this.focusIndex(t === "down" ? 0 : this._count - 1, n.inputMode) : r !== e;
1244
+ }
1245
+ tick(e) {
1246
+ return this.sync(), super.tick(e);
1247
+ }
1248
+ step() {
1249
+ let e = this.style.gap ?? 0;
1250
+ return this._rowHeight + (typeof e == "number" ? e : e[1]);
1251
+ }
1252
+ view() {
1253
+ let [e, , t] = h(this.style.padding), n = this.style.height, r = this.rect.height > 0 ? this.rect.height : typeof n == "number" ? n : this.ui?.height ?? 720;
1254
+ return Math.max(0, r - e - t);
1255
+ }
1256
+ sync() {
1257
+ let e = this.step(), t = e - this._rowHeight, n = this._count, r = Math.max(0, Math.min(n, Math.floor(this.scrollY / e) - this.overscan)), i = Math.max(r, Math.min(n, Math.ceil((this.scrollY + this.view()) / e) + this.overscan)), a = `${r}|${i}|${n}|${e}`;
1258
+ if (a === this.synced) return;
1259
+ this.synced = a, this.first = r, this.last = i;
1260
+ let o = this.ui?.focused;
1261
+ for (let [e, t] of this.built) (e < r || e >= i) && (t === o && this.ui?.focus(null), t.dispose(), this.built.delete(e));
1262
+ let s = [];
1263
+ r > 0 && s.push(this.top.setStyle({ height: r * e - t }));
1264
+ for (let e = r; e < i; e++) {
1265
+ let t = this.built.get(e);
1266
+ t || (t = new q(this, e, Y(this.source(e)), this._rowHeight), t.selected = e === this._selected, this.built.set(e, t)), s.push(t);
1267
+ }
1268
+ i < n && s.push(this.bottom.setStyle({ height: (n - i) * e - t }));
1269
+ for (let e of [...this.children]) s.includes(e) || this.remove(e);
1270
+ s.forEach((e, t) => {
1271
+ this.children[t] !== e && this.insert(e, t);
1272
+ }), this.invalidate();
1273
+ }
1274
+ };
1275
+ function Y(e) {
1276
+ if (typeof e == "string") return { label: e };
1277
+ if (!e || typeof e.label != "string") throw t("row(index) returns a string or a { label } entry", "ui");
1278
+ return e;
1279
+ }
1280
+ //#endregion
1281
+ //#region src/dom-spec.ts
1282
+ var X = Symbol("skip");
1283
+ function Z(e, t) {
1284
+ if (e instanceof P) return X;
1285
+ if (e instanceof i) return e.parent instanceof N ? X : {
1286
+ tag: "p",
1287
+ attrs: {},
1288
+ text: e.text,
1289
+ container: !1
1290
+ };
1291
+ if (e instanceof l) return {
1292
+ tag: "button",
1293
+ attrs: {
1294
+ type: "button",
1295
+ role: e.role,
1296
+ "aria-checked": String(e.checked),
1297
+ disabled: e.active ? null : ""
1298
+ },
1299
+ text: e.label,
1300
+ container: !1
1301
+ };
1302
+ if (e instanceof p) return {
1303
+ tag: "button",
1304
+ attrs: {
1305
+ type: "button",
1306
+ disabled: e.active ? null : ""
1307
+ },
1308
+ text: e.label,
1309
+ container: !1
1310
+ };
1311
+ if (e instanceof s) return {
1312
+ tag: "input",
1313
+ attrs: {
1314
+ type: "range",
1315
+ min: String(e.min),
1316
+ max: String(e.max),
1317
+ step: e.step > 0 ? String(e.step) : "any",
1318
+ "aria-label": e.label || "Slider",
1319
+ "aria-valuetext": e.valueText,
1320
+ disabled: e.active ? null : ""
1321
+ },
1322
+ value: String(e.value),
1323
+ container: !1
1324
+ };
1325
+ if (e instanceof d) return {
1326
+ tag: "div",
1327
+ attrs: {
1328
+ role: "progressbar",
1329
+ "aria-label": e.label || "Progress",
1330
+ "aria-valuemin": "0",
1331
+ "aria-valuemax": String(e.max),
1332
+ "aria-valuenow": String(e.value),
1333
+ "aria-valuetext": e.valueText
1334
+ },
1335
+ container: !1
1336
+ };
1337
+ if (e instanceof c) return {
1338
+ tag: "div",
1339
+ attrs: {
1340
+ role: "listbox",
1341
+ "aria-label": e.label || "List"
1342
+ },
1343
+ container: !0
1344
+ };
1345
+ if (e.role === "option" && e.parent instanceof c) return {
1346
+ tag: "div",
1347
+ attrs: {
1348
+ role: "option",
1349
+ tabindex: e.active ? "0" : null,
1350
+ "aria-selected": String(e.parent.children.indexOf(e) === e.parent.selected),
1351
+ "aria-disabled": e.active ? null : "true"
1352
+ },
1353
+ text: e.label,
1354
+ container: !1
1355
+ };
1356
+ if (e instanceof J) return {
1357
+ tag: "div",
1358
+ attrs: {
1359
+ role: "listbox",
1360
+ "aria-label": e.label || "List"
1361
+ },
1362
+ container: !0
1363
+ };
1364
+ if (e.role === "option" && e.parent instanceof J) {
1365
+ let t = e;
1366
+ return {
1367
+ tag: "div",
1368
+ attrs: {
1369
+ role: "option",
1370
+ tabindex: e.active ? "0" : null,
1371
+ "aria-selected": String(t.selected),
1372
+ "aria-disabled": e.active ? null : "true",
1373
+ "aria-setsize": String(e.parent.count),
1374
+ "aria-posinset": String(t.index + 1)
1375
+ },
1376
+ text: e.label,
1377
+ container: !1
1378
+ };
1379
+ }
1380
+ return e instanceof f ? {
1381
+ tag: "div",
1382
+ attrs: {
1383
+ role: "group",
1384
+ "aria-label": e.label || "Inventory"
1385
+ },
1386
+ container: !0
1387
+ } : e.parent instanceof f ? {
1388
+ tag: "button",
1389
+ attrs: {
1390
+ type: "button",
1391
+ "aria-pressed": String(e.parent.carried === e.parent.children.indexOf(e))
1392
+ },
1393
+ text: e.label,
1394
+ container: !1
1395
+ } : e instanceof A ? {
1396
+ tag: "div",
1397
+ attrs: {
1398
+ role: "dialog",
1399
+ "aria-modal": "true",
1400
+ "aria-label": e.find(`${e.id}-title`) ? null : e.label,
1401
+ "aria-labelledby": e.find(`${e.id}-title`) ? `${t}${e.id}-title` : null,
1402
+ "aria-describedby": e.find(`${e.id}-message`) ? `${t}${e.id}-message` : null
1403
+ },
1404
+ container: !0
1405
+ } : e instanceof N || e instanceof m && e.label ? {
1406
+ tag: "div",
1407
+ attrs: {
1408
+ role: "group",
1409
+ "aria-label": e.label
1410
+ },
1411
+ container: !0
1412
+ } : null;
1413
+ }
1414
+ //#endregion
1415
+ //#region src/mirror.ts
1416
+ var Q = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;";
1417
+ function $(e, t, n = {}) {
1418
+ let r = t.ownerDocument, i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new WeakMap(), o = [], c = (e, t = "", n = {}) => {
1419
+ let i = r.createElement(e);
1420
+ t && i.setAttribute("style", t);
1421
+ for (let [e, t] of Object.entries(n)) t !== null && i.setAttribute(e, t);
1422
+ return i;
1423
+ }, l = c("div", "position:absolute;overflow:hidden;pointer-events:none;opacity:0;", {
1424
+ role: "region",
1425
+ "aria-label": n.label ?? "Game interface",
1426
+ "data-margo-ui": "mirror"
1427
+ }), u = c("div", "position:absolute;inset:0;"), d = c("div", "position:absolute;inset:0;"), f = c("div", "display:none;"), p = c("div", Q, {
1428
+ "aria-live": "polite",
1429
+ "aria-atomic": "false",
1430
+ role: "log"
1431
+ }), m = c("div", Q, {
1432
+ "aria-live": "assertive",
1433
+ "aria-atomic": "false",
1434
+ role: "alert"
1435
+ });
1436
+ l.append(u, d, f, p, m), n.container ? n.container.append(l) : t.after(l), o.push(() => l.remove());
1437
+ let h = (e) => `margo-ui-${e.id}`, g = (e) => Z(e, "margo-ui-"), _ = (t) => `${Math.round(t * e.cssScale * 10) / 10}px`, v = /* @__PURE__ */ new Set(), b = (e, t) => {
1438
+ let n = new Set(t);
1439
+ for (let t of Array.from(e.children)) n.has(t) || t.remove();
1440
+ t.forEach((t, n) => {
1441
+ e.children[n] !== t && e.insertBefore(t, e.children[n] ?? null);
1442
+ });
1443
+ }, x = (e, t, n) => {
1444
+ for (let r of e) {
1445
+ if (!r.visible) continue;
1446
+ let e = g(r);
1447
+ if (e === X) continue;
1448
+ if (e === null) {
1449
+ x(r.children, t, n);
1450
+ continue;
1451
+ }
1452
+ let o = i.get(r);
1453
+ (!o || o.tag !== e.tag) && (o?.el.remove(), o = {
1454
+ el: c(e.tag, "", { id: h(r) }),
1455
+ widget: r,
1456
+ tag: e.tag,
1457
+ attrs: /* @__PURE__ */ new Map(),
1458
+ text: "",
1459
+ style: "",
1460
+ value: ""
1461
+ }, i.set(r, o), a.set(o.el, r)), v.add(r);
1462
+ let s = o.el, l = { ...e.attrs };
1463
+ if (r.tooltip || r.description) {
1464
+ o.description ??= c("span", "", { id: `${h(r)}-desc` });
1465
+ let e = [r.description, r.tooltip].filter(Boolean).join(". ");
1466
+ o.description.textContent !== e && (o.description.textContent = e), o.description.parentElement !== f && f.append(o.description), l["aria-describedby"] = l["aria-describedby"] ?? `${h(r)}-desc`;
1467
+ } else o.description && (o.description.remove(), o.description = void 0);
1468
+ for (let [e, t] of Object.entries(l)) o.attrs.get(e) !== t && (o.attrs.set(e, t), t === null ? s.removeAttribute(e) : s.setAttribute(e, t));
1469
+ e.text !== void 0 && e.text !== o.text && (o.text = e.text, s.textContent = e.text), e.value !== void 0 && e.value !== o.value && (o.value = e.value, s.value = e.value);
1470
+ let u = r.rect, d = `position:absolute;left:${_(u.x - t.x)};top:${_(u.y - t.y)};width:${_(u.width)};height:${_(u.height)};margin:0;padding:0;border:0;outline:none;background:transparent;pointer-events:none;font:inherit;`;
1471
+ if (d !== o.style && (o.style = d, s.setAttribute("style", d)), n.push(s), e.container) {
1472
+ let e = [];
1473
+ x(r.children, u, e), b(s, e);
1474
+ }
1475
+ }
1476
+ }, S = !1, C = 0, w = !1, T = () => {
1477
+ S = !1, e.layout(), v.clear();
1478
+ let n = e.modalLayer.children.length > 0;
1479
+ l.style.left = `${t.offsetLeft}px`, l.style.top = `${t.offsetTop}px`, l.style.width = `${t.clientWidth}px`, l.style.height = `${t.clientHeight}px`;
1480
+ let r = [], a = [], o = {
1481
+ x: 0,
1482
+ y: 0
1483
+ };
1484
+ x(e.root.children, o, r), x(e.toastLayer.children, o, r), x(e.modalLayer.children, o, a), b(u, r), b(d, a);
1485
+ for (let [e, t] of i) v.has(e) || (t.el.remove(), t.description?.remove(), i.delete(e));
1486
+ n ? u.setAttribute("inert", "") : u.removeAttribute("inert"), n ? u.setAttribute("aria-hidden", "true") : u.removeAttribute("aria-hidden"), A();
1487
+ }, E = () => {
1488
+ if (S) return;
1489
+ S = !0;
1490
+ let e = globalThis.requestAnimationFrame;
1491
+ e ? C = e(T) : queueMicrotask(T);
1492
+ };
1493
+ o.push(e.onChange(E)), T();
1494
+ let D = (e) => !!e && l.contains(e), O = (e) => {
1495
+ for (let t = e; t && t !== l; t = t.parentElement) {
1496
+ let e = a.get(t);
1497
+ if (e) return e;
1498
+ }
1499
+ }, k = (e, t) => {
1500
+ l.addEventListener(e, t), o.push(() => l.removeEventListener(e, t));
1501
+ };
1502
+ k("focusin", (t) => {
1503
+ if (w) return;
1504
+ let n = O(t.target);
1505
+ n?.focusable && e.focus(n, "keyboard");
1506
+ }), k("focusout", (t) => {
1507
+ if (w || D(t.relatedTarget)) return;
1508
+ let n = O(t.target);
1509
+ n && e.focused === n && e.focus(null);
1510
+ });
1511
+ function A() {
1512
+ let n = e.focused, a = n && i.get(n)?.el, o = r.activeElement;
1513
+ if (n && !n.pressed && a && (o === t || l.contains(o)) && o !== a) {
1514
+ w = !0;
1515
+ try {
1516
+ a.focus({ preventScroll: !0 });
1517
+ } finally {
1518
+ w = !1;
1519
+ }
1520
+ }
1521
+ }
1522
+ o.push(e.onFocus(() => {
1523
+ e.inputMode === "pointer" || e.inputMode === "touch" ? E() : A();
1524
+ })), k("click", (t) => {
1525
+ let n = O(t.target);
1526
+ n && n.focusable && (e.focus(n, e.inputMode === "pointer" ? "pointer" : "keyboard"), n instanceof s || n.activate());
1527
+ }), k("input", (e) => {
1528
+ let t = O(e.target);
1529
+ t instanceof s && t.setValue(Number(e.target.value));
1530
+ }), k("keydown", (t) => {
1531
+ let n = /* @__PURE__ */ new Set();
1532
+ if (t.target?.closest?.("button,input,textarea,select") && (n.add("Enter"), n.add(" ")), e.modalLayer.children.length || n.add("Tab"), t.target?.getAttribute?.("type") === "range") for (let e of [
1533
+ "ArrowLeft",
1534
+ "ArrowRight",
1535
+ "Home",
1536
+ "End",
1537
+ "PageUp",
1538
+ "PageDown"
1539
+ ]) n.add(e);
1540
+ y(e, t, n) && (t.preventDefault(), t.stopPropagation());
1541
+ });
1542
+ let j = /* @__PURE__ */ new Set();
1543
+ o.push(() => {
1544
+ for (let e of j) clearTimeout(e);
1545
+ j.clear();
1546
+ }), o.push(e.onAnnounce((e, t) => {
1547
+ let n = t === "assertive" ? m : p, r = c("p");
1548
+ r.textContent = e, n.append(r);
1549
+ let i = setTimeout(() => {
1550
+ j.delete(i), r.remove();
1551
+ }, 8e3);
1552
+ j.add(i);
1553
+ }));
1554
+ let M = (() => {
1555
+ let e = globalThis.cancelAnimationFrame;
1556
+ C && e && e(C);
1557
+ for (let e of o.splice(0).reverse()) e();
1558
+ i.clear();
1559
+ });
1560
+ M[Symbol.dispose ?? Symbol.for("Symbol.dispose")] = M;
1561
+ let N = n.signal;
1562
+ return N && (N.aborted ? M() : N.addEventListener("abort", M, { once: !0 })), M;
1563
+ }
1564
+ //#endregion
1565
+ //#region src/renderer.ts
1566
+ var ue = class n {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
1567
+ static attach(e, t, r) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
1568
+ let i = new n(t, r);
1569
+ return e.use(i), i;
1570
+ }
1571
+ constructor(n, r = {}) {
1572
+ if (this.ui = n, this.stats = {
1573
+ rects: 0,
1574
+ glyphs: 0,
1575
+ drawCalls: 0,
1576
+ uploads: 0
1577
+ }, this.descriptor = _(), this.disposed = !1, this.elapsed = 0, this.execute = (e) => {
1578
+ this.stats.rects = this.stats.glyphs = this.stats.drawCalls = this.stats.uploads = 0;
1579
+ let t = e.view;
1580
+ if (!(t && t.index !== t.count - 1)) {
1581
+ if (e.backend === "webgl2") return this.drawGl(e);
1582
+ this.device && this.engine && e.device === this.device && this.draw(e);
1583
+ }
1584
+ }, !n || typeof n.draw != "function") throw t("UiRenderer needs a Ui: new UiRenderer(new Ui())", "ui");
1585
+ if (this.name = r.name ?? "ui", this.order = r.order ?? 1e3, !this.name || typeof this.name != "string" || !Number.isFinite(this.order)) throw t("Invalid UiRenderer options", "ui");
1586
+ this.inputOptions = r.input === !1 ? !1 : r.input === !0 || !r.input ? {} : r.input, this.mirrorOptions = r.mirror === !1 ? !1 : r.mirror === !0 || !r.mirror ? {} : r.mirror, this.options = {
1587
+ atlasSize: r.atlasSize ?? 1024,
1588
+ colorAtlasSize: r.colorAtlasSize ?? 512,
1589
+ maxPages: r.maxPages ?? 16
1590
+ };
1591
+ for (let t of Object.values(this.options)) if (!Number.isInteger(t) || t < 1) throw e("Atlas sizes and page limits must be positive integers", "ui");
1592
+ if (this.options.atlasSize < 64 || this.options.colorAtlasSize < 64) throw e("Atlas pages must be at least 64 texels", "ui");
1593
+ this.painter = new v(this.options, this.stats);
1594
+ }
1595
+ setup(e) {
1596
+ this.engine = e;
1597
+ let t = e.canvas;
1598
+ this.inputOptions && t && !this.detach && typeof t.addEventListener == "function" && (this.detach = b(this.ui, t, this.inputOptions)), this.mirrorOptions && t && !this.detachMirror && t.ownerDocument && (this.detachMirror = $(this.ui, t, this.mirrorOptions)), e.backend !== "webgpu" || !e.gpu ? this.gl || import("./gl-load-wwKmojhb.js").then((e) => {
1599
+ this.disposed || (this.gl = e);
1600
+ }) : this.painter.setup(this.device = e.gpu);
1601
+ }
1602
+ registerPasses(e) {
1603
+ e.add({
1604
+ name: `${this.name}:draw`,
1605
+ order: this.order,
1606
+ execute: this.execute
1607
+ });
1608
+ }
1609
+ get time() {
1610
+ return this.elapsed;
1611
+ }
1612
+ advanceTime(e) {
1613
+ this.disposed || (this.ui.update(e), this.elapsed += e);
1614
+ }
1615
+ onPreUpdate(e) {
1616
+ this.advanceTime(e);
1617
+ }
1618
+ draw(e) {
1619
+ let t = this.ui, n = this.pixelRatio(e.width);
1620
+ if (t.resize(e.width / n, e.height / n, n), !this.painter.prepare(t.draw())) return;
1621
+ this.descriptor.colorAttachments[0].view = e.color, this.descriptor.depthStencilAttachment.view = e.depth;
1622
+ let r = e.encoder.beginRenderPass(this.descriptor);
1623
+ this.painter.encode(r, {
1624
+ width: e.width,
1625
+ height: e.height,
1626
+ format: e.format,
1627
+ depthFormat: e.descriptor.depthStencil.format,
1628
+ samples: e.descriptor.multisample?.count ?? 1
1629
+ }), r.end();
1630
+ }
1631
+ drawGl(e) {
1632
+ if (!this.gl || !this.engine || e.gl.isContextLost()) return;
1633
+ this.glPainter?.gl !== e.gl && (this.glPainter?.dispose(), this.glPainter = this.gl.glPainter(e.gl, this.options, this.stats));
1634
+ let t = this.pixelRatio(e.width);
1635
+ this.ui.resize(e.width / t, e.height / t, t), this.glPainter.draw(this.ui.draw(), e.width, e.height);
1636
+ }
1637
+ pixelRatio(e) {
1638
+ let t = this.engine.canvas?.clientWidth || 0;
1639
+ return t > 0 ? e / t : globalThis.devicePixelRatio ?? 1;
1640
+ }
1641
+ onDeviceRestored(e) {
1642
+ this.glPainter = void 0, this.painter.release(!1), this.device = void 0, this.setup(e);
1643
+ }
1644
+ dispose() {
1645
+ let e = this.engine;
1646
+ this.disposed = !0, this.detach?.(), this.detach = void 0, this.detachMirror?.(), this.detachMirror = void 0, this.painter.release(!0), this.glPainter && !this.glPainter.gl.isContextLost() && this.glPainter.dispose(), this.glPainter = this.gl = void 0, this.device = void 0, this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
1647
+ }
1648
+ [Symbol.dispose ?? Symbol.for("Symbol.dispose")]() {
1649
+ this.dispose();
1650
+ }
1651
+ };
1652
+ //#endregion
1653
+ export { P as _, J as a, G as c, H as d, z as f, N as g, A as h, Z as i, U as l, I as m, $ as n, le as o, F as p, X as r, W as s, ue as t, ie as u, k as v, D as y };