summaryrefslogtreecommitdiffstats
path: root/native/src/widget/operation/scrollable.rs
diff options
context:
space:
mode:
authorLibravatar Cory Forsstrom <cforsstrom18@gmail.com>2023-04-14 13:32:44 -0700
committerLibravatar Cory Forsstrom <cforsstrom18@gmail.com>2023-04-14 13:32:44 -0700
commitb623f280ed43b04cef16a82de4cdf13497b5d63e (patch)
tree278327c128ecc8716f3ad2b69c857363c548119a /native/src/widget/operation/scrollable.rs
parent4b05f42fd6d18bf572b772dd60d6a4309ea5f343 (diff)
downloadiced-b623f280ed43b04cef16a82de4cdf13497b5d63e.tar.gz
iced-b623f280ed43b04cef16a82de4cdf13497b5d63e.tar.bz2
iced-b623f280ed43b04cef16a82de4cdf13497b5d63e.zip
Add `scroll_to` operation for absolute scroll
Diffstat (limited to 'native/src/widget/operation/scrollable.rs')
-rw-r--r--native/src/widget/operation/scrollable.rs50
1 files changed, 49 insertions, 1 deletions
diff --git a/native/src/widget/operation/scrollable.rs b/native/src/widget/operation/scrollable.rs
index 3b20631f..62f4c91e 100644
--- a/native/src/widget/operation/scrollable.rs
+++ b/native/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,52 @@ 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 current absolute & relative offset of a [`Scrollable`]
+#[derive(Debug, Clone, Copy, PartialEq, Default)]
+pub struct CurrentOffset {
+ /// The [`AbsoluteOffset`] of a [`Scrollable`]
+ pub absolute: AbsoluteOffset,
+ /// The [`RelativeOffset`] of a [`Scrollable`]
+ pub relative: RelativeOffset,
+}
+
+/// 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)]