@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
package/dist/input.js ADDED
@@ -0,0 +1,315 @@
1
+ import { n as e, r as t } from "./errors-CDgOgzCd.js";
2
+ import { caretRect as n, graphemes as r, hitTest as i } from "@margoengine/text";
3
+ import { Widget as a, edges as o } from "@margoengine/ui";
4
+ //#region src/input-dom.ts
5
+ function s(e, t, n = {}) {
6
+ let r = t.ownerDocument, i = r.createElement("div");
7
+ i.dataset.margoUi = "text-inputs", i.style.cssText = "position:absolute;inset:0;pointer-events:none;overflow:hidden;", (n.container ?? t.parentElement ?? r.body).append(i);
8
+ let a = /* @__PURE__ */ new Map(), s = [], l = !1, u = !1, d = !1, f = (e, t, n, r = !1) => (e.addEventListener(t, n, r), () => e.removeEventListener(t, n, r)), p = (e, t) => {
9
+ l || e.edit(t.value, t.selectionStart ?? 0, t.selectionEnd ?? 0, t.selectionDirection ?? "none");
10
+ }, m = (n) => {
11
+ let a = r.createElement(n.multiline ? "textarea" : "input");
12
+ return a instanceof r.defaultView.HTMLInputElement && (a.type = n.type), a.dataset.margoTextInput = n.id, a.tabIndex = -1, a.style.cssText = "position:absolute;opacity:0;pointer-events:none;margin:0;border:0;padding:0;resize:none;overflow:hidden;", i.append(a), {
13
+ el: a,
14
+ clean: [
15
+ f(a, "input", () => p(n, a)),
16
+ f(a, "select", () => p(n, a)),
17
+ f(a, "keyup", () => p(n, a)),
18
+ f(a, "compositionstart", () => {
19
+ n.composing = !0, n.invalidate(!1);
20
+ }),
21
+ f(a, "compositionend", () => {
22
+ n.composing = !1, p(n, a), n.invalidate(!1);
23
+ }),
24
+ f(a, "focus", () => {
25
+ l || e.focus(n, "keyboard");
26
+ }),
27
+ f(a, "blur", (r) => {
28
+ n.composing && (n.composing = !1, n.invalidate(!1)), !l && r.relatedTarget !== t && e.focused === n && e.focus(null);
29
+ }),
30
+ f(a, "keydown", (e) => {
31
+ let r = e;
32
+ r.isComposing || n.composing || r.keyCode === 229 || (r.key === "Escape" ? (a.blur(), t.focus({ preventScroll: !0 }), r.preventDefault()) : r.key === "Enter" && !n.multiline && (n.emit("submit", { value: n.value }), r.preventDefault())), r.stopPropagation();
33
+ }),
34
+ n.on("editrequest", () => {
35
+ a.disabled || a.focus({ preventScroll: !0 });
36
+ })
37
+ ]
38
+ };
39
+ }, h = () => {
40
+ if (d = !1, !(u || l)) {
41
+ l = !0;
42
+ try {
43
+ e.layout();
44
+ let n = /* @__PURE__ */ new Set(), r = new Set(e.focusables()), i = (i) => {
45
+ if (!(i instanceof c)) return;
46
+ n.add(i);
47
+ let s = a.get(i);
48
+ s || (s = m(i), a.set(i, s));
49
+ let { el: l } = s;
50
+ l.hidden = !i.shown, l.disabled = !i.active || !r.has(i), l.readOnly = i.readOnly, l.placeholder = i.placeholder, l.setAttribute("autocomplete", i.autocomplete), l.spellcheck = i.spellcheck, l.setAttribute("aria-label", i.label || i.placeholder || (i.multiline ? "Text area" : "Text input")), l.setAttribute("aria-required", String(i.required)), l.setAttribute("aria-invalid", String(i.invalid)), i.description ? l.setAttribute("aria-description", i.description) : l.removeAttribute("aria-description"), i.maxLength >= 0 && (l.maxLength = i.maxLength), l.value !== i.value && (l.value = i.value), !i.composing && (l.selectionStart !== i.selectionStart || l.selectionEnd !== i.selectionEnd || l.selectionDirection !== i.selectionDirection) && l.setSelectionRange(i.selectionStart, i.selectionEnd, i.selectionDirection);
51
+ let u = i.rect, d = e.cssScale;
52
+ l.style.left = `${t.offsetLeft + u.x * d}px`, l.style.top = `${t.offsetTop + u.y * d}px`, l.style.width = `${u.width * d}px`, l.style.height = `${u.height * d}px`;
53
+ let [f, p, h, g] = o(i.style.padding);
54
+ l.style.boxSizing = "border-box", l.style.padding = `${f * d}px ${p * d}px ${h * d}px ${g * d}px`, l.style.font = `${e.font.style} ${e.font.weight} ${e.fontSize("body") * d}px ${e.font.family}`, l.style.lineHeight = "1.25", l.style.direction = e.textDirection(i.value);
55
+ };
56
+ e.root.walk(i), e.modalLayer.walk(i);
57
+ for (let [e, t] of a) if (!n.has(e)) {
58
+ for (let e of t.clean) e();
59
+ t.el.remove(), e.composing = !1, a.delete(e);
60
+ }
61
+ } finally {
62
+ l = !1;
63
+ }
64
+ }
65
+ }, g = () => {
66
+ !d && !u && (d = !0, queueMicrotask(() => {
67
+ h(), _();
68
+ }));
69
+ }, _ = () => {
70
+ if (l || u) return;
71
+ h();
72
+ let n = e.focused, o = n instanceof c ? a.get(n) : void 0;
73
+ if (!(n instanceof c && n.pressed)) {
74
+ if (o && !o.el.disabled && !o.el.hidden) {
75
+ l = !0;
76
+ try {
77
+ o.el.focus({ preventScroll: !0 });
78
+ } finally {
79
+ l = !1;
80
+ }
81
+ } else if (i.contains(r.activeElement)) {
82
+ l = !0;
83
+ try {
84
+ t.focus({ preventScroll: !0 });
85
+ } finally {
86
+ l = !1;
87
+ }
88
+ e.focusChanged();
89
+ }
90
+ }
91
+ };
92
+ s.push(e.onChange(g), e.onFocus(() => {
93
+ e.inputMode === "pointer" || e.inputMode === "touch" ? g() : _();
94
+ }), f(t, "focus", _)), s.push(f(r, "keydown", (n) => {
95
+ let a = n, o = a.target;
96
+ if (a.key !== "Tab" || a.ctrlKey || a.metaKey || a.altKey || a.isComposing || !o || e.focused instanceof c && e.focused.composing) return;
97
+ let s = o.id === `margo-ui-${e.focused?.id}` && t.parentElement?.contains(o);
98
+ if (!i.contains(o) && !s) return;
99
+ let l = !a.shiftKey;
100
+ if (!e.tab(l, "keyboard")) {
101
+ let e = Array.from(r.querySelectorAll("a[href],button,input,textarea,select,[tabindex]")).filter((e) => {
102
+ if (e === t || e.tabIndex < 0 || e.closest("[data-margo-ui],[hidden],[inert]") || e.matches(":disabled")) return !1;
103
+ for (let t = e; t; t = t.parentElement) {
104
+ let e = r.defaultView.getComputedStyle(t);
105
+ if (e.display === "none" || e.visibility === "hidden") return !1;
106
+ }
107
+ return !0;
108
+ }), n = l ? e.find((e) => !!(t.compareDocumentPosition(e) & 4)) : e.filter((e) => !!(t.compareDocumentPosition(e) & 2)).at(-1);
109
+ n ? n.focus({ preventScroll: !0 }) : r.activeElement?.blur();
110
+ }
111
+ a.preventDefault(), a.stopImmediatePropagation();
112
+ }, !0)), s.push(f(r, "selectionchange", () => {
113
+ for (let [e, { el: t }] of a) r.activeElement === t && p(e, t);
114
+ })), h(), _();
115
+ let v = (() => {
116
+ if (!u) {
117
+ u = !0, n.signal?.removeEventListener("abort", v);
118
+ for (let e of s) e();
119
+ for (let [e, t] of a) {
120
+ for (let e of t.clean) e();
121
+ e.composing = !1, e.invalidate(!1);
122
+ }
123
+ a.clear(), i.remove();
124
+ }
125
+ });
126
+ return v[Symbol.dispose ?? Symbol.for("Symbol.dispose")] = v, n.signal?.aborted ? v() : n.signal?.addEventListener("abort", v, { once: !0 }), v;
127
+ }
128
+ //#endregion
129
+ //#region src/input.ts
130
+ var c = class extends a {
131
+ constructor(n = {}) {
132
+ let { value: r = "", placeholder: i = "", type: a = "text", readOnly: o = !1, maxLength: s = -1, autocomplete: c = "off", spellcheck: l = a !== "password", required: u = !1, invalid: d = !1, onChange: f, onSubmit: p, ...m } = n;
133
+ if (super({
134
+ width: 240,
135
+ minHeight: 44,
136
+ padding: [10, 12],
137
+ ...m
138
+ }), this.multiline = !1, this.start = 0, this.end = 0, this.direction = "none", this.anchor = 0, this.blink = 0, this.offsetX = 0, this.offsetY = 0, this.clicks = 0, this.clickAge = Infinity, this.composing = !1, typeof r != "string" || typeof i != "string" || typeof c != "string") throw t("TextInput value, placeholder and autocomplete must be strings", "ui");
139
+ if (a !== "text" && a !== "password") throw t("TextInput type is 'text' or 'password'", "ui");
140
+ if (!Number.isInteger(s) || s < -1) throw e("maxLength is -1 or a non-negative integer", "ui");
141
+ this.type = a, this.maxLength = s, this.autocomplete = c, this.spellcheck = l, this.required = u, this.invalid = d, this._readOnly = o, this._placeholder = i, this._value = this.normalize(r), this.role = "textbox", this.focusable = !0, this.pointerEvents = "auto", f && this.on("change", f), p && this.on("submit", p), this.on("focus", () => {
142
+ this.blink = 0, this.invalidate(!1);
143
+ });
144
+ }
145
+ normalize(e) {
146
+ return e.replace(/[\r\n]/g, "");
147
+ }
148
+ get value() {
149
+ return this._value;
150
+ }
151
+ set value(e) {
152
+ if (typeof e != "string") throw t("TextInput value must be a string", "ui");
153
+ let n = this.normalize(e);
154
+ n !== this._value && (this._value = n, this.start = Math.min(this.start, n.length), this.end = Math.min(this.end, n.length), this.invalidate());
155
+ }
156
+ get placeholder() {
157
+ return this._placeholder;
158
+ }
159
+ set placeholder(e) {
160
+ this._placeholder = String(e), this.invalidate();
161
+ }
162
+ get readOnly() {
163
+ return this._readOnly;
164
+ }
165
+ set readOnly(e) {
166
+ this._readOnly = e, this.invalidate(!1);
167
+ }
168
+ get selectionStart() {
169
+ return this.start;
170
+ }
171
+ get selectionEnd() {
172
+ return this.end;
173
+ }
174
+ get selectionDirection() {
175
+ return this.direction;
176
+ }
177
+ setSelectionRange(n, r = n, i = "none") {
178
+ if (!Number.isFinite(n) || !Number.isFinite(r)) throw e("Selection offsets must be finite", "ui");
179
+ if (![
180
+ "forward",
181
+ "backward",
182
+ "none"
183
+ ].includes(i)) throw t("Invalid selection direction", "ui");
184
+ let a = Math.max(0, Math.min(this.value.length, Math.trunc(r))), o = Math.min(a, Math.max(0, Math.trunc(n)));
185
+ (o !== this.start || a !== this.end || i !== this.direction) && (this.start = o, this.end = a, this.direction = i, this.blink = 0, this.invalidate(!1), this.emit("select"));
186
+ }
187
+ select() {
188
+ this.setSelectionRange(0, this.value.length);
189
+ }
190
+ insertText(e) {
191
+ if (!this.active || this.readOnly) return;
192
+ if (typeof e != "string") throw t("insertText needs a string", "ui");
193
+ let n = this.maxLength < 0 ? Infinity : Math.max(0, this.maxLength - this.value.length + this.end - this.start), i = "";
194
+ for (let t of r(this.normalize(e))) {
195
+ if (i.length + t.cluster.length > n) break;
196
+ i += t.cluster;
197
+ }
198
+ let a = this.start + i.length, o = this.value.slice(0, this.start) + i + this.value.slice(this.end);
199
+ this.edit(o, a, a, "none");
200
+ }
201
+ edit(e, t, n, r) {
202
+ let i = this.value !== this.normalize(e);
203
+ this.value = e, this.setSelectionRange(t, n, r), this.blink = 0, this.invalidate(!1), i && this.emit("change", { value: this.value });
204
+ }
205
+ measure() {
206
+ return {
207
+ width: 216,
208
+ height: (this.ui?.fontSize("body") ?? 16) * 1.25 * (this.multiline ? 4 : 1)
209
+ };
210
+ }
211
+ display() {
212
+ let e = [0];
213
+ if (this.type !== "password") return {
214
+ text: this.value,
215
+ offsets: e
216
+ };
217
+ for (let t of r(this.value)) e.push(e.at(-1) + t.cluster.length);
218
+ return {
219
+ text: "•".repeat(e.length - 1),
220
+ offsets: e
221
+ };
222
+ }
223
+ geometry() {
224
+ let e = this.ui, t = this.rect, [r, i, a, s] = o(this.style.padding), c = Math.max(0, t.width - s - i), l = Math.max(0, t.height - r - a), u = this.display(), d = e.textLayout(u.text, "body", this.multiline ? c : Infinity), f = (e) => this.type === "password" ? Math.max(0, u.offsets.findIndex((t) => t >= e)) : e, p = n(d, f(this.direction === "backward" ? this.start : this.end)), m = e.textDirection(u.text) === "rtl" ? Math.max(0, c - d.bounds.right) : 0;
225
+ return this.offsetX = Math.max(0, Math.min(this.offsetX, Math.max(0, d.bounds.right - c + 2))), this.offsetY = Math.max(0, Math.min(this.offsetY, Math.max(0, -d.bounds.bottom - l))), this.focused && (p.x + m < this.offsetX ? this.offsetX = p.x + m : p.x + m + 2 > this.offsetX + c && (this.offsetX = p.x + m + 2 - c), -p.top < this.offsetY ? this.offsetY = -p.top : -p.bottom > this.offsetY + l && (this.offsetY = -p.bottom - l)), {
226
+ x: t.x + s,
227
+ y: t.y + r,
228
+ width: c,
229
+ height: l,
230
+ shift: m,
231
+ layout: d,
232
+ map: f,
233
+ offsets: u.offsets,
234
+ caret: p
235
+ };
236
+ }
237
+ indexAt(e, t) {
238
+ let n = this.geometry(), r = i(n.layout, e - n.x - n.shift + this.offsetX, n.y - t - this.offsetY);
239
+ return this.type === "password" ? n.offsets[r] ?? this.value.length : r;
240
+ }
241
+ pointerDown(e, t) {
242
+ if (!this.active || !this.ui) return !1;
243
+ this.blink = 0;
244
+ let n = this.indexAt(e, t);
245
+ if (this.clicks = this.clickAge < .4 && Math.abs(n - this.anchor) <= 1 ? this.clicks + 1 : 1, this.clickAge = 0, this.anchor = n, this.setSelectionRange(n), this.clicks === 2) {
246
+ let e = new Intl.Segmenter(void 0, { granularity: "word" }).segment(this.value);
247
+ for (let t of e) if (n >= t.index && n < t.index + t.segment.length) {
248
+ this.setSelectionRange(t.index, t.index + t.segment.length);
249
+ break;
250
+ }
251
+ } else this.clicks >= 3 && (this.select(), this.clicks = 0);
252
+ return !0;
253
+ }
254
+ pointerMove(e, t) {
255
+ if (!this.ui) return;
256
+ let n = this.indexAt(e, t);
257
+ this.setSelectionRange(Math.min(n, this.anchor), Math.max(n, this.anchor), n < this.anchor ? "backward" : "forward");
258
+ }
259
+ command(e) {
260
+ return e === "accept" && (this.emit("editrequest"), !0);
261
+ }
262
+ activate() {
263
+ this.emit("editrequest");
264
+ }
265
+ tick(e) {
266
+ let t = super.tick(e), n = Math.floor(this.blink * 2) % 2;
267
+ return this.clickAge += e, this.blink += e, t || this.focused && n !== Math.floor(this.blink * 2) % 2;
268
+ }
269
+ draw(e) {
270
+ let { ui: t, list: n, theme: r } = e, i = this.rect;
271
+ n.rect(i.x, i.y, i.width, i.height, {
272
+ fill: t.color(this.active ? "surfaceRaised" : "disabled"),
273
+ border: t.color(this.invalid ? "danger" : this.focused ? "focus" : "border"),
274
+ borderWidth: 1,
275
+ radius: r.radius
276
+ });
277
+ let a = this.geometry(), o = a.x + a.shift - this.offsetX, s = a.y - this.offsetY;
278
+ n.pushClip(a.x, a.y, a.width, a.height);
279
+ let c = this.focused && this.start !== this.end ? this.drawSelection(a.layout, a.map(this.start), a.map(this.end), o, s, e) : [];
280
+ !this.value && this.placeholder ? n.text(a.x, a.y, t.textLayout(this.placeholder, "body", this.multiline ? a.width : Infinity), t.font, t.color("textMuted")) : n.text(o, s, a.layout, t.font, t.color(this.active ? "text" : "onDisabled"));
281
+ for (let e of c) n.pushClip(e.x, e.y, e.width, e.height), n.text(o, s, {
282
+ ...a.layout,
283
+ glyphs: e.glyphs
284
+ }, t.font, t.color("onPrimary")), n.popClip();
285
+ this.focused && (this.composing || Math.floor(this.blink * 2) % 2 == 0) && n.rect(o + a.caret.x, s - a.caret.top, 1, a.caret.top - a.caret.bottom, { fill: t.color("text") }), n.popClip(), this.drawFocus(e);
286
+ }
287
+ drawSelection(e, t, n, r, i, { list: a, ui: o }) {
288
+ let s = [];
289
+ for (let c of e.glyphs) {
290
+ let l = Math.max(t, c.index), u = Math.min(n, c.index + c.length);
291
+ if (l >= u) continue;
292
+ let d = (e) => e <= c.index ? 0 : e >= c.index + c.length ? c.advance : c.carets?.find(([t]) => c.index + t >= e)?.[1] ?? c.advance, f = d(l), p = d(u), m = e.lines[c.line], h = c.x + (c.rtl ? c.advance - p : f);
293
+ a.rect(r + h, i - m.top, p - f, m.top - m.bottom, { fill: o.color("primary") });
294
+ let g = s.at(-1), _ = r + h, v = i - m.top;
295
+ g && g.y === v && Math.abs(g.x + g.width - _) < 1e-6 ? (g.width += p - f, g.glyphs.push(c)) : s.push({
296
+ x: _,
297
+ y: v,
298
+ width: p - f,
299
+ height: m.top - m.bottom,
300
+ glyphs: [c]
301
+ });
302
+ }
303
+ for (let s of e.lines) t <= s.end && n > s.end && e.text[s.end] === "\n" && a.rect(r + (s.rtl ? s.left : s.right), i - s.top, e.fontSize / 2, e.lineHeight, { fill: o.color("primary") });
304
+ return s;
305
+ }
306
+ }, l = class extends c {
307
+ constructor(e = {}) {
308
+ super(e), this.multiline = !0;
309
+ }
310
+ normalize(e) {
311
+ return e.replace(/\r\n?/g, "\n");
312
+ }
313
+ };
314
+ //#endregion
315
+ export { l as TextArea, c as TextInput, s as attachTextInputs };
@@ -0,0 +1,106 @@
1
+ import type { Binding, Input } from '@margoengine/input';
2
+ import { type Direction } from './focus.js';
3
+ import { Dialog } from './overlays.js';
4
+ import type { Ui } from './ui.js';
5
+ import type { UiCommand, Widget } from './widget.js';
6
+ /** Where the latest input came from. Keyboard and gamepad show the focus ring; pointer and touch hide it. */
7
+ export type InputSource = 'pointer' | 'touch' | 'keyboard' | 'gamepad';
8
+ /** Details of a pointer event given to {@link Ui.pointerDown} and friends. */
9
+ export interface PointerInfo {
10
+ /** Pointer id; a second pointer is ignored while the first is down. Default 0. */
11
+ id?: number;
12
+ /** Button number; only 0 (primary) clicks. Default 0. */
13
+ button?: number;
14
+ /** `'touch'` pointers never hover. Default `'mouse'`. */
15
+ type?: 'mouse' | 'touch' | 'pen';
16
+ }
17
+ /** The action names {@link uiActions} defines and {@link Ui.bindInput} reads. */
18
+ export declare const UI_ACTIONS: {
19
+ readonly up: 'ui.up';
20
+ readonly down: 'ui.down';
21
+ readonly left: 'ui.left';
22
+ readonly right: 'ui.right';
23
+ readonly accept: 'ui.accept';
24
+ readonly cancel: 'ui.cancel';
25
+ readonly next: 'ui.next';
26
+ readonly previous: 'ui.previous';
27
+ };
28
+ /** Options for {@link uiActions}. */
29
+ export interface UiActionOptions {
30
+ /** Also bind arrow keys, Enter, Space, Escape and Tab. Default false: {@link attachInput} already handles the keyboard. */
31
+ keyboard?: boolean;
32
+ /** Bind the standard gamepad: d-pad and left stick to move, A to accept, B to cancel, bumpers for next and previous. Default true. */
33
+ gamepad?: boolean;
34
+ /** Gamepad index. Default 0. */
35
+ pad?: number;
36
+ }
37
+ /**
38
+ * Bindings for an `Input` from `@margoengine/input` that drive the UI from a gamepad (and optionally the keyboard):
39
+ * `new Input(uiActions(), { target })`, then `ui.bindInput(input)`. Merge them with your game's own actions:
40
+ * `new Input({ ...uiActions(), jump: [...] })`.
41
+ * @example
42
+ * const input = new Input(uiActions());
43
+ * ui.bindInput(input);
44
+ */
45
+ export declare function uiActions({ keyboard, gamepad, pad, }?: UiActionOptions): Record<string, Binding[]>;
46
+ /**
47
+ * Focus, pointer, keyboard and gamepad handling for a {@link Ui}. The Ui owns one and forwards to it.
48
+ * @internal
49
+ */
50
+ export declare class Interaction {
51
+ private readonly ui;
52
+ focused: Widget | null;
53
+ inputMode: InputSource;
54
+ captureKeys: boolean;
55
+ wrapFocus: boolean;
56
+ private hoverChain;
57
+ private pressed;
58
+ private captured;
59
+ private pointer;
60
+ private flash;
61
+ private restore;
62
+ private binding?;
63
+ private tipTarget;
64
+ private tipTime;
65
+ private tipDismissed;
66
+ private tipShown;
67
+ constructor(ui: Ui);
68
+ /** The widgets keyboard navigation moves between: the top dialog's, or the screen's and the toasts'. */
69
+ scope(): Widget[];
70
+ focusables(): Widget[];
71
+ private contains;
72
+ setMode(source: InputSource): void;
73
+ focus(widget: Widget | null, source?: InputSource): boolean;
74
+ private first;
75
+ navigate(direction: Direction, source?: InputSource): boolean;
76
+ /** Tab order. Returns false when focus leaves the UI (Tab past the last widget with no dialog open). */
77
+ step(delta: 1 | -1, source?: InputSource): boolean;
78
+ accept(source?: InputSource): boolean;
79
+ cancel(source?: InputSource): boolean;
80
+ execute(command: UiCommand, source?: InputSource): boolean;
81
+ /** True when a key press should go to the UI rather than the game. */
82
+ wantsKeys(): boolean;
83
+ hitTest(x: number, y: number): Widget | null;
84
+ private setHover;
85
+ private toUnits;
86
+ pointerMove(cx: number, cy: number, info?: PointerInfo): boolean;
87
+ pointerDown(cx: number, cy: number, info?: PointerInfo): boolean;
88
+ pointerUp(cx: number, cy: number, info?: PointerInfo): boolean;
89
+ pointerCancel(): void;
90
+ leave(): void;
91
+ wheel(cx: number, cy: number, dy: number): boolean;
92
+ wantsPointer(cx: number, cy: number): boolean;
93
+ widgetGone(widget: Widget): void;
94
+ dialogOpened(dialog: Dialog): void;
95
+ dialogClosed(): void;
96
+ bindInput(input: Pick<Input, 'state'>): void;
97
+ unbindInput(): void;
98
+ private poll;
99
+ tick(dt: number): void;
100
+ private hideTip;
101
+ private tipSource;
102
+ private tickTip;
103
+ cancelTip(): void;
104
+ /** Put the tooltip next to the widget it describes, inside the safe area, below it or above when there is no room. */
105
+ placeTip(): void;
106
+ }
@@ -0,0 +1,40 @@
1
+ import { Panel, type WidgetOptions, type Ui } from '@margoengine/ui';
2
+ import { TextInput } from '@margoengine/ui/input';
3
+ /** A text key or W3C key name (Shift, CapsLock, Backspace, Delete, Enter, Space, arrows, Home, End). */
4
+ export interface KeyboardKey {
5
+ /** Text inserted, or a named operation. */
6
+ key: string;
7
+ /** Display caption, default key. */
8
+ label?: string;
9
+ /** Text when shifted, default locale uppercase. */
10
+ shifted?: string;
11
+ /** Relative key width, default 1. */
12
+ width?: number;
13
+ }
14
+ /** Options for an engine-rendered keyboard. */
15
+ export interface VirtualKeyboardOptions extends WidgetOptions {
16
+ /** Target field, default null. Focusing another TextInput in the same Ui reconnects automatically. */
17
+ target?: TextInput | null;
18
+ /** Rows of text or key descriptors. Default familiar QWERTY with numbers, punctuation and editing controls. */
19
+ rows?: readonly (readonly (string | KeyboardKey)[])[];
20
+ /** Minimum width of a normal key in UI pixels, default 44. */
21
+ keyWidth?: number;
22
+ }
23
+ /** Keyboard built from ordinary Buttons: spatial gamepad navigation and world-panel/XR rays work unchanged. */
24
+ export declare class VirtualKeyboard extends Panel {
25
+ private field;
26
+ private shifted;
27
+ private caps;
28
+ private offFocus?;
29
+ private readonly keys;
30
+ constructor(options?: VirtualKeyboardOptions);
31
+ /** Currently connected field; independent of focus on a keyboard button. */
32
+ get target(): TextInput | null;
33
+ /** Connect a field, or null to disconnect. Cross-UI targets support a separate XR keyboard panel. */
34
+ connect(field: TextInput | null): void;
35
+ protected attached(ui: Ui | null): void;
36
+ /** Process one virtual key; returns false when no editable target or an active IME composition owns it. */
37
+ press(key: string): boolean;
38
+ private caption;
39
+ private refreshKeys;
40
+ }
@@ -0,0 +1,132 @@
1
+ import { n as e, r as t } from "./errors-CDgOgzCd.js";
2
+ import { graphemes as n } from "@margoengine/text";
3
+ import { Button as r, Panel as i } from "@margoengine/ui";
4
+ import { TextInput as a } from "@margoengine/ui/input";
5
+ //#region src/keyboard.ts
6
+ var o = [
7
+ [..."1234567890"].map((e, t) => ({
8
+ key: e,
9
+ shifted: ")!@#$%^&*("[t === 9 ? 0 : t + 1]
10
+ })),
11
+ [..."qwertyuiop", {
12
+ key: "Backspace",
13
+ label: "Backspace",
14
+ width: 2
15
+ }],
16
+ [..."asdfghjkl", {
17
+ key: "Enter",
18
+ label: "Enter",
19
+ width: 2
20
+ }],
21
+ [
22
+ {
23
+ key: "Shift",
24
+ width: 1.5
25
+ },
26
+ ..."zxcvbnm",
27
+ {
28
+ key: ",",
29
+ shifted: "<"
30
+ },
31
+ {
32
+ key: ".",
33
+ shifted: ">"
34
+ },
35
+ {
36
+ key: "?",
37
+ shifted: "?"
38
+ }
39
+ ],
40
+ [
41
+ "CapsLock",
42
+ {
43
+ key: "Space",
44
+ label: "Space",
45
+ width: 5
46
+ },
47
+ "ArrowLeft",
48
+ "ArrowRight",
49
+ "Delete"
50
+ ]
51
+ ], s = class extends i {
52
+ constructor(n = {}) {
53
+ let { target: a = null, rows: s = o, keyWidth: c = 44, ...l } = n;
54
+ if (super({
55
+ direction: "column",
56
+ gap: 4,
57
+ padding: 8,
58
+ background: "surface",
59
+ ...l
60
+ }), this.field = null, this.shifted = !1, this.caps = !1, this.keys = [], !(c > 0) || !Number.isFinite(c) || !s.length || s.some((e) => !e.length)) throw e("VirtualKeyboard needs positive keyWidth and non-empty rows; supply at least one key", "ui");
61
+ this.connect(a), this.role = "group", this.label = n.label ?? "On-screen keyboard";
62
+ for (let e of s) {
63
+ let n = new i({
64
+ direction: "row",
65
+ gap: 4
66
+ });
67
+ for (let i of e) {
68
+ let e = typeof i == "string" ? { key: i } : { ...i };
69
+ if (!e.key || typeof e.key != "string" || e.width !== void 0 && (!(e.width > 0) || !Number.isFinite(e.width))) throw t("Keyboard keys need non-empty text and positive width; supply a key string", "ui");
70
+ let a = new r({
71
+ text: e.label ?? e.key,
72
+ width: c * (e.width ?? 1),
73
+ height: 44,
74
+ padding: 4,
75
+ onClick: () => this.press(e.key)
76
+ });
77
+ this.keys.push({
78
+ spec: e,
79
+ button: a
80
+ }), n.add(a);
81
+ }
82
+ this.add(n);
83
+ }
84
+ this.refreshKeys();
85
+ }
86
+ get target() {
87
+ return this.field;
88
+ }
89
+ connect(e) {
90
+ if (e !== null && !(e instanceof a)) throw t("Connect a TextInput or null to the keyboard", "ui");
91
+ this.field = e;
92
+ }
93
+ attached(e) {
94
+ if (this.offFocus?.(), this.offFocus = void 0, e) {
95
+ let t = () => {
96
+ e.focused instanceof a && this.connect(e.focused);
97
+ };
98
+ this.offFocus = e.onFocus(t), t();
99
+ }
100
+ }
101
+ press(e) {
102
+ if (!this.active) return !1;
103
+ if (e === "Shift") return this.shifted = !this.shifted, this.refreshKeys(), !0;
104
+ if (e === "CapsLock") return this.caps = !this.caps, this.refreshKeys(), !0;
105
+ let t = this.field;
106
+ if (!t || !t.active || t.readOnly || t.composing) return !1;
107
+ let r = t.selectionStart, i = t.selectionEnd, a = [0, ...n(t.value).map((e) => e.index + e.cluster.length)], o = (e) => a.filter((t) => t < e).at(-1) ?? 0, s = (e) => a.find((t) => t > e) ?? t.value.length;
108
+ if (e === "Backspace" || e === "Delete") r === i && (e === "Backspace" ? r = o(r) : i = s(i)), t.setSelectionRange(r, i), t.insertText("");
109
+ else if ([
110
+ "ArrowLeft",
111
+ "ArrowRight",
112
+ "Home",
113
+ "End"
114
+ ].includes(e)) {
115
+ let n = e === "Home" ? 0 : e === "End" ? t.value.length : e === "ArrowLeft" ? r === i ? o(r) : r : r === i ? s(i) : i;
116
+ t.setSelectionRange(n);
117
+ } else if (e === "Enter") t.multiline ? t.insertText("\n") : t.emit("submit", { value: t.value });
118
+ else {
119
+ let n = this.keys.find((t) => t.spec.key === e)?.spec, r = e === "Space" ? " " : this.caption(n ?? { key: e });
120
+ t.insertText(r), this.shifted && (this.shifted = !1, this.refreshKeys());
121
+ }
122
+ return this.emit("keypress", { value: e }), !0;
123
+ }
124
+ caption(e) {
125
+ return (e.key.toLocaleUpperCase() === e.key.toLocaleLowerCase() ? this.shifted : this.shifted !== this.caps) ? e.shifted ?? e.key.toLocaleUpperCase() : e.key;
126
+ }
127
+ refreshKeys() {
128
+ for (let { spec: e, button: t } of this.keys) t.text = e.label ?? (e.key.length === 1 ? this.caption(e) : e.key), (e.key === "Shift" || e.key === "CapsLock") && (t.variant = (e.key === "Shift" ? this.shifted : this.caps) ? "primary" : "secondary");
129
+ }
130
+ };
131
+ //#endregion
132
+ export { s as VirtualKeyboard };