Skip to main content

nativeview_scroll_physics/
table.rs

1//! Per-container sparse [`ScrollPhysics`] table (`nv.scroll_physics`) and typed override enums.
2//!
3//! Containers without an attached [`ScrollPhysics`] row pay **0 bytes** of memory and inherit the
4//! active surface physics engine's defaults (while still honoring `Scroll::paging` and
5//! `Scroll::bounce`).
6
7use nativeview_store::{NvTable, WordEnum};
8
9/// Per-container boundary resistance override for [`ScrollPhysics`].
10#[repr(u8)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, WordEnum)]
12pub enum ScrollBoundaryMode {
13    /// Inherit the surface physics engine's boundary behavior (and `Scroll::bounce`).
14    #[default]
15    Inherit = 0,
16    /// Hard clamp at `[0..=max]` with zero rubber-band overscroll.
17    Clamp = 1,
18    /// Enable rubber-band overscroll bounce (still subject to `OverscrollBehavior::None`).
19    Bounce = 2,
20}
21
22/// Per-container snap-point mode override for [`ScrollPhysics`].
23#[repr(u8)]
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, WordEnum)]
25pub enum ScrollSnapMode {
26    /// Inherit the surface physics engine's snap behavior (auto-honoring `Scroll::paging`).
27    #[default]
28    Inherit = 0,
29    /// Free scrolling with snapping disabled (overrides both engine snap and `Scroll::paging`).
30    None = 1,
31    /// Snap to multiples of the container's viewport extent (`PageSnap`).
32    Page = 2,
33    /// Snap to multiples of [`ScrollPhysics::snap_extent`] (falling back to viewport when `<= 0`).
34    FixedExtent = 3,
35}
36
37/// Optional per-container scroll physics overrides (`nv.scroll_physics`, sparse, 0 B when absent).
38///
39/// Attach via `nv_table:` on any scroll container (`VirtualList`, paging carousel, bottom sheet)
40/// to customize bounce, snapping, deceleration, return spring stiffness, or smooth-wheel easing
41/// independently from ancestor containers.
42#[derive(NvTable)]
43#[nv_table(name = "nv.scroll_physics", storage = "sparse", flags = "publish")]
44pub struct ScrollPhysics {
45    /// Boundary resistance override (`Inherit | Clamp | Bounce`).
46    #[effect(composite)]
47    pub boundary: ScrollBoundaryMode,
48    /// Snap-point mode override (`Inherit | None | Page | FixedExtent`).
49    #[effect(composite)]
50    pub snap_mode: ScrollSnapMode,
51    /// Item extent in logical px for `ScrollSnapMode::FixedExtent` (`<= 0.0` = inherit/viewport).
52    #[effect(composite)]
53    pub snap_extent: f32,
54    /// Per-millisecond momentum deceleration factor in `(0.0, 1.0)` (`<= 0.0` = inherit).
55    #[effect(composite)]
56    pub deceleration_per_ms: f32,
57    /// Critically-damped return/snap spring angular frequency in rad/s (`<= 0.0` = inherit).
58    #[effect(composite)]
59    pub spring_omega: f32,
60    /// Smooth-wheel exponential easing time constant in seconds (`< 0.0` = inherit; `0.0` = instant).
61    #[effect(composite)]
62    #[default(-1.0)]
63    pub wheel_smooth_tau: f32,
64}
65
66const _: () = assert!(ScrollPhysics::DESC.defaults.len() == 5);