@egret/runtime 0.0.0-stage → 7.0.2
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 +21 -0
- package/README.md +70 -2
- package/README.zh-CN.md +71 -0
- package/dist/AssetLease.d.ts +23 -0
- package/dist/AssetLease.js +69 -0
- package/dist/AssetManager.d.ts +58 -0
- package/dist/AssetManager.js +451 -0
- package/dist/AssetRef.d.ts +21 -0
- package/dist/AssetRef.js +33 -0
- package/dist/Bitmap.d.ts +40 -0
- package/dist/Bitmap.js +545 -0
- package/dist/ByteReader.d.ts +32 -0
- package/dist/ByteReader.js +178 -0
- package/dist/ByteWriter.d.ts +30 -0
- package/dist/ByteWriter.js +181 -0
- package/dist/CancellationController.d.ts +10 -0
- package/dist/CancellationController.js +66 -0
- package/dist/DisplayObject.d.ts +97 -0
- package/dist/DisplayObject.js +366 -0
- package/dist/DisplayObjectContainer.d.ts +28 -0
- package/dist/DisplayObjectContainer.js +226 -0
- package/dist/EgretError.d.ts +12 -0
- package/dist/EgretError.js +36 -0
- package/dist/Event.d.ts +47 -0
- package/dist/Event.js +84 -0
- package/dist/EventDispatcher.d.ts +24 -0
- package/dist/EventDispatcher.js +178 -0
- package/dist/Graphics.d.ts +117 -0
- package/dist/Graphics.js +548 -0
- package/dist/Mesh2D.d.ts +27 -0
- package/dist/Mesh2D.js +200 -0
- package/dist/NineSliceBitmap.d.ts +24 -0
- package/dist/NineSliceBitmap.js +164 -0
- package/dist/NineSliceGeometry2D.d.ts +19 -0
- package/dist/NineSliceGeometry2D.js +126 -0
- package/dist/Scope.d.ts +19 -0
- package/dist/Scope.js +133 -0
- package/dist/SequencePlayer.d.ts +12 -0
- package/dist/SequencePlayer.js +69 -0
- package/dist/Sprite.d.ts +8 -0
- package/dist/Sprite.js +12 -0
- package/dist/Stage.d.ts +14 -0
- package/dist/Stage.js +31 -0
- package/dist/Texture.d.ts +30 -0
- package/dist/Texture.js +178 -0
- package/dist/assetManagerOwnerState.d.ts +36 -0
- package/dist/assetManagerOwnerState.js +115 -0
- package/dist/assetState.d.ts +45 -0
- package/dist/assetState.js +2 -0
- package/dist/atlasFrameLayout.d.ts +21 -0
- package/dist/atlasFrameLayout.js +48 -0
- package/dist/bitmapDisplayState.d.ts +24 -0
- package/dist/bitmapDisplayState.js +22 -0
- package/dist/coordinateInverse.d.ts +4 -0
- package/dist/coordinateInverse.js +107 -0
- package/dist/diagnostics.d.ts +4 -0
- package/dist/diagnostics.js +13 -0
- package/dist/displayBounds.d.ts +9 -0
- package/dist/displayBounds.js +181 -0
- package/dist/displayCoordinates.d.ts +8 -0
- package/dist/displayCoordinates.js +81 -0
- package/dist/displayHitState.d.ts +18 -0
- package/dist/displayHitState.js +97 -0
- package/dist/displayHitTest.d.ts +8 -0
- package/dist/displayHitTest.js +219 -0
- package/dist/displayInputOperations.d.ts +12 -0
- package/dist/displayInputOperations.js +71 -0
- package/dist/displayTransform.d.ts +5 -0
- package/dist/displayTransform.js +38 -0
- package/dist/displayTreeState.d.ts +90 -0
- package/dist/displayTreeState.js +450 -0
- package/dist/displayVisualState.d.ts +76 -0
- package/dist/displayVisualState.js +141 -0
- package/dist/errorHelp.d.ts +15 -0
- package/dist/errorHelp.js +435 -0
- package/dist/errorHelpZh.d.ts +5 -0
- package/dist/errorHelpZh.js +342 -0
- package/dist/geometry.d.ts +173 -0
- package/dist/geometry.js +311 -0
- package/dist/graphicsFill.d.ts +37 -0
- package/dist/graphicsFill.js +302 -0
- package/dist/graphicsShape.d.ts +170 -0
- package/dist/graphicsShape.js +961 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +31 -0
- package/dist/input/PointerRouter.d.ts +4 -0
- package/dist/input/PointerRouter.js +453 -0
- package/dist/input/index.d.ts +3 -0
- package/dist/input/index.js +3 -0
- package/dist/input/types.d.ts +64 -0
- package/dist/input/types.js +3 -0
- package/dist/meshCaptureSnapshot.d.ts +5 -0
- package/dist/meshCaptureSnapshot.js +13 -0
- package/dist/meshDisplayState.d.ts +18 -0
- package/dist/meshDisplayState.js +21 -0
- package/dist/ownership.d.ts +75 -0
- package/dist/ownership.js +157 -0
- package/dist/paragraph/Paragraph.d.ts +13 -0
- package/dist/paragraph/Paragraph.js +193 -0
- package/dist/paragraph/ParagraphCache.d.ts +5 -0
- package/dist/paragraph/ParagraphCache.js +354 -0
- package/dist/paragraph/geometry.d.ts +4 -0
- package/dist/paragraph/geometry.js +180 -0
- package/dist/paragraph/index.d.ts +3 -0
- package/dist/paragraph/index.js +3 -0
- package/dist/paragraph/request.d.ts +6 -0
- package/dist/paragraph/request.js +191 -0
- package/dist/paragraph/types.d.ts +183 -0
- package/dist/paragraph/types.js +2 -0
- package/dist/preparedDisplaySubtree.d.ts +7 -0
- package/dist/preparedDisplaySubtree.js +205 -0
- package/dist/renderEffect.d.ts +57 -0
- package/dist/renderEffect.js +109 -0
- package/dist/sequenceClip.d.ts +48 -0
- package/dist/sequenceClip.js +197 -0
- package/dist/sharedFrameCapture.d.ts +24 -0
- package/dist/sharedFrameCapture.js +1128 -0
- package/dist/spriteSheet.d.ts +34 -0
- package/dist/spriteSheet.js +321 -0
- package/dist/srgbBytes.d.ts +3 -0
- package/dist/srgbBytes.js +64 -0
- package/dist/text/TextField.d.ts +13 -0
- package/dist/text/TextField.js +261 -0
- package/dist/text/TextRasterCache.d.ts +5 -0
- package/dist/text/TextRasterCache.js +1191 -0
- package/dist/text/budget.d.ts +51 -0
- package/dist/text/budget.js +116 -0
- package/dist/text/index.d.ts +4 -0
- package/dist/text/index.js +3 -0
- package/dist/text/plan.d.ts +16 -0
- package/dist/text/plan.js +238 -0
- package/dist/text/request.d.ts +44 -0
- package/dist/text/request.js +303 -0
- package/dist/text/types.d.ts +179 -0
- package/dist/text/types.js +2 -0
- package/dist/textDisplayState.d.ts +21 -0
- package/dist/textDisplayState.js +27 -0
- package/dist/ui/displayCommit.d.ts +43 -0
- package/dist/ui/displayCommit.js +384 -0
- package/dist/ui/editing.d.ts +3 -0
- package/dist/ui/editing.js +3 -0
- package/dist/ui/heightIndex.d.ts +35 -0
- package/dist/ui/heightIndex.js +295 -0
- package/dist/ui/index.d.ts +6 -0
- package/dist/ui/index.js +6 -0
- package/dist/ui/layout.d.ts +9 -0
- package/dist/ui/layout.js +349 -0
- package/dist/ui/managedState.d.ts +12 -0
- package/dist/ui/managedState.js +44 -0
- package/dist/ui/textEditState.d.ts +4 -0
- package/dist/ui/textEditState.js +206 -0
- package/dist/ui/textEditTypes.d.ts +48 -0
- package/dist/ui/textEditTypes.js +2 -0
- package/dist/ui/types.d.ts +99 -0
- package/dist/ui/types.js +2 -0
- package/package.json +44 -4
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { EgretError } from '../EgretError.js';
|
|
3
|
+
const CAP = 50000, MAX_HEIGHT = 1048576, MAX_TOTAL = 2 ** 40;
|
|
4
|
+
function invalid() {
|
|
5
|
+
throw new EgretError('UI_INVALID');
|
|
6
|
+
}
|
|
7
|
+
function finite(value, min, max) {
|
|
8
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < min || value > max)
|
|
9
|
+
invalid();
|
|
10
|
+
return value === 0 ? 0 : value;
|
|
11
|
+
}
|
|
12
|
+
function counter(value) {
|
|
13
|
+
const n = finite(value, 0, Number.MAX_SAFE_INTEGER);
|
|
14
|
+
return n % 1 === 0 ? n : invalid();
|
|
15
|
+
}
|
|
16
|
+
function key(value) {
|
|
17
|
+
if (typeof value !== 'string' || value.length === 0 || value.length > 128)
|
|
18
|
+
invalid();
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function index(value, max) {
|
|
22
|
+
if (!Number.isInteger(value) || value < 0 || value > max)
|
|
23
|
+
invalid();
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function arrayCount(value) {
|
|
27
|
+
if (!Array.isArray(value))
|
|
28
|
+
invalid();
|
|
29
|
+
const count = value.length;
|
|
30
|
+
if (typeof count !== 'number' || !Number.isInteger(count) || count < 0)
|
|
31
|
+
invalid();
|
|
32
|
+
if (count > CAP)
|
|
33
|
+
throw new EgretError('UI_LIMIT');
|
|
34
|
+
return count === 0 ? 0 : count;
|
|
35
|
+
}
|
|
36
|
+
function fields(value, allowed) {
|
|
37
|
+
if (value === null || typeof value !== 'object')
|
|
38
|
+
invalid();
|
|
39
|
+
const proto = Object.getPrototypeOf(value);
|
|
40
|
+
if (proto !== Object.prototype && proto !== null)
|
|
41
|
+
invalid();
|
|
42
|
+
const result = {};
|
|
43
|
+
for (const name of Reflect.ownKeys(value)) {
|
|
44
|
+
if (typeof name !== 'string' || !allowed.includes(name))
|
|
45
|
+
invalid();
|
|
46
|
+
result[name] = value[name];
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
class HeightView {
|
|
51
|
+
prefix(end) {
|
|
52
|
+
index(end, this.size);
|
|
53
|
+
let sum = 0;
|
|
54
|
+
for (let i = end; i > 0; i -= i & -i)
|
|
55
|
+
sum += this.cell(i);
|
|
56
|
+
return sum === 0 ? 0 : sum;
|
|
57
|
+
}
|
|
58
|
+
find(offset) {
|
|
59
|
+
finite(offset, -Number.MAX_VALUE, Number.MAX_VALUE);
|
|
60
|
+
if (!this.size)
|
|
61
|
+
return -1;
|
|
62
|
+
if (offset < 0)
|
|
63
|
+
return 0;
|
|
64
|
+
let found = 0, sum = 0, bit = 1;
|
|
65
|
+
while (bit * 2 <= this.size)
|
|
66
|
+
bit *= 2;
|
|
67
|
+
for (; bit > 0; bit = Math.floor(bit / 2)) {
|
|
68
|
+
const next = found + bit;
|
|
69
|
+
if (next <= this.size && sum + this.cell(next) <= offset) {
|
|
70
|
+
sum += this.cell(next);
|
|
71
|
+
found = next;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return Math.min(found, this.size - 1);
|
|
75
|
+
}
|
|
76
|
+
clampScroll(scroll, viewport) {
|
|
77
|
+
finite(scroll, -Number.MAX_VALUE, Number.MAX_VALUE);
|
|
78
|
+
finite(viewport, Number.MIN_VALUE, 16384);
|
|
79
|
+
return Math.max(0, Math.min(scroll, Math.max(0, this.totalHeight - viewport)));
|
|
80
|
+
}
|
|
81
|
+
range(scroll, viewport, overscan = 2) {
|
|
82
|
+
const s = this.clampScroll(scroll, viewport);
|
|
83
|
+
finite(overscan, 0, 32);
|
|
84
|
+
if (!Number.isInteger(overscan))
|
|
85
|
+
invalid();
|
|
86
|
+
if (!this.size)
|
|
87
|
+
return Object.freeze({ start: 0, end: 0, visibleStart: 0, visibleEnd: 0 });
|
|
88
|
+
const visibleStart = this.find(s);
|
|
89
|
+
let visibleEnd = visibleStart, top = this.prefix(visibleStart);
|
|
90
|
+
while (visibleEnd < this.size && top < s + viewport) {
|
|
91
|
+
top += this.heightAt(visibleEnd);
|
|
92
|
+
visibleEnd++;
|
|
93
|
+
}
|
|
94
|
+
return Object.freeze({
|
|
95
|
+
start: Math.max(0, visibleStart - overscan),
|
|
96
|
+
end: Math.min(this.size, visibleEnd + overscan),
|
|
97
|
+
visibleStart,
|
|
98
|
+
visibleEnd
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
anchor(scroll) {
|
|
102
|
+
finite(scroll, -Number.MAX_VALUE, Number.MAX_VALUE);
|
|
103
|
+
if (!this.size)
|
|
104
|
+
return undefined;
|
|
105
|
+
const s = Math.max(0, Math.min(scroll, this.totalHeight));
|
|
106
|
+
const i = this.find(s);
|
|
107
|
+
return Object.freeze({ key: this.keyAt(i), offset: s - this.prefix(i) });
|
|
108
|
+
}
|
|
109
|
+
preserveAnchor(anchor, old, viewport) {
|
|
110
|
+
if (!anchor || !this.size)
|
|
111
|
+
return this.clampScroll(0, viewport);
|
|
112
|
+
key(anchor.key);
|
|
113
|
+
finite(anchor.offset, 0, MAX_HEIGHT);
|
|
114
|
+
let at = this.indexOf(anchor.key), offset = anchor.offset;
|
|
115
|
+
if (at < 0) {
|
|
116
|
+
offset = 0;
|
|
117
|
+
const previous = old.indexOf(anchor.key);
|
|
118
|
+
if (previous >= 0) {
|
|
119
|
+
for (let i = previous + 1; i < old.size; i++) {
|
|
120
|
+
at = this.indexOf(old.keyAt(i));
|
|
121
|
+
if (at >= 0)
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
if (at < 0)
|
|
125
|
+
for (let i = previous - 1; i >= 0; i--) {
|
|
126
|
+
at = this.indexOf(old.keyAt(i));
|
|
127
|
+
if (at >= 0)
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (at < 0)
|
|
132
|
+
at = 0;
|
|
133
|
+
}
|
|
134
|
+
return this.clampScroll(this.prefix(at) + Math.min(offset, this.heightAt(at)), viewport);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** O(n) structural rebuild; point candidates retain only sparse changed cells. */
|
|
138
|
+
export class UiHeightIndex extends HeightView {
|
|
139
|
+
#keys = [];
|
|
140
|
+
#versions = [];
|
|
141
|
+
#heights;
|
|
142
|
+
#tree;
|
|
143
|
+
#positions = new Map();
|
|
144
|
+
#measured;
|
|
145
|
+
#width;
|
|
146
|
+
#density;
|
|
147
|
+
#measuredCount = 0;
|
|
148
|
+
#revision = 0;
|
|
149
|
+
constructor(records, width, density, previous) {
|
|
150
|
+
super();
|
|
151
|
+
const count = arrayCount(records);
|
|
152
|
+
this.#width = finite(width, Number.MIN_VALUE, 16384);
|
|
153
|
+
this.#density = finite(density, Number.MIN_VALUE, 8);
|
|
154
|
+
this.#heights = new Float64Array(count);
|
|
155
|
+
this.#tree = new Float64Array(count + 1);
|
|
156
|
+
this.#measured = new Uint8Array(count);
|
|
157
|
+
const canReuse = previous !== undefined && previous.#width === width && previous.#density === density;
|
|
158
|
+
for (let i = 0; i < count; i++) {
|
|
159
|
+
const record = fields(records[i], ['key', 'contentVersion', 'estimatedHeight', 'value']);
|
|
160
|
+
const k = key(record.key), v = counter(record.contentVersion), estimate = finite(record.estimatedHeight, 1, MAX_HEIGHT);
|
|
161
|
+
if (this.#positions.has(k))
|
|
162
|
+
throw new EgretError('UI_DUPLICATE_KEY');
|
|
163
|
+
this.#keys.push(k);
|
|
164
|
+
this.#versions.push(v);
|
|
165
|
+
this.#positions.set(k, i);
|
|
166
|
+
const old = canReuse ? previous.#positions.get(k) : undefined;
|
|
167
|
+
const reuse = old !== undefined && previous.#versions[old] === v && previous.#measured[old] === 1;
|
|
168
|
+
const h = reuse ? previous.#heights[old] : estimate;
|
|
169
|
+
this.#heights[i] = h;
|
|
170
|
+
if (reuse) {
|
|
171
|
+
this.#measured[i] = 1;
|
|
172
|
+
this.#measuredCount++;
|
|
173
|
+
}
|
|
174
|
+
this.#tree[i + 1] = h;
|
|
175
|
+
}
|
|
176
|
+
// Linear Fenwick construction, including validation before exposing this value.
|
|
177
|
+
let total = 0;
|
|
178
|
+
for (let i = 1; i <= count; i++) {
|
|
179
|
+
total += this.#heights[i - 1];
|
|
180
|
+
const parent = i + (i & -i);
|
|
181
|
+
if (parent <= count)
|
|
182
|
+
this.#tree[parent] = this.#tree[parent] + this.#tree[i];
|
|
183
|
+
}
|
|
184
|
+
if (!Number.isFinite(total) || total > MAX_TOTAL)
|
|
185
|
+
throw new EgretError('UI_LIMIT');
|
|
186
|
+
}
|
|
187
|
+
get size() {
|
|
188
|
+
return this.#keys.length;
|
|
189
|
+
}
|
|
190
|
+
get totalHeight() {
|
|
191
|
+
return this.prefix(this.size);
|
|
192
|
+
}
|
|
193
|
+
get measuredCount() {
|
|
194
|
+
return this.#measuredCount;
|
|
195
|
+
}
|
|
196
|
+
/** Numeric typed-array bytes only; string, map and JS heap storage excluded. */
|
|
197
|
+
get logicalBytes() {
|
|
198
|
+
return this.#tree.byteLength + this.#heights.byteLength + this.#measured.byteLength;
|
|
199
|
+
}
|
|
200
|
+
keyAt(i) {
|
|
201
|
+
return this.#keys[index(i, this.size - 1)];
|
|
202
|
+
}
|
|
203
|
+
versionAt(i) {
|
|
204
|
+
return this.#versions[index(i, this.size - 1)];
|
|
205
|
+
}
|
|
206
|
+
heightAt(i) {
|
|
207
|
+
return this.#heights[index(i, this.size - 1)];
|
|
208
|
+
}
|
|
209
|
+
indexOf(k) {
|
|
210
|
+
return this.#positions.get(k) ?? -1;
|
|
211
|
+
}
|
|
212
|
+
cell(i) {
|
|
213
|
+
return this.#tree[i];
|
|
214
|
+
}
|
|
215
|
+
stageHeights(updates) {
|
|
216
|
+
const count = arrayCount(updates);
|
|
217
|
+
const captured = [], seen = new Set();
|
|
218
|
+
// Validate all records before allocating journal cells or writing committed state.
|
|
219
|
+
for (let u = 0; u < count; u++) {
|
|
220
|
+
const update = fields(updates[u], ['key', 'contentVersion', 'height']);
|
|
221
|
+
const k = key(update.key), v = counter(update.contentVersion), h = finite(update.height, 1, MAX_HEIGHT);
|
|
222
|
+
if (seen.has(k))
|
|
223
|
+
throw new EgretError('UI_DUPLICATE_KEY');
|
|
224
|
+
seen.add(k);
|
|
225
|
+
const i = this.#positions.get(k);
|
|
226
|
+
if (i === undefined)
|
|
227
|
+
throw new EgretError('UI_UNKNOWN_KEY');
|
|
228
|
+
if (this.#versions[i] === v)
|
|
229
|
+
captured.push({ i, h });
|
|
230
|
+
}
|
|
231
|
+
const cells = new Map(), heights = new Map();
|
|
232
|
+
let work = 0, measured = this.#measuredCount, total = this.totalHeight;
|
|
233
|
+
for (const { i, h } of captured) {
|
|
234
|
+
const delta = h - this.#heights[i];
|
|
235
|
+
heights.set(i, h);
|
|
236
|
+
if (this.#measured[i] === 0)
|
|
237
|
+
measured++;
|
|
238
|
+
total += delta;
|
|
239
|
+
if (delta !== 0)
|
|
240
|
+
for (let c = i + 1; c <= this.size; c += c & -c) {
|
|
241
|
+
cells.set(c, (cells.get(c) ?? this.#tree[c]) + delta);
|
|
242
|
+
work++;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (!Number.isFinite(total) || total > MAX_TOTAL)
|
|
246
|
+
throw new EgretError('UI_LIMIT');
|
|
247
|
+
const base = this, revision = this.#revision;
|
|
248
|
+
let consumed = false;
|
|
249
|
+
return new (class extends HeightView {
|
|
250
|
+
cellWork = work;
|
|
251
|
+
changedCount = heights.size;
|
|
252
|
+
get size() {
|
|
253
|
+
return base.size;
|
|
254
|
+
}
|
|
255
|
+
get totalHeight() {
|
|
256
|
+
return total;
|
|
257
|
+
}
|
|
258
|
+
get measuredCount() {
|
|
259
|
+
return measured;
|
|
260
|
+
}
|
|
261
|
+
keyAt(i) {
|
|
262
|
+
return base.keyAt(i);
|
|
263
|
+
}
|
|
264
|
+
versionAt(i) {
|
|
265
|
+
return base.versionAt(i);
|
|
266
|
+
}
|
|
267
|
+
indexOf(k) {
|
|
268
|
+
return base.indexOf(k);
|
|
269
|
+
}
|
|
270
|
+
heightAt(i) {
|
|
271
|
+
index(i, this.size - 1);
|
|
272
|
+
return heights.get(i) ?? base.#heights[i];
|
|
273
|
+
}
|
|
274
|
+
cell(i) {
|
|
275
|
+
return cells.get(i) ?? base.#tree[i];
|
|
276
|
+
}
|
|
277
|
+
apply() {
|
|
278
|
+
if (consumed || base.#revision !== revision)
|
|
279
|
+
throw new EgretError('UI_STALE');
|
|
280
|
+
if (base.#revision === Number.MAX_SAFE_INTEGER)
|
|
281
|
+
throw new EgretError('UI_LIMIT');
|
|
282
|
+
consumed = true;
|
|
283
|
+
for (const [i, h] of heights) {
|
|
284
|
+
base.#heights[i] = h;
|
|
285
|
+
base.#measured[i] = 1;
|
|
286
|
+
}
|
|
287
|
+
for (const [i, h] of cells)
|
|
288
|
+
base.#tree[i] = h;
|
|
289
|
+
base.#measuredCount = measured;
|
|
290
|
+
base.#revision++;
|
|
291
|
+
}
|
|
292
|
+
})();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
//# sourceMappingURL=heightIndex.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { createUiDisplayOperations } from './displayCommit.js';
|
|
2
|
+
export type { UiDisplayOperations, UiDisplayUpdate } from './displayCommit.js';
|
|
3
|
+
export { captureUiLayout, planUiLayout } from './layout.js';
|
|
4
|
+
export { UiHeightIndex } from './heightIndex.js';
|
|
5
|
+
export type * from './types.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/ui/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Optional UI cooperation shares bounded layout and managed publication with Engine.
|
|
3
|
+
export { createUiDisplayOperations } from './displayCommit.js';
|
|
4
|
+
export { captureUiLayout, planUiLayout } from './layout.js';
|
|
5
|
+
export { UiHeightIndex } from './heightIndex.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { UiLayoutNode, UiLayoutTree, UiLayoutPlan, UiDirtyKind, UiSize } from './types.js';
|
|
2
|
+
/** Bounded capture reads each authored property once and retains no mutable child arrays. */
|
|
3
|
+
export declare function captureUiLayout(tree: UiLayoutNode, limits?: {
|
|
4
|
+
readonly maxNodes?: number;
|
|
5
|
+
readonly maxDepth?: number;
|
|
6
|
+
}): UiLayoutTree;
|
|
7
|
+
/** One postorder measure and preorder arrange. No convergence callbacks or live scene writes. */
|
|
8
|
+
export declare function planUiLayout(tree: UiLayoutTree, viewport: UiSize, density: number, previous?: UiLayoutPlan, dirty?: ReadonlyMap<string, UiDirtyKind>): UiLayoutPlan;
|
|
9
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { EgretError } from '../EgretError.js';
|
|
3
|
+
const CAP = 1048576;
|
|
4
|
+
const allowed = [
|
|
5
|
+
'id',
|
|
6
|
+
'kind',
|
|
7
|
+
'explicitWidth',
|
|
8
|
+
'explicitHeight',
|
|
9
|
+
'minWidth',
|
|
10
|
+
'minHeight',
|
|
11
|
+
'maxWidth',
|
|
12
|
+
'maxHeight',
|
|
13
|
+
'padding',
|
|
14
|
+
'gap',
|
|
15
|
+
'crossAlign',
|
|
16
|
+
'children',
|
|
17
|
+
'measure',
|
|
18
|
+
'fill',
|
|
19
|
+
'source',
|
|
20
|
+
'initialScrollOffset'
|
|
21
|
+
];
|
|
22
|
+
function invalid() {
|
|
23
|
+
throw new EgretError('UI_INVALID');
|
|
24
|
+
}
|
|
25
|
+
function finite(value, min = 0, max = CAP) {
|
|
26
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < min || value > max)
|
|
27
|
+
invalid();
|
|
28
|
+
return value === 0 ? 0 : value;
|
|
29
|
+
}
|
|
30
|
+
function integer(value, max) {
|
|
31
|
+
const n = finite(value, 0, max);
|
|
32
|
+
if (!Number.isInteger(n))
|
|
33
|
+
invalid();
|
|
34
|
+
return n;
|
|
35
|
+
}
|
|
36
|
+
function fields(value, names) {
|
|
37
|
+
if (value === null || typeof value !== 'object')
|
|
38
|
+
invalid();
|
|
39
|
+
const proto = Object.getPrototypeOf(value);
|
|
40
|
+
if (proto !== Object.prototype && proto !== null)
|
|
41
|
+
invalid();
|
|
42
|
+
const result = {};
|
|
43
|
+
for (const k of Reflect.ownKeys(value)) {
|
|
44
|
+
if (typeof k !== 'string' || !names.includes(k))
|
|
45
|
+
invalid();
|
|
46
|
+
result[k] = value[k];
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
function arrayCount(value, max) {
|
|
51
|
+
if (!Array.isArray(value))
|
|
52
|
+
invalid();
|
|
53
|
+
const count = value.length;
|
|
54
|
+
if (typeof count !== 'number' || !Number.isInteger(count) || count < 0)
|
|
55
|
+
invalid();
|
|
56
|
+
if (count > max)
|
|
57
|
+
throw new EgretError('UI_LIMIT');
|
|
58
|
+
return count === 0 ? 0 : count;
|
|
59
|
+
}
|
|
60
|
+
function id(value) {
|
|
61
|
+
if (typeof value !== 'string' || !value.length || value.length > 128)
|
|
62
|
+
invalid();
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
function readonlyMap(map) {
|
|
66
|
+
return Object.freeze({
|
|
67
|
+
get size() {
|
|
68
|
+
return map.size;
|
|
69
|
+
},
|
|
70
|
+
get: (key) => map.get(key),
|
|
71
|
+
has: (key) => map.has(key),
|
|
72
|
+
entries: () => map.entries(),
|
|
73
|
+
keys: () => map.keys(),
|
|
74
|
+
values: () => map.values(),
|
|
75
|
+
[Symbol.iterator]: () => map[Symbol.iterator](),
|
|
76
|
+
forEach: (cb, thisArg) => map.forEach((value, key) => cb.call(thisArg, value, key, readonlyMap(map)))
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const capturedTrees = new WeakSet();
|
|
80
|
+
/** Bounded capture reads each authored property once and retains no mutable child arrays. */
|
|
81
|
+
export function captureUiLayout(tree, limits = {}) {
|
|
82
|
+
const l = fields(limits, ['maxNodes', 'maxDepth']);
|
|
83
|
+
const maxNodes = l.maxNodes === undefined ? 4096 : integer(l.maxNodes, 4096), maxDepth = l.maxDepth === undefined ? 64 : integer(l.maxDepth, 64);
|
|
84
|
+
if (!maxNodes || !maxDepth)
|
|
85
|
+
invalid();
|
|
86
|
+
const seen = new Set(), nodes = new Map();
|
|
87
|
+
let lists = 0;
|
|
88
|
+
function visit(value, depth) {
|
|
89
|
+
if (depth > maxDepth || nodes.size >= maxNodes)
|
|
90
|
+
throw new EgretError('UI_LIMIT');
|
|
91
|
+
if (value === null || typeof value !== 'object')
|
|
92
|
+
invalid();
|
|
93
|
+
if (seen.has(value))
|
|
94
|
+
throw new EgretError('UI_LAYOUT_CYCLE');
|
|
95
|
+
seen.add(value);
|
|
96
|
+
const raw = fields(value, allowed), name = id(raw.id), kind = raw.kind;
|
|
97
|
+
if (nodes.has(name))
|
|
98
|
+
throw new EgretError('UI_DUPLICATE_ID');
|
|
99
|
+
if (kind !== 'leaf' && kind !== 'row' && kind !== 'column' && kind !== 'virtualList')
|
|
100
|
+
invalid();
|
|
101
|
+
const minWidth = raw.minWidth === undefined ? 0 : finite(raw.minWidth), minHeight = raw.minHeight === undefined ? 0 : finite(raw.minHeight), maxWidth = raw.maxWidth === undefined ? CAP : finite(raw.maxWidth), maxHeight = raw.maxHeight === undefined ? CAP : finite(raw.maxHeight);
|
|
102
|
+
if (minWidth > maxWidth || minHeight > maxHeight)
|
|
103
|
+
invalid();
|
|
104
|
+
const explicitWidth = raw.explicitWidth === undefined ? undefined : finite(raw.explicitWidth), explicitHeight = raw.explicitHeight === undefined ? undefined : finite(raw.explicitHeight);
|
|
105
|
+
if ((explicitWidth !== undefined && (explicitWidth < minWidth || explicitWidth > maxWidth)) ||
|
|
106
|
+
(explicitHeight !== undefined && (explicitHeight < minHeight || explicitHeight > maxHeight)))
|
|
107
|
+
invalid();
|
|
108
|
+
const padding = raw.padding === undefined ? 0 : finite(raw.padding), gap = raw.gap === undefined ? 0 : finite(raw.gap), crossAlign = raw.crossAlign ?? 'start';
|
|
109
|
+
if (!['start', 'center', 'end', 'stretch'].includes(crossAlign))
|
|
110
|
+
invalid();
|
|
111
|
+
const base = {
|
|
112
|
+
id: name,
|
|
113
|
+
kind,
|
|
114
|
+
minWidth,
|
|
115
|
+
minHeight,
|
|
116
|
+
maxWidth,
|
|
117
|
+
maxHeight,
|
|
118
|
+
padding,
|
|
119
|
+
gap,
|
|
120
|
+
crossAlign: crossAlign,
|
|
121
|
+
...(explicitWidth === undefined ? {} : { explicitWidth }),
|
|
122
|
+
...(explicitHeight === undefined ? {} : { explicitHeight })
|
|
123
|
+
};
|
|
124
|
+
nodes.set(name, base);
|
|
125
|
+
let result;
|
|
126
|
+
if (kind === 'row' || kind === 'column') {
|
|
127
|
+
if (raw.measure !== undefined ||
|
|
128
|
+
raw.fill !== undefined ||
|
|
129
|
+
raw.source !== undefined ||
|
|
130
|
+
raw.initialScrollOffset !== undefined)
|
|
131
|
+
invalid();
|
|
132
|
+
const count = arrayCount(raw.children, maxNodes), authoredChildren = raw.children;
|
|
133
|
+
// Index reads are bounded; holes are refused by visit rather than skipped.
|
|
134
|
+
const children = [];
|
|
135
|
+
for (let i = 0; i < count; i++)
|
|
136
|
+
children.push(visit(authoredChildren[i], depth + 1));
|
|
137
|
+
result = { ...base, children: Object.freeze(children) };
|
|
138
|
+
}
|
|
139
|
+
else if (kind === 'leaf') {
|
|
140
|
+
if (raw.children !== undefined || raw.source !== undefined || raw.initialScrollOffset !== undefined)
|
|
141
|
+
invalid();
|
|
142
|
+
if (raw.measure !== undefined && typeof raw.measure !== 'function')
|
|
143
|
+
invalid();
|
|
144
|
+
let fill;
|
|
145
|
+
if (raw.fill !== undefined) {
|
|
146
|
+
const f = fields(raw.fill, ['color', 'alpha']);
|
|
147
|
+
fill = Object.freeze({
|
|
148
|
+
color: integer(f.color, 0xffffff),
|
|
149
|
+
alpha: f.alpha === undefined ? 1 : finite(f.alpha, 0, 1)
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
result = {
|
|
153
|
+
...base,
|
|
154
|
+
...(raw.measure === undefined ? {} : { measure: raw.measure }),
|
|
155
|
+
...(fill === undefined ? {} : { fill })
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
if (++lists > 1)
|
|
160
|
+
throw new EgretError('UI_LIMIT');
|
|
161
|
+
if (raw.children !== undefined ||
|
|
162
|
+
raw.measure !== undefined ||
|
|
163
|
+
raw.fill !== undefined ||
|
|
164
|
+
padding !== 0 ||
|
|
165
|
+
gap !== 0)
|
|
166
|
+
invalid();
|
|
167
|
+
if (raw.source === undefined)
|
|
168
|
+
invalid();
|
|
169
|
+
const initialScrollOffset = raw.initialScrollOffset === undefined
|
|
170
|
+
? 0
|
|
171
|
+
: finite(raw.initialScrollOffset, -Number.MAX_VALUE, Number.MAX_VALUE);
|
|
172
|
+
result = { ...base, source: raw.source, initialScrollOffset };
|
|
173
|
+
}
|
|
174
|
+
result = Object.freeze(result);
|
|
175
|
+
nodes.set(name, result);
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
const root = visit(tree, 1), result = Object.freeze({ root, nodes: readonlyMap(nodes) });
|
|
179
|
+
capturedTrees.add(result);
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
const caches = new WeakMap();
|
|
183
|
+
function sameLeaf(a, b) {
|
|
184
|
+
return (a.kind === b.kind &&
|
|
185
|
+
a.measure === b.measure &&
|
|
186
|
+
a.explicitWidth === b.explicitWidth &&
|
|
187
|
+
a.explicitHeight === b.explicitHeight &&
|
|
188
|
+
a.minWidth === b.minWidth &&
|
|
189
|
+
a.minHeight === b.minHeight &&
|
|
190
|
+
a.maxWidth === b.maxWidth &&
|
|
191
|
+
a.maxHeight === b.maxHeight &&
|
|
192
|
+
a.padding === b.padding);
|
|
193
|
+
}
|
|
194
|
+
function bounded(value) {
|
|
195
|
+
return finite(value);
|
|
196
|
+
}
|
|
197
|
+
function resolve(intrinsic, explicit, min, max) {
|
|
198
|
+
return bounded(explicit ?? Math.max(min, Math.min(max, intrinsic)));
|
|
199
|
+
}
|
|
200
|
+
/** One postorder measure and preorder arrange. No convergence callbacks or live scene writes. */
|
|
201
|
+
export function planUiLayout(tree, viewport, density, previous, dirty) {
|
|
202
|
+
if (!capturedTrees.has(tree))
|
|
203
|
+
invalid();
|
|
204
|
+
const vp = fields(viewport, ['width', 'height']);
|
|
205
|
+
const width = finite(vp.width, Number.MIN_VALUE, 16384), height = finite(vp.height, Number.MIN_VALUE, 16384);
|
|
206
|
+
finite(density, Number.MIN_VALUE, 8);
|
|
207
|
+
const before = previous === undefined ? undefined : caches.get(previous), next = new Map();
|
|
208
|
+
let measurementCalls = 0, reusedMeasurements = 0;
|
|
209
|
+
if (dirty)
|
|
210
|
+
for (const [name, kind] of dirty) {
|
|
211
|
+
if (!tree.nodes.has(name) || !['measure', 'arrange', 'paint', 'interaction', 'semantic'].includes(kind))
|
|
212
|
+
invalid();
|
|
213
|
+
}
|
|
214
|
+
function measure(node, availableWidth, availableHeight, forcedWidth, forcedHeight, ancestorMeasureDirty = false) {
|
|
215
|
+
const pad = node.padding, minW = node.minWidth, minH = node.minHeight, maxW = node.maxWidth, maxH = node.maxHeight;
|
|
216
|
+
const explicitW = node.explicitWidth ?? forcedWidth, explicitH = node.explicitHeight ?? forcedHeight;
|
|
217
|
+
const aw = Math.max(0, resolve(availableWidth, explicitW, minW, maxW) - pad * 2), ah = Math.max(0, resolve(availableHeight, explicitH, minH, maxH) - pad * 2);
|
|
218
|
+
const dirtyMeasure = ancestorMeasureDirty || dirty?.get(node.id) === 'measure';
|
|
219
|
+
let measuredWidth = 0, measuredHeight = 0, dependencyRevision = 0, contentMeasured = false;
|
|
220
|
+
const children = [];
|
|
221
|
+
if (node.kind === 'leaf') {
|
|
222
|
+
const prior = before?.get(node.id);
|
|
223
|
+
if (!dirtyMeasure &&
|
|
224
|
+
prior &&
|
|
225
|
+
(!node.measure || (explicitW !== undefined && explicitH !== undefined) || prior.contentMeasured) &&
|
|
226
|
+
sameLeaf(prior.node, node) &&
|
|
227
|
+
prior.availableWidth === aw &&
|
|
228
|
+
prior.availableHeight === ah &&
|
|
229
|
+
prior.density === density) {
|
|
230
|
+
measuredWidth = prior.intrinsicWidth;
|
|
231
|
+
measuredHeight = prior.intrinsicHeight;
|
|
232
|
+
dependencyRevision = prior.dependencyRevision;
|
|
233
|
+
reusedMeasurements++;
|
|
234
|
+
const result = {
|
|
235
|
+
node,
|
|
236
|
+
intrinsicWidth: measuredWidth,
|
|
237
|
+
intrinsicHeight: measuredHeight,
|
|
238
|
+
contentMeasured: prior.contentMeasured,
|
|
239
|
+
width: resolve(measuredWidth, explicitW, minW, maxW),
|
|
240
|
+
height: resolve(measuredHeight, explicitH, minH, maxH),
|
|
241
|
+
dependencyRevision,
|
|
242
|
+
availableWidth: aw,
|
|
243
|
+
availableHeight: ah,
|
|
244
|
+
density,
|
|
245
|
+
children
|
|
246
|
+
};
|
|
247
|
+
next.set(node.id, result);
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
if (node.measure && (explicitW === undefined || explicitH === undefined)) {
|
|
251
|
+
const raw = fields(node.measure(Object.freeze({ availableWidth: aw, availableHeight: ah, density })), [
|
|
252
|
+
'width',
|
|
253
|
+
'height',
|
|
254
|
+
'dependencyRevision'
|
|
255
|
+
]);
|
|
256
|
+
measurementCalls++;
|
|
257
|
+
contentMeasured = true;
|
|
258
|
+
measuredWidth = finite(raw.width) + pad * 2;
|
|
259
|
+
measuredHeight = finite(raw.height) + pad * 2;
|
|
260
|
+
dependencyRevision = integer(raw.dependencyRevision, Number.MAX_SAFE_INTEGER);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
measuredWidth = pad * 2;
|
|
264
|
+
measuredHeight = pad * 2;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
else if (node.kind === 'virtualList') {
|
|
268
|
+
if (explicitW === undefined || explicitH === undefined)
|
|
269
|
+
throw new EgretError('UI_LAYOUT_CYCLE');
|
|
270
|
+
if (explicitW <= 0 || explicitH <= 0 || explicitW > 16384 || explicitH > 16384)
|
|
271
|
+
invalid();
|
|
272
|
+
measuredWidth = explicitW;
|
|
273
|
+
measuredHeight = explicitH;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
const row = node.kind === 'row', stretch = node.crossAlign === 'stretch';
|
|
277
|
+
if (stretch && (row ? explicitH : explicitW) === undefined)
|
|
278
|
+
throw new EgretError('UI_LAYOUT_CYCLE');
|
|
279
|
+
for (const child of node.children) {
|
|
280
|
+
const forcedW = !row && stretch && child.explicitWidth === undefined
|
|
281
|
+
? resolve(aw, undefined, child.minWidth, child.maxWidth)
|
|
282
|
+
: undefined, forcedH = row && stretch && child.explicitHeight === undefined
|
|
283
|
+
? resolve(ah, undefined, child.minHeight, child.maxHeight)
|
|
284
|
+
: undefined;
|
|
285
|
+
children.push(measure(child, aw, ah, forcedW, forcedH, dirtyMeasure));
|
|
286
|
+
}
|
|
287
|
+
let main = 0, cross = 0;
|
|
288
|
+
for (const child of children) {
|
|
289
|
+
main += row ? child.width : child.height;
|
|
290
|
+
cross = Math.max(cross, row ? child.height : child.width);
|
|
291
|
+
dependencyRevision = Math.max(dependencyRevision, child.dependencyRevision);
|
|
292
|
+
}
|
|
293
|
+
main += Math.max(0, children.length - 1) * node.gap;
|
|
294
|
+
measuredWidth = (row ? main : cross) + pad * 2;
|
|
295
|
+
measuredHeight = (row ? cross : main) + pad * 2;
|
|
296
|
+
}
|
|
297
|
+
const result = {
|
|
298
|
+
node,
|
|
299
|
+
intrinsicWidth: measuredWidth,
|
|
300
|
+
intrinsicHeight: measuredHeight,
|
|
301
|
+
contentMeasured,
|
|
302
|
+
width: resolve(measuredWidth, explicitW, minW, maxW),
|
|
303
|
+
height: resolve(measuredHeight, explicitH, minH, maxH),
|
|
304
|
+
dependencyRevision,
|
|
305
|
+
availableWidth: aw,
|
|
306
|
+
availableHeight: ah,
|
|
307
|
+
density,
|
|
308
|
+
children
|
|
309
|
+
};
|
|
310
|
+
next.set(node.id, result);
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
const measured = measure(tree.root, width, height), boxes = [];
|
|
314
|
+
function arrange(m, x, y, parentId, order) {
|
|
315
|
+
finite(x, -CAP, CAP);
|
|
316
|
+
finite(y, -CAP, CAP);
|
|
317
|
+
finite(x + m.width, -CAP, CAP);
|
|
318
|
+
finite(y + m.height, -CAP, CAP);
|
|
319
|
+
const node = m.node, pad = node.padding, innerX = x + pad, innerY = y + pad, innerWidth = Math.max(0, m.width - pad * 2), innerHeight = Math.max(0, m.height - pad * 2);
|
|
320
|
+
boxes.push(Object.freeze({
|
|
321
|
+
id: node.id,
|
|
322
|
+
parentId,
|
|
323
|
+
order,
|
|
324
|
+
x: x === 0 ? 0 : x,
|
|
325
|
+
y: y === 0 ? 0 : y,
|
|
326
|
+
width: m.width,
|
|
327
|
+
height: m.height,
|
|
328
|
+
innerX,
|
|
329
|
+
innerY,
|
|
330
|
+
innerWidth,
|
|
331
|
+
innerHeight,
|
|
332
|
+
clip: node.kind === 'virtualList',
|
|
333
|
+
dependencyRevision: m.dependencyRevision
|
|
334
|
+
}));
|
|
335
|
+
const row = node.kind === 'row';
|
|
336
|
+
let cursor = 0;
|
|
337
|
+
for (let i = 0; i < m.children.length; i++) {
|
|
338
|
+
const child = m.children[i], available = row ? innerHeight : innerWidth, size = row ? child.height : child.width, free = available - size, align = node.crossAlign;
|
|
339
|
+
const cross = align === 'center' ? free / 2 : align === 'end' ? free : 0;
|
|
340
|
+
arrange(child, innerX + (row ? cursor : cross), innerY + (row ? cross : cursor), node.id, i);
|
|
341
|
+
cursor += (row ? child.width : child.height) + node.gap;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
arrange(measured, 0, 0, undefined, 0);
|
|
345
|
+
const result = Object.freeze({ boxes: Object.freeze(boxes), measurementCalls, reusedMeasurements });
|
|
346
|
+
caches.set(result, next);
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
//# sourceMappingURL=layout.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DisplayObject } from '../DisplayObject.js';
|
|
2
|
+
/** Only runtime UI construction installs a captured per-wrapper terminal path. */
|
|
3
|
+
export declare function registerManagedWrapper(node: DisplayObject, authority: object, dispose: () => void): void;
|
|
4
|
+
export declare function isManagedWrapper(node: object): boolean;
|
|
5
|
+
export declare function managedBy(node: object, authority: object): boolean;
|
|
6
|
+
export declare function managedTerminal(node: object): boolean;
|
|
7
|
+
export declare function assertManagedMutable(node: object | undefined): void;
|
|
8
|
+
export declare function assertManagedEdgeMutable(parent: DisplayObject, child: DisplayObject): void;
|
|
9
|
+
export declare function assertManagedChildrenMutable(parent: DisplayObject): void;
|
|
10
|
+
/** Revoke this exact value before callbacks; never unlock another managed value. */
|
|
11
|
+
export declare function cleanupManagedWrapper(value: object): boolean;
|
|
12
|
+
//# sourceMappingURL=managedState.d.ts.map
|