@geastack/engine 0.1.5 → 0.1.7

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 (45) hide show
  1. package/README.md +70 -0
  2. package/canvas.cpp +86 -30
  3. package/css/animation.h +64 -0
  4. package/css/declarative.h +6 -0
  5. package/css/engine.h +25 -1
  6. package/image_store.cpp +136 -33
  7. package/package.json +2 -2
  8. package/ui/CSS_FEATURES.md +261 -0
  9. package/ui/absolute_leaf_refresh.cpp +42 -39
  10. package/ui/core.cpp +32 -1
  11. package/ui/css_atom.cpp +6 -0
  12. package/ui/css_atom.h +2 -0
  13. package/ui/css_features.h +282 -0
  14. package/ui/display_invalidation.cpp +29 -0
  15. package/ui/flex_balance.h +73 -0
  16. package/ui/image_node.cpp +33 -28
  17. package/ui/input.cpp +88 -38
  18. package/ui/input_render.cpp +17 -13
  19. package/ui/internal.h +51 -5
  20. package/ui/layout.cpp +2601 -441
  21. package/ui/layout_snapshot.cpp +49 -6
  22. package/ui/node_features.h +18 -0
  23. package/ui/node_lifecycle.cpp +204 -71
  24. package/ui/node_lifecycle.h +1 -0
  25. package/ui/node_model.h +930 -70
  26. package/ui/node_text.h +82 -0
  27. package/ui/render.cpp +793 -352
  28. package/ui/renderer_features.h +19 -0
  29. package/ui/root_scroll_refresh.cpp +46 -24
  30. package/ui/style.cpp +4155 -1073
  31. package/ui/style.h +143 -6
  32. package/ui/style_values.h +16 -0
  33. package/ui/text.cpp +401 -106
  34. package/ui/tree_events.cpp +138 -95
  35. package/ui/tree_events.h +28 -19
  36. package/ui/tree_inspection.h +15 -0
  37. package/ui/tree_internal.h +19 -0
  38. package/ui/tree_nodes.cpp +38 -4
  39. package/ui/tree_render.cpp +119 -40
  40. package/ui/tree_scroll_mirror.cpp +26 -0
  41. package/ui/tree_state.cpp +261 -114
  42. package/ui/tree_state.h +121 -16
  43. package/ui/tree_style.cpp +609 -82
  44. package/ui/view.cpp +720 -299
  45. package/ui/virtual_keyboard.cpp +10 -3
package/ui/tree_state.cpp CHANGED
@@ -6,6 +6,8 @@
6
6
  #include <cstring>
7
7
  #include <deque>
8
8
  #include <new>
9
+ #include <memory>
10
+ #include <vector>
9
11
  #include <utility>
10
12
 
11
13
  // TreeState is a single large struct (kMaxNodes-scaled per-node arrays: nodes,
@@ -20,7 +22,165 @@
20
22
  #include "esp_heap_caps.h"
21
23
  #endif
22
24
 
23
- namespace gea::embedded::ui {
25
+ namespace gea::embedded::ui
26
+ {
27
+
28
+ namespace
29
+ {
30
+ std::size_t g_nodeTextStorageBytes = 0;
31
+ template <class T> struct NodeTextAllocator {
32
+ using value_type = T;
33
+ NodeTextAllocator() = default;
34
+ template <class U> NodeTextAllocator(const NodeTextAllocator<U> &) {}
35
+ T *allocate(std::size_t n)
36
+ {
37
+ #if defined(ESP_PLATFORM)
38
+ // Match TreeState placement even when the board sends small ordinary
39
+ // allocations to SRAM. Node text must not steal the audio heap.
40
+ T *result = static_cast<T *>(heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM));
41
+ if (!result) result = std::allocator<T>{}.allocate(n);
42
+ #else
43
+ T *result = std::allocator<T>{}.allocate(n);
44
+ #endif
45
+ g_nodeTextStorageBytes += n * sizeof(T);
46
+ return result;
47
+ }
48
+ void deallocate(T *ptr, std::size_t n)
49
+ {
50
+ g_nodeTextStorageBytes -= n * sizeof(T);
51
+ #if defined(ESP_PLATFORM)
52
+ // ESP-IDF new/delete and capability allocations use the same heap.
53
+ heap_caps_free(ptr);
54
+ #else
55
+ std::allocator<T>{}.deallocate(ptr, n);
56
+ #endif
57
+ }
58
+ template <class U> bool operator==(const NodeTextAllocator<U> &) const
59
+ {
60
+ return true;
61
+ }
62
+ template <class U> bool operator!=(const NodeTextAllocator<U> &) const
63
+ {
64
+ return false;
65
+ }
66
+ };
67
+ union NodeTextEntry {
68
+ // A free entry has no live string, so its reuse link occupies the same
69
+ // bytes instead of adding a word to every nonempty label.
70
+ std::string text;
71
+ std::uint32_t nextFree;
72
+ NodeTextEntry() : nextFree(UINT32_MAX) {}
73
+ ~NodeTextEntry() {} // Live strings are destroyed when their owner clears.
74
+ };
75
+ struct NodeTextPool {
76
+ // Fixed small pages avoid libc++ deque's 4 KiB minimum block for a UI
77
+ // containing only a handful of labels. Page addresses never move.
78
+ static constexpr std::size_t kPageEntries = 16;
79
+ struct PageDeleter {
80
+ void operator()(NodeTextEntry *page) const {
81
+ for (std::size_t i = 0; i < kPageEntries; ++i) page[i].~NodeTextEntry();
82
+ NodeTextAllocator<NodeTextEntry>{}.deallocate(page, kPageEntries);
83
+ }
84
+ };
85
+ std::vector<NodeTextEntry *, NodeTextAllocator<NodeTextEntry *>> pages;
86
+ std::size_t size = 0;
87
+ std::uint32_t freeHead = UINT32_MAX;
88
+ NodeTextEntry &at(std::size_t index)
89
+ {
90
+ return pages[index / kPageEntries][index % kPageEntries];
91
+ }
92
+ void append()
93
+ {
94
+ if (size == pages.size() * kPageEntries) {
95
+ std::unique_ptr<NodeTextEntry[], PageDeleter> page(NodeTextAllocator<NodeTextEntry>{}.allocate(kPageEntries));
96
+ for (std::size_t i = 0; i < kPageEntries; ++i) new (&page[i]) NodeTextEntry;
97
+ pages.push_back(page.get());
98
+ page.release();
99
+ }
100
+ ++size;
101
+ }
102
+ };
103
+ NodeTextPool *g_nodeTextPool = nullptr;
104
+ const std::string g_emptyNodeText;
105
+ NodeTextPool &nodeTextPool()
106
+ {
107
+ if (!g_nodeTextPool)
108
+ g_nodeTextPool = new (NodeTextAllocator<NodeTextPool>{}.allocate(1)) NodeTextPool;
109
+ return *g_nodeTextPool;
110
+ }
111
+ } // namespace
112
+
113
+ std::size_t NodeText::storageBytes()
114
+ {
115
+ return g_nodeTextStorageBytes;
116
+ }
117
+
118
+ NodeText::NodeText(const NodeText &other)
119
+ {
120
+ if (!other.empty())
121
+ assign(other.c_str());
122
+ }
123
+ NodeText::NodeText(NodeText &&other) noexcept : handle_(other.handle_)
124
+ {
125
+ other.handle_ = kEmpty;
126
+ }
127
+ NodeText::~NodeText() { clear(); }
128
+ NodeText &NodeText::operator=(const NodeText &other)
129
+ {
130
+ if (this != &other) {
131
+ if (other.empty())
132
+ clear();
133
+ else
134
+ assign(other.c_str());
135
+ }
136
+ return *this;
137
+ }
138
+ NodeText &NodeText::operator=(NodeText &&other) noexcept
139
+ {
140
+ if (this != &other) {
141
+ clear();
142
+ handle_ = other.handle_;
143
+ other.handle_ = kEmpty;
144
+ }
145
+ return *this;
146
+ }
147
+ const std::string &NodeText::str() const
148
+ {
149
+ return empty() ? g_emptyNodeText : nodeTextPool().at(handle_).text;
150
+ }
151
+ void NodeText::clear()
152
+ {
153
+ if (empty())
154
+ return;
155
+ auto &pool = nodeTextPool();
156
+ auto &entry = pool.at(handle_);
157
+ // Release long text allocations on removal, rather than retaining the
158
+ // largest label ever seen in every reusable slot.
159
+ entry.text.~basic_string();
160
+ entry.nextFree = pool.freeHead;
161
+ pool.freeHead = handle_;
162
+ handle_ = kEmpty;
163
+ }
164
+ void NodeText::assign(const char *text)
165
+ {
166
+ if (!text || !*text) {
167
+ clear();
168
+ return;
169
+ }
170
+ auto &pool = nodeTextPool();
171
+ if (empty()) {
172
+ if (pool.freeHead == kEmpty) {
173
+ const auto index = pool.size;
174
+ pool.append();
175
+ handle_ = static_cast<std::uint32_t>(index);
176
+ } else {
177
+ handle_ = pool.freeHead;
178
+ pool.freeHead = pool.at(handle_).nextFree;
179
+ }
180
+ new (&pool.at(handle_).text) std::string;
181
+ }
182
+ pool.at(handle_).text.assign(text);
183
+ }
24
184
 
25
185
  namespace {
26
186
 
@@ -307,137 +467,118 @@ std::string NodeClassList::value() const
307
467
  return out;
308
468
  }
309
469
 
470
+ // Empty override stores own nothing. One allocation contains both metadata
471
+ // and values; computed styles remain ordinary aligned fields in Node.
472
+ struct NodeStyleOverrideStore::Block {
473
+ std::size_t count = 0, capacity = 0;
474
+ // Only CSS px overrides allocate unit metadata. Raw numeric styles keep
475
+ // their compact entries and do not lose precision by reverse-scaling ints.
476
+ std::unique_ptr<std::vector<std::pair<Property, float>>> cssPixels;
477
+ NodeStyleOverride *values() { return reinterpret_cast<NodeStyleOverride *>(this + 1); }
478
+ const NodeStyleOverride *values() const { return reinterpret_cast<const NodeStyleOverride *>(this + 1); }
479
+ };
310
480
  namespace {
311
-
312
- NodeStyleOverride &styleOverrideAt(NodeStyleOverrideStore &store, std::size_t index)
313
- {
314
- return store.spilled ? store.spillValues[index] : store.inlineValues[index];
315
- }
316
-
317
481
  void appendStyleOverride(NodeStyleOverrideStore &store, NodeStyleOverride entry)
318
482
  {
319
- if (!store.spilled && store.inlineCount < NodeStyleOverrideStore::kInlineCount) {
320
- store.inlineValues[store.inlineCount++] = entry;
321
- return;
322
- }
323
- if (!store.spilled) {
324
- store.spillCapacity = NodeStyleOverrideStore::kInlineCount * 2;
325
- store.spillValues = new NodeStyleOverride[store.spillCapacity];
326
- for (std::size_t i = 0; i < store.inlineCount; ++i)
327
- store.spillValues[i] = store.inlineValues[i];
328
- store.spillCount = store.inlineCount;
329
- store.spilled = true;
330
- }
331
- if (store.spillCount >= store.spillCapacity) {
332
- const std::size_t nextCapacity = store.spillCapacity ? store.spillCapacity * 2 : NodeStyleOverrideStore::kInlineCount * 2;
333
- auto *next = new NodeStyleOverride[nextCapacity];
334
- for (std::size_t i = 0; i < store.spillCount; ++i)
335
- next[i] = store.spillValues[i];
336
- delete[] store.spillValues;
337
- store.spillValues = next;
338
- store.spillCapacity = nextCapacity;
483
+ auto *old = store.block;
484
+ if (!old || old->count == old->capacity) {
485
+ const std::size_t capacity = old ? old->capacity * 2 : 2;
486
+ auto *next = new (::operator new(sizeof(NodeStyleOverrideStore::Block) + capacity * sizeof(NodeStyleOverride))) NodeStyleOverrideStore::Block;
487
+ next->capacity = capacity;
488
+ if (old) {
489
+ next->count = old->count;
490
+ next->cssPixels = std::move(old->cssPixels);
491
+ for (std::size_t i = 0; i < old->count; ++i) new (&next->values()[i]) NodeStyleOverride(old->values()[i]);
492
+ old->~Block();
493
+ ::operator delete(old);
494
+ }
495
+ store.block = next;
339
496
  }
340
- store.spillValues[store.spillCount++] = entry;
497
+ new (&store.block->values()[store.block->count++]) NodeStyleOverride(entry);
498
+ }
341
499
  }
342
-
343
- } // namespace
344
-
345
500
  NodeStyleOverrideStore::NodeStyleOverrideStore(const NodeStyleOverrideStore &other)
346
501
  {
347
- for (std::size_t i = 0, n = other.size(); i < n; ++i)
348
- appendStyleOverride(*this, other.at(i));
502
+ for (std::size_t i = 0; i < other.size(); ++i) appendStyleOverride(*this, other.at(i));
503
+ if (other.block && other.block->cssPixels)
504
+ block->cssPixels = std::make_unique<std::vector<std::pair<Property, float>>>(*other.block->cssPixels);
349
505
  }
350
-
351
506
  NodeStyleOverrideStore &NodeStyleOverrideStore::operator=(const NodeStyleOverrideStore &other)
352
507
  {
353
- if (this == &other) return *this;
354
- clear();
355
- for (std::size_t i = 0, n = other.size(); i < n; ++i)
356
- appendStyleOverride(*this, other.at(i));
508
+ if (this != &other) {
509
+ NodeStyleOverrideStore copy(other);
510
+ *this = std::move(copy);
511
+ }
357
512
  return *this;
358
513
  }
359
-
360
- NodeStyleOverrideStore::NodeStyleOverrideStore(NodeStyleOverrideStore &&other) noexcept
514
+ NodeStyleOverrideStore::NodeStyleOverrideStore(NodeStyleOverrideStore &&other) noexcept : block(other.block)
361
515
  {
362
- for (std::uint8_t i = 0; i < kInlineCount; ++i)
363
- inlineValues[i] = other.inlineValues[i];
364
- spillValues = other.spillValues;
365
- spillCount = other.spillCount;
366
- spillCapacity = other.spillCapacity;
367
- inlineCount = other.inlineCount;
368
- spilled = other.spilled;
369
- other.spillValues = nullptr;
370
- other.spillCount = 0;
371
- other.spillCapacity = 0;
372
- other.inlineCount = 0;
373
- other.spilled = false;
516
+ other.block = nullptr;
374
517
  }
375
-
376
518
  NodeStyleOverrideStore &NodeStyleOverrideStore::operator=(NodeStyleOverrideStore &&other) noexcept
377
519
  {
378
- if (this == &other) return *this;
379
- delete[] spillValues;
380
- for (std::uint8_t i = 0; i < kInlineCount; ++i)
381
- inlineValues[i] = other.inlineValues[i];
382
- spillValues = other.spillValues;
383
- spillCount = other.spillCount;
384
- spillCapacity = other.spillCapacity;
385
- inlineCount = other.inlineCount;
386
- spilled = other.spilled;
387
- other.spillValues = nullptr;
388
- other.spillCount = 0;
389
- other.spillCapacity = 0;
390
- other.inlineCount = 0;
391
- other.spilled = false;
520
+ if (this != &other) {
521
+ clear();
522
+ block = other.block;
523
+ other.block = nullptr;
524
+ }
392
525
  return *this;
393
526
  }
394
-
395
- NodeStyleOverrideStore::~NodeStyleOverrideStore()
396
- {
397
- delete[] spillValues;
398
- }
399
-
527
+ NodeStyleOverrideStore::~NodeStyleOverrideStore() { clear(); }
400
528
  void NodeStyleOverrideStore::clear()
401
529
  {
402
- if (spilled)
403
- spillCount = 0;
404
- else
405
- inlineCount = 0;
530
+ if (!block) return;
531
+ block->~Block();
532
+ ::operator delete(block);
533
+ block = nullptr;
406
534
  }
407
-
535
+ std::size_t NodeStyleOverrideStore::size() const { return block ? block->count : 0; }
408
536
  void NodeStyleOverrideStore::set(Property property, int value)
409
537
  {
410
- for (std::size_t i = 0, n = size(); i < n; ++i) {
411
- auto &entry = styleOverrideAt(*this, i);
412
- if (entry.property != property) continue;
413
- entry.value = value;
414
- return;
538
+ for (std::size_t i = 0; i < size(); ++i) {
539
+ auto &entry = block->values()[i];
540
+ if (entry.property == property) {
541
+ entry.value = value;
542
+ if (block->cssPixels) {
543
+ auto &units = *block->cssPixels;
544
+ units.erase(std::remove_if(units.begin(), units.end(), [property](const auto &item) { return item.first == property; }), units.end());
545
+ }
546
+ return;
547
+ }
415
548
  }
416
549
  appendStyleOverride(*this, {property, value});
417
550
  }
418
-
551
+ void NodeStyleOverrideStore::setCssPixels(Property property, float value)
552
+ {
553
+ if (!block) return;
554
+ bool present = false;
555
+ for (std::size_t i = 0; i < size(); ++i) if (at(i).property == property) { present = true; break; }
556
+ if (!present) return;
557
+ if (!block->cssPixels) block->cssPixels = std::make_unique<std::vector<std::pair<Property, float>>>();
558
+ for (auto &entry : *block->cssPixels) if (entry.first == property) { entry.second = value; return; }
559
+ block->cssPixels->emplace_back(property, value);
560
+ }
561
+ bool NodeStyleOverrideStore::getCssPixels(Property property, float &value) const
562
+ {
563
+ if (!block || !block->cssPixels) return false;
564
+ for (const auto &entry : *block->cssPixels) if (entry.first == property) { value = entry.second; return true; }
565
+ return false;
566
+ }
419
567
  bool NodeStyleOverrideStore::remove(Property property)
420
568
  {
421
- for (std::size_t i = 0, n = size(); i < n; ++i) {
569
+ for (std::size_t i = 0; i < size(); ++i) {
422
570
  if (at(i).property != property) continue;
423
- if (spilled) {
424
- for (std::size_t j = i + 1; j < spillCount; ++j)
425
- spillValues[j - 1] = spillValues[j];
426
- --spillCount;
427
- } else {
428
- for (std::size_t j = i + 1; j < inlineCount; ++j)
429
- inlineValues[j - 1] = inlineValues[j];
430
- --inlineCount;
571
+ if (block->cssPixels) {
572
+ auto &units = *block->cssPixels;
573
+ units.erase(std::remove_if(units.begin(), units.end(), [property](const auto &item) { return item.first == property; }), units.end());
431
574
  }
575
+ for (std::size_t j = i + 1; j < block->count; ++j) block->values()[j - 1] = block->values()[j];
576
+ if (--block->count == 0) clear();
432
577
  return true;
433
578
  }
434
579
  return false;
435
580
  }
436
-
437
- const NodeStyleOverride &NodeStyleOverrideStore::at(std::size_t index) const
438
- {
439
- return spilled ? spillValues[index] : inlineValues[index];
440
- }
581
+ const NodeStyleOverride &NodeStyleOverrideStore::at(std::size_t index) const { return block->values()[index]; }
441
582
 
442
583
  void NodeCustomPropertyStore::clear()
443
584
  {
@@ -450,9 +591,16 @@ void NodeCustomPropertyStore::set(const std::string &name, const std::string &va
450
591
  set(internCssAtom(name), value);
451
592
  }
452
593
 
453
- CssAtomId customPropertyValueAtom(const std::string &value)
594
+ void setCustomPropertyStorage(NodeCustomProperty &entry, const std::string &value)
454
595
  {
455
- return value.empty() ? kInvalidCssAtom : internCssAtom(value);
596
+ const CssAtomId atom = value.empty() ? kInvalidCssAtom : internCssAtom(value);
597
+ // Form the replacement before releasing the old string: callers may pass
598
+ // the value returned by get(), including an uninterned overflow value.
599
+ auto storage = atom != kInvalidCssAtom || value.empty()
600
+ ? std::shared_ptr<const std::string>(std::shared_ptr<const std::string>{}, &cssAtomString(atom))
601
+ : std::make_shared<const std::string>(value);
602
+ entry.value = std::move(storage);
603
+ entry.valueAtom = atom;
456
604
  }
457
605
 
458
606
  void NodeCustomPropertyStore::set(CssAtomId nameId, const std::string &value)
@@ -460,15 +608,13 @@ void NodeCustomPropertyStore::set(CssAtomId nameId, const std::string &value)
460
608
  if (nameId == kInvalidCssAtom) return;
461
609
  for (auto &entry : values) {
462
610
  if (entry.nameId != nameId) continue;
463
- entry.value = value;
464
- entry.valueAtom = customPropertyValueAtom(value);
611
+ setCustomPropertyStorage(entry, value);
465
612
  entry.flags = 0;
466
613
  return;
467
614
  }
468
615
  NodeCustomProperty entry;
469
616
  entry.nameId = nameId;
470
- entry.value = value;
471
- entry.valueAtom = customPropertyValueAtom(value);
617
+ setCustomPropertyStorage(entry, value);
472
618
  values.push_back(std::move(entry));
473
619
  }
474
620
 
@@ -481,8 +627,7 @@ void NodeCustomPropertyStore::setColor(CssAtomId nameId,
481
627
  if (nameId == kInvalidCssAtom) return;
482
628
  for (auto &entry : values) {
483
629
  if (entry.nameId != nameId) continue;
484
- entry.value = value;
485
- entry.valueAtom = customPropertyValueAtom(value);
630
+ setCustomPropertyStorage(entry, value);
486
631
  entry.colorStyle = styleColor;
487
632
  entry.colorNative = nativeColor;
488
633
  entry.colorAlpha = alpha;
@@ -491,8 +636,7 @@ void NodeCustomPropertyStore::setColor(CssAtomId nameId,
491
636
  }
492
637
  NodeCustomProperty entry;
493
638
  entry.nameId = nameId;
494
- entry.value = value;
495
- entry.valueAtom = customPropertyValueAtom(value);
639
+ setCustomPropertyStorage(entry, value);
496
640
  entry.colorStyle = styleColor;
497
641
  entry.colorNative = nativeColor;
498
642
  entry.colorAlpha = alpha;
@@ -505,11 +649,11 @@ void NodeCustomPropertyStore::setLength(CssAtomId nameId,
505
649
  float lengthValue,
506
650
  std::uint8_t lengthUnit)
507
651
  {
652
+ #if GEA_CSS_CUSTOM_PROPERTY_LENGTHS
508
653
  if (nameId == kInvalidCssAtom) return;
509
654
  for (auto &entry : values) {
510
655
  if (entry.nameId != nameId) continue;
511
- entry.value = value;
512
- entry.valueAtom = customPropertyValueAtom(value);
656
+ setCustomPropertyStorage(entry, value);
513
657
  entry.lengthValue = lengthValue;
514
658
  entry.lengthUnit = lengthUnit;
515
659
  entry.flags = static_cast<std::uint8_t>((entry.flags & ~1u) | 2u);
@@ -517,12 +661,15 @@ void NodeCustomPropertyStore::setLength(CssAtomId nameId,
517
661
  }
518
662
  NodeCustomProperty entry;
519
663
  entry.nameId = nameId;
520
- entry.value = value;
521
- entry.valueAtom = customPropertyValueAtom(value);
664
+ setCustomPropertyStorage(entry, value);
522
665
  entry.lengthValue = lengthValue;
523
666
  entry.lengthUnit = lengthUnit;
524
667
  entry.flags = 2;
525
668
  values.push_back(std::move(entry));
669
+ #else
670
+ (void)lengthValue; (void)lengthUnit;
671
+ set(nameId, value);
672
+ #endif
526
673
  }
527
674
 
528
675
  const std::string *NodeCustomPropertyStore::get(const std::string &name) const
@@ -534,7 +681,7 @@ const std::string *NodeCustomPropertyStore::get(const std::string &name) const
534
681
  const std::string *NodeCustomPropertyStore::get(CssAtomId nameId) const
535
682
  {
536
683
  if (nameId == kInvalidCssAtom) return nullptr;
537
- if (const NodeCustomProperty *entry = getEntry(nameId)) return &entry->value;
684
+ if (const NodeCustomProperty *entry = getEntry(nameId)) return entry->value.get();
538
685
  return nullptr;
539
686
  }
540
687
 
@@ -571,4 +718,4 @@ TreeState &treeState()
571
718
  return *g_treeState;
572
719
  }
573
720
 
574
- } // namespace gea::embedded::ui
721
+ } // namespace gea::embedded::ui