@geastack/engine 0.1.17 → 0.1.19

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/touch_runtime.cpp CHANGED
@@ -9,6 +9,9 @@
9
9
  #include "ui/tree_internal.h"
10
10
  #include "ui/virtual_keyboard.h"
11
11
  #include "ui/debugger_picker.h"
12
+ #include "ui/input_retirement.h"
13
+ #include <stdexcept>
14
+ #include <exception>
12
15
 
13
16
  #include <cstring>
14
17
 
@@ -89,6 +92,7 @@ public:
89
92
 
90
93
  void resetGesture()
91
94
  {
95
+ ++primaryTicket_;primaryOwner_={};primaryQuarantined_=false;primaryEnding_=false;
92
96
  wasActive_ = false;
93
97
  lastX_ = 0;
94
98
  lastY_ = 0;
@@ -97,7 +101,7 @@ public:
97
101
  dragged_ = false;
98
102
  activeDefaultPrevented_ = false;
99
103
  activeTargetId_ = -1;
100
- for (Secondary &s : secondary_) s = Secondary{};
104
+ for (Secondary &s : secondary_) {const auto next=s.ticket+1;s=Secondary{};s.ticket=next;}
101
105
  }
102
106
 
103
107
  // Returns true if this event may have changed on-screen state (a real
@@ -134,6 +138,13 @@ public:
134
138
  return dispatchSecondary(event);
135
139
  }
136
140
 
141
+ if (retireInvalidPrimary(!event.touching && event.touchPhase == TouchPhase::Up)) return false;
142
+ if(primaryQuarantined_){
143
+ if(event.touchPhase==TouchPhase::Move) gea::platform::touch::Touchscreen::consumeLatestMove(nullptr,nullptr);
144
+ if(!event.touching && event.touchPhase==TouchPhase::Up)primaryQuarantined_=false;
145
+ return false;
146
+ }
147
+ auto dispatchTicket=primaryTicket_;bool primaryBegan=false;
137
148
  bool changed = false;
138
149
 
139
150
  int tx = event.x;
@@ -151,19 +162,22 @@ public:
151
162
 
152
163
  if (touching && !wasActive_) {
153
164
  changed = true;
154
- startX_ = tx;
155
- startY_ = ty;
165
+ startX_ = lastX_ = tx;
166
+ startY_ = lastY_ = ty;
156
167
  dragged_ = false;
157
168
  activeDefaultPrevented_ = false;
158
169
  gea::embedded::ui::Tree::instance().pointerDown(tx, ty);
159
170
  const int targetId = gea::embedded::ui::Tree::instance().hitTestNode(tx, ty);
160
171
  if (targetId >= 0) {
161
- activeTargetId_ = targetId;
162
- activeDefaultPrevented_ = dispatchPointer(PointerEventType::TouchStart, targetId, tx, ty, true);
172
+ activeTargetId_ = targetId;primaryOwner_=gea::embedded::ui::NodeHandle(targetId);
173
+ wasActive_=true;primaryBegan=true;primaryEnding_=false;dispatchTicket=++primaryTicket_;
174
+ const bool prevented = dispatchPointer(PointerEventType::TouchStart, targetId, tx, ty, true);
175
+ if(dispatchTicket!=primaryTicket_ || retireInvalidPrimary(primaryEnding_))return true;
176
+ activeDefaultPrevented_ = prevented;
163
177
  }
164
178
  }
165
179
 
166
- if (touching && wasActive_ && (tx != lastX_ || ty != lastY_)) {
180
+ if (touching && wasActive_ && !primaryBegan && (tx != lastX_ || ty != lastY_)) {
167
181
  changed = true;
168
182
  if (!dragged_) {
169
183
  int dx = tx - startX_;
@@ -181,12 +195,13 @@ public:
181
195
  bool defaultPrevented = activeDefaultPrevented_;
182
196
  if (activeTargetId_ >= 0) {
183
197
  defaultPrevented = dispatchPointer(PointerEventType::TouchMove, activeTargetId_, tx, ty, true) || defaultPrevented;
198
+ if(dispatchTicket!=primaryTicket_ || retireInvalidPrimary(primaryEnding_))return true;
184
199
  }
185
200
  if (!defaultPrevented) gea::embedded::ui::Tree::instance().pointerMove(tx, ty);
186
201
  }
187
202
 
188
203
  if (!touching && wasActive_) {
189
- changed = true;
204
+ changed = true;primaryEnding_=true;
190
205
  // Captured BEFORE any click handler runs: if a handler
191
206
  // programmatically focuses an input (element.focus()), the
192
207
  // focus-management default below must not clear it.
@@ -208,9 +223,11 @@ public:
208
223
  gea::embedded::ui::VirtualKeyboard::instance().sync();
209
224
  } else if (activeTargetId_ >= 0) {
210
225
  dispatchPointer(PointerEventType::TouchEnd, activeTargetId_, lastX_, lastY_, false);
226
+ if(dispatchTicket!=primaryTicket_ || retireInvalidPrimary(primaryEnding_))return true;
211
227
  }
212
228
  if (!dragged_ && !activeDefaultPrevented_ && activeTargetId_ >= 0) {
213
229
  if (!keyboardDismissTap) dispatchPointer(PointerEventType::Click, activeTargetId_, lastX_, lastY_, false);
230
+ if(dispatchTicket!=primaryTicket_ || retireInvalidPrimary(primaryEnding_))return true;
214
231
  // Focus management — set the active input if the tap
215
232
  // landed on one (or the closest input ancestor); clear
216
233
  // focus on taps outside any input.
@@ -238,7 +255,8 @@ public:
238
255
  gea::embedded::ui::VirtualKeyboard::instance().sync();
239
256
  }
240
257
  }
241
- activeTargetId_ = -1;
258
+ if(dispatchTicket!=primaryTicket_ || retireInvalidPrimary(primaryEnding_))return true;
259
+ activeTargetId_ = -1;primaryOwner_={};primaryEnding_=false;
242
260
  activeDefaultPrevented_ = false;
243
261
  }
244
262
 
@@ -251,6 +269,44 @@ public:
251
269
  return changed;
252
270
  }
253
271
 
272
+ // A raw allocation index is never a gesture lease. Destructive removal
273
+ // invalidates the captured birth owner, even if that index is reused.
274
+ bool retireInvalidPrimary(bool physicalUp)
275
+ {
276
+ if (!wasActive_ || activeTargetId_ < 0 || primaryOwner_.valid()) return false;
277
+ primaryQuarantined_ = !physicalUp;primaryEnding_=false;++primaryTicket_;
278
+ primaryOwner_={};activeTargetId_=-1;wasActive_=false;activeDefaultPrevented_=false;
279
+ gea::embedded::ui::cancelPrimaryPointerInput();
280
+ return true;
281
+ }
282
+
283
+ void retireSubtrees(const gea::embedded::ui::InputRetirementRoots &removed,const gea::embedded::ui::InputRetirementRoots &retained)
284
+ {
285
+ using namespace gea::embedded::ui;
286
+ struct Cancel {NodeHandle owner;int x,y,pointer;bool primary,emit;};
287
+ std::vector<Cancel> cancelled;cancelled.reserve(1+kMaxSecondaryPointers);
288
+ if(inputOwnerLeaving(primaryOwner_,removed,retained)){
289
+ cancelled.push_back({primaryOwner_,lastX_,lastY_,1,true,!primaryEnding_});
290
+ primaryQuarantined_=!primaryEnding_;primaryEnding_=false;++primaryTicket_;
291
+ primaryOwner_={};activeTargetId_=-1;wasActive_=false;activeDefaultPrevented_=false;
292
+ cancelPrimaryPointerInput();
293
+ }
294
+ for(int i=0;i<kMaxSecondaryPointers;++i){auto &slot=secondary_[i];
295
+ if(!inputOwnerLeaving(slot.owner,removed,retained))continue;
296
+ cancelled.push_back({slot.owner,slot.lastX,slot.lastY,i+2,false,!slot.ending});
297
+ slot.quarantined=!slot.ending;slot.ending=false;++slot.ticket;slot.active=false;slot.targetId=-1;slot.owner={};
298
+ }
299
+ // All admitted leases are closed before the first user cancellation callback.
300
+ std::exception_ptr failure;
301
+ for(const auto &entry:cancelled)if(entry.emit&&entry.owner.valid()) {
302
+ try {dispatchPointer(PointerEventType::PointerCancel,entry.owner.id(),entry.x,entry.y,false,entry.pointer,entry.primary);}
303
+ catch(...) {if(!failure)failure=std::current_exception();}
304
+ }
305
+ if(failure)std::rethrow_exception(failure);
306
+ if(inputOwnerLeaving(primaryOwner_,removed,retained))throw std::runtime_error("primary gesture revived during retirement");
307
+ for(const auto &slot:secondary_)if(inputOwnerLeaving(slot.owner,removed,retained))throw std::runtime_error("secondary gesture revived during retirement");
308
+ }
309
+
254
310
  // Finger down now AND a down/move/up fired within the last windowMs. Used by
255
311
  // the frame loop to render a drag back-to-back; a static hold posts no moves,
256
312
  // so this lapses to false after the window and rendering quiesces.
@@ -285,65 +341,43 @@ private:
285
341
  // owned by the primary pointer (pointerId 0).
286
342
  bool dispatchSecondary(const Event &event)
287
343
  {
288
- const int slot = event.pointerId - 1;
289
- if (slot < 0 || slot >= kMaxSecondaryPointers)
290
- return false;
291
- Secondary &s = secondary_[slot];
292
- const int tx = event.x;
293
- const int ty = event.y;
294
- const bool touching = event.touching || event.touchPhase == TouchPhase::Move;
295
- // Web convention: pointerId 1 is the primary pointer, so extra fingers
296
- // are numbered from 2 upward.
297
- const int webPointerId = event.pointerId + 1;
298
-
299
- if (touching && !s.active) {
300
- s.active = true;
301
- s.startX = tx;
302
- s.startY = ty;
303
- s.lastX = tx;
304
- s.lastY = ty;
305
- s.dragged = false;
306
- s.defaultPrevented = false;
307
- s.targetId = gea::embedded::ui::Tree::instance().hitTestNode(tx, ty);
308
- if (s.targetId >= 0) {
309
- s.defaultPrevented =
310
- dispatchPointer(PointerEventType::TouchStart, s.targetId, tx, ty, true, webPointerId, false);
311
- }
312
- return true;
313
- }
314
-
315
- if (touching && s.active) {
316
- const bool moved = tx != s.lastX || ty != s.lastY;
317
- if (moved) {
318
- if (!s.dragged) {
319
- const int dx = tx - s.startX;
320
- const int dy = ty - s.startY;
321
- if (dx > 16 || dx < -16 || dy > 16 || dy < -16)
322
- s.dragged = true;
323
- }
324
- if (s.targetId >= 0) {
325
- dispatchPointer(PointerEventType::TouchMove, s.targetId, tx, ty, true, webPointerId, false);
326
- }
327
- s.lastX = tx;
328
- s.lastY = ty;
329
- }
330
- return moved;
331
- }
332
-
333
- if (!touching && s.active) {
334
- if (s.targetId >= 0) {
335
- dispatchPointer(PointerEventType::TouchEnd, s.targetId, s.lastX, s.lastY, false, webPointerId, false);
336
- if (!s.dragged && !s.defaultPrevented) {
337
- dispatchPointer(PointerEventType::Click, s.targetId, s.lastX, s.lastY, false, webPointerId, false);
338
- }
339
- }
340
- s.active = false;
341
- s.targetId = -1;
342
- s.dragged = false;
343
- s.defaultPrevented = false;
344
- return true;
345
- }
346
- return false;
344
+ const int index=event.pointerId-1;
345
+ if(index<0||index>=kMaxSecondaryPointers)return false;
346
+ auto &slot=secondary_[index];const int x=event.x,y=event.y;
347
+ const bool touching=event.touching||event.touchPhase==TouchPhase::Move;
348
+ const int pointer=event.pointerId+1;
349
+ auto retireInvalid=[&](){
350
+ if(!slot.active || slot.targetId<0 || slot.owner.valid())return false;
351
+ slot.quarantined=touching;slot.ending=false;++slot.ticket;slot.active=false;
352
+ slot.targetId=-1;slot.owner={};slot.dragged=false;slot.defaultPrevented=false;return true;
353
+ };
354
+ if(retireInvalid())return false;
355
+ if(slot.quarantined){if(!touching&&event.touchPhase==TouchPhase::Up)slot.quarantined=false;return false;}
356
+ if(touching&&!slot.active){
357
+ slot.active=true;slot.ending=false;const auto ticket=++slot.ticket;
358
+ slot.startX=slot.lastX=x;slot.startY=slot.lastY=y;slot.dragged=false;slot.defaultPrevented=false;
359
+ slot.targetId=gea::embedded::ui::Tree::instance().hitTestNode(x,y);
360
+ slot.owner=gea::embedded::ui::NodeHandle(slot.targetId);
361
+ if(slot.targetId>=0){const bool prevented=dispatchPointer(PointerEventType::TouchStart,slot.targetId,x,y,true,pointer,false);if(ticket!=slot.ticket || retireInvalid())return true;slot.defaultPrevented=prevented;}
362
+ return true;
363
+ }
364
+ const auto ticket=slot.ticket;
365
+ if(touching&&slot.active){
366
+ const bool moved=x!=slot.lastX||y!=slot.lastY;
367
+ if(moved){if(!slot.dragged){const int dx=x-slot.startX,dy=y-slot.startY;slot.dragged=dx>16||dx< -16||dy>16||dy< -16;}
368
+ if(slot.targetId>=0)dispatchPointer(PointerEventType::TouchMove,slot.targetId,x,y,true,pointer,false);
369
+ if(ticket!=slot.ticket || retireInvalid())return true;slot.lastX=x;slot.lastY=y;
370
+ }return moved;
371
+ }
372
+ if(!touching&&slot.active){
373
+ slot.ending=true;
374
+ if(slot.targetId>=0){dispatchPointer(PointerEventType::TouchEnd,slot.targetId,slot.lastX,slot.lastY,false,pointer,false);
375
+ if(ticket!=slot.ticket || retireInvalid())return true;
376
+ if(!slot.dragged&&!slot.defaultPrevented)dispatchPointer(PointerEventType::Click,slot.targetId,slot.lastX,slot.lastY,false,pointer,false);
377
+ if(ticket!=slot.ticket || retireInvalid())return true;
378
+ }
379
+ slot.active=false;slot.ending=false;slot.targetId=-1;slot.owner={};slot.dragged=false;slot.defaultPrevented=false;return true;
380
+ }return false;
347
381
  }
348
382
 
349
383
  // Immediate-mode (no UI tree) path: synthesize the pointer event from the raw
@@ -367,6 +401,8 @@ private:
367
401
  }
368
402
  PointerEvent pe{};
369
403
  pe.type = type;
404
+ pe.button = type == PointerEventType::TouchMove ? -1 : 0;
405
+ pe.buttons = event.touching ? 1u : 0u;
370
406
  pe.targetId = -1;
371
407
  // Match the mounted-tree path and the public TypeScript surface:
372
408
  // web-facing pointer ids start at 1, while internal touch slots start at 0.
@@ -395,6 +431,8 @@ private:
395
431
  {
396
432
  PointerEvent event{};
397
433
  event.type = type;
434
+ event.button = (type == PointerEventType::TouchStart || type == PointerEventType::TouchEnd || type == PointerEventType::Click) ? 0 : -1;
435
+ event.buttons = activeTouch ? 1u : 0u;
398
436
  // Skip entirely when no node listens for this type — don't build the
399
437
  // event payload or walk the tree. Apps that bind only touch* never pay
400
438
  // for the paired pointer* dispatch (and vice versa).
@@ -455,9 +493,15 @@ private:
455
493
  int startY_ = 0;
456
494
  bool dragged_ = false;
457
495
  int activeTargetId_ = -1;
496
+ gea::embedded::ui::NodeHandle primaryOwner_;
497
+ unsigned int primaryTicket_=0;
498
+ bool primaryQuarantined_=false,primaryEnding_=false;
458
499
 
459
500
  // Per-pointer state for non-primary (pointerId >= 1) simultaneous fingers.
460
501
  struct Secondary {
502
+ gea::embedded::ui::NodeHandle owner;
503
+ unsigned int ticket=0;
504
+ bool quarantined=false,ending=false;
461
505
  bool active = false;
462
506
  bool dragged = false;
463
507
  bool defaultPrevented = false;
@@ -473,6 +517,13 @@ private:
473
517
 
474
518
  } // namespace
475
519
 
520
+ } // namespace gea::framework::events
521
+ namespace gea::embedded::ui {
522
+ void retireTouchInputForSubtrees(const InputRetirementRoots &removed,const InputRetirementRoots &retained)
523
+ {gea::framework::events::TouchEventDispatcher::instance().retireSubtrees(removed,retained);}
524
+ }
525
+
526
+ namespace gea::framework::events {
476
527
  // Touch controllers report PANEL-NATIVE coordinates; the UI hit-tests in
477
528
  // LOGICAL (orientation-rotated) space. Inverse of the display's
478
529
  // panelForSourcePixel mapping. Applied to QUEUED events AND to the raw
@@ -8,7 +8,7 @@
8
8
 
9
9
  #include "canvas.h"
10
10
  #include <image.h>
11
- #include "display.h"
11
+ #include <display.h>
12
12
  #include "node.h"
13
13
 
14
14
  #include <cstdint>
@@ -0,0 +1,94 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "node.h"
5
+ #include <cstdint>
6
+ #include <cmath>
7
+ #include <limits>
8
+ #include <memory>
9
+ #include <optional>
10
+ #include <utility>
11
+
12
+ namespace gea::embedded::ui {
13
+
14
+ // The parent owns the token in its existing rare data. The token has no node
15
+ // backpointer; a retained list owns a birth-checked handle, never a raw slot.
16
+ struct ChildNodeListIdentity final {};
17
+
18
+ class ChildNodeListIterator;
19
+
20
+ class ChildNodeList {
21
+ public:
22
+ ChildNodeList() = default;
23
+ const void *identity() const { return identity_.get(); }
24
+ std::shared_ptr<const void> weakIdentityOwner() const { return identity_; }
25
+
26
+ std::uint32_t length() const
27
+ {
28
+ std::uint32_t count = 0;
29
+ for (auto child = parent_.firstChildHandle(); child; child = child.nextSiblingHandle()) ++count;
30
+ return count;
31
+ }
32
+
33
+ // The caller must already have performed WebIDL unsigned-long conversion.
34
+ // The collection does not conflate that conversion with JS property keys.
35
+ NodeHandle itemAt(std::uint32_t index) const
36
+ {
37
+ if (index > static_cast<std::uint32_t>(std::numeric_limits<int>::max())) return NodeHandle();
38
+ return parent_.childAt(static_cast<int>(index));
39
+ }
40
+
41
+ // WebIDL unsigned long converts nonfinite values to 0, truncates toward
42
+ // zero and wraps modulo 2^32. Conversion is bounded before the integer cast.
43
+ NodeHandle item(double index) const
44
+ {
45
+ double integer = std::isfinite(index) ? std::trunc(index) : 0.0;
46
+ double wrapped = std::fmod(integer, 4294967296.0);
47
+ if (wrapped < 0) wrapped += 4294967296.0;
48
+ return itemAt(static_cast<std::uint32_t>(wrapped));
49
+ }
50
+
51
+ // Internal absence is explicit. A host adapter must publish undefined for a
52
+ // missing indexed property, unlike item's null, without boxing a typed node.
53
+ std::optional<NodeHandle> indexed(std::uint32_t index) const
54
+ {
55
+ auto child = itemAt(index);
56
+ if (!child) return std::nullopt;
57
+ return child;
58
+ }
59
+
60
+ ChildNodeListIterator values() const;
61
+
62
+ private:
63
+ friend class NodeHandle;
64
+ ChildNodeList(NodeHandle parent, std::shared_ptr<const ChildNodeListIdentity> identity)
65
+ : parent_(std::move(parent)), identity_(std::move(identity)) {}
66
+ NodeHandle parent_;
67
+ std::shared_ptr<const ChildNodeListIdentity> identity_;
68
+ };
69
+
70
+ class ChildNodeListIterator {
71
+ public:
72
+ explicit ChildNodeListIterator(ChildNodeList list) : list_(std::move(list)) {}
73
+
74
+ std::optional<NodeHandle> next()
75
+ {
76
+ if (done_) return std::nullopt;
77
+ auto value = list_.indexed(index_);
78
+ if (!value) {
79
+ done_ = true;
80
+ return std::nullopt;
81
+ }
82
+ ++index_;
83
+ return value;
84
+ }
85
+
86
+ private:
87
+ ChildNodeList list_;
88
+ std::uint32_t index_ = 0;
89
+ bool done_ = false;
90
+ };
91
+
92
+ inline ChildNodeListIterator ChildNodeList::values() const { return ChildNodeListIterator(*this); }
93
+
94
+ } // namespace gea::embedded::ui
@@ -0,0 +1,23 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+ #include "node.h"
4
+ #include "selector.h"
5
+ #include "tree_internal.h"
6
+ #include <string>
7
+ namespace gea::embedded::ui {
8
+ // User edit transaction, not an attribute observer. A recycled slot cannot
9
+ // inherit the allocation captured when editing began.
10
+ class ControlCommit {
11
+ NodeHandle owner_;std::string field_="value",initial_;bool edited_=false;
12
+ std::string value()const{const auto*p=owner_.getAttribute(field_);return p?p:"";}
13
+ public:
14
+ void begin(int node,const char*field="value"){owner_=NodeHandle(node);field_=field;initial_=owner_.valid()?value():"";edited_=false;}
15
+ bool owns(int node)const{return owner_.valid()&&owner_.id()==node;}
16
+ bool admitted(int node)const{return owns(node)&&!selector::disabled(Tree::instance(),node);}
17
+ void edited(int node){if(admitted(node))edited_=true;}
18
+ // Retire this commit before callbacks; never retain a Tree pointer across
19
+ // listener-driven teardown, replacement or nested focus transitions.
20
+ bool commit(){const int id=owner_.id();if(!admitted(id)){edited_=false;return false;}const auto current=value();const bool changed=edited_&&current!=initial_;initial_=current;edited_=false;if(!changed)return false;
21
+ gea::framework::events::PointerEvent event{};event.type=gea::framework::events::PointerEventType::Change;event.targetId=id;event.cancelable=false;Tree::instance().dispatchEvent(event);return true;}
22
+ };
23
+ }
package/ui/css_features.h CHANGED
@@ -367,3 +367,8 @@
367
367
  #ifndef GEA_CSS_DISPLAY_EXPLICIT
368
368
  #define GEA_CSS_DISPLAY_EXPLICIT 1
369
369
  #endif
370
+
371
+ // Desktop targets opt in; embedded node storage remains unchanged by default.
372
+ #ifndef GEA_CSS_FONT_STRETCH
373
+ #define GEA_CSS_FONT_STRETCH 0
374
+ #endif
package/ui/document.cpp CHANGED
@@ -1,11 +1,14 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  #include "document.h"
3
+ #include "selector.h"
4
+ #include "tree_state.h"
3
5
 
4
6
  #include "internal.h"
5
7
 
6
8
  #include "display.h"
7
9
  #include "display_underlay.h"
8
10
  #include "tree_internal.h"
11
+ #include "tree_events.h"
9
12
 
10
13
  #include <algorithm>
11
14
  #include <cctype>
@@ -37,24 +40,6 @@ std::string lowerAscii(std::string value)
37
40
  return value;
38
41
  }
39
42
 
40
- bool matchesSelector(Tree &tree, int nodeId, const std::string &selector)
41
- {
42
- if (selector.empty()) return false;
43
- if (selector == "body") {
44
- const int mountedRoot = tree.mountedRoot();
45
- return (mountedRoot >= 0 && nodeId == mountedRoot) || (mountedRoot < 0 && nodeId == 0);
46
- }
47
- if (selector[0] == '#') {
48
- if (selector.size() <= 1) return false;
49
- return std::strcmp(tree.getAttribute(nodeId, "id"), selector.c_str() + 1) == 0;
50
- }
51
- if (selector[0] == '.') {
52
- if (selector.size() <= 1) return false;
53
- return tree.hasClass(nodeId, selector.substr(1));
54
- }
55
- return lowerAscii(tree.tagName(nodeId)) == lowerAscii(selector);
56
- }
57
-
58
43
  NodeHandle findElementById(Tree &tree, const char *id)
59
44
  {
60
45
  if (!id || id[0] == '\0') return NodeHandle();
@@ -135,7 +120,15 @@ NodeHandle Document::createAudio() const
135
120
 
136
121
  NodeHandle Document::createDocumentFragment() const
137
122
  {
138
- return NodeHandle(Tree::instance().createView());
123
+ return NodeHandle(Tree::instance().createDocumentFragment());
124
+ }
125
+
126
+ NodeHandle Document::createComment(const char *text) const
127
+ {
128
+ auto &tree = Tree::instance();
129
+ const int id = tree.createComment();
130
+ if (id >= 0) tree.setText(id, text ? text : "");
131
+ return NodeHandle(id);
139
132
  }
140
133
 
141
134
  ViewElement Document::createVirtualList() const
@@ -143,6 +136,12 @@ ViewElement Document::createVirtualList() const
143
136
  return ViewElement(Tree::instance().createVirtualList());
144
137
  }
145
138
 
139
+ NodeHandle Document::activeElement() const {
140
+ const auto native=queryNativeFocus();
141
+ auto node=native.handled?native.node:NodeHandle(Tree::instance().activeInputId());
142
+ return node.valid()&&node.isConnected()?node:NodeHandle();
143
+ }
144
+
146
145
  NodeHandle Document::createElement(const char *tag) const
147
146
  {
148
147
  std::string lowered = tag ? tag : "";
@@ -161,36 +160,28 @@ NodeHandle Document::createElement(const char *tag) const
161
160
 
162
161
  NodeHandle Document::getElementById(const char *id) const
163
162
  {
164
- if (!id || id[0] == '\0') return NodeHandle();
165
- Tree &tree = Tree::instance();
166
- NodeHandle existing = findElementById(tree, id);
167
- if (existing) return existing;
168
- return std::strcmp(id, "app") == 0 ? ensureAppRoot("app") : NodeHandle();
163
+ if (!id || !*id) return {};
164
+ auto &tree=Tree::instance();
165
+ if(tree.mountedRoot()<0) return std::strcmp(id,"app")==0?ensureAppRoot("app"):NodeHandle();
166
+ NodeHandle root(tree.mountedRoot());
167
+ if(root.valid()&&std::strcmp(root.getAttribute("id"),id)==0)return root;
168
+ for(auto node:root.querySelectorAll("*"))if(std::strcmp(node.getAttribute("id"),id)==0)return node;
169
+ return {};
169
170
  }
170
-
171
- NodeHandle Document::querySelector(const char *selector) const
171
+ NodeHandle Document::querySelector(const char *s) const
172
172
  {
173
- const std::string normalized = normalizeSelector(selector);
174
- if (normalized == "body" || normalized == "#app") return ensureAppRoot("app");
175
- const std::vector<NodeHandle> matches = querySelectorAll(normalized.c_str());
176
- return matches.empty() ? NodeHandle() : matches.front();
173
+ auto all=querySelectorAll(s);return all.empty()?NodeHandle():all.front();
177
174
  }
178
-
179
- std::vector<NodeHandle> Document::querySelectorAll(const char *selector) const
175
+ std::vector<NodeHandle> Document::querySelectorAll(const char *s) const
180
176
  {
181
- std::vector<NodeHandle> matches;
182
- const std::string normalized = normalizeSelector(selector);
183
- if (normalized.empty()) return matches;
184
- if (normalized == "body" || normalized == "#app") {
185
- matches.push_back(ensureAppRoot("app"));
186
- return matches;
187
- }
188
-
189
- Tree &tree = Tree::instance();
190
- for (int nodeId = 0; nodeId < tree.nodeCount(); nodeId++) {
191
- if (matchesSelector(tree, nodeId, normalized)) matches.push_back(NodeHandle(nodeId));
192
- }
193
- return matches;
177
+ auto q=selector::parse(s);q.requireValid();auto &tree=Tree::instance();
178
+ // Generated application bootstrap queries its single native root before
179
+ // any node is mounted. Ordinary queries never manufacture nodes.
180
+ const auto normalized=normalizeSelector(s);
181
+ if(tree.mountedRoot()<0&&(normalized=="body"||normalized=="#app"))ensureAppRoot("app");
182
+ NodeHandle root(tree.mountedRoot());std::vector<NodeHandle> out;if(!root.valid())return out;
183
+ if(q.matches(tree,root.id(),root.id()))out.push_back(root);
184
+ auto descendants=root.querySelectorAll(s);out.insert(out.end(),descendants.begin(),descendants.end());return out;
194
185
  }
195
186
 
196
187
  namespace {
@@ -200,29 +191,6 @@ namespace {
200
191
  int g_preferred_mount_w = 0;
201
192
  int g_preferred_mount_h = 0;
202
193
 
203
- struct DocumentEventListener {
204
- std::string type;
205
- gea::framework::events::EventListener listener;
206
- int boundRoot = -1;
207
- };
208
-
209
- std::vector<DocumentEventListener> &documentEventListeners()
210
- {
211
- static std::vector<DocumentEventListener> listeners;
212
- return listeners;
213
- }
214
-
215
- void bindDocumentEventListeners()
216
- {
217
- const int root = Tree::instance().mountedRoot();
218
- if (root < 0) return;
219
- for (auto &entry : documentEventListeners()) {
220
- if (entry.boundRoot == root) continue;
221
- Tree::instance().setEventListener(root, entry.type.c_str(), entry.listener);
222
- entry.boundRoot = root;
223
- }
224
- }
225
-
226
194
  // Map a pointer/touch event type to a slot index, aliasing pointer* onto its
227
195
  // touch* sibling (the web binds onPointerDown/Move/Up; the device synthesizes
228
196
  // touch*). Mirrors eventTypeIndex's pointer/touch grouping in tree_events.cpp.
@@ -232,9 +200,8 @@ int pointerTypeIndex(const char *type)
232
200
  if (std::strcmp(type, "click") == 0) return 0;
233
201
  if (std::strcmp(type, "touchstart") == 0 || std::strcmp(type, "pointerdown") == 0) return 1;
234
202
  if (std::strcmp(type, "touchmove") == 0 || std::strcmp(type, "pointermove") == 0) return 2;
235
- if (std::strcmp(type, "touchend") == 0 || std::strcmp(type, "pointerup") == 0 ||
236
- std::strcmp(type, "pointercancel") == 0)
237
- return 3;
203
+ if (std::strcmp(type, "touchend") == 0 || std::strcmp(type, "pointerup") == 0) return 3;
204
+ if (std::strcmp(type, "pointercancel") == 0 || std::strcmp(type, "touchcancel") == 0) return 4;
238
205
  return -1;
239
206
  }
240
207
  } // namespace
@@ -243,21 +210,13 @@ bool dispatchDocumentPointer(gea::framework::events::PointerEvent &event)
243
210
  {
244
211
  const int wantIdx = pointerTypeIndex(event.typeName());
245
212
  if (wantIdx < 0) return false;
246
- bool fired = false;
247
- for (auto &entry : documentEventListeners()) {
248
- if (pointerTypeIndex(entry.type.c_str()) != wantIdx) continue;
249
- if (!static_cast<bool>(entry.listener)) continue;
250
- entry.listener(event);
251
- fired = true;
252
- if (event.propagationStopped) break;
253
- }
213
+ bool fired = dispatchWindowEvent(event, true);
214
+ if (!event.propagationStopped) fired = dispatchDocumentEvent(event, true) || fired;
215
+ if (!event.propagationStopped) fired = dispatchDocumentEvent(event, false) || fired;
216
+ if (!event.propagationStopped && event.bubbles) fired = dispatchWindowEvent(event, false) || fired;
254
217
  return fired;
255
218
  }
256
219
 
257
- void resetDocumentDelegatedEventListeners()
258
- {
259
- documentEventListeners().clear();
260
- }
261
220
 
262
221
  void Document::setPreferredMountSize(int width, int height)
263
222
  {
@@ -324,7 +283,6 @@ void Document::mount(NodeHandle root, int width, int height) const
324
283
  root.style().height(height);
325
284
  }
326
285
  Tree::instance().mount(root.id(), width, height);
327
- bindDocumentEventListeners();
328
286
  }
329
287
 
330
288
  void Document::refresh(NodeHandle root, int width, int height) const
@@ -352,18 +310,7 @@ void Document::frame(int timestampMs) const
352
310
 
353
311
  void Document::addEventListener(const char *type, gea::framework::events::EventListener listener) const
354
312
  {
355
- if (!type || type[0] == '\0' || !listener) return;
356
- // `rotary` (and `keydown` bound on a non-input) is a genuine document-level
357
- // event — it has no element target, so it is stored off-tree as a singleton
358
- // and dispatched directly by the runtime, never bound to a node.
359
- if (std::strcmp(type, "rotary") == 0 || std::strcmp(type, "keydown") == 0 || std::strcmp(type, "keyup") == 0) {
360
- setDocumentEventListener(type, std::move(listener));
361
- return;
362
- }
363
- // Everything else delegates through the native body node (event bubbles up
364
- // the tree to the mounted root, where this listener filters by target).
365
- documentEventListeners().push_back(DocumentEventListener{type, std::move(listener), -1});
366
- bindDocumentEventListeners();
313
+ setDocumentEventListener(type, std::move(listener));
367
314
  }
368
315
 
369
316
  } // namespace gea::embedded::ui