@geastack/engine 0.1.5

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 (86) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +29 -0
  3. package/bitmap_font.cpp +165 -0
  4. package/canvas.cpp +4608 -0
  5. package/components/Button.d.ts +4 -0
  6. package/components/Button.tsx +32 -0
  7. package/components/Image.d.ts +3 -0
  8. package/components/Image.tsx +17 -0
  9. package/components/Text.d.ts +3 -0
  10. package/components/Text.tsx +9 -0
  11. package/components/View.d.ts +3 -0
  12. package/components/View.tsx +24 -0
  13. package/components/index.d.ts +8 -0
  14. package/components/index.ts +4 -0
  15. package/css/animation.h +252 -0
  16. package/css/declarative.h +131 -0
  17. package/css/easing.h +160 -0
  18. package/css/engine.h +223 -0
  19. package/css/interpolate.h +38 -0
  20. package/image_store.cpp +807 -0
  21. package/package.json +62 -0
  22. package/rasterized_font.cpp +570 -0
  23. package/touch_runtime.cpp +513 -0
  24. package/ui/absolute_leaf_refresh.cpp +279 -0
  25. package/ui/absolute_leaf_refresh.h +12 -0
  26. package/ui/canvas_element.cpp +2314 -0
  27. package/ui/canvas_element.h +353 -0
  28. package/ui/canvas_store.cpp +188 -0
  29. package/ui/canvas_store.h +59 -0
  30. package/ui/core.cpp +135 -0
  31. package/ui/css_atom.cpp +155 -0
  32. package/ui/css_atom.h +21 -0
  33. package/ui/dirty_regions.cpp +66 -0
  34. package/ui/dirty_regions.h +34 -0
  35. package/ui/display_invalidation.cpp +101 -0
  36. package/ui/display_invalidation.h +15 -0
  37. package/ui/document.cpp +356 -0
  38. package/ui/document.h +101 -0
  39. package/ui/image.h +16 -0
  40. package/ui/image_element.cpp +13 -0
  41. package/ui/image_node.cpp +165 -0
  42. package/ui/input.cpp +936 -0
  43. package/ui/input_render.cpp +213 -0
  44. package/ui/internal.h +567 -0
  45. package/ui/layout.cpp +2025 -0
  46. package/ui/layout_snapshot.cpp +82 -0
  47. package/ui/layout_snapshot.h +12 -0
  48. package/ui/mirror.h +25 -0
  49. package/ui/node.cpp +243 -0
  50. package/ui/node.h +154 -0
  51. package/ui/node_lifecycle.cpp +137 -0
  52. package/ui/node_lifecycle.h +13 -0
  53. package/ui/node_model.h +597 -0
  54. package/ui/refresh_perf.h +235 -0
  55. package/ui/refresh_trace.h +68 -0
  56. package/ui/render.cpp +12097 -0
  57. package/ui/root_scroll_refresh.cpp +859 -0
  58. package/ui/root_scroll_refresh.h +30 -0
  59. package/ui/state_init.h +21 -0
  60. package/ui/style.cpp +14994 -0
  61. package/ui/style.h +727 -0
  62. package/ui/style_values.h +31 -0
  63. package/ui/text.cpp +2010 -0
  64. package/ui/text.h +15 -0
  65. package/ui/text_element.cpp +15 -0
  66. package/ui/tree_events.cpp +621 -0
  67. package/ui/tree_events.h +69 -0
  68. package/ui/tree_internal.h +191 -0
  69. package/ui/tree_nodes.cpp +628 -0
  70. package/ui/tree_render.cpp +3138 -0
  71. package/ui/tree_scroll_mirror.cpp +101 -0
  72. package/ui/tree_state.cpp +574 -0
  73. package/ui/tree_state.h +235 -0
  74. package/ui/tree_style.cpp +698 -0
  75. package/ui/ui.h +9 -0
  76. package/ui/view.cpp +2232 -0
  77. package/ui/view.h +14 -0
  78. package/ui/view_element.cpp +13 -0
  79. package/ui/viewport_region.cpp +36 -0
  80. package/ui/viewport_region.h +12 -0
  81. package/ui/virtual_keyboard.cpp +908 -0
  82. package/ui/virtual_keyboard.h +59 -0
  83. package/vendor/AnimatedGIF/AnimatedGIF.c +982 -0
  84. package/vendor/AnimatedGIF/AnimatedGIF.h +216 -0
  85. package/vendor/stb/stb_image.h +7988 -0
  86. package/vendor/stb/stb_truetype.h +5079 -0
package/ui/text.h ADDED
@@ -0,0 +1,15 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "node.h"
5
+
6
+ namespace gea::embedded::ui {
7
+
8
+ class TextElement : public NodeHandle {
9
+ public:
10
+ using NodeHandle::NodeHandle;
11
+ static TextElement create(const char *text = "");
12
+ void textContent(const char *text) const { setText(text); }
13
+ };
14
+
15
+ } // namespace gea::embedded::ui
@@ -0,0 +1,15 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "text.h"
3
+
4
+ #include "tree_internal.h"
5
+
6
+ namespace gea::embedded::ui {
7
+
8
+ TextElement TextElement::create(const char *text)
9
+ {
10
+ TextElement node(Tree::instance().createText());
11
+ node.textContent(text);
12
+ return node;
13
+ }
14
+
15
+ } // namespace gea::embedded::ui
@@ -0,0 +1,621 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "tree_events.h"
3
+
4
+ #include "audio.h"
5
+ #include "internal.h"
6
+ #include "tree_internal.h"
7
+ #include "tree_state.h"
8
+
9
+ #include <cstddef>
10
+ #include <cstdio>
11
+ #include <cstring>
12
+ #include <deque>
13
+ #include <utility>
14
+ #include <vector>
15
+
16
+ extern "C" double gea_host_image_load_asset_path(const char *path);
17
+
18
+ namespace gea::embedded::ui {
19
+
20
+ namespace {
21
+
22
+ void copyBounded(char *dest, int capacity, const char *value)
23
+ {
24
+ if (capacity <= 0) return;
25
+ if (!value) value = "";
26
+ std::snprintf(dest, static_cast<std::size_t>(capacity), "%s", value);
27
+ }
28
+
29
+ bool sameName(const char *a, const char *b)
30
+ {
31
+ return a && b && std::strcmp(a, b) == 0;
32
+ }
33
+
34
+ bool audioBooleanAttributeEnabled(const char *value)
35
+ {
36
+ if (!value) return false;
37
+ if (value[0] == '\0') return true;
38
+ return std::strcmp(value, "true") == 0 || std::strcmp(value, "1") == 0;
39
+ }
40
+
41
+ void applyAudioAttribute(Tree &tree, int node, const char *name, const char *value)
42
+ {
43
+ if (tree.node(node).type != NodeType::Audio) return;
44
+ const bool srcChanged = sameName(name, "src");
45
+ const bool autoplayChanged = sameName(name, "autoplay");
46
+ if (!srcChanged && !autoplayChanged) return;
47
+ const char *src = srcChanged ? value : tree.getAttribute(node, "src");
48
+ const bool hasAutoplay = autoplayChanged || tree.hasAttribute(node, "autoplay");
49
+ const char *autoplay = autoplayChanged ? value : (hasAutoplay ? tree.getAttribute(node, "autoplay") : nullptr);
50
+ if (!src || src[0] == '\0' || !hasAutoplay || !audioBooleanAttributeEnabled(autoplay)) return;
51
+ gea::platform::audio::AudioSystem::playFile(src);
52
+ }
53
+
54
+ // Per-event-type count of nodes that currently have a listener for that type.
55
+ // Lets the dispatcher skip building the event payload and walking the tree for
56
+ // a type nobody listens to (e.g. `touchmove` when an app binds only `click`).
57
+ // Index order mirrors NodeEventListeners' slots and is kept in sync by
58
+ // setEventListener (on the empty->set transition) and NodeEventListeners::clear()
59
+ // (which every removal path calls).
60
+ constexpr int kEventTypeCount = 6;
61
+ int g_listenerTypeCounts[kEventTypeCount] = {};
62
+ EventListenerId g_nextEventListenerId = 1;
63
+
64
+ int eventTypeIndex(const char *type)
65
+ {
66
+ if (sameName(type, "click")) return 0;
67
+ // Pointer events are the cross-platform input API (the web build binds the
68
+ // same onPointerDown/Move/Up handlers, where the browser raises them for both
69
+ // mouse and touch). On device the controller only synthesizes touch*, so a
70
+ // pointer* listener shares its touch* sibling's slot and is fired by the same
71
+ // touch dispatch — otherwise addEventListener() would drop it as an unknown
72
+ // type and the handler would never run (maps' pan/zoom did exactly that).
73
+ if (sameName(type, "touchstart") || sameName(type, "pointerdown")) return 1;
74
+ if (sameName(type, "touchmove") || sameName(type, "pointermove")) return 2;
75
+ if (sameName(type, "touchend") || sameName(type, "pointerup")) return 3;
76
+ if (sameName(type, "input")) return 4;
77
+ if (sameName(type, "keydown")) return 5;
78
+ return -1;
79
+ }
80
+
81
+ // Document-level (off-tree) event handlers. `rotary` (encoder dial) belongs to
82
+ // the document, not to any element — it is never stored per-node. `keydown`
83
+ // bound on a non-input element is treated the same way (hardware buttons /
84
+ // app-level shortcuts); keydown on an <input> stays a per-node listener.
85
+ // Dispatched directly by the runtime (dispatch_rotary_input / dispatch_key_input),
86
+ // never by the tree walk. Reset on app switch via resetDocumentEventListeners().
87
+ gea::framework::events::EventListener g_documentRotaryListener;
88
+ gea::framework::events::EventListener g_documentKeyDownListener;
89
+
90
+ void chainDocumentListener(gea::framework::events::EventListener &slot,
91
+ gea::framework::events::EventListener listener)
92
+ {
93
+ if (!listener) return;
94
+ if (static_cast<bool>(slot)) {
95
+ slot = [previous = std::move(slot), next = std::move(listener)](
96
+ gea::framework::events::PointerEvent &event) mutable {
97
+ previous(event);
98
+ if (event.propagationStopped) return;
99
+ next(event);
100
+ };
101
+ return;
102
+ }
103
+ slot = std::move(listener);
104
+ }
105
+
106
+ } // namespace
107
+
108
+ void resetDocumentEventListeners()
109
+ {
110
+ g_documentRotaryListener = {};
111
+ g_documentKeyDownListener = {};
112
+ }
113
+
114
+ // Register a document-level handler. Only "rotary" and "keydown" are
115
+ // document-native; anything else is ignored (those route through per-node
116
+ // setEventListener instead). Called by Document::addEventListener.
117
+ void setDocumentEventListener(const char *type, gea::framework::events::EventListener listener)
118
+ {
119
+ if (!type || !listener) return;
120
+ if (sameName(type, "rotary")) {
121
+ chainDocumentListener(g_documentRotaryListener, std::move(listener));
122
+ return;
123
+ }
124
+ if (sameName(type, "keydown")) {
125
+ chainDocumentListener(g_documentKeyDownListener, std::move(listener));
126
+ }
127
+ }
128
+
129
+ // Fire the document-level rotary handler with a fresh Rotary event carrying the
130
+ // encoder delta. No tree walk, no node target.
131
+ void dispatchDocumentRotary(int delta)
132
+ {
133
+ if (!static_cast<bool>(g_documentRotaryListener)) return;
134
+ gea::framework::events::PointerEvent event{};
135
+ event.type = gea::framework::events::PointerEventType::Rotary;
136
+ event.delta = delta;
137
+ event.bubbles = true;
138
+ event.cancelable = true;
139
+ g_documentRotaryListener(event);
140
+ }
141
+
142
+ // Fire the document-level keydown handler (hardware buttons / app-level
143
+ // shortcuts bound on a non-input element). Returns true if a handler ran.
144
+ bool dispatchDocumentKeyDown(gea::framework::events::PointerEvent &event)
145
+ {
146
+ if (!static_cast<bool>(g_documentKeyDownListener)) return false;
147
+ g_documentKeyDownListener(event);
148
+ return true;
149
+ }
150
+
151
+ // ---- NodeRareData pool ------------------------------------------------------
152
+ //
153
+ // Cold/optional per-node state (currently event listeners) lives here instead
154
+ // of a dense [kMaxNodes] array. A node holds an int16 handle (Node::rare_data,
155
+ // -1 = none) into this pool; a block is allocated lazily the first time a node
156
+ // needs one and returned to a free list when the node is reset. A std::deque
157
+ // backs the pool because its element addresses are STABLE across growth — the
158
+ // dispatcher holds a NodeRareData* across the (re-entrant) handler call, exactly
159
+ // as the old fixed array allowed. The free list keeps it bounded and reused.
160
+ namespace {
161
+ std::deque<NodeRareData> g_rareDataPool;
162
+ std::vector<int16_t> g_rareDataFreeList;
163
+ } // namespace
164
+
165
+ NodeRareData &ensureRareData(int node)
166
+ {
167
+ auto &state = treeState();
168
+ int16_t handle = state.nodes[node].rare_data;
169
+ if (handle >= 0) return g_rareDataPool[static_cast<std::size_t>(handle)];
170
+ if (!g_rareDataFreeList.empty()) {
171
+ handle = g_rareDataFreeList.back();
172
+ g_rareDataFreeList.pop_back();
173
+ } else {
174
+ handle = static_cast<int16_t>(g_rareDataPool.size());
175
+ g_rareDataPool.emplace_back();
176
+ }
177
+ state.nodes[node].rare_data = handle;
178
+ return g_rareDataPool[static_cast<std::size_t>(handle)];
179
+ }
180
+
181
+ NodeRareData *rareDataFor(int node)
182
+ {
183
+ auto &state = treeState();
184
+ if (node < 0 || node >= kMaxNodes) return nullptr;
185
+ const int16_t handle = state.nodes[node].rare_data;
186
+ return handle >= 0 ? &g_rareDataPool[static_cast<std::size_t>(handle)] : nullptr;
187
+ }
188
+
189
+ void releaseRareData(int node)
190
+ {
191
+ auto &state = treeState();
192
+ if (node < 0 || node >= kMaxNodes) return;
193
+ const int16_t handle = state.nodes[node].rare_data;
194
+ if (handle < 0) return;
195
+ g_rareDataPool[static_cast<std::size_t>(handle)].clear();
196
+ g_rareDataFreeList.push_back(handle);
197
+ state.nodes[node].rare_data = -1;
198
+ }
199
+
200
+ void NodeAttributeStore::clear()
201
+ {
202
+ pressId = -1;
203
+ pressValue = -1;
204
+ idAtom = kInvalidCssAtom;
205
+ count = 0;
206
+ for (auto &attr : values) {
207
+ attr.name[0] = '\0';
208
+ attr.value[0] = '\0';
209
+ }
210
+ }
211
+
212
+ void NodeAttributeStore::set(const char *name, const char *value)
213
+ {
214
+ if (!name || name[0] == '\0') return;
215
+ const bool isId = sameName(name, "id");
216
+ for (uint8_t i = 0; i < count; i++) {
217
+ if (!sameName(values[i].name, name)) continue;
218
+ copyBounded(values[i].value, kNodeAttributeValueMax, value);
219
+ if (isId) idAtom = internCssAtom(values[i].value);
220
+ return;
221
+ }
222
+ if (count >= kMaxNodeAttributes) return;
223
+ copyBounded(values[count].name, kNodeAttributeNameMax, name);
224
+ copyBounded(values[count].value, kNodeAttributeValueMax, value);
225
+ if (isId) idAtom = internCssAtom(values[count].value);
226
+ count++;
227
+ }
228
+
229
+ bool NodeAttributeStore::remove(const char *name)
230
+ {
231
+ if (!name) return false;
232
+ for (uint8_t i = 0; i < count; i++) {
233
+ if (!sameName(values[i].name, name)) continue;
234
+ if (sameName(name, "id")) idAtom = kInvalidCssAtom;
235
+ for (uint8_t j = i; j + 1 < count; j++) {
236
+ values[j] = values[j + 1];
237
+ }
238
+ count--;
239
+ values[count].name[0] = '\0';
240
+ values[count].value[0] = '\0';
241
+ return true;
242
+ }
243
+ return false;
244
+ }
245
+
246
+ const char *NodeAttributeStore::get(const char *name) const
247
+ {
248
+ if (!name) return "";
249
+ for (uint8_t i = 0; i < count; i++) {
250
+ if (sameName(values[i].name, name)) return values[i].value;
251
+ }
252
+ return "";
253
+ }
254
+
255
+ bool NodeAttributeStore::has(const char *name) const
256
+ {
257
+ if (!name) return false;
258
+ for (uint8_t i = 0; i < count; i++) {
259
+ if (sameName(values[i].name, name)) return true;
260
+ }
261
+ return false;
262
+ }
263
+
264
+ void NodeEventListeners::clear()
265
+ {
266
+ // Releasing a non-empty list drops a node from its type's live-listener count, so
267
+ // the dispatcher stops walking the tree for a type once its last listener
268
+ // goes away.
269
+ const auto release = [](NodeEventListenerList &list, int index) {
270
+ if (!list.empty() && g_listenerTypeCounts[index] > 0) g_listenerTypeCounts[index]--;
271
+ list.entries.clear();
272
+ };
273
+ release(click, 0);
274
+ release(touchstart, 1);
275
+ release(touchmove, 2);
276
+ release(touchend, 3);
277
+ release(input, 4);
278
+ release(keydown, 5);
279
+ }
280
+
281
+ NodeEventListenerList *NodeEventListeners::listenersFor(const char *type)
282
+ {
283
+ if (sameName(type, "click")) return &click;
284
+ // Pointer events alias onto their touch* sibling slot — see eventTypeIndex.
285
+ if (sameName(type, "touchstart") || sameName(type, "pointerdown")) return &touchstart;
286
+ if (sameName(type, "touchmove") || sameName(type, "pointermove")) return &touchmove;
287
+ if (sameName(type, "touchend") || sameName(type, "pointerup")) return &touchend;
288
+ if (sameName(type, "input")) return &input;
289
+ if (sameName(type, "keydown")) return &keydown;
290
+ return nullptr;
291
+ }
292
+
293
+ const NodeEventListenerList *NodeEventListeners::listenersFor(const char *type) const
294
+ {
295
+ return const_cast<NodeEventListeners *>(this)->listenersFor(type);
296
+ }
297
+
298
+ bool NodeEventListeners::hasAny() const
299
+ {
300
+ return !click.empty() || !touchstart.empty() || !touchmove.empty() ||
301
+ !touchend.empty() || !input.empty() || !keydown.empty();
302
+ }
303
+
304
+ EventListenerId Tree::setEventListener(int node, const char *type, gea::framework::events::EventListener listener)
305
+ {
306
+ auto &state = treeState();
307
+ if (node < 0 || node >= state.nodeCount) return kInvalidEventListenerId;
308
+ if (!listener) return kInvalidEventListenerId;
309
+ // Don't allocate a rare-data block for an unrecognized event type (the set
310
+ // listenerFor knows == the set eventTypeIndex tracks).
311
+ if (eventTypeIndex(type) < 0) return kInvalidEventListenerId;
312
+ auto *list = ensureRareData(node).listeners.listenersFor(type);
313
+ if (!list) return kInvalidEventListenerId;
314
+ // First listener for this (node, type): bump the type's live count so the
315
+ // dispatcher knows the type is worth walking the tree for.
316
+ const int typeIndex = eventTypeIndex(type);
317
+ if (list->empty() && typeIndex >= 0) g_listenerTypeCounts[typeIndex]++;
318
+ EventListenerId id = g_nextEventListenerId++;
319
+ if (id == kInvalidEventListenerId) id = g_nextEventListenerId++;
320
+ list->entries.push_back(NodeEventListenerEntry{
321
+ id,
322
+ std::make_shared<gea::framework::events::EventListener>(std::move(listener))});
323
+ return id;
324
+ }
325
+
326
+ bool Tree::removeEventListener(int node, const char *type, EventListenerId listenerId)
327
+ {
328
+ auto &state = treeState();
329
+ if (node < 0 || node >= state.nodeCount || listenerId == kInvalidEventListenerId) return false;
330
+ NodeRareData *rd = rareDataFor(node);
331
+ if (!rd) return false;
332
+ NodeEventListenerList *list = rd->listeners.listenersFor(type);
333
+ if (!list) return false;
334
+ for (auto it = list->entries.begin(); it != list->entries.end(); ++it) {
335
+ if (it->id != listenerId) continue;
336
+ list->entries.erase(it);
337
+ if (list->empty()) {
338
+ const int typeIndex = eventTypeIndex(type);
339
+ if (typeIndex >= 0 && g_listenerTypeCounts[typeIndex] > 0) g_listenerTypeCounts[typeIndex]--;
340
+ }
341
+ return true;
342
+ }
343
+ return false;
344
+ }
345
+
346
+ bool Tree::hasEventListener(int node) const
347
+ {
348
+ const auto &state = treeState();
349
+ if (node < 0 || node >= state.nodeCount) return false;
350
+ const NodeRareData *rd = rareDataFor(node);
351
+ return rd && rd->listeners.hasAny();
352
+ }
353
+
354
+ bool Tree::hasListenersForType(const char *type) const
355
+ {
356
+ const int index = eventTypeIndex(type);
357
+ // Unknown/untracked types fall through to dispatch — never silently drop an
358
+ // event we don't account for.
359
+ return index < 0 || g_listenerTypeCounts[index] > 0;
360
+ }
361
+
362
+ bool Tree::dispatchEvent(gea::framework::events::PointerEvent &event)
363
+ {
364
+ auto &state = treeState();
365
+ if (event.targetId < 0 || event.targetId >= state.nodeCount) return false;
366
+
367
+ event.target = gea::framework::events::EventTarget(event.targetId);
368
+ bool dispatched = false;
369
+ for (int node = event.targetId; node >= 0 && node < state.nodeCount; node = state.nodes[node].parent) {
370
+ NodeRareData *rd = rareDataFor(node);
371
+ if (!rd) continue;
372
+ auto *initialList = rd->listeners.listenersFor(event.typeName());
373
+ if (!initialList || initialList->empty()) continue;
374
+
375
+ event.currentTargetId = node;
376
+ event.currentTarget = gea::framework::events::EventTarget(node);
377
+ event.eventPhase = node == event.targetId ? gea::framework::events::EventPhase::AtTarget
378
+ : gea::framework::events::EventPhase::Bubbling;
379
+ // pressId/pressValue ride on the event from the first ancestor that has one
380
+ // (the listener-bearing node here, `rd`), else the original target's. Both
381
+ // now live in the rare-data block, so read them through it.
382
+ const NodeRareData *targetRd = rareDataFor(event.targetId);
383
+ const int currentPressId = rd->attributes.pressId;
384
+ const int currentPressValue = rd->attributes.pressValue;
385
+ event.pressId = currentPressId >= 0 ? currentPressId : (targetRd ? targetRd->attributes.pressId : -1);
386
+ event.pressValue = currentPressValue >= 0 ? currentPressValue : (targetRd ? targetRd->attributes.pressValue : -1);
387
+ // A delegated handler may dispose its own conditional branch, removing
388
+ // listeners from this same body list during dispatch. The shared callback
389
+ // keeps the in-flight function alive after its entry is erased. Reacquire the
390
+ // list after every callback and advance by monotonic listener id; listeners
391
+ // added during this dispatch are above maxId and do not receive this event.
392
+ const EventListenerId maxId = initialList->entries.back().id;
393
+ EventListenerId cursor = kInvalidEventListenerId;
394
+ for (;;) {
395
+ rd = rareDataFor(node);
396
+ if (!rd) break;
397
+ auto *list = rd->listeners.listenersFor(event.typeName());
398
+ if (!list || list->empty()) break;
399
+ EventListenerId nextId = kInvalidEventListenerId;
400
+ std::shared_ptr<gea::framework::events::EventListener> callback;
401
+ for (const auto &entry : list->entries) {
402
+ if (entry.id <= cursor || entry.id > maxId) continue;
403
+ nextId = entry.id;
404
+ callback = entry.listener;
405
+ break;
406
+ }
407
+ if (!callback) break;
408
+ cursor = nextId;
409
+ (*callback)(event);
410
+ dispatched = true;
411
+ if (event.propagationStopped || !event.bubbles) break;
412
+ }
413
+ if (event.propagationStopped || !event.bubbles) break;
414
+ }
415
+ return dispatched;
416
+ }
417
+
418
+ bool Tree::containsNode(int ancestor, int node) const
419
+ {
420
+ const auto &state = treeState();
421
+ bool result = false;
422
+ if (ancestor >= 0 && ancestor < state.nodeCount && node >= 0 && node < state.nodeCount) {
423
+ for (int current = node; current >= 0 && current < state.nodeCount; current = state.nodes[current].parent) {
424
+ if (current == ancestor) {
425
+ result = true;
426
+ break;
427
+ }
428
+ }
429
+ }
430
+
431
+ return result;
432
+ }
433
+
434
+ namespace {
435
+
436
+ // True when changing this (node, attribute) pair actually affects what
437
+ // pixels get painted, so the framework can skip dirty-region invalidation
438
+ // for the common case (data-* attributes, internal bookkeeping, ARIA
439
+ // labels, etc.) and only repaint when the change really matters.
440
+ //
441
+ // Currently the only "renderable attribute" in the framework is on
442
+ // `<input>`: InputRenderer reads `value` and `placeholder` straight from
443
+ // the attribute store to paint the text. Other tags use the style /
444
+ // className / setText paths for visual updates, which already mark
445
+ // their nodes dirty in the right spot. Add new entries here if you wire
446
+ // up another tag where an attribute name drives rendering directly.
447
+ bool attributeAffectsRendering(const Node &node, const char *attrName)
448
+ {
449
+ if (std::strcmp(tagFromId(node.tag_id), "input") == 0 &&
450
+ (sameName(attrName, "value") || sameName(attrName, "placeholder"))) {
451
+ return true;
452
+ }
453
+ // A runtime `src`/`fit` change on an <img> loads a new image id (see the
454
+ // Image branch of setAttribute above) but paints nothing unless the node is
455
+ // invalidated: the first render drew the original src, so a later reactive /
456
+ // imperative src swap (e.g. an EPUB cover extracted to the SD card and
457
+ // applied via setAttribute) needs an explicit repaint to reach the panel.
458
+ if (node.type == NodeType::Image && (sameName(attrName, "src") || sameName(attrName, "fit"))) {
459
+ return true;
460
+ }
461
+ return false;
462
+ }
463
+
464
+ int imageFitAttributeValue(const char *value)
465
+ {
466
+ if (!value) return 0;
467
+ if (sameName(value, "contain")) return 1;
468
+ if (sameName(value, "cover")) return 2;
469
+ if (sameName(value, "none")) return 3;
470
+ if (sameName(value, "scale-down")) return 4;
471
+ return 0;
472
+ }
473
+
474
+ } // namespace
475
+
476
+ void Tree::setAttribute(int node, const char *name, const char *value)
477
+ {
478
+ auto &state = treeState();
479
+ if (node < 0 || node >= state.nodeCount) return;
480
+ if (sameName(name, "class")) {
481
+ setClassName(node, value ? value : "");
482
+ return;
483
+ }
484
+ ensureRareData(node).attributes.set(name, value);
485
+ if (state.nodes[node].type == NodeType::Image && sameName(name, "src")) {
486
+ const int imageId = static_cast<int>(gea_host_image_load_asset_path(value ? value : ""));
487
+ if (imageId >= 0) {
488
+ setStyleFromClass(node, Property::ImageId, imageId);
489
+ // A runtime src swap makes the image node emit a blit command it
490
+ // didn't have before (its initial record, with no image, produced
491
+ // nothing) — a structural change to the display list. Mark it
492
+ // structural so refresh does a full recordNode() rebuild that records
493
+ // the new blit, not a partial per-node re-record that keeps the empty
494
+ // command range. Without this a reactively/imperatively loaded image
495
+ // (e.g. an EPUB cover applied via setAttribute) never paints.
496
+ markDisplayListDirty();
497
+ state.nodes[node].render.dirty = 1;
498
+ state.nodes[node].render.non_scroll_dirty = 1;
499
+ }
500
+ }
501
+ if (state.nodes[node].type == NodeType::Image && sameName(name, "fit")) {
502
+ Style(node).imageFit(imageFitAttributeValue(value));
503
+ }
504
+ applyAudioAttribute(*this, node, name, value ? value : "");
505
+ if (state.nodes[node].type == NodeType::VirtualList) VirtualListRenderer::configureAttribute(node, name, value);
506
+ // Tree::setText marks render.dirty + non_scroll_dirty so the
507
+ // dirty-region tracker pushes the new pixels to the panel. We do
508
+ // the same here for the small set of attributes that drive
509
+ // per-node painting (see attributeAffectsRendering above) so
510
+ // reactive bindings like `value={todo.draft}` actually repaint the
511
+ // input. Other attribute writes — data-*, ARIA, internal flags —
512
+ // stay free.
513
+ if (attributeAffectsRendering(state.nodes[node], name)) {
514
+ markNodeDisplayCommandsDirty(node);
515
+ state.displayListDirty = true;
516
+ state.nodes[node].render.dirty = 1;
517
+ state.nodes[node].render.layout_dirty = 1;
518
+ state.nodes[node].render.non_scroll_dirty = 1;
519
+ }
520
+ }
521
+
522
+ void Tree::removeAttribute(int node, const char *name)
523
+ {
524
+ auto &state = treeState();
525
+ if (node < 0 || node >= state.nodeCount || !name) return;
526
+ if (sameName(name, "class")) {
527
+ setClassName(node, "");
528
+ return;
529
+ }
530
+ NodeRareData *rd = rareDataFor(node);
531
+ if (!rd) return; // no rare-data block → no attributes to remove
532
+ const bool hadAttribute = rd->attributes.remove(name);
533
+ if (!hadAttribute) return;
534
+ if (sameName(name, "data-press-id")) rd->attributes.pressId = -1;
535
+ if (sameName(name, "data-press-value")) rd->attributes.pressValue = -1;
536
+ if (attributeAffectsRendering(state.nodes[node], name)) {
537
+ markNodeDisplayCommandsDirty(node);
538
+ state.displayListDirty = true;
539
+ state.nodes[node].render.dirty = 1;
540
+ state.nodes[node].render.layout_dirty = 1;
541
+ state.nodes[node].render.non_scroll_dirty = 1;
542
+ }
543
+ }
544
+
545
+ bool Tree::toggleAttribute(int node, const char *name, bool force)
546
+ {
547
+ auto &state = treeState();
548
+ if (node < 0 || node >= state.nodeCount || !name || name[0] == '\0') return false;
549
+ if (force) {
550
+ setAttribute(node, name, "");
551
+ return true;
552
+ }
553
+ removeAttribute(node, name);
554
+ return false;
555
+ }
556
+
557
+ const char *Tree::getAttribute(int node, const char *name) const
558
+ {
559
+ const auto &state = treeState();
560
+ if (node < 0 || node >= state.nodeCount) return "";
561
+ // `class` is not stored in the attribute table (that would force a rare-data
562
+ // block onto every styled node); it lives in the dense NodeClassList. Serve
563
+ // reads from there via a stable static buffer (the runtime is single-threaded
564
+ // and getAttribute's pointer is consumed before the next call, like the DOM).
565
+ if (sameName(name, "class")) {
566
+ static std::string classBuf;
567
+ classBuf = className(node);
568
+ return classBuf.c_str();
569
+ }
570
+ const NodeRareData *rd = rareDataFor(node);
571
+ return rd ? rd->attributes.get(name) : "";
572
+ }
573
+
574
+ bool Tree::hasAttribute(int node, const char *name) const
575
+ {
576
+ const auto &state = treeState();
577
+ if (node < 0 || node >= state.nodeCount) return false;
578
+ if (sameName(name, "class")) return !className(node).empty();
579
+ const NodeRareData *rd = rareDataFor(node);
580
+ return rd && rd->attributes.has(name);
581
+ }
582
+
583
+ void Tree::setPressId(int node, int pressId)
584
+ {
585
+ auto &state = treeState();
586
+ if (node < 0 || node >= state.nodeCount) return;
587
+ auto &attrs = ensureRareData(node).attributes;
588
+ attrs.pressId = static_cast<int16_t>(pressId);
589
+ char value[16];
590
+ std::snprintf(value, sizeof(value), "%d", pressId);
591
+ attrs.set("data-press-id", value);
592
+ }
593
+
594
+ int Tree::pressId(int node) const
595
+ {
596
+ const auto &state = treeState();
597
+ if (node < 0 || node >= state.nodeCount) return -1;
598
+ const NodeRareData *rd = rareDataFor(node);
599
+ return rd ? rd->attributes.pressId : -1;
600
+ }
601
+
602
+ void Tree::setPressValue(int node, int pressValue)
603
+ {
604
+ auto &state = treeState();
605
+ if (node < 0 || node >= state.nodeCount) return;
606
+ auto &attrs = ensureRareData(node).attributes;
607
+ attrs.pressValue = static_cast<int16_t>(pressValue);
608
+ char value[16];
609
+ std::snprintf(value, sizeof(value), "%d", pressValue);
610
+ attrs.set("data-press-value", value);
611
+ }
612
+
613
+ int Tree::pressValue(int node) const
614
+ {
615
+ const auto &state = treeState();
616
+ if (node < 0 || node >= state.nodeCount) return -1;
617
+ const NodeRareData *rd = rareDataFor(node);
618
+ return rd ? rd->attributes.pressValue : -1;
619
+ }
620
+
621
+ } // namespace gea::embedded::ui