pub struct ScrollState { /* private fields */ }Expand description
Scroll state for smooth, momentum-based scrolling with rubber-banding.
Keeps track of the current fractional scroll offset, velocity, and drag-to-scroll gestures. Completely separate from drawing, and generic over time: takes a time delta step to decay velocity or animate snap-back, making it deterministic and suitable for unit tests.
For a row-based viewport (a menu, a list of fixed-height items) where content scrolls a
whole row at a time and there’s no momentum to animate, reach for crate::ListState
instead: its offset is a plain usize, clamped only at zero, with no velocity or physics
step. The two don’t compose into one type on purpose (see crate::ListState’s own doc
comment): pick whichever one matches what’s actually scrolling: continuous/pixel-ish
content reaches for ScrollState, a discrete item list reaches for ListState.
Implementations§
Source§impl ScrollState
impl ScrollState
Sourcepub const fn with_physics(physics: ScrollPhysics) -> Self
pub const fn with_physics(physics: ScrollPhysics) -> Self
Create a new ScrollState with custom physics.
Sourcepub const fn set_offset(&mut self, offset: f32, max_offset: f32)
pub const fn set_offset(&mut self, offset: f32, max_offset: f32)
Set the offset directly, clamping it to bounds.
Sourcepub fn integer_offset(&self) -> usize
pub fn integer_offset(&self) -> usize
Returns the integer part of the offset, clamped to positive.
Sourcepub fn fractional_offset(&self) -> f32
pub fn fractional_offset(&self) -> f32
Returns the fractional remainder of the offset (0.0..1.0).
Sourcepub fn tick(&mut self, dt: Duration, max_offset: f32)
pub fn tick(&mut self, dt: Duration, max_offset: f32)
Update physics for a single frame step.
Decays momentum if in bounds, or animates the rubber-band spring back to boundaries if out of bounds. Has no effect if dragging is active.
Sourcepub const fn begin_drag(&mut self, y: f32)
pub const fn begin_drag(&mut self, y: f32)
Begin a drag gesture at pointer coordinate y.
Sourcepub fn update_drag(&mut self, y: f32, max_offset: f32)
pub fn update_drag(&mut self, y: f32, max_offset: f32)
Update the drag gesture with a new pointer coordinate y.
Sourcepub fn end_drag(&mut self)
pub fn end_drag(&mut self)
End the current drag gesture, initiating a fling if pointer speed was sufficient.
Sourcepub fn scroll_by_wheel(&mut self, delta: f32)
pub fn scroll_by_wheel(&mut self, delta: f32)
Apply a scroll wheel impulse directly to velocity.
Sourcepub fn apply<Id>(&mut self, response: &Response<Id>)
pub fn apply<Id>(&mut self, response: &Response<Id>)
Feeds a frame’s resolved Response::scroll_delta straight into
scroll_by_wheel, so a widget wires wheel input by calling
Interaction::interact with Sense::SCROLL
and handing the Response here, instead of re-deriving it from raw
MouseEventKind::Scroll events the way a widget
with no route to ScrollState has to.
A no-op if nothing scrolled this frame (scroll_delta is 0), which also makes this safe
to call unconditionally every frame rather than gating it on a dirty check first. Drag-to-scroll
isn’t covered here: Response reports only whether a drag is in progress
(Response::dragging), not a pointer position, so that gesture still goes through
begin_drag/update_drag/end_drag
directly, using Interaction::pointer’s position alongside
this Response.
§Examples
use retroglyph_core::grid::Rect;
use retroglyph_ui::{Interaction, ScrollState, Sense};
#[derive(Clone, Copy, PartialEq, Eq)]
struct Id;
let mut interaction = Interaction::new();
let mut scroll = ScrollState::new();
interaction.begin_frame();
let response = interaction.interact(Rect::new(0, 0, 10, 5), Id, Sense::scroll());
scroll.apply(&response); // a no-op here: nothing scrolled this frame
interaction.end_frame();
assert_eq!(scroll.velocity(), 0.0);Trait Implementations§
Source§impl Clone for ScrollState
impl Clone for ScrollState
Source§fn clone(&self) -> ScrollState
fn clone(&self) -> ScrollState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ScrollState
impl Debug for ScrollState
Source§impl Default for ScrollState
impl Default for ScrollState
Source§impl PartialEq for ScrollState
impl PartialEq for ScrollState
Source§fn eq(&self, other: &ScrollState) -> bool
fn eq(&self, other: &ScrollState) -> bool
self and other values to be equal, and is used by ==.