Skip to main content

ScrollState

Struct ScrollState 

Source
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

Source

pub const fn new() -> Self

Create a new ScrollState at offset 0.0 with default physics.

Source

pub const fn with_physics(physics: ScrollPhysics) -> Self

Create a new ScrollState with custom physics.

Source

pub const fn offset(&self) -> f32

The current fractional scroll offset.

Source

pub const fn set_offset(&mut self, offset: f32, max_offset: f32)

Set the offset directly, clamping it to bounds.

Source

pub const fn velocity(&self) -> f32

The current velocity in items/second.

Source

pub const fn dragging(&self) -> bool

Whether a drag gesture is currently active.

Source

pub fn integer_offset(&self) -> usize

Returns the integer part of the offset, clamped to positive.

Source

pub fn fractional_offset(&self) -> f32

Returns the fractional remainder of the offset (0.0..1.0).

Source

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.

Source

pub const fn begin_drag(&mut self, y: f32)

Begin a drag gesture at pointer coordinate y.

Source

pub fn update_drag(&mut self, y: f32, max_offset: f32)

Update the drag gesture with a new pointer coordinate y.

Source

pub fn end_drag(&mut self)

End the current drag gesture, initiating a fling if pointer speed was sufficient.

Source

pub fn scroll_by_wheel(&mut self, delta: f32)

Apply a scroll wheel impulse directly to velocity.

Source

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

Source§

fn clone(&self) -> ScrollState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ScrollState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ScrollState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for ScrollState

Source§

fn eq(&self, other: &ScrollState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for ScrollState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.