euv_core/scene/tree/struct.rs
1use super::*;
2
3/// Flat generation-indexed arena holding the entire retained UI scene graph with dense SoA metadata and intrusive child topology.
4#[derive(CustomDebug, Data)]
5pub struct SceneTree {
6 /// Contiguous node storage vector.
7 pub(crate) nodes: Vec<SceneNode>,
8 /// Dense 8-byte SoA metadata parallel to `nodes` (generation, structural flags, dirty mask).
9 #[get(pub(crate))]
10 #[get_mut(pub(crate))]
11 #[set(skip)]
12 pub(crate) meta: Vec<NodeMeta>,
13 /// Dense 24-byte SoA intrusive doubly-linked tree topology parallel to `nodes` (0 `Vec<NodeId>` heap allocations).
14 #[get(pub(crate))]
15 #[get_mut(pub(crate))]
16 #[set(skip)]
17 pub(crate) topology: Vec<NodeTopology>,
18 /// Dense 32-byte SoA compact paint style array parallel to `nodes`.
19 #[get(pub(crate))]
20 #[get_mut(pub(crate))]
21 #[set(skip)]
22 pub(crate) compact_paints: Vec<CompactPaintStyle>,
23 /// Sparse slot-recycled table of rare paint effects (`Shadow`, `LinearGradient`, `Transform2D`, `focus_ring`, `backdrop_blur`).
24 #[get(pub(crate))]
25 #[get_mut(pub(crate))]
26 #[set(skip)]
27 pub(crate) effect_table: EffectTable,
28 /// Recycled slot indices for O(1) node allocation.
29 #[get(pub(crate))]
30 #[get_mut(pub(crate))]
31 #[set(pub(crate))]
32 pub(crate) free_list: Vec<u32>,
33 /// Incrementally maintained count of live nodes for O(1) `live_node_count()`.
34 #[get(pub(crate), type(copy))]
35 #[get_mut(pub(crate))]
36 #[set(pub(crate))]
37 pub(crate) live_count: usize,
38 /// Incrementally maintained count of attached `Lifecycle` observers for O(1) zero-lifecycle traversal skipping.
39 #[get(pub(crate), type(copy))]
40 #[get_mut(pub(crate))]
41 #[set(pub(crate))]
42 pub(crate) live_lifecycle_count: usize,
43 /// Root scene node handle.
44 #[get(type(copy))]
45 pub(crate) root: NodeId,
46 /// Focus, hover, and pointer-press tracking state.
47 #[get(type(copy))]
48 pub(crate) focus: FocusManager,
49 /// Monotonic structural/visual mutation counter.
50 #[get(type(copy))]
51 pub(crate) revision: u64,
52 /// Monotonic tree-topology mutation counter bumped only on node attach, detach, reorder, replace, or creation.
53 #[get(type(copy))]
54 pub(crate) topology_revision: u64,
55 /// Active paint/transform node transitions ticked on every frame until settled.
56 pub(crate) transitions: Vec<NodeTransition>,
57 /// Blinking visibility state of the focused text input caret (`true` = visible phase).
58 #[get(type(copy))]
59 pub(crate) caret_visible: bool,
60 /// Incrementally maintained registry of live `NodeKind::Portal` and `Position::Fixed` nodes.
61 #[get(skip)]
62 #[get_mut(pub(crate))]
63 #[set(skip)]
64 pub(crate) portal_nodes: Vec<NodeId>,
65 /// Incrementally maintained registry of live `Overflow::Scroll` container nodes.
66 #[get(skip)]
67 #[get_mut(pub(crate))]
68 #[set(skip)]
69 pub(crate) scroll_nodes: Vec<NodeId>,
70 /// Bounded list of nodes marked paint/scroll dirty since the last `collect_paint_damage` pass.
71 #[get(pub(crate))]
72 #[get_mut(pub(crate))]
73 #[set(pub(crate))]
74 pub(crate) dirty_paint_nodes: Vec<NodeId>,
75 /// Flag raised when structural/layout mutations or >64 dirty nodes require full viewport damage.
76 #[get(pub(crate), type(copy))]
77 #[get_mut(pub(crate))]
78 #[set(pub(crate))]
79 pub(crate) full_damage_pending: bool,
80 /// Flag raised when one or more layout/children/text mutations occurred since the last damage collection.
81 #[get(pub(crate), type(copy))]
82 #[get_mut(pub(crate))]
83 #[set(pub(crate))]
84 pub(crate) layout_damage_pending: bool,
85 /// Flag raised when `dirty_paint_nodes` exceeded `DIRTY_PAINT_FULL_THRESHOLD` since the last collection.
86 #[get(pub(crate), type(copy))]
87 #[get_mut(pub(crate))]
88 #[set(pub(crate))]
89 pub(crate) full_damage_overflow: bool,
90 /// Number of newly allocated nodes created since the last damage collection (promotes to overflow above threshold).
91 #[get(pub(crate), type(copy))]
92 #[get_mut(pub(crate))]
93 #[set(pub(crate))]
94 pub(crate) created_since_damage: usize,
95 /// Pre-mutation conservative logical screen rectangles for disposed, reparented, or shrinking-halo nodes.
96 #[get(pub(crate))]
97 #[get_mut(pub(crate))]
98 #[set(pub(crate))]
99 pub(crate) extra_logical_damage: Vec<Rect>,
100 /// Active native text selection state across one or more selectable `NodeKind::Text` nodes, if any.
101 #[get(skip)]
102 #[get_mut(skip)]
103 #[set(skip)]
104 pub(crate) text_selection: Option<SelectionState>,
105 /// Precomputed ordered per-node `(node_id, start_char, end_char)` spans for the active `text_selection`.
106 #[get(skip)]
107 #[get_mut(skip)]
108 #[set(skip)]
109 pub(crate) text_selection_spans: Vec<(NodeId, usize, usize)>,
110 /// Registry of live nodes with active backdrop-blur radii `(node_id, blur_radius)` for effect-dependency damage propagation.
111 #[get(pub(crate))]
112 #[get_mut(pub(crate))]
113 #[set(skip)]
114 pub(crate) backdrop_blur_nodes: Vec<(NodeId, f32)>,
115 /// Cumulative count of genuine layout-invalidating mutations (`DIRTY_LAYOUT`, `DIRTY_CHILDREN`, `DIRTY_TEXT`, node creation).
116 #[get(type(copy))]
117 #[get_mut(skip)]
118 #[set(pub(crate))]
119 pub(crate) layout_invalidations: u64,
120 /// Whether the most recent `collect_surgical_paint_damage` pass observed only composite/scroll changes with zero layout or paint mutations.
121 #[get(pub(crate), type(copy))]
122 #[get_mut(skip)]
123 #[set(pub(crate))]
124 pub(crate) last_collect_composite_only: bool,
125 /// Sparse generation-guarded theme ownership and origin snapshots keyed by arena slot index.
126 #[get(skip)]
127 #[get_mut(skip)]
128 #[set(skip)]
129 pub(crate) theme_entries: HashMap<u32, ThemeNodeEntry>,
130 /// Dense per-node monotonic paint epoch parallel to `nodes` for O(1) `PaintFragmentCache` invalidation.
131 #[get(pub(crate))]
132 #[get_mut(pub(crate))]
133 #[set(skip)]
134 pub(crate) paint_epochs: Vec<u32>,
135 /// Dense per-node monotonic mutation epoch parallel to `nodes` preserving lossless `SceneLayoutSoa` coherence across serial and paint dirty-clear passes.
136 #[get(pub(crate))]
137 #[get_mut(pub(crate))]
138 #[set(skip)]
139 pub(crate) node_mutation_epochs: Vec<u32>,
140 /// Monotonic counter bumped whenever a serial layout pass computes nodes or clears dirty layout flags.
141 #[get(pub(crate), type(copy))]
142 #[get_mut(skip)]
143 #[set(pub(crate))]
144 pub(crate) serial_layout_epoch: u64,
145 /// Dense per-node `(layout_sig, text_metric_sig)` fingerprints parallel to `nodes` for automatic dirty-flag promotion on direct style mutations.
146 #[get(pub(crate))]
147 #[get_mut(pub(crate))]
148 #[set(skip)]
149 pub(crate) style_sigs: Vec<(u64, u64)>,
150 /// Flag raised when `LayoutEngine` skips a `Display::None` node carrying `DIRTY_SUBTREE_LAYOUT` so `mark_dirty` never breaks early on stale descendant bits.
151 #[get(pub(crate), type(copy))]
152 #[get_mut(skip)]
153 #[set(pub(crate))]
154 pub(crate) has_stale_hidden_dirty: bool,
155 /// Bounded queue of target nodes awaiting post-layout ancestor `ScrollView` viewport reveal.
156 #[get(pub(crate))]
157 #[get_mut(pub(crate))]
158 #[set(pub(crate))]
159 pub(crate) pending_scroll_into_view: Vec<NodeId>,
160 /// Active embedded WebView viewport container node synchronized with the host overlay while visible.
161 #[get(skip)]
162 #[get_mut(skip)]
163 #[set(skip)]
164 pub(crate) active_webview_node: Option<NodeId>,
165}
166
167/// Zero-allocation double-ended iterator over a node's direct children in `SceneTree`'s intrusive SoA topology.
168#[derive(Clone, Debug)]
169pub struct SceneChildIter<'a> {
170 /// Shared slice of scene metadata carrying slot generations.
171 pub(crate) meta: &'a [NodeMeta],
172 /// Shared slice of intrusive sibling links.
173 pub(crate) topology: &'a [NodeTopology],
174 /// Current forward cursor slot index (`NODE_INDEX_NONE` when exhausted).
175 pub(crate) head: u32,
176 /// Current backward cursor slot index (`NODE_INDEX_NONE` when exhausted).
177 pub(crate) tail: u32,
178 /// Remaining unyielded direct children count.
179 pub(crate) remaining: usize,
180}
181
182/// Shared scene storage pairing interior-mutable `SceneTree` access with a zero-steady-state-allocation deferred write queue.
183#[derive(CustomDebug)]
184pub(crate) struct SceneHandleInner {
185 /// Interior-mutable retained UI scene tree.
186 pub(crate) tree: RefCell<SceneTree>,
187 /// Fast-path flag indicating at least one deferred mutation is queued (avoids `RefCell` touch in steady state).
188 pub(crate) has_deferred: Cell<bool>,
189 /// Lazily populated queue of re-entrant scene mutations deferred while `tree` is actively borrowed.
190 #[debug(skip)]
191 pub(crate) deferred_writes: RefCell<Vec<DeferredSceneWrite>>,
192}
193
194/// RAII guard tracking active `SceneTree` borrow nesting depth on the current thread.
195#[derive(Debug)]
196pub(crate) struct SceneBorrowGuard;
197
198/// Shared handle wrapping a `SceneTree` so reactive `Effect` closures can patch nodes directly.
199#[derive(Clone, CustomDebug)]
200pub struct SceneHandle {
201 /// Shared interior-mutable scene storage and deferred mutation queue.
202 pub(crate) inner: Rc<SceneHandleInner>,
203}
204
205/// Ambient framework-managed UI context binding the active `SceneHandle`, responsive signals, and platform metadata.
206#[derive(Clone, CustomDebug, Data)]
207pub struct Context {
208 /// Shared scene tree handle for creating and binding nodes.
209 pub(crate) scene: SceneHandle,
210 /// Current viewport logical breakpoint signal for responsive layouts.
211 #[get(type(copy))]
212 pub(crate) breakpoint: Signal<Breakpoint>,
213 /// Current logical viewport dimensions signal updated immediately on window resize.
214 #[get(type(copy))]
215 pub(crate) viewport_size: Signal<Size>,
216 /// Current OS/app theme mode signal (`Light` / `Dark`).
217 #[get(type(copy))]
218 pub(crate) theme_mode: Signal<ThemeMode>,
219 /// Active platform kind for capability-aware adaptation.
220 #[get(type(copy))]
221 pub(crate) platform: PlatformKind,
222 /// Active safe-area insets signal.
223 #[get(type(copy))]
224 pub(crate) safe_area: Signal<SafeAreaInsets>,
225 /// Reactive stream of recognized cross-platform gestures (`Tap`, `DoubleTap`, `LongPress`, `Swipe*`, `Pan`, `Pinch`).
226 #[get(type(copy))]
227 pub(crate) gesture: Signal<Option<GestureEvent>>,
228 /// Live horizontal touch-swipe drag displacement in logical pixels (`0.0` when idle) for interactive page-swipe feedback.
229 #[get(type(copy))]
230 pub(crate) swipe_drag_offset: Signal<f32>,
231 /// Monotonic counter incremented on OS Back button / Escape / edge-back triggers for unified overlay & router stack dismissal.
232 #[get(type(copy))]
233 pub(crate) system_back: Signal<u64>,
234 /// Shared dynamic update hub enabling ambient component-level and page-level live updates.
235 pub(crate) dynamic_hub: DynamicUpdateHub,
236 /// Optional target FPS cap across all platforms (`None` = unlimited maximum FPS by default).
237 #[get(type(copy))]
238 pub(crate) fps_limit: Signal<Option<u32>>,
239 /// Smoothed live rendered frames-per-second telemetry signal.
240 #[get(type(copy))]
241 pub(crate) live_fps: Signal<f32>,
242 /// Idle-aware active presented frames-per-second signal (`0.0` while idle).
243 #[get(type(copy))]
244 pub(crate) present_fps: Signal<f32>,
245 /// Reactive flag indicating whether the frame loop is currently idle (`true` when skipping clean frames).
246 #[get(type(copy))]
247 pub(crate) is_idle: Signal<bool>,
248 /// Native hardware display refresh rate in Hertz reported by the active OS/browser host.
249 #[get(type(copy))]
250 pub(crate) display_refresh_rate: Signal<f32>,
251 /// Live snapshot of all currently active touch/pointer contacts `(pointer_id, position)` for multi-touch UIs.
252 #[get(type(copy))]
253 pub(crate) active_touches: Signal<Vec<(u64, Point)>>,
254 /// Reactive flag raised when the application requests graceful termination (e.g. double-back-to-exit at root).
255 #[get(type(copy))]
256 pub(crate) exit_requested: Signal<bool>,
257 /// Canonical cross-platform application lifecycle state signal (`Active`, `Inactive`, `Background`, `Terminated`).
258 #[get(type(copy))]
259 pub(crate) app_state: Signal<AppState>,
260 /// Live network reachability status signal (`Online`, `Offline`, `Wifi`, `Cellular`, `Unknown`).
261 #[get(type(copy))]
262 pub(crate) network_status: Signal<NetworkStatus>,
263 /// Whether the soft virtual keyboard / IME is currently presented.
264 #[get(type(copy))]
265 pub(crate) keyboard_visible: Signal<bool>,
266 /// Current soft virtual keyboard height in logical pixels (`0.0` when hidden).
267 #[get(type(copy))]
268 pub(crate) keyboard_height: Signal<f32>,
269 /// Current screen or window orientation signal.
270 #[get(type(copy))]
271 pub(crate) orientation: Signal<ScreenOrientation>,
272 /// Whether OS battery saver / low-power mode is currently enabled.
273 #[get(type(copy))]
274 pub(crate) low_power_mode: Signal<bool>,
275 /// Active OS/browser preferred BCP-47 locale string signal.
276 #[get(type(copy))]
277 pub(crate) locale: Signal<String>,
278 /// Whether the primary application window currently holds keyboard/input focus.
279 #[get(type(copy))]
280 pub(crate) window_focused: Signal<bool>,
281 /// Reactive flag indicating whether immersive fullscreen mode is active across app chrome and OS system bars.
282 #[get(type(copy))]
283 #[set(skip)]
284 pub(crate) immersive_fullscreen: Signal<bool>,
285 /// Live screen-aligned 6-DOF gravity, linear acceleration, and gyroscope sensor sample signal.
286 #[get(type(copy))]
287 pub(crate) device_motion: Signal<DeviceMotionSample>,
288 /// Shared reference-counted context lifetime owner storing clock, fullscreen depth, and owned signals disposed on final drop.
289 #[debug(skip)]
290 #[get(skip)]
291 #[get_mut(skip)]
292 #[set(skip)]
293 pub(crate) shared: Rc<ContextShared>,
294}
295
296/// Shared interior-mutable runtime metadata and signal ownership table disposed automatically when the last `Context` clone drops.
297#[derive(Debug)]
298pub(crate) struct ContextShared {
299 /// Nested immersive fullscreen entry counter enabling balanced RAII enter/exit calls.
300 pub(crate) fullscreen_depth: Cell<u32>,
301 /// Monotonic runtime clock in milliseconds advanced deterministically by `App` frame steps.
302 pub(crate) clock_ms: Cell<u64>,
303 /// Detached reactive signals created by `Context::new` and disposed on final drop to guarantee zero lifecycle leak.
304 pub(crate) owned_signals: [SignalId; 24],
305 /// Reactive runtime epoch active when `owned_signals` were allocated.
306 pub(crate) runtime_epoch: u64,
307}
308
309/// RAII scope guard restoring the previous ambient `Context` on drop.
310#[derive(CustomDebug, New)]
311pub struct ContextGuard {
312 /// Previously bound ambient context to restore when leaving scope.
313 pub(crate) previous: Option<Context>,
314}