use std::collections::HashMap;
use nativeview_store::{ChangeSet, NodeId, NodeStore, NvTable};
use nativeview_tables::tables::{Overflow, Scroll, ScrollAxes, Visual};
use crate::StoreLayoutEngine;
mod anchor;
mod chain;
pub use chain::push_unique_node;
pub(crate) mod engine;
pub(crate) mod extent;
mod into_view;
use extent::compute_scroll_content_size;
use super::LaidOutScene;
use anchor::CapturedScrollAnchor;
use engine::ScrollPhysicsEngine;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SeqSlot {
node: NodeId,
seq: u32,
}
const EMPTY_SEQ_SLOT: SeqSlot = SeqSlot {
node: NodeId::NULL,
seq: 0,
};
#[inline]
pub fn scroll_axes_allow(store: &NodeStore, id: NodeId) -> (bool, bool) {
match store.try_row::<Scroll>(id).map(|s| s.axes()) {
Some(ScrollAxes::Vertical) => (false, true),
Some(ScrollAxes::Horizontal) => (true, false),
Some(ScrollAxes::Both) => (true, true),
Some(ScrollAxes::None) => (false, false),
None => store.try_row::<Visual>(id).map_or((false, false), |v| {
(
matches!(v.overflow_x(), Overflow::Scroll | Overflow::Auto),
matches!(v.overflow_y(), Overflow::Scroll | Overflow::Auto),
)
}),
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ScrollChainOutcome {
pub consumed_x: bool,
pub consumed_y: bool,
pub any_visual_changed: bool,
pub any_clamped_changed: bool,
pub active_physics: bool,
}
#[derive(Debug, Default, Clone)]
pub struct ScrollOffsets {
offsets: HashMap<NodeId, (f32, f32)>,
last_requested_seq: Vec<SeqSlot>,
reconcile_scratch: Vec<ScrollReconcileEntry>,
pub(crate) changed_scratch: Vec<NodeId>,
pub(crate) commit_changed: Vec<NodeId>,
pub(crate) captured_anchors: Vec<CapturedScrollAnchor>,
pub(crate) chain_changed_scratch: Vec<NodeId>,
pub(crate) chain_clamped_scratch: Vec<NodeId>,
pub(crate) step_visual_scratch: Vec<NodeId>,
pub(crate) step_clamped_scratch: Vec<NodeId>,
moved: bool,
}
impl ScrollOffsets {
#[inline]
pub(crate) fn take_moved(&mut self) -> bool {
core::mem::take(&mut self.moved)
}
#[inline]
pub fn get(&self, id: NodeId) -> (f32, f32) {
self.offsets.get(&id).copied().unwrap_or((0.0, 0.0))
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (NodeId, (f32, f32))> + '_ {
self.offsets.iter().map(|(&id, &xy)| (id, xy))
}
#[inline]
pub fn contains(&self, id: NodeId) -> bool {
self.offsets.contains_key(&id)
}
#[inline]
pub(crate) fn offsets_is_empty(&self) -> bool {
self.offsets.is_empty()
}
#[inline]
pub fn last_requested_seq(&self, id: NodeId) -> u32 {
self.last_requested_seq
.get(id.index())
.copied()
.filter(|slot| slot.node == id)
.map_or(0, |slot| slot.seq)
}
#[inline]
fn set_last_requested_seq(&mut self, id: NodeId, seq: u32) {
let idx = id.index();
if seq == 0 {
if let Some(slot) = self.last_requested_seq.get_mut(idx)
&& slot.node == id
{
*slot = EMPTY_SEQ_SLOT;
}
return;
}
if idx >= self.last_requested_seq.len() {
self.last_requested_seq.resize(idx + 1, EMPTY_SEQ_SLOT);
}
self.last_requested_seq[idx] = SeqSlot { node: id, seq };
}
#[inline]
pub fn last_changed(&self) -> &[NodeId] {
&self.changed_scratch
}
#[inline]
pub fn set_raw(&mut self, id: NodeId, x: f32, y: f32) {
if self.offsets.insert(id, (x, y)) != Some((x, y)) {
self.moved = true;
}
}
#[inline]
pub fn remove(&mut self, id: NodeId) {
self.offsets.remove(&id);
if let Some(slot) = self.last_requested_seq.get_mut(id.index())
&& slot.node == id
{
*slot = EMPTY_SEQ_SLOT;
}
}
#[inline]
pub fn clear(&mut self) {
self.offsets.clear();
self.last_requested_seq.fill(EMPTY_SEQ_SLOT);
self.reconcile_scratch.clear();
self.changed_scratch.clear();
self.captured_anchors.clear();
self.chain_changed_scratch.clear();
self.chain_clamped_scratch.clear();
self.step_visual_scratch.clear();
self.step_clamped_scratch.clear();
}
#[inline]
pub fn visual_shift(&self, store: &NodeStore, id: NodeId) -> (f32, f32) {
let (sx, sy) = self.get(id);
Self::visual_shift_with(store, id, sx, sy)
}
#[inline]
pub fn visual_shift_with(store: &NodeStore, id: NodeId, sx: f32, sy: f32) -> (f32, f32) {
match store
.try_row::<Scroll>(id)
.and_then(|scroll| scroll.virtual_top_spacer())
.filter(|&v| v.is_finite() && v > 0.0)
{
Some(top_spacer) => (sx, (sy - top_spacer).max(0.0)),
None => (sx, sy),
}
}
}
#[derive(Debug, Clone, Copy)]
struct ScrollReconcileEntry {
id: NodeId,
x: f32,
y: f32,
max_x: f32,
max_y: f32,
anchor_dx: f32,
anchor_dy: f32,
seq: u32,
explicit_seek: bool,
changed: bool,
}
impl<L: StoreLayoutEngine> LaidOutScene<L> {
#[inline]
pub fn scroll_offsets(&self) -> &ScrollOffsets {
&self.scroll
}
#[inline]
pub fn last_reconciled_scroll_nodes(&self) -> &[NodeId] {
self.scroll.last_changed()
}
#[inline]
pub fn last_scroll_chain_changed(&self) -> &[NodeId] {
&self.scroll.chain_changed_scratch
}
#[inline]
pub fn last_scroll_chain_clamped_changed(&self) -> &[NodeId] {
&self.scroll.chain_clamped_scratch
}
#[inline]
pub fn last_physics_visual_changed(&self) -> &[NodeId] {
&self.scroll.step_visual_scratch
}
#[inline]
pub fn last_physics_clamped_changed(&self) -> &[NodeId] {
&self.scroll.step_clamped_scratch
}
#[inline]
pub fn set_scroll_offset_raw(&mut self, id: NodeId, dx: f32, dy: f32) {
self.scroll.set_raw(id, dx, dy);
}
#[inline]
pub fn scroll_offset(&self, id: NodeId) -> (f32, f32) {
self.scroll.get(id)
}
#[inline]
pub fn scroll_overscroll(&self, id: NodeId) -> (f32, f32) {
self.scroll_physics.overscroll(id)
}
#[inline]
pub fn visual_scroll_shift(&self, store: &NodeStore, id: NodeId) -> (f32, f32) {
self.scroll.visual_shift(store, id)
}
#[inline]
pub fn compositor_scroll_shift(&self, store: &NodeStore, id: NodeId) -> (f32, f32) {
let (vx, vy) = self.scroll.visual_shift(store, id);
let (ox, oy) = self.scroll_physics.overscroll(id);
(vx + ox, vy + oy)
}
#[inline]
pub(crate) fn compositor_scroll_offset(&self, id: NodeId) -> (f32, f32) {
let (sx, sy) = self.scroll.get(id);
let (ox, oy) = self.scroll_physics.overscroll(id);
(sx + ox, sy + oy)
}
#[inline]
pub fn set_scroll_physics(&mut self, engine: Box<dyn ScrollPhysicsEngine>) {
self.scroll_physics = engine;
}
#[inline]
pub fn scroll_physics(&self) -> &dyn ScrollPhysicsEngine {
self.scroll_physics.as_ref()
}
#[inline]
pub fn scroll_physics_mut(&mut self) -> &mut dyn ScrollPhysicsEngine {
self.scroll_physics.as_mut()
}
#[inline]
pub fn scroll_px_per_line(&self) -> f32 {
self.scroll_physics.px_per_line()
}
pub fn set_scroll_reduced_motion(&mut self, reduced: bool) -> bool {
self.scroll.step_visual_scratch.clear();
self.scroll.step_clamped_scratch.clear();
let mut out_visual = std::mem::take(&mut self.scroll.step_visual_scratch);
let mut out_clamped = std::mem::take(&mut self.scroll.step_clamped_scratch);
let snapped = self.scroll_physics.set_reduced_motion(
reduced,
&mut self.scroll,
&mut out_visual,
&mut out_clamped,
);
self.scroll.step_visual_scratch = out_visual;
self.scroll.step_clamped_scratch = out_clamped;
snapped
}
#[inline]
pub fn set_scroll_synthetic_momentum(&mut self, enabled: bool) {
self.scroll_physics.set_synthetic_momentum(enabled);
}
#[inline]
pub fn has_active_scroll_physics(&self) -> bool {
self.scroll_physics.has_active()
}
pub fn scroll_content_size(&self, store: &NodeStore, node_id: NodeId) -> Option<(f32, f32)> {
let cascade = self.style_cascade();
compute_scroll_content_size(
store,
|id| self.layout.layout_of(id),
|id| cascade.display(store, id),
node_id,
)
}
#[inline]
pub(crate) fn scroll_bounds(
&self,
store: &NodeStore,
id: NodeId,
) -> Option<(f32, f32, f32, f32)> {
let rect = self.layout.layout_of(id)?;
let (cw, ch) = self
.scroll_content_size(store, id)
.unwrap_or((rect.width, rect.height));
let (allow_x, allow_y) = scroll_axes_allow(store, id);
let max_x = if allow_x {
(cw - rect.width).max(0.0)
} else {
0.0
};
let max_y = if allow_y {
(ch - rect.height).max(0.0)
} else {
0.0
};
Some((rect.width.max(1.0), rect.height.max(1.0), max_x, max_y))
}
#[inline]
pub fn cancel_scroll_physics(&mut self, node_id: NodeId) -> bool {
let (ox, oy) = self.scroll_physics.overscroll(node_id);
self.scroll_physics.cancel_node(node_id);
ox.abs() > 1e-4 || oy.abs() > 1e-4
}
pub fn set_scroll_offset_clamped(
&mut self,
store: &NodeStore,
node_id: NodeId,
x: f32,
y: f32,
) -> (f32, f32, bool) {
let Some((_, _, max_x, max_y)) = self.scroll_bounds(store, node_id) else {
return (0.0, 0.0, false);
};
self.set_scroll_offset_clamped_max(node_id, x, y, max_x, max_y)
}
#[allow(clippy::too_many_arguments)]
pub fn set_scroll_offset_clamped_with_bounds(
&mut self,
store: &NodeStore,
node_id: NodeId,
x: f32,
y: f32,
vw: f32,
vh: f32,
cw: f32,
ch: f32,
) -> (f32, f32, bool) {
let (allow_x, allow_y) = scroll_axes_allow(store, node_id);
let max_x = if allow_x { (cw - vw).max(0.0) } else { 0.0 };
let max_y = if allow_y { (ch - vh).max(0.0) } else { 0.0 };
self.set_scroll_offset_clamped_max(node_id, x, y, max_x, max_y)
}
#[inline]
fn set_scroll_offset_clamped_max(
&mut self,
node_id: NodeId,
x: f32,
y: f32,
max_x: f32,
max_y: f32,
) -> (f32, f32, bool) {
let clamped_x = if x.is_finite() { x.clamp(0.0, max_x) } else { 0.0 };
let clamped_y = if y.is_finite() { y.clamp(0.0, max_y) } else { 0.0 };
let had_overscroll = self.cancel_scroll_physics(node_id);
let prev = self.scroll.get(node_id);
self.scroll.set_raw(node_id, clamped_x, clamped_y);
let changed = had_overscroll
|| (prev.0 - clamped_x).abs() > 1e-4
|| (prev.1 - clamped_y).abs() > 1e-4;
(clamped_x, clamped_y, changed)
}
pub fn apply_scroll_delta(
&mut self,
store: &NodeStore,
node_id: NodeId,
dx: f32,
dy: f32,
) -> (f32, f32, bool) {
let (cur_x, cur_y) = self.scroll.get(node_id);
let nx = cur_x + if dx.is_finite() { dx } else { 0.0 };
let ny = cur_y + if dy.is_finite() { dy } else { 0.0 };
self.set_scroll_offset_clamped(store, node_id, nx, ny)
}
#[inline]
pub(crate) fn reconcile_scroll_offsets_if_needed(
&mut self,
store: &NodeStore,
changes: &ChangeSet,
laid_out: bool,
) -> &[NodeId] {
self.scroll.changed_scratch.clear();
if let Some(tc) = changes.table(Scroll::slot()) {
for &id in &tc.detached {
self.scroll.remove(id);
self.scroll_physics.cancel_node(id);
}
}
if !laid_out && changes.removed.is_empty() && changes.table(Scroll::slot()).is_none() {
self.scroll.captured_anchors.clear();
return &self.scroll.changed_scratch;
}
self.reconcile_scroll_offsets(store)
}
pub fn reconcile_scroll_offsets(&mut self, store: &NodeStore) -> &[NodeId] {
self.scroll.changed_scratch.clear();
if !self.scroll.offsets.is_empty() {
let last_seq = &mut self.scroll.last_requested_seq;
let physics = self.scroll_physics.as_mut();
self.scroll.offsets.retain(|&id, _| {
let keep = store.is_live(id) && store.has::<Scroll>(id);
if !keep {
physics.cancel_node(id);
if let Some(slot) = last_seq.get_mut(id.index())
&& slot.node == id
{
*slot = EMPTY_SEQ_SLOT;
}
}
keep
});
}
self.scroll_physics.prune_missing(store);
let Some(nativeview_store::TableStorage {
owners, row_count, ..
}) = store.table_storage(Scroll::slot())
else {
self.scroll.captured_anchors.clear();
return &self.scroll.changed_scratch;
};
if row_count == 0 {
self.scroll.captured_anchors.clear();
return &self.scroll.changed_scratch;
}
let mut updates = std::mem::take(&mut self.scroll.reconcile_scratch);
updates.clear();
if !owners.is_empty() {
for &id in &owners[..row_count.min(owners.len())] {
self.collect_scroll_node(store, id, &mut updates);
}
} else {
for idx in 1..store.capacity() {
if let Some(id) = store.id_at(idx) {
self.collect_scroll_node(store, id, &mut updates);
}
}
}
for entry in updates.drain(..) {
self.scroll.set_last_requested_seq(entry.id, entry.seq);
if entry.x != 0.0 || entry.y != 0.0 || self.scroll.contains(entry.id) {
self.scroll.set_raw(entry.id, entry.x, entry.y);
}
self.scroll_physics.reconcile_node(
entry.id,
(entry.x, entry.y),
(entry.max_x, entry.max_y),
(entry.anchor_dx, entry.anchor_dy),
entry.explicit_seek,
);
if entry.changed {
self.scroll.changed_scratch.push(entry.id);
}
}
self.scroll.reconcile_scratch = updates;
self.scroll.captured_anchors.clear();
&self.scroll.changed_scratch
}
fn collect_scroll_node(
&self,
store: &NodeStore,
id: NodeId,
out: &mut Vec<ScrollReconcileEntry>,
) {
if !store.is_live(id) {
return;
}
let Some(scroll) = store.try_row::<Scroll>(id) else {
return;
};
let Some(rect) = self.layout.layout_of(id) else {
return;
};
let (cw, ch) = self
.scroll_content_size(store, id)
.unwrap_or((rect.width, rect.height));
let (allow_x, allow_y) = scroll_axes_allow(store, id);
let max_x = if allow_x {
(cw - rect.width).max(0.0)
} else {
0.0
};
let max_y = if allow_y {
(ch - rect.height).max(0.0)
} else {
0.0
};
let (prev_x, prev_y) = self.scroll.get(id);
let (mut sx, mut sy) = (prev_x, prev_y);
let seq = scroll.requested_scroll_seq();
let prev_seq = self.scroll.last_requested_seq(id);
let explicit_seek = seq != 0 && seq != prev_seq;
let mut anchor_dx = 0.0f32;
let mut anchor_dy = 0.0f32;
if explicit_seek {
if let Some(req_x) = scroll.requested_scroll_x().filter(|v| v.is_finite()) {
sx = req_x;
}
if let Some(req_y) = scroll.requested_scroll_y().filter(|v| v.is_finite()) {
sy = req_y;
}
} else if !self.scroll.captured_anchors.is_empty() {
let (dx, dy) = self.scroll_anchor_delta(store, id, rect, allow_x, allow_y);
if dx != 0.0 || dy != 0.0 {
sx += dx;
sy += dy;
anchor_dx = dx;
anchor_dy = dy;
}
}
let nx = sx.clamp(0.0, max_x);
let ny = sy.clamp(0.0, max_y);
let (ox, oy) = self.scroll_physics.overscroll(id);
let had_overscroll = ox.abs() > 1e-4 || oy.abs() > 1e-4;
let changed = (nx - prev_x).abs() > 1e-4
|| (ny - prev_y).abs() > 1e-4
|| (explicit_seek && had_overscroll);
out.push(ScrollReconcileEntry {
id,
x: nx,
y: ny,
max_x,
max_y,
anchor_dx,
anchor_dy,
seq,
explicit_seek,
changed,
});
}
}