Skip to main content

Module scroll

Module scroll 

Source
Expand description

LVGL-style scroll state + math, living next to layout.

Widgets in zest are transient: the Runtime rebuilds the whole tree from view(&self) every frame, so a container cannot itself remember a drag origin or a fling velocity. All cross-frame scroll/gesture state therefore lives in a single host-owned value — ScrollState — which the screen stores as a field and passes by reference into view. Containers only read it during measure/arrange/ draw and emit ScrollMsg in handle_touch; the owned copy is mutated only in update() via ScrollState::apply and ScrollState::tick.

Touch events carry no timestamp (see event), so velocity for fling is sampled in update() from embassy_time::Instant deltas the caller passes as now_ms, never inside handle_touch. Momentum is driven by a self-rescheduling tick_task (a 16 ms Timer-delayed Task), not a subscription, so the animation cleanly stops when it settles.

The math deliberately avoids libm: the tap-vs-scroll threshold uses Manhattan distance, and friction is a per-frame-constant multiply (no powf/sqrt), valid because the tick dt is approximately constant at 16 ms.

Structs§

ScrollState
Cross-frame scroll/gesture/velocity state owned by the host screen.

Enums§

GesturePhase
Gesture / animation phase of a ScrollState.
ScrollDirection
Which axes a container scrolls on.
ScrollMsg
Message a scrollable container emits in handle_touch; the host applies it in update(). Carries the geometry (and snap lines on release) so the owned ScrollState can integrate without touching layout.
ScrollbarMode
When the scrollbar thumb is visible.
SnapMode
How scrolling settles to content boundaries.

Constants§

FRICTION
Velocity multiplier applied each animation frame during a fling.
MAX_FLING_V
Maximum fling speed (px/s, per axis) the release sample is capped to.
MIN_FLING_V
Minimum fling speed (px/s, per axis) below which a fling becomes a spring.
RUBBER_C
Rubber-band resistance coefficient: larger → stiffer over-scroll.
SCROLL_THRESHOLD
Pixel distance a finger must travel before a press is reinterpreted as a scroll (Manhattan distance, |dx| + |dy|).
SPRING_K
Spring stiffness: fraction of the remaining distance closed each frame.
TICK_MS
Animation frame period in milliseconds (~60 fps).

Functions§

tick_task
Self-rescheduling animation clock: a Task that waits one frame (~16 ms) and then yields msg, so the host’s ScrollTick arm can call ScrollState::tick and re-arm while ScrollState::is_animating.