Skip to main content

ScrollController

Struct ScrollController 

Source
pub struct ScrollController { /* private fields */ }
Expand description

A cloneable handle onto a ScrollView or a ListView: drive it with jump_to/ animate_to (and, on a keyed list, scroll_to_item), read where it stands with offset/max_offset/ viewport_extent, and observe it with on_change. Every clone shares one state, so the handle an app keeps in its state and the one attached to the view are the same. See the module docs for the apply/publish contract.

use frust_widgets::{ScrollController, ScrollView, scroll_view, text};
let controller = ScrollController::new();
let view: ScrollView<()> = scroll_view(text("hi")).controller(controller.clone());
controller.jump_to(120.0); // applied at the view's next layout or paint

Implementations§

Source§

impl ScrollController

Source

pub fn new() -> Self

A controller attached to nothing yet, reading offset, extent and viewport 0.0.

Source

pub fn jump_to(&self, offset: f64)

Scroll to offset (px scrolled down) at once.

Applied by the attached surface at the start of its next layout or paint, clamped to [0, max_offset] against that pass’s extent; it stops any fling, release-settle or ballistic simulation in flight. NaN is ignored; f64::INFINITY lands on the bottom edge. Issued while nothing is attached (or before the attached surface has laid out), it waits for the first layout of the next surface to attach.

Source

pub fn animate_to(&self, offset: f64, options: AnimateTo)

Ease to offset over options.duration_ms/options.curve, clamped to [0, max_offset].

Applied — like ScrollController::jump_to — at the attached surface’s next layout or paint, and runs through the same ballistic driver a release fling does, so paint cadence, request_frame and the installed physics’ boundary handling are shared rather than duplicated. A later jump_to/animate_to recorded before this one finishes replaces it outright (drained in recording order, so the later command’s own reset always runs after this one’s). Any user pointer Down or wheel input on the surface cancels it too, leaving the surface wherever it had eased to — user input always wins. The app’s motion.reduce_motion theme flag collapses this to an instant jump. NaN is ignored.

Source

pub fn scroll_to_item( &self, key: impl Into<ChildKey>, alignment: ItemAlignment, animated: bool, )

Scroll a keyed ListView so the row whose key is key lands at alignment in the viewport — jumping there, or easing there when animated is true.

key is the same identity the list’s builder_keyed key_of returns for the row (anything that converts into a ChildKey: an id, a name, a ChildKey itself). Applied like ScrollController::jump_to — at the attached list’s next rebuild, layout or paint, replacing any jump or animation in flight — and resolved then against the list’s current data:

  • Resolution is O(item count) at worst. The list keeps no key-to-index map beyond its materialized window, so a key outside the window is found by calling key_of over the items in order. One scan per request (and per settle frame for a row that left the window), never per frame.
  • Estimated, then corrected. In variable-extent mode (ListView::estimated_item_extent) the row’s position is computed from the heights measured so far and the estimate for every row not yet laid out. Once the jump lands (or the animation finishes), each following layout measures the rows now on screen, re-resolves the row’s aligned position and corrects to it, for at most a few frames (the list’s own docs state the bound) — so the row ends aligned even when the rows ahead of it were never measured. A user Down, wheel or Cancel ends the request at once.
  • An unknown key is a no-op. A key no row carries — or any key on a positional ListView::builder list, which has no keys — leaves the list where it is and reports itself only through a debug-build log, since a recorded command has no caller left to return a Result to.
  • Lists only. A ScrollView holding this handle ignores the request (debug-build log); its offset commands are unaffected.

animated eases through the same tween ScrollController::animate_to runs, timed from the theme’s motion tokens (motion.durations.slow and motion.easing.spatial, the neutral scheme’s values when no theme is threaded); the theme’s motion.reduce_motion flag collapses it to a jump.

Source

pub fn is_animating(&self) -> bool

Whether the currently attached surface is running an animate_to tween — false once it completes, is replaced by a later jump/animate, or is interrupted by user input; false before any surface has ever started one; and false once the surface that was running one is dropped, taken over, or rebinds to a different handle (see the module docs for the reconciliation that guarantees this).

Source

pub fn offset(&self) -> f64

The clamped offset the attached surface last published (0.0 until a surface attaches and lays out; the last published value after it detaches).

Source

pub fn max_offset(&self) -> f64

The maximum offset (content − viewport, never negative) the attached surface last published.

Source

pub fn viewport_extent(&self) -> f64

The attached surface’s last laid-out extent along the scroll axis (its height, for the vertical ScrollView).

Source

pub fn is_attached(&self) -> bool

Whether a surface currently holds this handle.

Source

pub fn on_change<F: Fn(ScrollInfo) + 'static>( &self, listener: F, ) -> ScrollSubscription

Call listener with every ScrollInfo the attached surface publishes that differs from the previous one — user drag, wheel, fling/settle frames, relayout and applied jumps alike.

The listener runs with no &mut State (it may fire during layout or paint), so it reacts by writing a signal or recording into the handle, never by mutating app state directly. It stays subscribed for as long as the returned ScrollSubscription lives.

Trait Implementations§

Source§

impl Clone for ScrollController

Source§

fn clone(&self) -> Self

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 ScrollController

Source§

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

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

impl Default for ScrollController

Source§

fn default() -> Self

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

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> ErasedDestructor for T
where T: 'static,

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> StorageAccess<T> for T

Source§

fn as_borrowed(&self) -> &T

Borrows the value.
Source§

fn into_taken(self) -> T

Takes the value.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.