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