@margoengine/ui 0.0.0-stage → 1.0.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 +1224 -2
  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-Vs-YtiWv.js +1741 -0
  50. package/dist/renderer.d.ts +138 -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 +117 -4
package/dist/forms.js ADDED
@@ -0,0 +1,354 @@
1
+ import { n as e, r as t } from "./errors-CDgOgzCd.js";
2
+ import { hexColor as n } from "@margoengine/math/color";
3
+ import { Button as r, Dialog as i, List as a, Panel as o, Slider as s, Toggle as c, Widget as l, parseRgba as u } from "@margoengine/ui";
4
+ import { TextInput as d } from "@margoengine/ui/input";
5
+ //#region src/forms.ts
6
+ function f(e) {
7
+ if (!Array.isArray(e) || e.some((e) => !e || typeof e.label != "string" || typeof e.value != "string") || new Set(e.map((e) => e.value)).size !== e.length) throw t("Form options need labels and unique string values", "ui");
8
+ return e.map((e) => ({ ...e }));
9
+ }
10
+ var p = class extends r {
11
+ constructor({ options: e, value: t, onChange: n, ...r }) {
12
+ super({ ...r }), this.options = f(e), this._value = t ?? this.options.find((e) => !e.disabled)?.value ?? "", this.validate(this._value), this.refresh(), n && this.on("change", n);
13
+ }
14
+ validate(t) {
15
+ if (t !== "" && !this.options.some((e) => e.value === t)) throw e("Select value must name an option or be empty", "ui");
16
+ }
17
+ refresh() {
18
+ this.text = `${this.options.find((e) => e.value === this._value)?.label ?? "Choose"} \u25be`, this.ui?.touchMirror();
19
+ }
20
+ get value() {
21
+ return this._value;
22
+ }
23
+ set value(e) {
24
+ this.validate(e), this._value = e, this.refresh();
25
+ }
26
+ get expanded() {
27
+ return !!this.popup?.open;
28
+ }
29
+ get popupId() {
30
+ return this.popup?.id;
31
+ }
32
+ select(e, t = !1) {
33
+ let n = this.options.find((t) => t.value === e);
34
+ if (!this.active || !n || n.disabled) return;
35
+ let r = this.value !== e;
36
+ this.value = e, r && t && this.emit("change", {
37
+ value: e,
38
+ index: this.options.indexOf(n)
39
+ });
40
+ }
41
+ dispose() {
42
+ this.popup?.close(), this.popup = void 0, super.dispose();
43
+ }
44
+ command(e) {
45
+ if (e !== "up" && e !== "down" && e !== "left" && e !== "right") return !1;
46
+ let t = this.options.filter((e) => !e.disabled);
47
+ if (!t.length) return !0;
48
+ let n = t.findIndex((e) => e.value === this.value), r = e === "up" || e === "left" ? -1 : 1;
49
+ return this.select(t[(n + r + t.length) % t.length].value, !0), !0;
50
+ }
51
+ activate() {
52
+ if (!this.active || !this.ui || this.expanded) return;
53
+ let e = this.ui, t = new i({
54
+ title: this.label || "Choose an option",
55
+ buttons: [],
56
+ width: Math.min(400, e.width)
57
+ }), n = new a({
58
+ id: `${this.id}-options`,
59
+ label: this.label || "Options",
60
+ items: this.options.map((e) => ({
61
+ label: e.label,
62
+ value: e.value,
63
+ disabled: e.disabled
64
+ })),
65
+ maxHeight: Math.max(44, e.height - 100),
66
+ overflow: "scroll"
67
+ });
68
+ n.select(this.options.findIndex((e) => e.value === this.value)), n.on("select", (e) => {
69
+ this.select(String(e.item), !0), t.close();
70
+ }), t.add(n), this.popup = t, e.modalLayer.add(t), e.dialogOpened(t), e.focus(n.children.find((e, t) => e.enabled && this.options[t].value === this.value) ?? n.children.find((e) => e.enabled) ?? null, "keyboard"), this.invalidate(!1), t.result.then(() => {
71
+ this.popup === t && (this.popup = void 0, this.invalidate(!1));
72
+ });
73
+ }
74
+ }, m = class extends c {
75
+ constructor(e = {}) {
76
+ super({
77
+ ...e,
78
+ variant: "checkbox"
79
+ });
80
+ }
81
+ }, h = class extends r {
82
+ constructor(e, t, n) {
83
+ super(n), this.group = e, this.index = t;
84
+ }
85
+ activate() {
86
+ this.active && this.group.selectIndex(this.index, !0);
87
+ }
88
+ command(e) {
89
+ return this.group.move(this.index, e);
90
+ }
91
+ }, g = class extends o {
92
+ get buttons() {
93
+ return this.choiceButtons;
94
+ }
95
+ constructor({ options: t, value: n, onChange: r, orientation: i = "vertical", ...a }) {
96
+ if (super({
97
+ label: "Choices",
98
+ direction: i === "horizontal" ? "row" : "column",
99
+ gap: 8,
100
+ ...a
101
+ }), this.choiceButtons = [], this.options = f(t), this.orientation = i, this._value = n ?? this.options.find((e) => !e.disabled)?.value ?? "", this._value !== "" && !this.options.some((e) => e.value === this._value)) throw e("RadioGroup value must name an option", "ui");
102
+ this.options.forEach((e, t) => {
103
+ let n = new h(this, t, {
104
+ id: `${this.id}-choice-${t}`,
105
+ text: e.label,
106
+ enabled: !e.disabled
107
+ });
108
+ this.choiceButtons.push(n), this.add(n);
109
+ }), this.refresh(), r && this.on("change", r);
110
+ }
111
+ get value() {
112
+ return this._value;
113
+ }
114
+ set value(t) {
115
+ if (t !== "" && !this.options.some((e) => e.value === t)) throw e("Choice value must name an option", "ui");
116
+ this._value = t, this.refresh();
117
+ }
118
+ refresh() {
119
+ let e = this.options.findIndex((e) => e.value === this.value), t = this.options.findIndex((e) => !e.disabled), n = e >= 0 && !this.options[e].disabled ? e : t;
120
+ this.choiceButtons.forEach((t, r) => {
121
+ t.focusable = r === n, t.variant = r === e ? "primary" : "secondary", this instanceof _ || (t.text = `${r === e ? "◉" : "○"} ${this.options[r].label}`);
122
+ }), this.ui?.touchMirror();
123
+ }
124
+ selectIndex(e, t = !1) {
125
+ let n = this.options[e];
126
+ if (!this.active || !n || n.disabled) return;
127
+ let r = this.value !== n.value;
128
+ this.value = n.value, t && r && this.emit("change", {
129
+ value: this.value,
130
+ index: e
131
+ });
132
+ }
133
+ move(e, t) {
134
+ let n = t === "left" || t === "up";
135
+ if (!n && t !== "right" && t !== "down") return !1;
136
+ for (let t = 1; t <= this.options.length; t++) {
137
+ let r = (e + (n ? -t : t) + this.options.length) % this.options.length;
138
+ if (!this.options[r].disabled) {
139
+ this.selectIndex(r, !0), this.ui?.focus(this.choiceButtons[r], this.ui.inputMode);
140
+ break;
141
+ }
142
+ }
143
+ return !0;
144
+ }
145
+ }, _ = class extends g {
146
+ constructor({ options: e, ...t }) {
147
+ super({
148
+ ...t,
149
+ options: e,
150
+ orientation: t.orientation ?? "horizontal"
151
+ }), this.setStyle({ direction: "column" });
152
+ let n = new o({
153
+ id: `${this.id}-tablist`,
154
+ label: this.label,
155
+ direction: this.orientation === "horizontal" ? "row" : "column"
156
+ });
157
+ for (let e of this.choiceButtons) n.add(e);
158
+ this.add(n), this.panels = e.map((e, t) => new o({
159
+ id: `${this.id}-panel-${t}`,
160
+ label: e.label,
161
+ children: [e.panel]
162
+ })), this.add(...this.panels), this.refresh();
163
+ }
164
+ refresh() {
165
+ super.refresh(), this.panels && this.panels.forEach((e, t) => {
166
+ e.visible = this.options[t].value === this.value;
167
+ });
168
+ }
169
+ };
170
+ function v(e) {
171
+ let [t, n, r] = e, i = Math.max(t, n, r), a = i - Math.min(t, n, r);
172
+ return [
173
+ a === 0 ? 0 : (i === t ? (n - r) / a + (n < r ? 6 : 0) : i === n ? (r - t) / a + 2 : (t - n) / a + 4) / 6,
174
+ i === 0 ? 0 : a / i,
175
+ i
176
+ ];
177
+ }
178
+ function y(e, t, n) {
179
+ let r = (r) => {
180
+ let i = (r + e * 6) % 6;
181
+ return n - n * t * Math.max(0, Math.min(i, 4 - i, 1));
182
+ };
183
+ return [
184
+ r(5),
185
+ r(3),
186
+ r(1)
187
+ ];
188
+ }
189
+ var b = (e) => "#" + e.map((e) => Math.round(e * 255).toString(16).padStart(2, "0")).join(""), x = class extends l {
190
+ constructor(e) {
191
+ super({
192
+ width: "fill",
193
+ height: 140
194
+ }), this.picker = e, this.pointerEvents = "auto";
195
+ }
196
+ pointerDown(e, t) {
197
+ return this.active ? (this.pointerMove(e, t), !0) : !1;
198
+ }
199
+ pointerMove(e, t) {
200
+ let n = this.rect;
201
+ this.picker.setHSV(this.picker.hue, Math.max(0, Math.min(1, (e - n.x) / n.width)), 1 - Math.max(0, Math.min(1, (t - n.y) / n.height)));
202
+ }
203
+ draw({ list: e }) {
204
+ let t = this.rect;
205
+ for (let n = 0; n < 64; n++) for (let r = 0; r < 64; r++) e.rect(t.x + r * t.width / 64, t.y + n * t.height / 64, t.width / 64 + .5, t.height / 64 + .5, { fill: [...y(this.picker.hue, (r + .5) / 64, 1 - (n + .5) / 64), 1] });
206
+ let [, , n] = v(u(this.picker.value)), r = this.picker.saturation;
207
+ e.rect(t.x + r * t.width - 4, t.y + (1 - n) * t.height - 4, 8, 8, {
208
+ border: [
209
+ 0,
210
+ 0,
211
+ 0,
212
+ 1
213
+ ],
214
+ borderWidth: 2
215
+ }), e.rect(t.x + r * t.width - 2, t.y + (1 - n) * t.height - 2, 4, 4, {
216
+ border: [
217
+ 1,
218
+ 1,
219
+ 1,
220
+ 1
221
+ ],
222
+ borderWidth: 1
223
+ });
224
+ }
225
+ }, S = class extends o {
226
+ constructor({ value: e = "#ffffff", onChange: t, ...r } = {}) {
227
+ super({
228
+ label: "Colour",
229
+ width: 280,
230
+ gap: 6,
231
+ ...r
232
+ }), this.rgba = [
233
+ 1,
234
+ 1,
235
+ 1,
236
+ 1
237
+ ], this.hueValue = 0, this.saturationValue = 0, this.channels = [], this.rgba = u(e), [this.hueValue, this.saturationValue] = v(this.rgba), this.add(new x(this));
238
+ for (let e of [
239
+ "Hue",
240
+ "Saturation",
241
+ "Value",
242
+ "Red",
243
+ "Green",
244
+ "Blue",
245
+ "Alpha"
246
+ ]) {
247
+ let t = this.channels.length, n = new s({
248
+ text: e,
249
+ min: 0,
250
+ max: 1,
251
+ step: 1 / 255,
252
+ onChange: (e) => {
253
+ let n = Number(e.value), r = [
254
+ this.hue,
255
+ this.saturation,
256
+ v(this.rgba)[2]
257
+ ];
258
+ if (t < 3) r[t] = n, this.setHSV(...r);
259
+ else {
260
+ let e = [...this.rgba];
261
+ e[t - 3] = n, this.change(e);
262
+ }
263
+ }
264
+ });
265
+ this.channels.push(n), this.add(n);
266
+ }
267
+ this.hexInput = new d({
268
+ label: "Hex colour",
269
+ maxLength: 9,
270
+ onChange: () => {
271
+ let e = this.hexInput.value, t = e.length === 7 || e.length === 9 ? n(e) : void 0;
272
+ t && this.change(t, !1);
273
+ }
274
+ }), this.add(this.hexInput), this.refresh(), t && this.on("change", t);
275
+ }
276
+ get value() {
277
+ return b(this.rgba);
278
+ }
279
+ set value(e) {
280
+ this.rgba = u(e), this.keep(), this.refresh();
281
+ }
282
+ get hue() {
283
+ return this.hueValue;
284
+ }
285
+ get saturation() {
286
+ return this.saturationValue;
287
+ }
288
+ setHSV(t, n, r) {
289
+ if (![
290
+ t,
291
+ n,
292
+ r
293
+ ].every((e) => Number.isFinite(e) && e >= 0 && e <= 1)) throw e("HSV channels must be in [0,1]", "ui");
294
+ this.active && (this.hueValue = t, this.saturationValue = n, this.change([...y(t, n, r), this.rgba[3]]));
295
+ }
296
+ change(e, t = !0) {
297
+ if (!this.active) return;
298
+ let n = this.value;
299
+ this.rgba = e, this.keep(), this.refresh(t), this.value !== n && this.emit("change", { value: this.value });
300
+ }
301
+ keep() {
302
+ let [e, t, n] = v(this.rgba);
303
+ t > 0 && (this.hueValue = e), n > 0 && (this.saturationValue = t);
304
+ }
305
+ refresh(e = !0) {
306
+ let t = [
307
+ this.hue,
308
+ this.saturation,
309
+ v(this.rgba)[2],
310
+ ...this.rgba
311
+ ];
312
+ this.channels.forEach((e, n) => {
313
+ e.value = t[n];
314
+ }), e && this.hexInput && (this.hexInput.value = this.value), this.invalidate(!1);
315
+ }
316
+ };
317
+ function C(e, t, n = {}) {
318
+ let r = !1, i = !1, a = 0, o = !1, s = () => {
319
+ if (i = !1, a = 0, r) return;
320
+ let n = !1, s = (e) => {
321
+ let r = t.ownerDocument.getElementById(`margo-ui-${e.id}`);
322
+ if (r && (e instanceof p && (n ||= e.expanded, r.setAttribute("role", "combobox"), r.setAttribute("aria-haspopup", "dialog"), r.setAttribute("aria-expanded", String(e.expanded)), r.setAttribute("aria-label", e.label || "Select"), r.textContent = e.options.find((t) => t.value === e.value)?.label ?? "Choose", e.popupId ? r.setAttribute("aria-controls", `margo-ui-${e.popupId}`) : r.removeAttribute("aria-controls")), e instanceof g)) {
323
+ r.setAttribute("role", e instanceof _ ? "group" : "radiogroup");
324
+ let n = e instanceof _ ? t.ownerDocument.getElementById(`margo-ui-${e.id}-tablist`) : r;
325
+ n && (n.setAttribute("role", e instanceof _ ? "tablist" : "radiogroup"), n.setAttribute("aria-orientation", e.orientation)), e.buttons.forEach((n, r) => {
326
+ let i = t.ownerDocument.getElementById(`margo-ui-${n.id}`);
327
+ if (!i) return;
328
+ let a = e.value === e.options[r].value;
329
+ if (i.setAttribute("role", e instanceof _ ? "tab" : "radio"), i.setAttribute("aria-label", e.options[r].label), i.setAttribute(e instanceof _ ? "aria-selected" : "aria-checked", String(a)), i.tabIndex = n.focusable ? 0 : -1, e instanceof _) {
330
+ let n = e.panels[r];
331
+ i.setAttribute("aria-controls", `margo-ui-${n.id}`);
332
+ let a = t.ownerDocument.getElementById(`margo-ui-${n.id}`);
333
+ a && (a.setAttribute("role", "tabpanel"), a.setAttribute("aria-labelledby", i.id));
334
+ }
335
+ });
336
+ }
337
+ };
338
+ if (e.root.walk(s), e.modalLayer.walk(s), (n || o) && e.focused) {
339
+ let n = t.ownerDocument.getElementById(`margo-ui-${e.focused.id}`);
340
+ n && n !== t.ownerDocument.activeElement && n.focus({ preventScroll: !0 });
341
+ }
342
+ o = n;
343
+ }, c = () => {
344
+ if (i || r) return;
345
+ i = !0;
346
+ let e = globalThis.requestAnimationFrame;
347
+ e ? a = e(s) : queueMicrotask(s);
348
+ }, l = e.onChange(c), u = e.onFocus(c), d = (() => {
349
+ r || (r = !0, a && globalThis.cancelAnimationFrame && globalThis.cancelAnimationFrame(a), l(), u(), n.signal?.removeEventListener("abort", d));
350
+ });
351
+ return d[Symbol.dispose ?? Symbol.for("Symbol.dispose")] = d, n.signal?.aborted ? d() : n.signal?.addEventListener("abort", d, { once: !0 }), c(), d;
352
+ }
353
+ //#endregion
354
+ export { m as Checkbox, S as ColorPicker, g as RadioGroup, p as Select, _ as Tabs, C as attachFormsMirror };
@@ -0,0 +1,165 @@
1
+ import { n as e, t } from "./errors-CDgOgzCd.js";
2
+ import { s as n } from "./dom-CWJhpotq.js";
3
+ import { SDF_DEFAULTS as r, ShelfPacker as i, releaseSlot as a } from "@margoengine/text";
4
+ //#region src/gl.ts
5
+ var o = "#version 300 es\nprecision highp float;\nprecision highp int;\n", s = "uniform vec4 frame; // width, height, linear, 0\nuniform vec2 sdf;\nvec2 corner(int vi) { return vec2(float((0x32 >> vi) & 1), float((0x2c >> vi) & 1)); }\nvec4 ndc(vec2 p) { return vec4(p.x / frame.x * 2.0 - 1.0, 1.0 - p.y / frame.y * 2.0, 0.0, 1.0); }\nvec3 toLinear(vec3 c) { return mix(pow((c + 0.055) / 1.055, vec3(2.4)), c / 12.92, vec3(lessThanEqual(c, vec3(0.04045)))); }\nvec4 prep(vec4 c) { vec3 rgb = frame.z > 0.5 ? toLinear(c.rgb) : c.rgb; return vec4(rgb * c.a, c.a); }\n", c = `${o}${s}
6
+ layout(location = 0) in vec4 box;
7
+ layout(location = 1) in vec4 fill;
8
+ layout(location = 2) in vec4 border;
9
+ layout(location = 3) in vec4 params;
10
+ out vec2 local;
11
+ flat out vec4 vBox;
12
+ flat out vec4 vFill;
13
+ flat out vec4 vBorder;
14
+ flat out vec4 vParams;
15
+ void main() {
16
+ float pad = params.z * 2.0 + 2.0;
17
+ vec2 p = mix(box.xy - vec2(pad), box.xy + box.zw + vec2(pad), corner(gl_VertexID));
18
+ gl_Position = ndc(p);
19
+ local = p - (box.xy + box.zw * 0.5);
20
+ vBox = box; vFill = fill; vBorder = border; vParams = params;
21
+ }`, l = `${o}${s}
22
+ in vec2 local;
23
+ flat in vec4 vBox;
24
+ flat in vec4 vFill;
25
+ flat in vec4 vBorder;
26
+ flat in vec4 vParams;
27
+ out vec4 color;
28
+ void main() {
29
+ vec2 ext = vBox.zw * 0.5;
30
+ float radius = min(vParams.x, min(ext.x, ext.y));
31
+ vec2 q = abs(local) - ext + vec2(radius);
32
+ float d = length(max(q, vec2(0.0))) + min(max(q.x, q.y), 0.0) - radius;
33
+ float cover = clamp(0.5 - d, 0.0, 1.0);
34
+ if (vParams.z > 0.0) cover = 1.0 - smoothstep(-vParams.z, vParams.z, d);
35
+ float inner = 1.0;
36
+ if (vParams.y > 0.0) inner = clamp(0.5 - (d + vParams.y), 0.0, 1.0);
37
+ color = mix(prep(vBorder), prep(vFill), inner) * cover;
38
+ }`, u = `${o}${s}
39
+ layout(location = 0) in vec4 box;
40
+ layout(location = 1) in vec4 uv;
41
+ layout(location = 2) in vec4 tint;
42
+ layout(location = 3) in vec4 info;
43
+ out vec2 vUv;
44
+ flat out vec4 vTint;
45
+ flat out vec4 vInfo;
46
+ void main() {
47
+ vec2 c = corner(gl_VertexID);
48
+ gl_Position = ndc(mix(box.xy, box.zw, c));
49
+ vUv = vec2(mix(uv.x, uv.z, c.x), mix(uv.y, uv.w, c.y));
50
+ vTint = tint; vInfo = info;
51
+ }`, d = `${o}${s}
52
+ uniform highp sampler2DArray sdfAtlas;
53
+ uniform highp sampler2DArray colorAtlas;
54
+ in vec2 vUv;
55
+ flat in vec4 vTint;
56
+ flat in vec4 vInfo;
57
+ out vec4 color;
58
+ void main() {
59
+ vec2 texels = vUv * vec2(textureSize(sdfAtlas, 0).xy);
60
+ float w = max(0.5 * (length(dFdx(texels)) + length(dFdy(texels))), 1e-3);
61
+ float texel = textureLod(sdfAtlas, vec3(vUv, vInfo.x), 0.0).r;
62
+ vec4 t = textureLod(colorAtlas, vec3(vUv, vInfo.x), 0.0);
63
+ if (vInfo.y > 0.5) {
64
+ if (frame.z > 0.5 && t.a > 0.0) t = vec4(toLinear(t.rgb / t.a) * t.a, t.a);
65
+ if (vInfo.y > 1.5) {
66
+ vec3 tint = frame.z > 0.5 ? toLinear(vTint.rgb) : vTint.rgb;
67
+ color = vec4(t.rgb * tint, t.a) * vTint.a;
68
+ return;
69
+ }
70
+ color = t * vTint.a;
71
+ return;
72
+ }
73
+ float d = ((1.0 - texel) - sdf.y) * sdf.x;
74
+ float field = clamp(texel * 127.5, 0.0, 1.0);
75
+ float cover = clamp(0.5 - (d - vInfo.z * w) / w, 0.0, 1.0) * field;
76
+ color = prep(vec4(vTint.rgb, vTint.a * cover));
77
+ }`, f = class {
78
+ constructor(e, t, n, r) {
79
+ this.gl = e, this.rgba = t, this.slots = /* @__PURE__ */ new Map(), this.generation = 0, this.texture = null, this.layers = 0, this.packer = new i(n, r, 1);
80
+ }
81
+ get size() {
82
+ return this.packer.size;
83
+ }
84
+ bind(e) {
85
+ let t = this.gl;
86
+ t.activeTexture(t.TEXTURE0 + e), this.texture || this.allocate(0), t.bindTexture(t.TEXTURE_2D_ARRAY, this.texture);
87
+ }
88
+ place(t) {
89
+ let n = this.slots.get(t);
90
+ if (n) return n;
91
+ let r = this.packer.pack(t.width, t.height);
92
+ if (!r) throw e(`Glyph atlas is full (${this.packer.maxPages} pages of ${this.size}px); raise maxPages or atlasSize in UiRenderer options`, "ui");
93
+ return n = r, this.slots.set(t, n), n.page >= this.layers ? this.allocate(Math.min(this.packer.maxPages, Math.max(n.page + 1, this.layers * 2))) : this.upload(t, n), n;
94
+ }
95
+ release(e) {
96
+ return a(this.slots, this.packer, e) !== void 0;
97
+ }
98
+ allocate(e) {
99
+ let t = this.gl, n = e ? this.size : 1;
100
+ if (this.texture && t.deleteTexture(this.texture), this.texture = t.createTexture(), t.bindTexture(t.TEXTURE_2D_ARRAY, this.texture), t.texStorage3D(t.TEXTURE_2D_ARRAY, 1, this.rgba ? t.RGBA8 : t.R8, n, n, Math.max(1, e)), t.texParameteri(t.TEXTURE_2D_ARRAY, t.TEXTURE_MIN_FILTER, t.LINEAR), t.texParameteri(t.TEXTURE_2D_ARRAY, t.TEXTURE_MAG_FILTER, t.LINEAR), t.texParameteri(t.TEXTURE_2D_ARRAY, t.TEXTURE_WRAP_S, t.CLAMP_TO_EDGE), t.texParameteri(t.TEXTURE_2D_ARRAY, t.TEXTURE_WRAP_T, t.CLAMP_TO_EDGE), this.layers = e, e && this.generation++, e) for (let [e, t] of this.slots) this.upload(e, t);
101
+ }
102
+ upload(e, t) {
103
+ let n = this.gl;
104
+ n.bindTexture(n.TEXTURE_2D_ARRAY, this.texture), n.bindBuffer(n.PIXEL_UNPACK_BUFFER, null), n.pixelStorei(n.UNPACK_ALIGNMENT, 1), n.pixelStorei(n.UNPACK_ROW_LENGTH, 0), n.pixelStorei(n.UNPACK_SKIP_ROWS, 0), n.pixelStorei(n.UNPACK_SKIP_PIXELS, 0), n.pixelStorei(n.UNPACK_SKIP_IMAGES, 0), n.pixelStorei(n.UNPACK_IMAGE_HEIGHT, 0), n.pixelStorei(n.UNPACK_FLIP_Y_WEBGL, !1), n.pixelStorei(n.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !1), n.texSubImage3D(n.TEXTURE_2D_ARRAY, 0, t.x, t.y, t.page, e.width, e.height, 1, this.rgba ? n.RGBA : n.RED, n.UNSIGNED_BYTE, e.data);
105
+ }
106
+ dispose() {
107
+ this.texture && this.gl.deleteTexture(this.texture), this.texture = null, this.layers = 0, this.slots.clear();
108
+ }
109
+ }, p = class {
110
+ constructor(e, t, r) {
111
+ this.gl = e, this.stats = r, this.batch = new n(), this.uploaded = -1, this.uploadedAtlas = "";
112
+ let i = e.getParameter(e.MAX_TEXTURE_SIZE), a = e.getParameter(e.MAX_ARRAY_TEXTURE_LAYERS);
113
+ this.sdfAtlas = new f(e, !1, Math.min(t.atlasSize, i), Math.min(t.maxPages, a)), this.colorAtlas = new f(e, !0, Math.min(t.colorAtlasSize, i), Math.min(t.maxPages, a)), this.rect = this.program(c, l), this.glyph = this.program(u, d), e.useProgram(this.glyph.program), e.uniform1i(e.getUniformLocation(this.glyph.program, "sdfAtlas"), 0), e.uniform1i(e.getUniformLocation(this.glyph.program, "colorAtlas"), 1);
114
+ }
115
+ program(e, n) {
116
+ let r = this.gl, i = r.createProgram();
117
+ for (let [t, a] of [[r.VERTEX_SHADER, e], [r.FRAGMENT_SHADER, n]]) {
118
+ let e = r.createShader(t);
119
+ r.shaderSource(e, a), r.compileShader(e), r.attachShader(i, e), r.deleteShader(e);
120
+ }
121
+ if (r.linkProgram(i), !r.getProgramParameter(i, r.LINK_STATUS) && !r.isContextLost()) throw t("failed", `@margoengine/ui could not build its WebGL2 shaders (${r.getProgramInfoLog(i)}); report this with your browser and GPU`, "ui");
122
+ let a = r.createVertexArray(), o = r.createBuffer();
123
+ r.bindVertexArray(a), r.bindBuffer(r.ARRAY_BUFFER, o);
124
+ for (let e = 0; e < 4; e++) r.enableVertexAttribArray(e), r.vertexAttribDivisor(e, 1);
125
+ return r.bindVertexArray(null), {
126
+ program: i,
127
+ vao: a,
128
+ buffer: o,
129
+ frame: r.getUniformLocation(i, "frame"),
130
+ sdf: r.getUniformLocation(i, "sdf"),
131
+ capacity: 0
132
+ };
133
+ }
134
+ upload(e, t, n) {
135
+ let r = this.gl;
136
+ r.bindBuffer(r.ARRAY_BUFFER, e.buffer), n > e.capacity && (e.capacity = Math.max(n, e.capacity * 2, 1024), r.bufferData(r.ARRAY_BUFFER, e.capacity * 4, r.DYNAMIC_DRAW)), n && r.bufferSubData(r.ARRAY_BUFFER, 0, t, 0, n);
137
+ }
138
+ draw(e, t, n, i = null) {
139
+ let a = this.gl, o = this.batch, s = this.stats;
140
+ s.rects = s.glyphs = s.drawCalls = s.uploads = 0;
141
+ let c = () => `${this.sdfAtlas.generation}|${this.colorAtlas.generation}`;
142
+ if ((this.uploaded !== e.version || this.uploadedAtlas !== c()) && (o.build(e, this.sdfAtlas, this.colorAtlas), this.upload(this.rect, o.rects, o.rectCount * 16), this.upload(this.glyph, o.glyphs, o.glyphCount * 16), s.uploads++, this.uploaded = e.version, this.uploadedAtlas = c()), s.rects = o.rectCount, s.glyphs = o.glyphCount, !o.runs.length) return;
143
+ let l = a.isEnabled(a.DEPTH_TEST), u = a.isEnabled(a.CULL_FACE);
144
+ a.bindFramebuffer(a.FRAMEBUFFER, i), a.viewport(0, 0, t, n), a.disable(a.DEPTH_TEST), a.disable(a.CULL_FACE), a.enable(a.BLEND), a.blendFuncSeparate(a.ONE, a.ONE_MINUS_SRC_ALPHA, a.ONE, a.ONE_MINUS_SRC_ALPHA), a.blendEquation(a.FUNC_ADD), a.enable(a.SCISSOR_TEST), this.sdfAtlas.bind(0), this.colorAtlas.bind(1), this.uploadedAtlas = c();
145
+ let d;
146
+ for (let e of o.runs) {
147
+ let i = e.glyphs ? this.glyph : this.rect;
148
+ e.glyphs, i !== d && (a.useProgram(i.program), a.uniform4f(i.frame, t, n, 0, 0), a.uniform2f(i.sdf, r.radius, r.cutoff), a.bindVertexArray(i.vao), a.bindBuffer(a.ARRAY_BUFFER, i.buffer), d = i);
149
+ let o = e.clip, c = o ? Math.max(0, Math.min(t, o[0])) : 0, l = o ? Math.max(0, Math.min(n, o[1])) : 0, u = o ? Math.max(0, Math.min(t - c, o[2] - (c - o[0]))) : t, f = o ? Math.max(0, Math.min(n - l, o[3] - (l - o[1]))) : n;
150
+ if (u && f) {
151
+ a.scissor(c, n - l - f, u, f);
152
+ for (let t = 0; t < 4; t++) a.vertexAttribPointer(t, 4, a.FLOAT, !1, 64, e.first * 64 + t * 16);
153
+ a.drawArraysInstanced(a.TRIANGLES, 0, 6, e.count), s.drawCalls++;
154
+ }
155
+ }
156
+ a.bindVertexArray(null), a.disable(a.SCISSOR_TEST), a.disable(a.BLEND), l && a.enable(a.DEPTH_TEST), u && a.enable(a.CULL_FACE);
157
+ }
158
+ dispose() {
159
+ let e = this.gl;
160
+ for (let t of [this.rect, this.glyph]) e.deleteProgram(t.program), e.deleteVertexArray(t.vao), e.deleteBuffer(t.buffer);
161
+ this.sdfAtlas.dispose(), this.colorAtlas.dispose();
162
+ }
163
+ };
164
+ //#endregion
165
+ export { p as t };
@@ -0,0 +1,5 @@
1
+ import { t as e } from "./gl-bb768Mje.js";
2
+ //#region src/gl-load.ts
3
+ var t = (...t) => new e(...t);
4
+ //#endregion
5
+ export { t as glPainter };
@@ -0,0 +1,3 @@
1
+ import { GlUiPainter } from './gl.js';
2
+ /** Creates the WebGL2 UI painter. */
3
+ export declare const glPainter: (...args: ConstructorParameters<typeof GlUiPainter>) => GlUiPainter;
package/dist/gl.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import type { DrawList } from './draw.js';
2
+ import { type PaintStats } from './painter.js';
3
+ /**
4
+ * The WebGL2 drawing core of the screen UI, for core's WebGL2 preview backend: the same instances as the WebGPU
5
+ * painter (one {@link UiBatch}), drawn with instanced attributes, the attribute offsets moved to each run's first
6
+ * instance since WebGL2 has no base instance, and one scissor per clip region.
7
+ * @internal
8
+ */
9
+ export declare class GlUiPainter {
10
+ readonly gl: WebGL2RenderingContext;
11
+ private readonly stats;
12
+ private readonly batch;
13
+ private readonly sdfAtlas;
14
+ private readonly colorAtlas;
15
+ private readonly rect;
16
+ private readonly glyph;
17
+ private uploaded;
18
+ private uploadedAtlas;
19
+ constructor(gl: WebGL2RenderingContext, options: {
20
+ atlasSize: number;
21
+ colorAtlasSize: number;
22
+ maxPages: number;
23
+ }, stats: PaintStats);
24
+ private program;
25
+ private upload;
26
+ /** Draw `list` into `framebuffer` (default: canvas), `width` by `height` pixels. */
27
+ draw(list: Pick<DrawList, 'commands' | 'version'>, width: number, height: number, framebuffer?: WebGLFramebuffer | null): void;
28
+ /** Delete the GL objects; skipped work on a lost context is harmless. */
29
+ dispose(): void;
30
+ }
package/dist/i18n.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { Ui } from '@margoengine/ui';
3
+ /** A message is plain text or cardinal plural forms. Every plural message needs other. */
4
+ export type LocalizedMessage = string | ({
5
+ other: string;
6
+ } & Partial<Record<Intl.LDMLPluralRule | `=${number}`, string>>);
7
+ /** A locale's flat string-key table. Keys may contain dots. */
8
+ export type LocaleTable = Readonly<Record<string, LocalizedMessage>>;
9
+ /** Named substitutions; count also chooses plural forms. */
10
+ export type MessageParameters = Readonly<Record<string, string | number>>;
11
+ /** Locale selection and table options. */
12
+ export interface LocalizationOptions {
13
+ /** Initial locale, default first table locale (or en with no tables). */
14
+ locale?: string;
15
+ /** Final fallback locale, default first table locale (or en). Parent locales are tried first. */
16
+ fallback?: string;
17
+ /** Optional explicit directions by locale. Default inferred from the locale's maximized script. */
18
+ directions?: Readonly<Record<string, 'ltr' | 'rtl'>>;
19
+ /** Default 'key'; 'error' throws a linked error for missing keys or parameters. */
20
+ missing?: 'key' | 'error';
21
+ }
22
+ /** A translation assigned through a normal widget setter, which invalidates layout and accessibility mirrors. */
23
+ export interface TranslationBinding {
24
+ target: {
25
+ text?: string;
26
+ label?: string;
27
+ placeholder?: string;
28
+ };
29
+ key: string;
30
+ /** Default text. */
31
+ property?: 'text' | 'label' | 'placeholder';
32
+ /** Re-evaluated on locale changes or refresh(). */
33
+ parameters?: MessageParameters | (() => MessageParameters);
34
+ }
35
+ /** Locale tables with parent/fallback lookup, plural selection, substitutions and live widget bindings. */
36
+ export declare class Localization {
37
+ private readonly tables;
38
+ private readonly listeners;
39
+ private readonly bindings;
40
+ private readonly directions;
41
+ private current;
42
+ readonly fallback: string;
43
+ private readonly missing;
44
+ constructor(tables?: Readonly<Record<string, LocaleTable>>, options?: LocalizationOptions);
45
+ get locale(): string;
46
+ set locale(locale: string);
47
+ /** Explicit locale metadata wins; otherwise use the maximized Unicode script (ar-Latn stays LTR). */
48
+ get direction(): 'ltr' | 'rtl';
49
+ /** Replace a table atomically. Input tables are cloned so later caller mutations cannot bypass relayout. */
50
+ setTable(locale: string, table: LocaleTable): void;
51
+ private parents;
52
+ /** Resolve a key, using exact-count then Intl cardinal plural forms and {name} substitutions. */
53
+ t(key: string, parameters?: MessageParameters): string;
54
+ /** Reapply translations after changing dynamic parameters. */
55
+ refresh(): void;
56
+ /** Subscribe to table/locale changes. Unsubscribe is disposable. */
57
+ onChange(listener: () => void): (() => void) & {
58
+ [Symbol.dispose](): void;
59
+ };
60
+ /** Apply now and on every locale/table change. Layout recomputes on the UI's next frame. */
61
+ bind(ui: Ui, bindings: readonly TranslationBinding[], options?: {
62
+ signal?: AbortSignal;
63
+ }): (() => void) & {
64
+ [Symbol.dispose](): void;
65
+ };
66
+ /** Detach every binding/subscriber; does not dispose the widgets. */
67
+ dispose(): void;
68
+ [Symbol.dispose]: () => void;
69
+ }