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