pub struct ScrollController { /* private fields */ }Expand description
Handle to drive and read a scrollable area programmatically.
By default a scrollable owns its scroll position and only the user can move it, through the
wheel, the scrollbar, arrow keys or dragging. A ScrollController lets your own code read and
change that position instead. Create one with use_scroll_controller and hand it to a
scrollable through its new_controlled constructor.
Some cases where a controller is needed:
- Jumping to the top or bottom in response to an action, for example scrolling a chat to the newest message after sending one.
- Keeping several scrollables in sync, like a diff view with two panes that move together.
- Reading the current scroll position to drive something else, such as a “scroll to top” button that only appears once the user has scrolled down.
§Scrolling from code
scroll_to queues a jump to the start or end of an axis, applied
on the next layout. This is the common way to snap a list to its top or bottom.
fn app() -> impl IntoElement {
let mut scroll_controller = use_scroll_controller(ScrollConfig::default);
rect()
.child(
Button::new()
.on_press(move |_| {
scroll_controller.scroll_to(ScrollPosition::End, Direction::Vertical);
})
.child("Scroll to bottom"),
)
.child(
ScrollView::new_controlled(scroll_controller)
.children((0..100).map(|i| label().key(i).text(format!("Item {i}")).into())),
)
}For an exact pixel offset use scroll_to_y or
scroll_to_x. The current position is available by converting
the controller into a (i32, i32) tuple of (x, y) pixels.
§Keeping scrollables in sync
Because a ScrollController is a cheap Copy handle, pass the same one to several
scrollables and they share a single scroll position: moving any of them moves the rest.
fn app() -> impl IntoElement {
let scroll_controller = use_scroll_controller(ScrollConfig::default);
rect()
.horizontal()
.spacing(6.)
.child(
ScrollView::new_controlled(scroll_controller)
.width(Size::flex(1.))
.children((0..30).map(|i| label().key(i).text(format!("Left {i}")).into())),
)
.child(
ScrollView::new_controlled(scroll_controller)
.width(Size::flex(1.))
.children((0..30).map(|i| label().key(i).text(format!("Right {i}")).into())),
)
}§Starting position
The ScrollConfig passed to use_scroll_controller also decides where each axis starts.
Set default_vertical_position to
ScrollPosition::End to open a list already scrolled to the bottom.
fn app() -> impl IntoElement {
let scroll_controller = use_scroll_controller(|| ScrollConfig {
default_vertical_position: ScrollPosition::End,
..Default::default()
});
ScrollView::new_controlled(scroll_controller)
.children((0..100).map(|i| label().key(i).text(format!("Item {i}")).into()))
}Implementations§
Source§impl ScrollController
impl ScrollController
Sourcepub fn new(
x: i32,
y: i32,
initial_requests: Vec<ScrollRequest>,
) -> ScrollController
pub fn new( x: i32, y: i32, initial_requests: Vec<ScrollRequest>, ) -> ScrollController
Creates a controller starting at scroll position (x, y) with a list of requests to apply.
Sourcepub fn managed(
notifier: State<()>,
requests: State<Vec<ScrollRequest>>,
on_scroll: State<Callback<ScrollEvent, bool>>,
get_scroll: State<Callback<(), (i32, i32)>>,
) -> ScrollController
pub fn managed( notifier: State<()>, requests: State<Vec<ScrollRequest>>, on_scroll: State<Callback<ScrollEvent, bool>>, get_scroll: State<Callback<(), (i32, i32)>>, ) -> ScrollController
Builds a controller from externally owned state, letting the caller manage its storage.
Sourcepub fn use_apply(&mut self, width: f32, height: f32)
pub fn use_apply(&mut self, width: f32, height: f32)
Applies any pending requests against the given content size. Called by the scrollable on every layout.
Sourcepub fn scroll_to_x(&mut self, to: i32) -> bool
pub fn scroll_to_x(&mut self, to: i32) -> bool
Scrolls the horizontal axis to to pixels. Returns whether the position actually changed.
Sourcepub fn scroll_to_y(&mut self, to: i32) -> bool
pub fn scroll_to_y(&mut self, to: i32) -> bool
Scrolls the vertical axis to to pixels. Returns whether the position actually changed.
Sourcepub fn scroll_to(
&mut self,
scroll_position: ScrollPosition,
scroll_direction: Direction,
)
pub fn scroll_to( &mut self, scroll_position: ScrollPosition, scroll_direction: Direction, )
Queues a scroll of scroll_direction to scroll_position, applied on the next layout.
Trait Implementations§
Source§impl Clone for ScrollController
impl Clone for ScrollController
Source§fn clone(&self) -> ScrollController
fn clone(&self) -> ScrollController
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl PartialEq for ScrollController
impl PartialEq for ScrollController
impl Copy for ScrollController
impl StructuralPartialEq for ScrollController
Auto Trait Implementations§
impl Freeze for ScrollController
impl !RefUnwindSafe for ScrollController
impl !Send for ScrollController
impl !Sync for ScrollController
impl Unpin for ScrollController
impl UnsafeUnpin for ScrollController
impl !UnwindSafe for ScrollController
Blanket Implementations§
§impl<T> AnyEq for T
impl<T> AnyEq for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> ComponentProps for T
impl<T> ComponentProps for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more