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