mod close_request;
mod commit;
mod focus_recovery;
mod focus_traversal;
mod hit;
mod interaction;
mod light;
mod overlay;
pub mod pointer;
pub mod top_layer_conformance;
mod scroll;
mod snapshot;
pub mod stacking;
mod style_cascade;
#[cfg(test)]
mod style_cascade_tests;
pub(crate) use style_cascade::state_rules_of;
pub use style_cascade::{
EffectiveBox, NoResponsivePatches, ResponsivePatches, StyleCascade, state_rules_patch_box,
};
use std::cell::Cell;
use std::sync::Arc;
use nativeview_engine::InteractionTracker;
use nativeview_engine::{LayoutRect, Viewport};
use nativeview_store::{ChangeSet, NodeId, NodeStore, NvTable};
use nativeview_tables::tables::{Interaction, Scroll};
use crate::{
HeuristicTextMeasurer, ParagraphStore, SizeConstraints, StoreLayoutEngine, SurfaceMeasure,
TaffyStoreLayout, TextMeasurer, TopLayerIndex,
};
pub use close_request::CloseRequestAction;
pub use commit::{CommitHooks, CommitOutcome, NoCommitHooks};
pub use focus_traversal::{
FocusTraversalCtx, FocusTraversalOutcome, FocusTraversalPolicy, FocusTraversalScratch,
FocusTraverseFlags, FocusTraverseOutFlags, TabOrderPolicy,
};
pub use pointer::SceneGeometry;
pub use interaction::InteractionChange;
pub use hit::helpers::paragraph_content_insets;
pub use light::{PressOutcome, PressedOverlay, TopLayerPress, hide_popovers_until, top_layer_press};
pub use overlay::scene::OverlayState;
pub use overlay::resolved::{ResolvedOverlay, close_request_owner, topmost_modal_index};
pub use scroll::{ScrollChainOutcome, ScrollOffsets, push_unique_node, scroll_axes_allow};
pub use scroll::engine::{
InstantScrollPhysics, ScrollAxis, ScrollBoundsLookup, ScrollContainerCtx, ScrollDeltaResult,
ScrollInputKind, ScrollPhysicsEngine, ScrollTimestampMs, ScrollTouchPhase,
default_px_per_line, normalize_shift_wheel,
};
pub use scroll::extent::{
compute_scroll_content_size, for_each_scrollbar_thumb_geom, intersect_rects, intersects_clip,
};
pub use snapshot::{layout_snapshot_of, visual_snapshot_of};
pub use stacking::{
CS_CLIPS, CS_HAS_SCROLL, CS_IS_STICKY, CS_IS_TOP_LAYER, CS_IS_TRANSFORM, ClipScrollIdx,
ClipScrollNode, ItemCount, ItemPaintPhase, SCOPE_HAS_SCROLL,
SCOPE_IS_MODAL, SCOPE_IS_STICKY, SCOPE_IS_TOP_LAYER, SceneStackingStyleSource, ScopeIdx,
ScopeKind, StackingBucket, StackingItem, StackingNodeRole, StackingOverrides, StackingPlan,
StackingScope, StackingStyleSource, TopLayerScope, ZOrder, bucket_for_sc_z,
classify_node_sc_with_parent_display, resolve_node_sc_state,
};
pub struct LaidOutScene<L: StoreLayoutEngine = TaffyStoreLayout> {
pub(crate) layout: L,
pub(crate) viewport: Viewport,
pub(crate) text_measurer: Arc<dyn TextMeasurer>,
pub(crate) interaction: InteractionTracker,
pub(crate) interaction_epoch: u64,
pub(crate) scroll: ScrollOffsets,
pub(crate) scroll_physics: Box<dyn ScrollPhysicsEngine>,
pub(crate) overlays: OverlayState,
pub(crate) stacking_cache: Cell<StackingPlan>,
pub(crate) stacking_cache_key: Cell<StackingCacheKey>,
pub(crate) stacking_cache_has_tf_override: Cell<bool>,
pub(crate) last_layout_ran: bool,
pub(crate) commit_changed_rects: Vec<NodeId>,
pub(crate) commit_changed_marks: Vec<u32>,
pub(crate) commit_changed_epoch: u32,
pub(crate) commit_changed_override: Option<bool>,
pub(crate) focus_dfs_scratch: Vec<NodeId>,
pub(crate) focus_policy: Box<dyn FocusTraversalPolicy>,
pub(crate) focus_scratch: FocusTraversalScratch,
pub(crate) last_focus_transition: Option<(Option<NodeId>, Option<NodeId>, bool)>,
}
#[inline]
pub(crate) fn cascade_of<'a, L: StoreLayoutEngine>(
layout: &'a L,
interaction: &'a InteractionTracker,
) -> StyleCascade<'a, L> {
StyleCascade {
responsive: layout,
interaction: Some(interaction),
inert: None,
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct StackingCacheKey {
store: u64,
commit_epoch: u64,
interaction_epoch: u64,
overlay_gen: u64,
responsive_epoch: u64,
}
impl Default for LaidOutScene<TaffyStoreLayout> {
fn default() -> Self {
Self::new(TaffyStoreLayout::new())
}
}
impl<L: StoreLayoutEngine> LaidOutScene<L> {
pub fn new(layout: L) -> Self {
Self::with_text_measurer(layout, Arc::new(HeuristicTextMeasurer))
}
pub fn with_text_measurer(mut layout: L, text_measurer: Arc<dyn TextMeasurer>) -> Self {
layout.set_text_measurer(Arc::clone(&text_measurer));
Self {
layout,
viewport: Viewport::default(),
text_measurer,
interaction: InteractionTracker::new(),
interaction_epoch: 1,
scroll: ScrollOffsets::default(),
scroll_physics: Box::new(InstantScrollPhysics),
overlays: OverlayState::default(),
stacking_cache: Cell::new(StackingPlan::default()),
stacking_cache_key: Cell::new(StackingCacheKey::default()),
stacking_cache_has_tf_override: Cell::new(false),
last_layout_ran: false,
commit_changed_rects: Vec::new(),
commit_changed_marks: Vec::new(),
commit_changed_epoch: 0,
commit_changed_override: None,
focus_dfs_scratch: Vec::new(),
focus_policy: Box::new(TabOrderPolicy),
focus_scratch: FocusTraversalScratch::default(),
last_focus_transition: None,
}
}
#[inline]
pub fn layout_engine_name(&self) -> &'static str {
self.layout.name()
}
#[inline]
pub fn layout_engine(&self) -> &L {
&self.layout
}
#[inline]
pub fn layout_engine_mut(&mut self) -> &mut L {
&mut self.layout
}
#[inline]
pub fn viewport(&self) -> Viewport {
self.viewport
}
pub fn set_viewport(&mut self, viewport: Viewport) -> bool {
let changed = self.viewport != viewport;
self.viewport = viewport;
changed
}
pub fn set_media_env(&mut self, env: nativeview_tables::media::MediaEnv) -> bool {
self.layout.set_media_env(env)
}
#[inline]
pub fn text_measurer(&self) -> &Arc<dyn TextMeasurer> {
&self.text_measurer
}
pub fn set_text_measurer(&mut self, measurer: Arc<dyn TextMeasurer>) {
self.text_measurer = Arc::clone(&measurer);
self.layout.set_text_measurer(measurer);
}
pub fn swap_layout_engine<L2: StoreLayoutEngine>(mut self, mut next: L2) -> LaidOutScene<L2> {
next.set_text_measurer(Arc::clone(&self.text_measurer));
self.overlays.clear();
LaidOutScene {
layout: next,
viewport: self.viewport,
text_measurer: self.text_measurer,
interaction: self.interaction,
interaction_epoch: self.interaction_epoch,
scroll: self.scroll,
scroll_physics: self.scroll_physics,
overlays: self.overlays,
stacking_cache: self.stacking_cache,
stacking_cache_key: self.stacking_cache_key,
stacking_cache_has_tf_override: self.stacking_cache_has_tf_override,
last_layout_ran: self.last_layout_ran,
commit_changed_rects: Vec::new(),
commit_changed_marks: Vec::new(),
commit_changed_epoch: 0,
commit_changed_override: None,
focus_dfs_scratch: self.focus_dfs_scratch,
focus_policy: self.focus_policy,
focus_scratch: self.focus_scratch,
last_focus_transition: self.last_focus_transition,
}
}
#[inline]
pub fn last_layout_ran(&self) -> bool {
self.last_layout_ran
}
pub fn replace_layout_engine(&mut self, mut next: L) {
next.set_text_measurer(Arc::clone(&self.text_measurer));
self.layout = next;
self.overlays.clear();
self.commit_changed_override = None;
}
#[inline]
pub fn layout_of(&self, id: NodeId) -> Option<LayoutRect> {
self.layout.layout_of(id)
}
#[inline]
pub fn changed_rects(&self) -> Option<&[NodeId]> {
match self.commit_changed_override {
None => self.layout.changed_rects(),
Some(true) => Some(&self.commit_changed_rects),
Some(false) => None,
}
}
#[inline]
pub fn paragraphs(&self) -> Option<&ParagraphStore> {
self.layout.paragraphs()
}
#[inline]
pub fn paragraphs_mut(&mut self) -> Option<&mut ParagraphStore> {
self.layout.paragraphs_mut()
}
#[inline]
pub fn top_layer(&self) -> &TopLayerIndex {
self.layout.top_layer()
}
#[inline]
pub fn topmost_modal(&self) -> Option<NodeId> {
self.overlays.topmost_modal()
}
#[inline]
pub fn is_inert_under_modal(&self, store: &NodeStore, id: NodeId) -> bool {
self.overlays.is_inert_under_modal(store, id)
}
fn sync_changes(&mut self, store: &NodeStore, changes: &ChangeSet) {
for &id in &changes.removed {
let _ = self.interaction.remove_node_slice(id);
self.scroll.remove(id);
self.scroll_physics.cancel_node(id);
self.bump_interaction_epoch();
}
if let Some(tc) = changes.table(Scroll::slot()) {
for &id in &tc.detached {
self.scroll.remove(id);
self.scroll_physics.cancel_node(id);
}
}
for &id in changes.created.iter().chain(&changes.touched) {
if !store.is_live(id) {
continue;
}
let disabled = store
.try_row::<Interaction>(id)
.is_some_and(|i| i.disabled());
if self.interaction.is_disabled(id) != disabled {
let _ = self.interaction.set_disabled_slice(id, disabled);
self.bump_interaction_epoch();
}
}
if self.interaction.prune_disconnected(|id| store.is_connected(id)) {
self.bump_interaction_epoch();
}
if self.prune_invalid_captures(store) {
self.bump_interaction_epoch();
}
self.layout
.sync_changes_with_interaction(store, changes, Some(&self.interaction));
}
pub fn recompute_layout(&mut self, store: &NodeStore) -> bool {
self.commit_changed_override = None;
self.capture_scroll_anchors(store);
let ran = self.layout.compute_layout_with_interaction(
store,
NodeId::ROOT,
self.viewport,
Some(&self.interaction),
);
if !ran {
self.scroll.captured_anchors.clear();
}
self.last_layout_ran = ran;
ran
}
pub fn measure_root(
&mut self,
store: &NodeStore,
constraints: SizeConstraints,
ideal: bool,
) -> SurfaceMeasure {
self.layout
.measure_root(store, NodeId::ROOT, constraints, ideal)
}
pub fn reset(&mut self) {
self.layout.reset();
self.interaction.clear();
self.bump_interaction_epoch();
self.scroll.clear();
self.scroll_physics.clear();
self.overlays.clear();
let mut plan = self.stacking_cache.take();
plan.clear();
self.stacking_cache.set(plan);
self.stacking_cache_key.set(StackingCacheKey::default());
self.stacking_cache_has_tf_override.set(false);
self.last_layout_ran = false;
self.commit_changed_rects.clear();
self.commit_changed_marks.clear();
self.commit_changed_epoch = 0;
self.commit_changed_override = None;
self.focus_dfs_scratch.clear();
self.focus_scratch.clear();
self.last_focus_transition = None;
}
#[inline]
pub fn hit_scratch_stats(&self) -> (usize, usize) {
let plan = self.stacking_cache.take();
let stats = (plan.ordered_items.len(), plan.ordered_items.capacity());
self.stacking_cache.set(plan);
stats
}
}