diff options
author | 2023-05-02 06:40:48 +0200 | |
---|---|---|
committer | 2023-05-02 06:40:48 +0200 | |
commit | 8e8808f0e187ed6671441f5016f07bfcba426452 (patch) | |
tree | bc4d2eeb57f60e654a4a8ed7c562f79ee507abab /core | |
parent | 2d7d9a130ece3fb6fa4cd52f9b32b4abd7887cf5 (diff) | |
parent | c8952ee4a1118fe67bfdf40fc77f3ff30f5d0278 (diff) | |
download | iced-8e8808f0e187ed6671441f5016f07bfcba426452.tar.gz iced-8e8808f0e187ed6671441f5016f07bfcba426452.tar.bz2 iced-8e8808f0e187ed6671441f5016f07bfcba426452.zip |
Merge branch 'master' into advanced-text
Diffstat (limited to 'core')
-rw-r--r-- | core/src/widget/operation/scrollable.rs | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/core/src/widget/operation/scrollable.rs b/core/src/widget/operation/scrollable.rs index 3b20631f..f947344d 100644 --- a/core/src/widget/operation/scrollable.rs +++ b/core/src/widget/operation/scrollable.rs @@ -5,6 +5,9 @@ use crate::widget::{Id, Operation}; pub trait Scrollable { /// Snaps the scroll of the widget to the given `percentage` along the horizontal & vertical axis. fn snap_to(&mut self, offset: RelativeOffset); + + /// Scroll the widget to the given [`AbsoluteOffset`] along the horizontal & vertical axis. + fn scroll_to(&mut self, offset: AbsoluteOffset); } /// Produces an [`Operation`] that snaps the widget with the given [`Id`] to @@ -34,7 +37,43 @@ pub fn snap_to<T>(target: Id, offset: RelativeOffset) -> impl Operation<T> { SnapTo { target, offset } } -/// The amount of offset in each direction of a [`Scrollable`]. +/// Produces an [`Operation`] that scrolls the widget with the given [`Id`] to +/// the provided [`AbsoluteOffset`]. +pub fn scroll_to<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> { + struct ScrollTo { + target: Id, + offset: AbsoluteOffset, + } + + impl<T> Operation<T> for ScrollTo { + fn container( + &mut self, + _id: Option<&Id>, + operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>), + ) { + operate_on_children(self) + } + + fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) { + if Some(&self.target) == id { + state.scroll_to(self.offset); + } + } + } + + ScrollTo { target, offset } +} + +/// The amount of absolute offset in each direction of a [`Scrollable`]. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct AbsoluteOffset { + /// The amount of horizontal offset + pub x: f32, + /// The amount of vertical offset + pub y: f32, +} + +/// The amount of relative offset in each direction of a [`Scrollable`]. /// /// A value of `0.0` means start, while `1.0` means end. #[derive(Debug, Clone, Copy, PartialEq, Default)] |