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