summaryrefslogtreecommitdiffstats
path: root/native/src/widget/operation/scrollable.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2022-08-05 22:31:26 +0200
committerLibravatar GitHub <noreply@github.com>2022-08-05 22:31:26 +0200
commitf294c2d16249ce8b7989f8af6332678f53d8fb12 (patch)
treebb8d6f00fe25de9187b1d3b90a88ae95225b7428 /native/src/widget/operation/scrollable.rs
parenta003e797e8a1bb5d365c1db5de6af88e61a47329 (diff)
parentad5bd0970d7106a97d455a164a582ab1d0bff18b (diff)
downloadiced-f294c2d16249ce8b7989f8af6332678f53d8fb12.tar.gz
iced-f294c2d16249ce8b7989f8af6332678f53d8fb12.tar.bz2
iced-f294c2d16249ce8b7989f8af6332678f53d8fb12.zip
Merge pull request #1399 from iced-rs/widget-operations
Widget Operations
Diffstat (limited to 'native/src/widget/operation/scrollable.rs')
-rw-r--r--native/src/widget/operation/scrollable.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/native/src/widget/operation/scrollable.rs b/native/src/widget/operation/scrollable.rs
new file mode 100644
index 00000000..2210137d
--- /dev/null
+++ b/native/src/widget/operation/scrollable.rs
@@ -0,0 +1,35 @@
+//! Operate on widgets that can be scrolled.
+use crate::widget::{Id, Operation};
+
+/// The internal state of a widget that can be scrolled.
+pub trait Scrollable {
+ /// Snaps the scroll of the widget to the given `percentage`.
+ fn snap_to(&mut self, percentage: f32);
+}
+
+/// Produces an [`Operation`] that snaps the widget with the given [`Id`] to
+/// the provided `percentage`.
+pub fn snap_to<T>(target: Id, percentage: f32) -> impl Operation<T> {
+ struct SnapTo {
+ target: Id,
+ percentage: f32,
+ }
+
+ impl<T> Operation<T> for SnapTo {
+ fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) {
+ if Some(&self.target) == id {
+ state.snap_to(self.percentage);
+ }
+ }
+
+ fn container(
+ &mut self,
+ _id: Option<&Id>,
+ operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
+ ) {
+ operate_on_children(self)
+ }
+ }
+
+ SnapTo { target, percentage }
+}