@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +708 -3
- package/dist/basic.d.ts +87 -0
- package/dist/collections.d.ts +180 -0
- package/dist/color.d.ts +24 -0
- package/dist/controls.d.ts +130 -0
- package/dist/curved.d.ts +41 -0
- package/dist/curved.js +105 -0
- package/dist/dom-CWJhpotq.js +340 -0
- package/dist/dom-host.d.ts +67 -0
- package/dist/dom-host.js +563 -0
- package/dist/dom-spec.d.ts +21 -0
- package/dist/dom.d.ts +63 -0
- package/dist/draw.d.ts +107 -0
- package/dist/editor.d.ts +83 -0
- package/dist/editor.js +323 -0
- package/dist/errors-CDgOgzCd.js +7 -0
- package/dist/focus.d.ts +12 -0
- package/dist/forms.d.ts +114 -0
- package/dist/forms.js +354 -0
- package/dist/gl-bb768Mje.js +165 -0
- package/dist/gl-load-wwKmojhb.js +5 -0
- package/dist/gl-load.d.ts +3 -0
- package/dist/gl.d.ts +30 -0
- package/dist/i18n.d.ts +69 -0
- package/dist/i18n.js +117 -0
- package/dist/image.d.ts +74 -0
- package/dist/image.js +295 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +4 -0
- package/dist/input-dom.d.ts +14 -0
- package/dist/input.d.ts +92 -0
- package/dist/input.js +315 -0
- package/dist/interaction.d.ts +106 -0
- package/dist/keyboard.d.ts +40 -0
- package/dist/keyboard.js +132 -0
- package/dist/layout.d.ts +116 -0
- package/dist/markup.d.ts +89 -0
- package/dist/markup.js +369 -0
- package/dist/meters-yaZRtwsX.js +1401 -0
- package/dist/meters.d.ts +72 -0
- package/dist/mirror.d.ts +30 -0
- package/dist/overlays.d.ts +89 -0
- package/dist/painter.d.ts +115 -0
- package/dist/pass-CAn29pqc.js +15 -0
- package/dist/react.d.ts +99 -0
- package/dist/react.js +246 -0
- package/dist/renderer-Bdhc_0Cu.js +1653 -0
- package/dist/renderer.d.ts +92 -0
- package/dist/rsc/react.js +3 -0
- package/dist/shader.d.ts +6 -0
- package/dist/theme.d.ts +104 -0
- package/dist/ui.d.ts +328 -0
- package/dist/virtual.d.ts +81 -0
- package/dist/widget.d.ts +196 -0
- package/dist/world-gl.d.ts +32 -0
- package/dist/world-gl.js +195 -0
- package/dist/world.d.ts +216 -0
- package/dist/world.js +561 -0
- package/package.json +119 -6
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { SDF_DEFAULTS as e, glyphAtlases as t } from "@margoengine/text";
|
|
5
|
+
//#region src/shader.ts
|
|
6
|
+
var n = "\nstruct Frame { size: vec2f, linear: f32, pad: f32, sdf: vec2f, pad2: vec2f }\nstruct Rect { box: vec4f, fill: vec4f, border: vec4f, params: vec4f }\nstruct Glyph { box: vec4f, uv: vec4f, color: vec4f, info: vec4f }\n@group(0) @binding(0) var<uniform> frame: Frame;\n@group(0) @binding(1) var atlasSampler: sampler;\n@group(0) @binding(2) var sdfAtlas: texture_2d_array<f32>;\n@group(0) @binding(3) var colorAtlas: texture_2d_array<f32>;\n@group(0) @binding(4) var<storage, read> rects: array<Rect>;\n@group(0) @binding(5) var<storage, read> glyphs: array<Glyph>;\n\n// Two triangles: corners 0 (0,0), 1 (1,0), 2 (0,1), 3 (0,1), 4 (1,0), 5 (1,1).\nfn corner(vi: u32) -> vec2f {\n return vec2f(f32((0x32u >> vi) & 1u), f32((0x2cu >> vi) & 1u));\n}\nfn ndc(p: vec2f) -> vec4f {\n return vec4f(p.x / frame.size.x * 2.0 - 1.0, 1.0 - p.y / frame.size.y * 2.0, 0.0, 1.0);\n}\nfn toLinear(c: vec3f) -> vec3f {\n return select(pow((c + 0.055) / 1.055, vec3f(2.4)), c / 12.92, c <= vec3f(0.04045));\n}\n// Straight sRGB colour to premultiplied colour in the target's space.\nfn prep(c: vec4f) -> vec4f {\n let rgb = select(c.rgb, toLinear(c.rgb), frame.linear > 0.5);\n return vec4f(rgb * c.a, c.a);\n}\n\nstruct RectOut {\n @builtin(position) clip: vec4f,\n @location(0) local: vec2f,\n @location(1) @interpolate(flat) index: u32,\n}\n@vertex fn rectVertex(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> RectOut {\n let r = rects[ii];\n let pad = r.params.z * 2.0 + 2.0;\n let p = mix(r.box.xy - vec2f(pad), r.box.xy + r.box.zw + vec2f(pad), corner(vi));\n var o: RectOut;\n o.clip = ndc(p);\n o.local = p - (r.box.xy + r.box.zw * 0.5);\n o.index = ii;\n return o;\n}\n@fragment fn rectFragment(i: RectOut) -> @location(0) vec4f {\n let r = rects[i.index];\n let ext = r.box.zw * 0.5;\n let radius = min(r.params.x, min(ext.x, ext.y));\n let q = abs(i.local) - ext + vec2f(radius);\n let d = length(max(q, vec2f(0.0))) + min(max(q.x, q.y), 0.0) - radius;\n var cover = clamp(0.5 - d, 0.0, 1.0);\n if (r.params.z > 0.0) { cover = 1.0 - smoothstep(-r.params.z, r.params.z, d); }\n var inner = 1.0;\n if (r.params.y > 0.0) { inner = clamp(0.5 - (d + r.params.y), 0.0, 1.0); }\n return mix(prep(r.border), prep(r.fill), inner) * cover;\n}\n\nstruct GlyphOut {\n @builtin(position) clip: vec4f,\n @location(0) uv: vec2f,\n @location(1) @interpolate(flat) page: u32,\n @location(2) @interpolate(flat) index: u32,\n}\n@vertex fn glyphVertex(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> GlyphOut {\n let g = glyphs[ii];\n let c = corner(vi);\n var o: GlyphOut;\n o.clip = ndc(mix(g.box.xy, g.box.zw, c));\n o.uv = vec2f(mix(g.uv.x, g.uv.z, c.x), mix(g.uv.y, g.uv.w, c.y));\n o.page = u32(g.info.x);\n o.index = ii;\n return o;\n}\n@fragment fn glyphFragment(i: GlyphOut) -> @location(0) vec4f {\n let g = glyphs[i.index];\n // Screen-space derivatives first, in uniform control flow: texels of the field per screen pixel.\n let texels = i.uv * vec2f(textureDimensions(sdfAtlas).xy);\n let w = max(0.5 * (length(dpdx(texels)) + length(dpdy(texels))), 1e-3);\n let texel = textureSampleLevel(sdfAtlas, atlasSampler, i.uv, i.page, 0.0).r;\n var tint = textureSampleLevel(colorAtlas, atlasSampler, i.uv, i.page, 0.0);\n if (g.info.y > 0.5) {\n // Colour glyph (emoji): premultiplied sRGB texels, no field.\n if (frame.linear > 0.5 && tint.a > 0.0) { tint = vec4f(toLinear(tint.rgb / tint.a) * tint.a, tint.a); }\n if (g.info.y > 1.5) {\n var color = g.color.rgb;\n if (frame.linear > 0.5) { color = toLinear(color); }\n return vec4f(tint.rgb * color, tint.a) * g.color.a;\n }\n return tint * g.color.a;\n }\n // Signed distance in texels, positive outside; the outline grows it by info.z screen pixels.\n let d = ((1.0 - texel) - frame.sdf.y) * frame.sdf.x;\n let field = clamp(texel * 127.5, 0.0, 1.0);\n let cover = clamp(0.5 - (d - g.info.z * w) / w, 0.0, 1.0) * field;\n return prep(vec4f(g.color.rgb, g.color.a * cover));\n}\n", r = {
|
|
7
|
+
color: {
|
|
8
|
+
srcFactor: "one",
|
|
9
|
+
dstFactor: "one-minus-src-alpha",
|
|
10
|
+
operation: "add"
|
|
11
|
+
},
|
|
12
|
+
alpha: {
|
|
13
|
+
srcFactor: "one",
|
|
14
|
+
dstFactor: "one-minus-src-alpha",
|
|
15
|
+
operation: "add"
|
|
16
|
+
}
|
|
17
|
+
}, i = {
|
|
18
|
+
sampleType: "float",
|
|
19
|
+
viewDimension: "2d-array"
|
|
20
|
+
}, a = { primitive: {
|
|
21
|
+
topology: "triangle-list",
|
|
22
|
+
cullMode: "none"
|
|
23
|
+
} }, o = class {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
24
|
+
constructor(e, t) {
|
|
25
|
+
this.options = e, this.stats = t, this.groupKey = "", this.rectCapacity = 0, this.glyphCapacity = 0, this.pipelines = /* @__PURE__ */ new Map(), this.frameData = /* @__PURE__ */ new Float32Array(8), this.batch = new s(), this.uploaded = -1, this.uploadedAtlas = "", this.fresh = !1;
|
|
26
|
+
}
|
|
27
|
+
setup(e) {
|
|
28
|
+
this.device = e;
|
|
29
|
+
let r = GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT;
|
|
30
|
+
this.layout = e.createBindGroupLayout({ entries: [
|
|
31
|
+
{
|
|
32
|
+
binding: 0,
|
|
33
|
+
visibility: r,
|
|
34
|
+
buffer: {
|
|
35
|
+
type: "uniform",
|
|
36
|
+
minBindingSize: 32
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
binding: 1,
|
|
41
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
42
|
+
sampler: { type: "filtering" }
|
|
43
|
+
},
|
|
44
|
+
...[2, 3].map((e) => ({
|
|
45
|
+
binding: e,
|
|
46
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
47
|
+
texture: i
|
|
48
|
+
})),
|
|
49
|
+
...[4, 5].map((e) => ({
|
|
50
|
+
binding: e,
|
|
51
|
+
visibility: r,
|
|
52
|
+
buffer: { type: "read-only-storage" }
|
|
53
|
+
}))
|
|
54
|
+
] }), this.pipelineLayout = e.createPipelineLayout({ bindGroupLayouts: [this.layout] }), this.module = e.createShaderModule({ code: n }), this.sampler = e.createSampler({
|
|
55
|
+
magFilter: "linear",
|
|
56
|
+
minFilter: "linear",
|
|
57
|
+
addressModeU: "clamp-to-edge",
|
|
58
|
+
addressModeV: "clamp-to-edge"
|
|
59
|
+
}), this.frameBuffer = e.createBuffer({
|
|
60
|
+
size: 32,
|
|
61
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
62
|
+
});
|
|
63
|
+
let a = t(e, {
|
|
64
|
+
...this.options,
|
|
65
|
+
maxPages: Math.min(this.options.maxPages, e.limits.maxTextureArrayLayers)
|
|
66
|
+
}, this.sdfAtlas && this.colorAtlas ? {
|
|
67
|
+
sdf: this.sdfAtlas,
|
|
68
|
+
color: this.colorAtlas
|
|
69
|
+
} : void 0);
|
|
70
|
+
this.sdfAtlas = a.sdf, this.colorAtlas = a.color, this.group = void 0, this.groupKey = "", this.uploaded = -1;
|
|
71
|
+
}
|
|
72
|
+
get ready() {
|
|
73
|
+
return !!this.device;
|
|
74
|
+
}
|
|
75
|
+
get version() {
|
|
76
|
+
return this.uploaded;
|
|
77
|
+
}
|
|
78
|
+
get atlasKey() {
|
|
79
|
+
return `${this.sdfAtlas?.generation}|${this.colorAtlas?.generation}`;
|
|
80
|
+
}
|
|
81
|
+
prepare(e) {
|
|
82
|
+
let t = this.stats;
|
|
83
|
+
if (t.rects = t.glyphs = t.drawCalls = t.uploads = 0, !this.device) return !1;
|
|
84
|
+
let n = this.device;
|
|
85
|
+
if (this.uploaded !== e.version || this.uploadedAtlas !== this.atlasKey) {
|
|
86
|
+
let r = this.batch;
|
|
87
|
+
r.build(e, this.sdfAtlas, this.colorAtlas), this.fresh = this.ensureBuffers(r.rectCount, r.glyphCount) || this.fresh, r.rectCount && n.queue.writeBuffer(this.rectBuffer, 0, r.rects, 0, r.rectCount * 16), r.glyphCount && n.queue.writeBuffer(this.glyphBuffer, 0, r.glyphs, 0, r.glyphCount * 16), t.uploads++, this.uploaded = e.version, this.uploadedAtlas = this.atlasKey;
|
|
88
|
+
}
|
|
89
|
+
return t.rects = this.batch.rectCount, t.glyphs = this.batch.glyphCount, this.batch.runs.length > 0;
|
|
90
|
+
}
|
|
91
|
+
encode(t, n) {
|
|
92
|
+
let r = this.device, i = this.atlasKey;
|
|
93
|
+
(!this.group || this.fresh || i !== this.groupKey) && (this.group = r.createBindGroup({
|
|
94
|
+
layout: this.layout,
|
|
95
|
+
entries: [
|
|
96
|
+
{
|
|
97
|
+
binding: 0,
|
|
98
|
+
resource: { buffer: this.frameBuffer }
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
binding: 1,
|
|
102
|
+
resource: this.sampler
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
binding: 2,
|
|
106
|
+
resource: this.sdfAtlas.view
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
binding: 3,
|
|
110
|
+
resource: this.colorAtlas.view
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
binding: 4,
|
|
114
|
+
resource: { buffer: this.rectBuffer }
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
binding: 5,
|
|
118
|
+
resource: { buffer: this.glyphBuffer }
|
|
119
|
+
}
|
|
120
|
+
]
|
|
121
|
+
}), this.groupKey = this.uploadedAtlas = this.atlasKey, this.fresh = !1);
|
|
122
|
+
let { width: a, height: o, format: s } = n, c = this.frameData;
|
|
123
|
+
c[0] = a, c[1] = o, c[2] = s.endsWith("-srgb") || s.includes("float") ? 1 : 0, c[3] = 0, c[4] = e.radius, c[5] = e.cutoff, r.queue.writeBuffer(this.frameBuffer, 0, c), t.setBindGroup(0, this.group);
|
|
124
|
+
let l;
|
|
125
|
+
for (let e of this.batch.runs) {
|
|
126
|
+
e.glyphs !== l && (t.setPipeline(this.pipeline(n, e.glyphs)), l = e.glyphs);
|
|
127
|
+
let r = e.clip, i = r ? Math.max(0, Math.min(a, r[0])) : 0, s = r ? Math.max(0, Math.min(o, r[1])) : 0, c = r ? Math.max(0, Math.min(a - i, r[2] - (i - r[0]))) : a, u = r ? Math.max(0, Math.min(o - s, r[3] - (s - r[1]))) : o;
|
|
128
|
+
c && u && (t.setScissorRect(i, s, c, u), t.draw(6, e.count, 0, e.first), this.stats.drawCalls++);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
pipeline(e, t) {
|
|
132
|
+
let { format: n, depthFormat: i, samples: o } = e, s = `${n}|${i}|${o}|${t}`, c = this.pipelines.get(s);
|
|
133
|
+
if (!c) {
|
|
134
|
+
let e = this.module, l = t ? "glyph" : "rect";
|
|
135
|
+
c = this.device.createRenderPipeline({
|
|
136
|
+
...a,
|
|
137
|
+
layout: this.pipelineLayout,
|
|
138
|
+
vertex: {
|
|
139
|
+
module: e,
|
|
140
|
+
entryPoint: `${l}Vertex`
|
|
141
|
+
},
|
|
142
|
+
fragment: {
|
|
143
|
+
module: e,
|
|
144
|
+
entryPoint: `${l}Fragment`,
|
|
145
|
+
targets: [{
|
|
146
|
+
format: n,
|
|
147
|
+
blend: r
|
|
148
|
+
}]
|
|
149
|
+
},
|
|
150
|
+
multisample: { count: o },
|
|
151
|
+
...i ? { depthStencil: {
|
|
152
|
+
format: i,
|
|
153
|
+
depthWriteEnabled: !1,
|
|
154
|
+
depthCompare: "always"
|
|
155
|
+
} } : {}
|
|
156
|
+
}), this.pipelines.set(s, c);
|
|
157
|
+
}
|
|
158
|
+
return c;
|
|
159
|
+
}
|
|
160
|
+
ensureBuffers(e, t) {
|
|
161
|
+
let n = this.device, r = !1;
|
|
162
|
+
return (!this.rectBuffer || e > this.rectCapacity) && (this.rectBuffer?.destroy(), this.rectCapacity = Math.max(64, 2 ** Math.ceil(Math.log2(Math.max(1, e)))), this.rectBuffer = n.createBuffer({
|
|
163
|
+
size: this.rectCapacity * 16 * 4,
|
|
164
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
165
|
+
}), r = !0), (!this.glyphBuffer || t > this.glyphCapacity) && (this.glyphBuffer?.destroy(), this.glyphCapacity = Math.max(256, 2 ** Math.ceil(Math.log2(Math.max(1, t)))), this.glyphBuffer = n.createBuffer({
|
|
166
|
+
size: this.glyphCapacity * 16 * 4,
|
|
167
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
168
|
+
}), r = !0), r;
|
|
169
|
+
}
|
|
170
|
+
release(e) {
|
|
171
|
+
this.frameBuffer?.destroy(), this.rectBuffer?.destroy(), this.glyphBuffer?.destroy(), e && (this.sdfAtlas?.dispose(), this.colorAtlas?.dispose(), this.sdfAtlas = this.colorAtlas = void 0), this.frameBuffer = this.rectBuffer = this.glyphBuffer = this.group = this.module = this.layout = this.pipelineLayout = this.sampler = this.device = void 0, this.rectCapacity = this.glyphCapacity = 0, this.pipelines.clear(), this.uploaded = -1;
|
|
172
|
+
}
|
|
173
|
+
}, s = class {
|
|
174
|
+
constructor() {
|
|
175
|
+
this.rects = /* @__PURE__ */ new Float32Array(4096), this.glyphs = /* @__PURE__ */ new Float32Array(16384), this.runs = [], this.rectCount = 0, this.glyphCount = 0, this.images = /* @__PURE__ */ new Set(), this.drawn = /* @__PURE__ */ new Set();
|
|
176
|
+
}
|
|
177
|
+
build(e, t, n) {
|
|
178
|
+
this.sdf = t, this.color = n, this.runs.length = 0;
|
|
179
|
+
let r = 0, i = 0, a = [], o = null, s = (e, t) => {
|
|
180
|
+
let n = this.runs.at(-1), a = e ? i - t : r - t;
|
|
181
|
+
n && n.glyphs === e && n.clip === o && n.first + n.count === a ? n.count += t : this.runs.push({
|
|
182
|
+
glyphs: e,
|
|
183
|
+
first: a,
|
|
184
|
+
count: t,
|
|
185
|
+
clip: o
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
for (let t of e.commands) switch (t.type) {
|
|
189
|
+
case "clip":
|
|
190
|
+
a.push(o = [
|
|
191
|
+
t.x,
|
|
192
|
+
t.y,
|
|
193
|
+
t.width,
|
|
194
|
+
t.height
|
|
195
|
+
]);
|
|
196
|
+
break;
|
|
197
|
+
case "unclip":
|
|
198
|
+
a.pop(), o = a.at(-1) ?? null;
|
|
199
|
+
break;
|
|
200
|
+
case "rect": {
|
|
201
|
+
(r + 1) * 16 > this.rects.length && (this.rects = c(this.rects, (r + 1) * 16));
|
|
202
|
+
let e = r++ * 16, n = this.rects;
|
|
203
|
+
n.set([
|
|
204
|
+
t.x,
|
|
205
|
+
t.y,
|
|
206
|
+
t.width,
|
|
207
|
+
t.height
|
|
208
|
+
], e), n.set(t.fill, e + 4), n.set(t.border, e + 8), n.set([
|
|
209
|
+
t.radius,
|
|
210
|
+
t.borderWidth,
|
|
211
|
+
t.blur,
|
|
212
|
+
0
|
|
213
|
+
], e + 12), s(!1, 1);
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case "image":
|
|
217
|
+
i = this.placeImage(t, i), s(!0, 1);
|
|
218
|
+
break;
|
|
219
|
+
case "text": {
|
|
220
|
+
let e = i;
|
|
221
|
+
i = this.placeText(t, i), i > e && s(!0, i - e);
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
this.rectCount = r, this.glyphCount = i;
|
|
226
|
+
for (let e of this.images) this.drawn.has(e) || n.release?.(e);
|
|
227
|
+
[this.images, this.drawn] = [this.drawn, this.images], this.drawn.clear();
|
|
228
|
+
}
|
|
229
|
+
placeImage(e, t) {
|
|
230
|
+
this.drawn.add(e.bitmap);
|
|
231
|
+
let n = this.color.place(e.bitmap), r = this.color.size, i = e.source;
|
|
232
|
+
(t + 1) * 16 > this.glyphs.length && (this.glyphs = c(this.glyphs, (t + 1) * 16));
|
|
233
|
+
let a = t * 16;
|
|
234
|
+
return this.glyphs.set([
|
|
235
|
+
e.x,
|
|
236
|
+
e.y,
|
|
237
|
+
e.x + e.width,
|
|
238
|
+
e.y + e.height,
|
|
239
|
+
(n.x + i.x) / r,
|
|
240
|
+
(n.y + i.y) / r,
|
|
241
|
+
(n.x + i.x + i.width) / r,
|
|
242
|
+
(n.y + i.y + i.height) / r,
|
|
243
|
+
...e.color,
|
|
244
|
+
n.page,
|
|
245
|
+
2,
|
|
246
|
+
0,
|
|
247
|
+
0
|
|
248
|
+
], a), t + 1;
|
|
249
|
+
}
|
|
250
|
+
placeText(e, t) {
|
|
251
|
+
let { layout: n, font: r, scale: i } = e;
|
|
252
|
+
if (!r.loaded) return r.load(), t;
|
|
253
|
+
let a = n.fontSize / r.size, o = e.outlineWidth > 0 ? 2 : 1;
|
|
254
|
+
for (let s = 0; s < o; s++) {
|
|
255
|
+
let l = o === 2 && s === 0, u = l ? e.outlineColor : e.color;
|
|
256
|
+
for (let o of n.glyphs) {
|
|
257
|
+
if (o.whitespace) continue;
|
|
258
|
+
let n = r.glyph(o.cluster);
|
|
259
|
+
if (!n || l && n.color) continue;
|
|
260
|
+
let s = n.color ? this.color : this.sdf, d = s.place(n), f = s.size;
|
|
261
|
+
(t + 1) * 16 > this.glyphs.length && (this.glyphs = c(this.glyphs, (t + 1) * 16));
|
|
262
|
+
let p = t++ * 16, m = this.glyphs, h = e.x + (o.x + n.left * a) * i, g = e.y - o.y * i - n.top * a * i;
|
|
263
|
+
m[p] = h, m[p + 1] = g, m[p + 2] = h + n.width * a * i, m[p + 3] = g + n.height * a * i, m[p + 4] = d.x / f, m[p + 5] = d.y / f, m[p + 6] = (d.x + n.width) / f, m[p + 7] = (d.y + n.height) / f, m.set(u, p + 8), m[p + 12] = d.page, m[p + 13] = +!!n.color, m[p + 14] = l ? e.outlineWidth : 0, m[p + 15] = 0;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return t;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
function c(e, t) {
|
|
270
|
+
let n = new Float32Array(Math.max(t, e.length * 2));
|
|
271
|
+
return n.set(e), n;
|
|
272
|
+
}
|
|
273
|
+
//#endregion
|
|
274
|
+
//#region src/dom.ts
|
|
275
|
+
var l = {
|
|
276
|
+
ArrowUp: "up",
|
|
277
|
+
ArrowDown: "down",
|
|
278
|
+
ArrowLeft: "left",
|
|
279
|
+
ArrowRight: "right",
|
|
280
|
+
Enter: "accept",
|
|
281
|
+
" ": "accept",
|
|
282
|
+
Escape: "cancel"
|
|
283
|
+
};
|
|
284
|
+
function u(e, t, n = d) {
|
|
285
|
+
if (t.altKey || t.ctrlKey || t.metaKey || n.has(t.key)) return !1;
|
|
286
|
+
if (t.key === "Tab") return e.focusables().length > 0 && e.tab(!t.shiftKey, "keyboard");
|
|
287
|
+
let r = l[t.key];
|
|
288
|
+
return !!r && e.wantsKeys() && e.execute(r, "keyboard");
|
|
289
|
+
}
|
|
290
|
+
var d = /* @__PURE__ */ new Set(), f = (e) => {
|
|
291
|
+
e.preventDefault(), e.stopImmediatePropagation();
|
|
292
|
+
};
|
|
293
|
+
function p(e, t, n = {}) {
|
|
294
|
+
let { signal: r, keyboard: i = !0, pointer: a = !0, wheel: o = !0 } = n, s = [], c = h(t, s);
|
|
295
|
+
return m(e, t, c, {
|
|
296
|
+
pointer: a,
|
|
297
|
+
wheel: o
|
|
298
|
+
}), i && (t.hasAttribute("tabindex") || (t.setAttribute("tabindex", "0"), s.push(() => t.removeAttribute("tabindex"))), c("keydown", (t) => {
|
|
299
|
+
u(e, t) && f(t);
|
|
300
|
+
}), c("blur", () => e.pointerCancel())), g(s, r);
|
|
301
|
+
}
|
|
302
|
+
function m(e, t, n, { pointer: r = !0, wheel: i = !0 } = {}) {
|
|
303
|
+
let a = (e) => {
|
|
304
|
+
let n = t.getBoundingClientRect();
|
|
305
|
+
return [e.clientX - n.left, e.clientY - n.top];
|
|
306
|
+
}, o = (e) => ({
|
|
307
|
+
id: e.pointerId,
|
|
308
|
+
button: e.button,
|
|
309
|
+
type: e.pointerType || "mouse"
|
|
310
|
+
});
|
|
311
|
+
r && (n("pointermove", (t) => {
|
|
312
|
+
let [n, r] = a(t);
|
|
313
|
+
e.pointerMove(n, r, o(t)) && t.buttons && t.stopImmediatePropagation();
|
|
314
|
+
}), n("pointerdown", (n) => {
|
|
315
|
+
let [r, i] = a(n);
|
|
316
|
+
e.pointerDown(r, i, o(n)) && (f(n), t.setPointerCapture?.(n.pointerId), t.focus?.({ preventScroll: !0 }));
|
|
317
|
+
}), n("pointerup", (n) => {
|
|
318
|
+
let [r, i] = a(n);
|
|
319
|
+
e.pointerUp(r, i, o(n)) && (n.stopImmediatePropagation(), t.releasePointerCapture?.(n.pointerId));
|
|
320
|
+
}), n("pointercancel", () => e.pointerCancel()), n("pointerleave", () => e.pointerLeave())), i && n("wheel", (t) => {
|
|
321
|
+
let [n, r] = a(t);
|
|
322
|
+
e.wheel(n, r, t.deltaMode === 1 ? t.deltaY * 16 : t.deltaY) && f(t);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
function h(e, t) {
|
|
326
|
+
return (n, r) => {
|
|
327
|
+
e.addEventListener(n, r, {
|
|
328
|
+
capture: !0,
|
|
329
|
+
passive: !1
|
|
330
|
+
}), t.push(() => e.removeEventListener(n, r, { capture: !0 }));
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function g(e, t) {
|
|
334
|
+
let n = (() => {
|
|
335
|
+
for (let t of e.splice(0).reverse()) t();
|
|
336
|
+
});
|
|
337
|
+
return n[Symbol.dispose ?? Symbol.for("Symbol.dispose")] = n, t && (t.aborted ? n() : t.addEventListener("abort", n, { once: !0 })), n;
|
|
338
|
+
}
|
|
339
|
+
//#endregion
|
|
340
|
+
export { h as a, o as c, u as i, m as n, r as o, g as r, s, p as t };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { Ui, Widget, type Theme } from '@margoengine/ui';
|
|
3
|
+
/** Options for {@link mountDom}. */
|
|
4
|
+
export interface DomHostOptions {
|
|
5
|
+
/** The look. Default the UI's own theme, followed when it changes. */
|
|
6
|
+
theme?: Theme;
|
|
7
|
+
/** Accessible name of the mounted region. Default none (the page names it). */
|
|
8
|
+
label?: string;
|
|
9
|
+
/** Smallest pointer target of a control in CSS pixels, 24 or more (WCAG 2.2 SC 2.5.8). Default 44. */
|
|
10
|
+
targetSize?: number;
|
|
11
|
+
/** Advance toasts and transitions with `ui.update` on animation frames. Pass false when a UiRenderer drives the same UI. Default true. */
|
|
12
|
+
update?: boolean;
|
|
13
|
+
/** Send `ui.announce` messages and toasts to live regions in the host. Pass false when a mirror already does. Default true. */
|
|
14
|
+
announce?: boolean;
|
|
15
|
+
/** Nonce of the fallback `<style>` element, for a page whose CSP has no constructable stylesheets. */
|
|
16
|
+
nonce?: string;
|
|
17
|
+
/** Start of every element id. Default `'margo-dom-'`. */
|
|
18
|
+
idPrefix?: string;
|
|
19
|
+
/** Unmount when the signal aborts. */
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
/** Returned by {@link mountDom}. Dispose it (or use `using`) to remove every element and listener. */
|
|
23
|
+
export interface DomHost {
|
|
24
|
+
/** The element the widgets are mounted in (the container). */
|
|
25
|
+
readonly element: HTMLElement;
|
|
26
|
+
/** The element that shows `widget`, or undefined while it has none. */
|
|
27
|
+
elementOf(widget: Widget): HTMLElement | undefined;
|
|
28
|
+
/** The widget an element (or any node inside it) shows. */
|
|
29
|
+
widgetOf(node: Node | null): Widget | undefined;
|
|
30
|
+
/** Apply every pending widget change now instead of on the next microtask. */
|
|
31
|
+
flush(): void;
|
|
32
|
+
dispose(): void;
|
|
33
|
+
[Symbol.dispose](): void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The longest run of `positions` (-1 = not there yet) that is already in order: those elements stay where they are and
|
|
37
|
+
* only the others move, the fewest moves a keyed list needs (patience sorting, O(n log n); Fredman 1975).
|
|
38
|
+
*/
|
|
39
|
+
export declare function stayingInOrder(positions: readonly number[]): Set<number>;
|
|
40
|
+
/**
|
|
41
|
+
* Show a widget tree as real, visible HTML: the same widgets, events and themes as a canvas {@link Ui}, with the
|
|
42
|
+
* browser's own keyboard handling, screen reader support, text input (IME, autofill, spellcheck), zoom and touch.
|
|
43
|
+
* Buttons are `<button>`, toggles and checkboxes `<input type="checkbox">` (a switch role for toggles), sliders range
|
|
44
|
+
* inputs, selects `<select>`, radio groups a `<fieldset>` of radios, tabs the tablist pattern, lists listboxes,
|
|
45
|
+
* dialogs a modal `<dialog>`, meters `<progress>` and text inputs `<input>` or `<textarea>`.
|
|
46
|
+
*
|
|
47
|
+
* Changes flow both ways and only touch what changed: setting a widget's text, value or style patches its own element
|
|
48
|
+
* (focus, caret and scroll stay), and using an element calls the widget's own setters, so its `'click'`, `'change'`
|
|
49
|
+
* and `'select'` events fire just as on the canvas. The theme becomes CSS custom properties (`--mg-primary`, ...) on
|
|
50
|
+
* the container and one shared stylesheet, so the page can restyle it; no style attributes are written, so a strict
|
|
51
|
+
* Content Security Policy passes.
|
|
52
|
+
*
|
|
53
|
+
* @param source A {@link Ui} (its screen, toasts and dialogs), or one widget of it (that subtree only).
|
|
54
|
+
* @throws TypeError when `source` is not a Ui or a widget in one, or `container` is not an element.
|
|
55
|
+
* @throws RangeError for a `targetSize` under 24.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* import { Button, Label, Panel, Ui } from '@margoengine/ui';
|
|
59
|
+
* import { mountDom } from '@margoengine/ui/dom';
|
|
60
|
+
*
|
|
61
|
+
* const ui = new Ui();
|
|
62
|
+
* const save = new Button({ text: 'Save', variant: 'primary' });
|
|
63
|
+
* ui.add(new Panel({ gap: 8, children: [new Label({ text: 'Your changes' }), save] }));
|
|
64
|
+
* save.on('click', () => ui.toast('Saved'));
|
|
65
|
+
* using host = mountDom(ui, document.body);
|
|
66
|
+
*/
|
|
67
|
+
export declare function mountDom(source: Ui | Widget, container: HTMLElement, options?: DomHostOptions): DomHost;
|