Skip to main content

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    /// True when the host platform suppresses all graphical overlay scrollbars (mobile, tablet, wearable, miniapp).
166    #[get(pub(crate), type(copy))]
167    #[get_mut(skip)]
168    #[set(pub(crate))]
169    pub(crate) scrollbars_suppressed: bool,
170    /// Active soft-keyboard height in logical dp (`0.0` when hidden).
171    #[get(pub(crate), type(copy))]
172    #[get_mut(skip)]
173    #[set(pub(crate))]
174    pub(crate) keyboard_inset_height: f32,
175    /// Logical viewport height paired with `keyboard_inset_height`.
176    #[get(pub(crate), type(copy))]
177    #[get_mut(skip)]
178    #[set(pub(crate))]
179    pub(crate) keyboard_viewport_height: f32,
180    /// Logical top safe-area inset paired with `keyboard_inset_height`.
181    #[get(pub(crate), type(copy))]
182    #[get_mut(skip)]
183    #[set(pub(crate))]
184    pub(crate) keyboard_safe_top: f32,
185    /// Flag requesting post-layout keyboard visibility adjustment for the currently focused editable input.
186    #[get(pub(crate), type(copy))]
187    #[get_mut(skip)]
188    #[set(pub(crate))]
189    pub(crate) keyboard_avoidance_pending: bool,
190    /// Temporary vertical lift `(overlay_node, lift_dp)` applied to a bottom-anchored overlay panel while a soft keyboard is visible.
191    #[get(skip)]
192    #[get_mut(skip)]
193    #[set(skip)]
194    pub(crate) keyboard_lifted_overlay: Option<(NodeId, f32)>,
195}
196
197/// Zero-allocation double-ended iterator over a node's direct children in `SceneTree`'s intrusive SoA topology.
198#[derive(Clone, Debug)]
199pub struct SceneChildIter<'a> {
200    /// Shared slice of scene metadata carrying slot generations.
201    pub(crate) meta: &'a [NodeMeta],
202    /// Shared slice of intrusive sibling links.
203    pub(crate) topology: &'a [NodeTopology],
204    /// Current forward cursor slot index (`NODE_INDEX_NONE` when exhausted).
205    pub(crate) head: u32,
206    /// Current backward cursor slot index (`NODE_INDEX_NONE` when exhausted).
207    pub(crate) tail: u32,
208    /// Remaining unyielded direct children count.
209    pub(crate) remaining: usize,
210}
211
212/// Shared scene storage pairing interior-mutable `SceneTree` access with a zero-steady-state-allocation deferred write queue.
213#[derive(CustomDebug)]
214pub(crate) struct SceneHandleInner {
215    /// Interior-mutable retained UI scene tree.
216    pub(crate) tree: RefCell<SceneTree>,
217    /// Fast-path flag indicating at least one deferred mutation is queued (avoids `RefCell` touch in steady state).
218    pub(crate) has_deferred: Cell<bool>,
219    /// Lazily populated queue of re-entrant scene mutations deferred while `tree` is actively borrowed.
220    #[debug(skip)]
221    pub(crate) deferred_writes: RefCell<Vec<DeferredSceneWrite>>,
222}
223
224/// RAII guard tracking active `SceneTree` borrow nesting depth on the current thread.
225#[derive(Debug)]
226pub(crate) struct SceneBorrowGuard;
227
228/// Shared handle wrapping a `SceneTree` so reactive `Effect` closures can patch nodes directly.
229#[derive(Clone, CustomDebug)]
230pub struct SceneHandle {
231    /// Shared interior-mutable scene storage and deferred mutation queue.
232    pub(crate) inner: Rc<SceneHandleInner>,
233}
234
235/// Ambient framework-managed UI context binding the active `SceneHandle`, responsive signals, and platform metadata.
236#[derive(Clone, CustomDebug, Data)]
237pub struct Context {
238    /// Shared scene tree handle for creating and binding nodes.
239    pub(crate) scene: SceneHandle,
240    /// Current viewport logical breakpoint signal for responsive layouts.
241    #[get(type(copy))]
242    pub(crate) breakpoint: Signal<Breakpoint>,
243    /// Current logical viewport dimensions signal updated immediately on window resize.
244    #[get(type(copy))]
245    pub(crate) viewport_size: Signal<Size>,
246    /// Current OS/app theme mode signal (`Light` / `Dark`).
247    #[get(type(copy))]
248    pub(crate) theme_mode: Signal<ThemeMode>,
249    /// Active platform kind for capability-aware adaptation.
250    #[get(type(copy))]
251    pub(crate) platform: PlatformKind,
252    /// Active safe-area insets signal.
253    #[get(type(copy))]
254    pub(crate) safe_area: Signal<SafeAreaInsets>,
255    /// Reactive stream of recognized cross-platform gestures (`Tap`, `DoubleTap`, `LongPress`, `Swipe*`, `Pan`, `Pinch`).
256    #[get(type(copy))]
257    pub(crate) gesture: Signal<Option<GestureEvent>>,
258    /// Live horizontal touch-swipe drag displacement in logical pixels (`0.0` when idle) for interactive page-swipe feedback.
259    #[get(type(copy))]
260    pub(crate) swipe_drag_offset: Signal<f32>,
261    /// Monotonic counter incremented on OS Back button / Escape / edge-back triggers for unified overlay & router stack dismissal.
262    #[get(type(copy))]
263    pub(crate) system_back: Signal<u64>,
264    /// Shared dynamic update hub enabling ambient component-level and page-level live updates.
265    pub(crate) dynamic_hub: DynamicUpdateHub,
266    /// Optional target FPS cap across all platforms (`None` = unlimited maximum FPS by default).
267    #[get(type(copy))]
268    pub(crate) fps_limit: Signal<Option<u32>>,
269    /// Smoothed live rendered frames-per-second telemetry signal.
270    #[get(type(copy))]
271    pub(crate) live_fps: Signal<f32>,
272    /// Idle-aware active presented frames-per-second signal (`0.0` while idle).
273    #[get(type(copy))]
274    pub(crate) present_fps: Signal<f32>,
275    /// Reactive flag indicating whether the frame loop is currently idle (`true` when skipping clean frames).
276    #[get(type(copy))]
277    pub(crate) is_idle: Signal<bool>,
278    /// Native hardware display refresh rate in Hertz reported by the active OS/browser host.
279    #[get(type(copy))]
280    pub(crate) display_refresh_rate: Signal<f32>,
281    /// Live snapshot of all currently active touch/pointer contacts `(pointer_id, position)` for multi-touch UIs.
282    #[get(type(copy))]
283    pub(crate) active_touches: Signal<Vec<(u64, Point)>>,
284    /// Reactive flag raised when the application requests graceful termination (e.g. double-back-to-exit at root).
285    #[get(type(copy))]
286    pub(crate) exit_requested: Signal<bool>,
287    /// Canonical cross-platform application lifecycle state signal (`Active`, `Inactive`, `Background`, `Terminated`).
288    #[get(type(copy))]
289    pub(crate) app_state: Signal<AppState>,
290    /// Live network reachability status signal (`Online`, `Offline`, `Wifi`, `Cellular`, `Unknown`).
291    #[get(type(copy))]
292    pub(crate) network_status: Signal<NetworkStatus>,
293    /// Whether the soft virtual keyboard / IME is currently presented.
294    #[get(type(copy))]
295    pub(crate) keyboard_visible: Signal<bool>,
296    /// Current soft virtual keyboard height in logical pixels (`0.0` when hidden).
297    #[get(type(copy))]
298    pub(crate) keyboard_height: Signal<f32>,
299    /// Current screen or window orientation signal.
300    #[get(type(copy))]
301    pub(crate) orientation: Signal<ScreenOrientation>,
302    /// Whether OS battery saver / low-power mode is currently enabled.
303    #[get(type(copy))]
304    pub(crate) low_power_mode: Signal<bool>,
305    /// Active OS/browser preferred BCP-47 locale string signal.
306    #[get(type(copy))]
307    pub(crate) locale: Signal<String>,
308    /// Whether the primary application window currently holds keyboard/input focus.
309    #[get(type(copy))]
310    pub(crate) window_focused: Signal<bool>,
311    /// Reactive flag indicating whether immersive fullscreen mode is active across app chrome and OS system bars.
312    #[get(type(copy))]
313    #[set(skip)]
314    pub(crate) immersive_fullscreen: Signal<bool>,
315    /// Live screen-aligned 6-DOF gravity, linear acceleration, and gyroscope sensor sample signal.
316    #[get(type(copy))]
317    pub(crate) device_motion: Signal<DeviceMotionSample>,
318    /// Shared reference-counted context lifetime owner storing clock, fullscreen depth, and owned signals disposed on final drop.
319    #[debug(skip)]
320    #[get(skip)]
321    #[get_mut(skip)]
322    #[set(skip)]
323    pub(crate) shared: Rc<ContextShared>,
324}
325
326/// Shared interior-mutable runtime metadata and signal ownership table disposed automatically when the last `Context` clone drops.
327#[derive(Debug)]
328pub(crate) struct ContextShared {
329    /// Nested immersive fullscreen entry counter enabling balanced RAII enter/exit calls.
330    pub(crate) fullscreen_depth: Cell<u32>,
331    /// Monotonic runtime clock in milliseconds advanced deterministically by `App` frame steps.
332    pub(crate) clock_ms: Cell<u64>,
333    /// Detached reactive signals created by `Context::new` and disposed on final drop to guarantee zero lifecycle leak.
334    pub(crate) owned_signals: [SignalId; 24],
335    /// Reactive runtime epoch active when `owned_signals` were allocated.
336    pub(crate) runtime_epoch: u64,
337}
338
339/// RAII scope guard restoring the previous ambient `Context` on drop.
340#[derive(CustomDebug, New)]
341pub struct ContextGuard {
342    /// Previously bound ambient context to restore when leaving scope.
343    pub(crate) previous: Option<Context>,
344}