@web-canvas-ui/react 0.0.0-stage → 0.1.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.
Files changed (98) hide show
  1. package/Canvas.tsx +142 -0
  2. package/README.md +988 -2
  3. package/__tests__/pure-logic.test.ts +153 -0
  4. package/__tests__/scene-and-node.test.ts +131 -0
  5. package/__tests__/scratch-space.test.ts +30 -0
  6. package/__tests__/textarea-caret.test.ts +96 -0
  7. package/__tests__/utils.test.ts +114 -0
  8. package/canvas-handle.ts +114 -0
  9. package/components/Button/button.ts +57 -0
  10. package/components/Button/index.ts +3 -0
  11. package/components/Button/logic.ts +11 -0
  12. package/components/CanvasView/canvasView.ts +103 -0
  13. package/components/CanvasView/index.ts +7 -0
  14. package/components/Carousel/carousel.ts +107 -0
  15. package/components/Carousel/index.ts +3 -0
  16. package/components/Carousel/logic.ts +36 -0
  17. package/components/Checkbox/checkbox.ts +14 -0
  18. package/components/Checkbox/index.ts +2 -0
  19. package/components/ChoiceGroup/choiceGroup.ts +172 -0
  20. package/components/ChoiceGroup/index.ts +3 -0
  21. package/components/ChoiceGroup/logic.ts +14 -0
  22. package/components/Circle/circle.ts +31 -0
  23. package/components/Circle/index.ts +2 -0
  24. package/components/Collapse/collapse.ts +69 -0
  25. package/components/Collapse/index.ts +3 -0
  26. package/components/Collapse/logic.ts +14 -0
  27. package/components/CustomDraw/customDraw.ts +50 -0
  28. package/components/CustomDraw/index.ts +7 -0
  29. package/components/DatePicker/datePicker.ts +70 -0
  30. package/components/DatePicker/index.ts +2 -0
  31. package/components/Dialog/dialog.ts +102 -0
  32. package/components/Dialog/index.ts +2 -0
  33. package/components/Dropdown/dropdown.ts +125 -0
  34. package/components/Dropdown/index.ts +3 -0
  35. package/components/Dropdown/logic.ts +29 -0
  36. package/components/Grid/grid.ts +15 -0
  37. package/components/Grid/index.ts +2 -0
  38. package/components/GridItem/gridItem.ts +11 -0
  39. package/components/GridItem/index.ts +2 -0
  40. package/components/Group/group.ts +8 -0
  41. package/components/Group/index.ts +2 -0
  42. package/components/Icon/icon.ts +669 -0
  43. package/components/Icon/index.ts +2 -0
  44. package/components/Image/image.ts +69 -0
  45. package/components/Image/index.ts +2 -0
  46. package/components/Input/index.ts +2 -0
  47. package/components/Input/input.ts +139 -0
  48. package/components/Progress/index.ts +2 -0
  49. package/components/Progress/progress.ts +59 -0
  50. package/components/Radio/index.ts +2 -0
  51. package/components/Radio/radio.ts +14 -0
  52. package/components/Rect/index.ts +2 -0
  53. package/components/Rect/rect.ts +35 -0
  54. package/components/Slider/index.ts +3 -0
  55. package/components/Slider/logic.ts +19 -0
  56. package/components/Slider/slider.ts +58 -0
  57. package/components/Switch/index.ts +3 -0
  58. package/components/Switch/logic.ts +8 -0
  59. package/components/Switch/switch.ts +37 -0
  60. package/components/Tabs/index.ts +3 -0
  61. package/components/Tabs/logic.ts +14 -0
  62. package/components/Tabs/tabs.ts +78 -0
  63. package/components/Text/index.ts +2 -0
  64. package/components/Text/text.ts +95 -0
  65. package/components/Textarea/index.ts +2 -0
  66. package/components/Textarea/textarea.ts +16 -0
  67. package/components/Upload/index.ts +2 -0
  68. package/components/Upload/upload.ts +66 -0
  69. package/components/draw-utils.ts +230 -0
  70. package/components/shared.ts +15 -0
  71. package/components/size.ts +20 -0
  72. package/components/types.ts +37 -0
  73. package/components.ts +98 -0
  74. package/engine/__tests__/carousel.test.ts +218 -0
  75. package/engine/__tests__/hit-test.test.ts +282 -0
  76. package/engine/__tests__/layout.test.ts +474 -0
  77. package/engine/carousel.ts +150 -0
  78. package/engine/events.ts +73 -0
  79. package/engine/hit-test.ts +321 -0
  80. package/engine/images.ts +41 -0
  81. package/engine/interaction.ts +442 -0
  82. package/engine/layout.ts +355 -0
  83. package/engine/nodes.ts +166 -0
  84. package/engine/state.ts +75 -0
  85. package/engine/text-controls.ts +122 -0
  86. package/engine/transform.ts +61 -0
  87. package/engine/vnodes.ts +204 -0
  88. package/engine.ts +162 -0
  89. package/image-assets.ts +37 -0
  90. package/index.ts +78 -0
  91. package/input-bridge.ts +285 -0
  92. package/package.json +27 -4
  93. package/renderer.ts +303 -0
  94. package/scene.ts +122 -0
  95. package/theme.css +327 -0
  96. package/theme.scss +234 -0
  97. package/types.ts +256 -0
  98. package/web-canvas-ui-react-0.1.1.tgz +0 -0
@@ -0,0 +1,355 @@
1
+ import { descriptorKey } from '../scene';
2
+ import type { SceneNode } from '../scene';
3
+ import type { TextControlProps, Transform, VNode } from '../types';
4
+ import { number } from '../components/draw-utils';
5
+ import { measureTextBox } from '../components/Text';
6
+ import type { TextBox, TextDrawProps } from '../components/Text';
7
+ import { multiplyTransform } from './transform';
8
+ import { setTreeVisibility } from './nodes';
9
+ import {
10
+ carouselItemOffset,
11
+ layoutGridChildren,
12
+ setVNodeVisibility,
13
+ translateChildren,
14
+ } from './vnodes';
15
+ import { transitionProgress } from './carousel';
16
+ import type { EngineCore, EngineState, TextControlDescription } from './state';
17
+
18
+ /**
19
+ * The declarative-tree normalization pass. {@link prepare} walks the incoming VNode
20
+ * tree once per update, resolving controlled/uncontrolled component state (tabs,
21
+ * carousel, collapse, grid scroll, dialog centering, text-control overlays) into
22
+ * engine-internal props before the scene graph is diffed.
23
+ */
24
+
25
+ /** Body height used when a collapse item declares nothing and its children need even less. */
26
+ const COLLAPSE_MIN_BODY_HEIGHT = 100;
27
+
28
+ /**
29
+ * Box a Text node needs once its content is wrapped and clamped. Shares {@link measureTextBox} with
30
+ * the renderer, so a measured box can never disagree with the painted glyphs.
31
+ */
32
+ function measureTextNode(props: Record<string, unknown>): TextBox {
33
+ return measureTextBox(props as unknown as TextDrawProps);
34
+ }
35
+
36
+ /**
37
+ * Body height of an expanded collapse item. An explicit `height` always wins; otherwise the direct
38
+ * children decide the space, floored at the historical 100px default so existing trees keep their
39
+ * spacing while a wrapped (or clamped) Text pushes the next header down instead of being drawn over
40
+ * it. Measured on the raw vnodes, before the header offset is added to their `y`.
41
+ */
42
+ function resolveCollapseBody(item: VNode): number {
43
+ const declared = number(item.props.height, 0);
44
+ if (declared > 0) return declared;
45
+ let bottom = COLLAPSE_MIN_BODY_HEIGHT;
46
+ for (const child of item.children) {
47
+ const height =
48
+ child.type === 'text'
49
+ ? measureTextNode(child.props).height
50
+ : number(child.props.height, 0);
51
+ bottom = Math.max(bottom, number(child.props.y, 0) + height);
52
+ }
53
+ return bottom;
54
+ }
55
+
56
+ function keyForState(props: Record<string, unknown>, key: string): string {
57
+ const value = props[key];
58
+ if (Array.isArray(value)) return value.join(',');
59
+ return typeof value === 'string' ? value : '';
60
+ }
61
+
62
+ function activeTabKey(
63
+ state: EngineState,
64
+ stateKey: string,
65
+ props: Record<string, unknown>,
66
+ items: VNode[],
67
+ ): string {
68
+ const controlled = props.activeKey;
69
+ if (typeof controlled === 'string') return controlled;
70
+ const current = state.tabSelections.get(stateKey);
71
+ const fallback =
72
+ typeof props.defaultActiveKey === 'string'
73
+ ? props.defaultActiveKey
74
+ : String(items[0]?.props.id ?? '');
75
+ const selected = current ?? fallback;
76
+ state.tabSelections.set(stateKey, selected);
77
+ return selected;
78
+ }
79
+
80
+ export function activeCollapseKeys(
81
+ state: EngineState,
82
+ stateKey: string,
83
+ props: Record<string, unknown>,
84
+ ): Set<string> {
85
+ if (Array.isArray(props.activeKeys)) {
86
+ const keys = props.activeKeys.filter((key): key is string => typeof key === 'string');
87
+ return new Set(props.accordion ? keys.slice(0, 1) : keys);
88
+ }
89
+ const current = state.collapseSelections.get(stateKey);
90
+ const initialKeys = Array.isArray(props.defaultActiveKeys)
91
+ ? props.defaultActiveKeys.filter((key): key is string => typeof key === 'string')
92
+ : [];
93
+ const initial = props.accordion ? initialKeys.slice(0, 1) : initialKeys;
94
+ const active = current ?? new Set(initial);
95
+ state.collapseSelections.set(stateKey, active);
96
+ return active;
97
+ }
98
+
99
+ export function layoutCollapseNodes(parent: SceneNode, active: Set<string>): void {
100
+ const headerHeight = number(parent.props.headerHeight, 40);
101
+ const width = number(parent.props.width, 300);
102
+ let top = 0;
103
+ for (const item of parent.children) {
104
+ if (item.type !== 'collapseItem') continue;
105
+ const expanded = active.has(String(item.props.id ?? ''));
106
+ item.setProps({
107
+ y: top + number(item.props._userY, 0),
108
+ _expanded: expanded,
109
+ _collapseWidth: width,
110
+ _collapseHeaderHeight: headerHeight,
111
+ });
112
+ item.children.forEach((child) => setTreeVisibility(child, expanded));
113
+ top += headerHeight + (expanded ? collapseBodyHeight(item) : 0);
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Reads back the body height {@link prepare} resolved onto `_collapseBodyHeight`. Running the
119
+ * measurement here a second time would be wrong: by now the children's `y` already carries the
120
+ * header offset. The fallbacks keep hand-built scene nodes (and older trees) laying out as before.
121
+ */
122
+ function collapseBodyHeight(item: SceneNode): number {
123
+ return number(
124
+ item.props._collapseBodyHeight,
125
+ number(item.props.height, COLLAPSE_MIN_BODY_HEIGHT),
126
+ );
127
+ }
128
+
129
+ export function prepare(
130
+ core: EngineCore,
131
+ state: EngineState,
132
+ vnodes: VNode[],
133
+ parentTransform: Transform,
134
+ parentVisible: boolean,
135
+ parentPath: string,
136
+ descriptions: TextControlDescription[],
137
+ ): VNode[] {
138
+ return vnodes.map((vnode, index) => {
139
+ const path = descriptorKey(vnode, index, parentPath);
140
+ const props = vnode.props;
141
+ const transform = multiplyTransform(parentTransform, props);
142
+ const visible =
143
+ parentVisible &&
144
+ props.visible !== false &&
145
+ (vnode.type !== 'dialog' || props.open === true);
146
+ const nextProps = { ...props };
147
+ nextProps._userVisible =
148
+ typeof props._userVisible === 'boolean' ? props._userVisible : props.visible !== false;
149
+
150
+ if (vnode.type === 'text') {
151
+ // Wrapping decides how wide and tall this node is, so containers and hit-testing get the
152
+ // measured box instead of assuming a single fixed line.
153
+ const box = measureTextNode(nextProps);
154
+ nextProps._textWidth = box.width;
155
+ nextProps._textHeight = box.height;
156
+ }
157
+
158
+ if (vnode.type === 'input' || vnode.type === 'datePicker' || vnode.type === 'textarea') {
159
+ const id = `${vnode.type}:${path}`;
160
+ nextProps._canvasInputId = id;
161
+ nextProps._controlledValue = props.value !== undefined;
162
+ descriptions.push({
163
+ id,
164
+ props: props as unknown as TextControlProps,
165
+ layout: {
166
+ width: number(props.width, 200),
167
+ height: number(props.height, 36),
168
+ transform,
169
+ visible,
170
+ },
171
+ });
172
+ }
173
+
174
+ let children = vnode.children;
175
+ if (vnode.type === 'grid') {
176
+ const stateKey = path;
177
+ const layout = layoutGridChildren(vnode);
178
+ const maxScroll =
179
+ typeof props.height === 'number'
180
+ ? Math.max(0, layout.contentHeight - props.height)
181
+ : 0;
182
+ const scrollTop = Math.max(
183
+ 0,
184
+ Math.min(maxScroll, state.gridScrollPositions.get(stateKey) ?? 0),
185
+ );
186
+ state.gridScrollPositions.set(stateKey, scrollTop);
187
+ nextProps._gridStateKey = stateKey;
188
+ nextProps._gridWidth = layout.width;
189
+ nextProps._gridContentHeight = layout.contentHeight;
190
+ nextProps._scrollTop = scrollTop;
191
+ children = layout.children.map((child) => ({
192
+ ...child,
193
+ props: {
194
+ ...child.props,
195
+ _gridBaseY: child.props.y,
196
+ y: number(child.props.y, 0) - scrollTop,
197
+ },
198
+ }));
199
+ }
200
+ if (vnode.type === 'dialog') {
201
+ nextProps._viewportWidth = core.context.canvas.width / core.pixelRatio;
202
+ nextProps._viewportHeight = core.context.canvas.height / core.pixelRatio;
203
+ const dialogWidth = number(props.width, 420);
204
+ const dialogHeight = number(props.height, 240);
205
+ const panelX = (number(nextProps._viewportWidth, 0) - dialogWidth) / 2;
206
+ const panelY = (number(nextProps._viewportHeight, 0) - dialogHeight) / 2;
207
+ nextProps._dialogPanelX = panelX;
208
+ nextProps._dialogPanelY = panelY;
209
+ children = translateChildren(children, panelX + 24, panelY + 64);
210
+ } else if (vnode.type === 'tabs') {
211
+ const items = children.filter((child) => child.type === 'tabItem');
212
+ const stateKey = `${path}:${keyForState(props, 'defaultActiveKey')}`;
213
+ const activeKey = activeTabKey(state, stateKey, props, items);
214
+ nextProps._activeKey = activeKey;
215
+ nextProps._tabItems = items.map((item) => ({
216
+ title: item.props.title,
217
+ disabled: item.props.disabled,
218
+ selected: item.props.id === activeKey,
219
+ }));
220
+ const headerHeight = number(props.headerHeight, 40);
221
+ children = children.map((item) => ({
222
+ ...item,
223
+ props: {
224
+ ...item.props,
225
+ visible:
226
+ item.type !== 'tabItem' ||
227
+ (item.props.visible !== false && item.props.id === activeKey),
228
+ _userVisible: item.props._userVisible ?? item.props.visible !== false,
229
+ },
230
+ children:
231
+ item.type === 'tabItem'
232
+ ? setVNodeVisibility(
233
+ translateChildren(item.children, 0, headerHeight),
234
+ item.props.visible !== false && item.props.id === activeKey,
235
+ )
236
+ : item.children,
237
+ }));
238
+ nextProps._stateKey = stateKey;
239
+ } else if (vnode.type === 'carousel') {
240
+ const items = children.filter((child) => child.type === 'carouselItem');
241
+ const stateKey = path;
242
+ const controlled = props.activeIndex;
243
+ const requested =
244
+ typeof controlled === 'number' && Number.isInteger(controlled)
245
+ ? controlled
246
+ : (state.carouselSelections.get(stateKey) ??
247
+ (typeof props.defaultActiveIndex === 'number'
248
+ ? props.defaultActiveIndex
249
+ : 0));
250
+ let selected = requested;
251
+ let transition = state.carouselTransitions.get(stateKey);
252
+ if (transition && controlled !== undefined && requested !== transition.to) {
253
+ state.carouselTransitions.delete(stateKey);
254
+ transition = undefined;
255
+ }
256
+ const activeIndex = items.length
257
+ ? Math.max(0, Math.min(items.length - 1, selected))
258
+ : 0;
259
+ if (transition && (transition.from >= items.length || transition.to >= items.length)) {
260
+ state.carouselTransitions.delete(stateKey);
261
+ transition = undefined;
262
+ }
263
+ if (transition) selected = transition.to;
264
+ if (props.activeIndex === undefined)
265
+ state.carouselSelections.set(stateKey, activeIndex);
266
+ nextProps._stateKey = stateKey;
267
+ nextProps._activeIndex = transition ? transition.to : activeIndex;
268
+ nextProps._itemCount = items.length;
269
+ nextProps._transitionProgress = transition
270
+ ? transitionProgress(transition, state.currentFrameTime)
271
+ : 1;
272
+ const progress = number(nextProps._transitionProgress, 1);
273
+ const eased = progress * progress * (3 - 2 * progress);
274
+ children = children.map((item) => ({
275
+ ...item,
276
+ props: {
277
+ ...item.props,
278
+ _carouselBaseX: item.props._carouselBaseX ?? number(item.props.x, 0),
279
+ x:
280
+ item.type === 'carouselItem'
281
+ ? number(item.props.x, 0) +
282
+ carouselItemOffset(
283
+ items.indexOf(item),
284
+ transition,
285
+ eased,
286
+ number(props.width, 300),
287
+ )
288
+ : item.props.x,
289
+ visible:
290
+ item.type !== 'carouselItem' ||
291
+ (item.props.visible !== false &&
292
+ (transition
293
+ ? items.indexOf(item) === transition.from ||
294
+ items.indexOf(item) === transition.to
295
+ : items[activeIndex] === item)),
296
+ _userVisible: item.props._userVisible ?? item.props.visible !== false,
297
+ },
298
+ children:
299
+ item.type === 'carouselItem'
300
+ ? setVNodeVisibility(
301
+ item.children,
302
+ item.props.visible !== false &&
303
+ (transition
304
+ ? items.indexOf(item) === transition.from ||
305
+ items.indexOf(item) === transition.to
306
+ : items[activeIndex] === item),
307
+ )
308
+ : item.children,
309
+ }));
310
+ } else if (vnode.type === 'collapse') {
311
+ const stateKey = `${path}:${keyForState(props, 'defaultActiveKeys')}`;
312
+ const activeKeys = activeCollapseKeys(state, stateKey, props);
313
+ const headerHeight = number(props.headerHeight, 40);
314
+ const width = number(props.width, 300);
315
+ let top = 0;
316
+ children = children.map((item) => {
317
+ if (item.type !== 'collapseItem') return item;
318
+ const expanded = activeKeys.has(String(item.props.id ?? ''));
319
+ const bodyHeight = resolveCollapseBody(item);
320
+ const contentHeight = expanded ? bodyHeight : 0;
321
+ const result: VNode = {
322
+ ...item,
323
+ props: {
324
+ ...item.props,
325
+ x: number(item.props.x, 0),
326
+ y: top + number(item.props.y, 0),
327
+ _userY: number(item.props.y, 0),
328
+ visible: item.props.visible !== false,
329
+ _userVisible: item.props._userVisible ?? item.props.visible !== false,
330
+ _expanded: expanded,
331
+ _collapseWidth: width,
332
+ _collapseHeaderHeight: headerHeight,
333
+ _collapseBodyHeight: bodyHeight,
334
+ backgroundColor: item.props.backgroundColor ?? props.backgroundColor,
335
+ textColor: item.props.textColor ?? props.textColor,
336
+ borderColor: item.props.borderColor ?? props.borderColor,
337
+ },
338
+ children: setVNodeVisibility(
339
+ translateChildren(item.children, 0, headerHeight),
340
+ expanded && item.props.visible !== false,
341
+ ),
342
+ };
343
+ top += headerHeight + contentHeight;
344
+ return result;
345
+ });
346
+ nextProps._stateKey = stateKey;
347
+ nextProps._activeKeys = [...activeKeys];
348
+ }
349
+ return {
350
+ ...vnode,
351
+ props: nextProps,
352
+ children: prepare(core, state, children, transform, visible, path, descriptions),
353
+ };
354
+ });
355
+ }
@@ -0,0 +1,166 @@
1
+ import type { SceneNode } from '../scene';
2
+ import type { CanvasUIEventTarget, InputLayout } from '../types';
3
+ import { number } from '../components/draw-utils';
4
+ import { choiceGroupSize } from '../components/ChoiceGroup';
5
+ import { componentHeight, componentWidth } from '../components/size';
6
+ import { invertPoint, nodeTransform } from './transform';
7
+
8
+ /**
9
+ * Pure predicates and derived getters over a {@link SceneNode} subtree.
10
+ * None of these read engine-level mutable state — they only inspect nodes.
11
+ */
12
+
13
+ export function hasVisibleAncestors(node: SceneNode): boolean {
14
+ let current: SceneNode | null = node;
15
+ while (current) {
16
+ if (current.props.visible === false) return false;
17
+ current = current.parent;
18
+ }
19
+ return true;
20
+ }
21
+
22
+ export function hasVisibleOpenDialog(node: SceneNode, parentVisible: boolean): boolean {
23
+ const visible = parentVisible && node.props.visible !== false;
24
+ if (!visible) return false;
25
+ if (node.type === 'dialog' && node.props.open === true) return true;
26
+ return node.children.some((child) => hasVisibleOpenDialog(child, visible));
27
+ }
28
+
29
+ export function eventTarget(node: SceneNode): CanvasUIEventTarget {
30
+ if (node.type === 'root') throw new Error('The Canvas root is not an event target.');
31
+ const storedKey = node.props._key;
32
+ const separator = typeof storedKey === 'string' ? storedKey.indexOf(':') : -1;
33
+ return {
34
+ type: node.type,
35
+ key: typeof storedKey !== 'string' || separator < 0 ? null : storedKey.slice(separator + 1),
36
+ };
37
+ }
38
+
39
+ export function isCanvasElement(value: unknown): value is HTMLCanvasElement {
40
+ return (
41
+ typeof value === 'object' &&
42
+ value !== null &&
43
+ 'nodeName' in value &&
44
+ value.nodeName === 'CANVAS' &&
45
+ 'width' in value &&
46
+ typeof value.width === 'number' &&
47
+ 'height' in value &&
48
+ typeof value.height === 'number'
49
+ );
50
+ }
51
+
52
+ export function isInteractive(type: string): boolean {
53
+ return (
54
+ type === 'button' ||
55
+ type === 'upload' ||
56
+ type === 'switch' ||
57
+ type === 'slider' ||
58
+ type === 'dropdown' ||
59
+ type === 'tabs' ||
60
+ type === 'collapse' ||
61
+ type === 'grid' ||
62
+ type === 'radio' ||
63
+ type === 'checkbox' ||
64
+ type === 'carousel' ||
65
+ type === 'customDraw' ||
66
+ type === 'canvasView'
67
+ );
68
+ }
69
+
70
+ export function isDescendantOf(node: SceneNode, ancestor: SceneNode): boolean {
71
+ let current = node.parent;
72
+ while (current) {
73
+ if (current === ancestor) return true;
74
+ current = current.parent;
75
+ }
76
+ return false;
77
+ }
78
+
79
+ export function setTreeVisibility(node: SceneNode, visible: boolean): void {
80
+ const effectiveVisibility = visible && node.props._userVisible !== false;
81
+ node.setProps({ visible: effectiveVisibility });
82
+ node.children.forEach((child) => setTreeVisibility(child, effectiveVisibility));
83
+ }
84
+
85
+ export function controlSize(node: SceneNode): { width: number; height: number } {
86
+ if (node.type === 'radio' || node.type === 'checkbox') return choiceGroupSize(node.props);
87
+ if (node.type === 'icon') {
88
+ const size = number(node.props.size, 24);
89
+ return { width: size, height: size };
90
+ }
91
+ if (node.type === 'customDraw') {
92
+ return {
93
+ width: number(node.props.width, 0),
94
+ height: number(node.props.height, 0),
95
+ };
96
+ }
97
+ if (node.type === 'text') {
98
+ // A Text node has no intrinsic control size: `prepare` measures the box its wrapped lines
99
+ // need, and an explicitly declared width/height still takes precedence.
100
+ return {
101
+ width: componentWidth(node.props, number(node.props._textWidth, 0)),
102
+ height: componentHeight(node.props, number(node.props._textHeight, 0)),
103
+ };
104
+ }
105
+ const widthFallback =
106
+ node.type === 'upload'
107
+ ? 120
108
+ : node.type === 'switch'
109
+ ? 44
110
+ : node.type === 'slider'
111
+ ? 160
112
+ : node.type === 'button'
113
+ ? 120
114
+ : 200;
115
+ const heightFallback =
116
+ node.type === 'upload'
117
+ ? 40
118
+ : node.type === 'switch'
119
+ ? 24
120
+ : node.type === 'slider'
121
+ ? 32
122
+ : node.type === 'button'
123
+ ? 40
124
+ : 36;
125
+ if (node.type === 'switch' || node.type === 'slider') {
126
+ return {
127
+ width: number(node.props.width, widthFallback),
128
+ height: number(node.props.height, heightFallback),
129
+ };
130
+ }
131
+ if (
132
+ node.type === 'button' ||
133
+ node.type === 'upload' ||
134
+ node.type === 'dropdown' ||
135
+ node.type === 'datePicker'
136
+ ) {
137
+ return {
138
+ width: number(node.props.width, widthFallback),
139
+ height: number(node.props.height, heightFallback),
140
+ };
141
+ }
142
+ return {
143
+ width: componentWidth(node.props, widthFallback),
144
+ height: componentHeight(node.props, heightFallback),
145
+ };
146
+ }
147
+
148
+ export function isTextControlVisible(node: SceneNode, layout: InputLayout): boolean {
149
+ let current: SceneNode | null = node;
150
+ while (current) {
151
+ if (current.props.visible === false) return false;
152
+ if (current.type === 'grid' && typeof current.props.height === 'number') {
153
+ const [a, b, c, d, e, f] = layout.transform;
154
+ const centerX = (a * layout.width) / 2 + (c * layout.height) / 2 + e;
155
+ const centerY = (b * layout.width) / 2 + (d * layout.height) / 2 + f;
156
+ const point = invertPoint(centerX, centerY, nodeTransform(current));
157
+ const width = number(current.props._gridWidth, number(current.props.width, 0));
158
+ const height = number(current.props.height, 0);
159
+ if (!point || point.x < 0 || point.x > width || point.y < 0 || point.y > height) {
160
+ return false;
161
+ }
162
+ }
163
+ current = current.parent;
164
+ }
165
+ return true;
166
+ }
@@ -0,0 +1,75 @@
1
+ import type { SceneNode } from '../scene';
2
+ import type { CanvasRenderer } from '../renderer';
3
+ import type { CanvasUIEvent, InputLayout, TextControlProps, WebInputBridge } from '../types';
4
+
5
+ /** A text input/date/textarea currently mirrored into the DOM overlay bridge. */
6
+ export interface MountedTextControl {
7
+ node: SceneNode;
8
+ dispose: () => void;
9
+ layout: InputLayout;
10
+ }
11
+
12
+ /** A text control discovered during the `prepare` pass, awaiting sync. */
13
+ export interface TextControlDescription {
14
+ id: string;
15
+ props: TextControlProps;
16
+ layout: InputLayout;
17
+ }
18
+
19
+ export interface CarouselTransition {
20
+ from: number;
21
+ to: number;
22
+ direction: -1 | 1;
23
+ startedAt: number;
24
+ duration: number;
25
+ }
26
+
27
+ export interface PointerSequence {
28
+ target: SceneNode | null;
29
+ x: number;
30
+ y: number;
31
+ eligible: boolean;
32
+ }
33
+
34
+ export interface GridScrollGesture {
35
+ node: SceneNode;
36
+ pointerY: number;
37
+ scrollTop: number;
38
+ }
39
+
40
+ /**
41
+ * Immutable services and configuration shared by every engine behaviour module:
42
+ * the scene graph, renderer, canvas context, DOM input bridge and callbacks.
43
+ * {@link CanvasEngine} owns exactly one of these for its lifetime.
44
+ */
45
+ export interface EngineCore {
46
+ readonly scene: SceneNode;
47
+ readonly renderer: CanvasRenderer;
48
+ readonly context: CanvasRenderingContext2D;
49
+ readonly inputBridge: WebInputBridge;
50
+ readonly pixelRatio: number;
51
+ readonly onFrame?: (time: number) => void;
52
+ readonly onEvent?: (event: CanvasUIEvent) => void;
53
+ }
54
+
55
+ /**
56
+ * All engine-mutable state that survives across frames and pointer events.
57
+ * Extracted from the former {@link CanvasEngine} private fields so behaviour
58
+ * modules can operate on it explicitly instead of via a God-class `this`.
59
+ */
60
+ export class EngineState {
61
+ readonly textControls = new Map<string, MountedTextControl>();
62
+ readonly inputValues = new Map<string, string>();
63
+ readonly imageSubscriptions = new Map<string, () => void>();
64
+ readonly tabSelections = new Map<string, string>();
65
+ readonly carouselSelections = new Map<string, number>();
66
+ readonly carouselFrameTimes = new Map<string, number>();
67
+ readonly carouselTransitions = new Map<string, CarouselTransition>();
68
+ readonly collapseSelections = new Map<string, Set<string>>();
69
+ readonly gridScrollPositions = new Map<string, number>();
70
+ readonly pointerSequences = new Map<number, PointerSequence>();
71
+ activeSlider: SceneNode | null = null;
72
+ activeButton: SceneNode | null = null;
73
+ activeGridScroll: GridScrollGesture | null = null;
74
+ currentFrameTime = 0;
75
+ }