1mod close_request;
10mod commit;
11mod focus_recovery;
12mod focus_traversal;
13mod hit;
14mod interaction;
15mod light;
16mod overlay;
17pub mod pointer;
18pub mod top_layer_conformance;
19mod scroll;
20mod snapshot;
21pub mod stacking;
22mod style_cascade;
23#[cfg(test)]
24mod style_cascade_tests;
25pub(crate) use style_cascade::state_rules_of;
26pub use style_cascade::{
27 EffectiveBox, NoResponsivePatches, ResponsivePatches, StyleCascade, state_rules_patch_box,
28};
29
30use std::cell::Cell;
31use std::sync::Arc;
32
33use nativeview_engine::InteractionTracker;
34use nativeview_engine::{LayoutRect, Viewport};
35use nativeview_store::{ChangeSet, NodeId, NodeStore, NvTable};
36use nativeview_tables::tables::{Interaction, Scroll};
37
38use crate::{
39 HeuristicTextMeasurer, ParagraphStore, SizeConstraints, StoreLayoutEngine, SurfaceMeasure,
40 TaffyStoreLayout, TextMeasurer, TopLayerIndex,
41};
42
43pub use close_request::CloseRequestAction;
44pub use commit::{CommitHooks, CommitOutcome, NoCommitHooks};
45pub use focus_traversal::{
46 FocusTraversalCtx, FocusTraversalOutcome, FocusTraversalPolicy, FocusTraversalScratch,
47 FocusTraverseFlags, FocusTraverseOutFlags, TabOrderPolicy,
48};
49pub use pointer::SceneGeometry;
50pub use interaction::InteractionChange;
51pub use hit::helpers::paragraph_content_insets;
52pub use light::{PressOutcome, PressedOverlay, TopLayerPress, hide_popovers_until, top_layer_press};
53pub use overlay::scene::OverlayState;
54pub use overlay::resolved::{ResolvedOverlay, close_request_owner, topmost_modal_index};
55pub use scroll::{ScrollChainOutcome, ScrollOffsets, push_unique_node, scroll_axes_allow};
56pub use scroll::engine::{
57 InstantScrollPhysics, ScrollAxis, ScrollBoundsLookup, ScrollContainerCtx, ScrollDeltaResult,
58 ScrollInputKind, ScrollPhysicsEngine, ScrollTimestampMs, ScrollTouchPhase,
59 default_px_per_line, normalize_shift_wheel,
60};
61pub use scroll::extent::{
62 compute_scroll_content_size, for_each_scrollbar_thumb_geom, intersect_rects, intersects_clip,
63};
64pub use snapshot::{layout_snapshot_of, visual_snapshot_of};
65pub use stacking::{
66 CS_CLIPS, CS_HAS_SCROLL, CS_IS_STICKY, CS_IS_TOP_LAYER, CS_IS_TRANSFORM, ClipScrollIdx,
67 ClipScrollNode, ItemCount, ItemPaintPhase, SCOPE_HAS_SCROLL,
68 SCOPE_IS_MODAL, SCOPE_IS_STICKY, SCOPE_IS_TOP_LAYER, SceneStackingStyleSource, ScopeIdx,
69 ScopeKind, StackingBucket, StackingItem, StackingNodeRole, StackingOverrides, StackingPlan,
70 StackingScope, StackingStyleSource, TopLayerScope, ZOrder, bucket_for_sc_z,
71 classify_node_sc_with_parent_display, resolve_node_sc_state,
72};
73
74pub struct LaidOutScene<L: StoreLayoutEngine = TaffyStoreLayout> {
78 pub(crate) layout: L,
79 pub(crate) viewport: Viewport,
80 pub(crate) text_measurer: Arc<dyn TextMeasurer>,
81 pub(crate) interaction: InteractionTracker,
82 pub(crate) interaction_epoch: u64,
83 pub(crate) scroll: ScrollOffsets,
84 pub(crate) scroll_physics: Box<dyn ScrollPhysicsEngine>,
85 pub(crate) overlays: OverlayState,
86 pub(crate) stacking_cache: Cell<StackingPlan>,
89 pub(crate) stacking_cache_key: Cell<StackingCacheKey>,
90 pub(crate) stacking_cache_has_tf_override: Cell<bool>,
91 pub(crate) last_layout_ran: bool,
92 pub(crate) commit_changed_rects: Vec<NodeId>,
93 pub(crate) commit_changed_marks: Vec<u32>,
94 pub(crate) commit_changed_epoch: u32,
95 pub(crate) commit_changed_override: Option<bool>,
96 pub(crate) focus_dfs_scratch: Vec<NodeId>,
97 pub(crate) focus_policy: Box<dyn FocusTraversalPolicy>,
98 pub(crate) focus_scratch: FocusTraversalScratch,
99 pub(crate) last_focus_transition: Option<(Option<NodeId>, Option<NodeId>, bool)>,
100}
101
102#[inline]
105pub(crate) fn cascade_of<'a, L: StoreLayoutEngine>(
106 layout: &'a L,
107 interaction: &'a InteractionTracker,
108) -> StyleCascade<'a, L> {
109 StyleCascade {
110 responsive: layout,
111 interaction: Some(interaction),
112 inert: None,
113 }
114}
115
116#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
118pub(crate) struct StackingCacheKey {
119 store: u64,
120 commit_epoch: u64,
121 interaction_epoch: u64,
122 overlay_gen: u64,
123 responsive_epoch: u64,
124}
125
126impl Default for LaidOutScene<TaffyStoreLayout> {
127 fn default() -> Self {
128 Self::new(TaffyStoreLayout::new())
129 }
130}
131
132impl<L: StoreLayoutEngine> LaidOutScene<L> {
133 pub fn new(layout: L) -> Self {
135 Self::with_text_measurer(layout, Arc::new(HeuristicTextMeasurer))
136 }
137
138 pub fn with_text_measurer(mut layout: L, text_measurer: Arc<dyn TextMeasurer>) -> Self {
140 layout.set_text_measurer(Arc::clone(&text_measurer));
141 Self {
142 layout,
143 viewport: Viewport::default(),
144 text_measurer,
145 interaction: InteractionTracker::new(),
146 interaction_epoch: 1,
147 scroll: ScrollOffsets::default(),
148 scroll_physics: Box::new(InstantScrollPhysics),
149 overlays: OverlayState::default(),
150 stacking_cache: Cell::new(StackingPlan::default()),
151 stacking_cache_key: Cell::new(StackingCacheKey::default()),
152 stacking_cache_has_tf_override: Cell::new(false),
153 last_layout_ran: false,
154 commit_changed_rects: Vec::new(),
155 commit_changed_marks: Vec::new(),
156 commit_changed_epoch: 0,
157 commit_changed_override: None,
158 focus_dfs_scratch: Vec::new(),
159 focus_policy: Box::new(TabOrderPolicy),
160 focus_scratch: FocusTraversalScratch::default(),
161 last_focus_transition: None,
162 }
163 }
164
165 #[inline]
166 pub fn layout_engine_name(&self) -> &'static str {
167 self.layout.name()
168 }
169
170 #[inline]
171 pub fn layout_engine(&self) -> &L {
172 &self.layout
173 }
174
175 #[inline]
176 pub fn layout_engine_mut(&mut self) -> &mut L {
177 &mut self.layout
178 }
179
180 #[inline]
181 pub fn viewport(&self) -> Viewport {
182 self.viewport
183 }
184
185 pub fn set_viewport(&mut self, viewport: Viewport) -> bool {
187 let changed = self.viewport != viewport;
188 self.viewport = viewport;
189 changed
190 }
191
192 pub fn set_media_env(&mut self, env: nativeview_tables::media::MediaEnv) -> bool {
196 self.layout.set_media_env(env)
197 }
198
199 #[inline]
200 pub fn text_measurer(&self) -> &Arc<dyn TextMeasurer> {
201 &self.text_measurer
202 }
203
204 pub fn set_text_measurer(&mut self, measurer: Arc<dyn TextMeasurer>) {
205 self.text_measurer = Arc::clone(&measurer);
206 self.layout.set_text_measurer(measurer);
207 }
208
209 pub fn swap_layout_engine<L2: StoreLayoutEngine>(mut self, mut next: L2) -> LaidOutScene<L2> {
213 next.set_text_measurer(Arc::clone(&self.text_measurer));
214 self.overlays.clear();
215 LaidOutScene {
216 layout: next,
217 viewport: self.viewport,
218 text_measurer: self.text_measurer,
219 interaction: self.interaction,
220 interaction_epoch: self.interaction_epoch,
221 scroll: self.scroll,
222 scroll_physics: self.scroll_physics,
223 overlays: self.overlays,
224 stacking_cache: self.stacking_cache,
225 stacking_cache_key: self.stacking_cache_key,
226 stacking_cache_has_tf_override: self.stacking_cache_has_tf_override,
227 last_layout_ran: self.last_layout_ran,
228 commit_changed_rects: Vec::new(),
229 commit_changed_marks: Vec::new(),
230 commit_changed_epoch: 0,
231 commit_changed_override: None,
232 focus_dfs_scratch: self.focus_dfs_scratch,
233 focus_policy: self.focus_policy,
234 focus_scratch: self.focus_scratch,
235 last_focus_transition: self.last_focus_transition,
236 }
237 }
238
239 #[inline]
241 pub fn last_layout_ran(&self) -> bool {
242 self.last_layout_ran
243 }
244
245 pub fn replace_layout_engine(&mut self, mut next: L) {
248 next.set_text_measurer(Arc::clone(&self.text_measurer));
249 self.layout = next;
250 self.overlays.clear();
251 self.commit_changed_override = None;
252 }
253
254 #[inline]
256 pub fn layout_of(&self, id: NodeId) -> Option<LayoutRect> {
257 self.layout.layout_of(id)
258 }
259
260 #[inline]
265 pub fn changed_rects(&self) -> Option<&[NodeId]> {
266 match self.commit_changed_override {
267 None => self.layout.changed_rects(),
268 Some(true) => Some(&self.commit_changed_rects),
269 Some(false) => None,
270 }
271 }
272
273 #[inline]
275 pub fn paragraphs(&self) -> Option<&ParagraphStore> {
276 self.layout.paragraphs()
277 }
278
279 #[inline]
281 pub fn paragraphs_mut(&mut self) -> Option<&mut ParagraphStore> {
282 self.layout.paragraphs_mut()
283 }
284
285 #[inline]
287 pub fn top_layer(&self) -> &TopLayerIndex {
288 self.layout.top_layer()
289 }
290
291 #[inline]
294 pub fn topmost_modal(&self) -> Option<NodeId> {
295 self.overlays.topmost_modal()
296 }
297
298 #[inline]
302 pub fn is_inert_under_modal(&self, store: &NodeStore, id: NodeId) -> bool {
303 self.overlays.is_inert_under_modal(store, id)
304 }
305
306 fn sync_changes(&mut self, store: &NodeStore, changes: &ChangeSet) {
309 for &id in &changes.removed {
310 let _ = self.interaction.remove_node_slice(id);
311 self.scroll.remove(id);
312 self.scroll_physics.cancel_node(id);
313 self.bump_interaction_epoch();
314 }
315 if let Some(tc) = changes.table(Scroll::slot()) {
316 for &id in &tc.detached {
317 self.scroll.remove(id);
318 self.scroll_physics.cancel_node(id);
319 }
320 }
321 for &id in changes.created.iter().chain(&changes.touched) {
322 if !store.is_live(id) {
323 continue;
324 }
325 let disabled = store
326 .try_row::<Interaction>(id)
327 .is_some_and(|i| i.disabled());
328 if self.interaction.is_disabled(id) != disabled {
329 let _ = self.interaction.set_disabled_slice(id, disabled);
330 self.bump_interaction_epoch();
331 }
332 }
333 if self.interaction.prune_disconnected(|id| store.is_connected(id)) {
334 self.bump_interaction_epoch();
335 }
336 if self.prune_invalid_captures(store) {
337 self.bump_interaction_epoch();
338 }
339 self.layout
340 .sync_changes_with_interaction(store, changes, Some(&self.interaction));
341 }
342
343 pub fn recompute_layout(&mut self, store: &NodeStore) -> bool {
346 self.commit_changed_override = None;
347 self.capture_scroll_anchors(store);
348 let ran = self.layout.compute_layout_with_interaction(
349 store,
350 NodeId::ROOT,
351 self.viewport,
352 Some(&self.interaction),
353 );
354 if !ran {
355 self.scroll.captured_anchors.clear();
356 }
357 self.last_layout_ran = ran;
358 ran
359 }
360
361 pub fn measure_root(
363 &mut self,
364 store: &NodeStore,
365 constraints: SizeConstraints,
366 ideal: bool,
367 ) -> SurfaceMeasure {
368 self.layout
369 .measure_root(store, NodeId::ROOT, constraints, ideal)
370 }
371
372 pub fn reset(&mut self) {
374 self.layout.reset();
375 self.interaction.clear();
376 self.bump_interaction_epoch();
377 self.scroll.clear();
378 self.scroll_physics.clear();
379 self.overlays.clear();
380 let mut plan = self.stacking_cache.take();
381 plan.clear();
382 self.stacking_cache.set(plan);
383 self.stacking_cache_key.set(StackingCacheKey::default());
384 self.stacking_cache_has_tf_override.set(false);
385 self.last_layout_ran = false;
386 self.commit_changed_rects.clear();
387 self.commit_changed_marks.clear();
388 self.commit_changed_epoch = 0;
389 self.commit_changed_override = None;
390 self.focus_dfs_scratch.clear();
391 self.focus_scratch.clear();
392 self.last_focus_transition = None;
393 }
394
395 #[inline]
397 pub fn hit_scratch_stats(&self) -> (usize, usize) {
398 let plan = self.stacking_cache.take();
399 let stats = (plan.ordered_items.len(), plan.ordered_items.capacity());
400 self.stacking_cache.set(plan);
401 stats
402 }
403}