summaryrefslogtreecommitdiffstats
path: root/native/src/widget/scrollable.rs
diff options
context:
space:
mode:
Diffstat (limited to 'native/src/widget/scrollable.rs')
-rw-r--r--native/src/widget/scrollable.rs42
1 files changed, 39 insertions, 3 deletions
diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs
index 91c13eb5..b7a0b6ee 100644
--- a/native/src/widget/scrollable.rs
+++ b/native/src/widget/scrollable.rs
@@ -5,11 +5,12 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::touch;
+use crate::widget;
+use crate::widget::operation::{self, Operation};
use crate::widget::tree::{self, Tree};
-use crate::widget::Operation;
use crate::{
- Background, Clipboard, Color, Element, Layout, Length, Point, Rectangle,
- Shell, Size, Vector, Widget,
+ Background, Clipboard, Color, Command, Element, Layout, Length, Point,
+ Rectangle, Shell, Size, Vector, Widget,
};
use std::{f32, u32};
@@ -31,6 +32,7 @@ where
Renderer: crate::Renderer,
Renderer::Theme: StyleSheet,
{
+ id: Option<Id>,
height: Length,
scrollbar_width: u16,
scrollbar_margin: u16,
@@ -48,6 +50,7 @@ where
/// Creates a new [`Scrollable`].
pub fn new(content: impl Into<Element<'a, Message, Renderer>>) -> Self {
Scrollable {
+ id: None,
height: Length::Shrink,
scrollbar_width: 10,
scrollbar_margin: 0,
@@ -58,6 +61,12 @@ where
}
}
+ /// Sets the [`Id`] of the [`Scrollable`].
+ pub fn id(mut self, id: Id) -> Self {
+ self.id = Some(id);
+ self
+ }
+
/// Sets the height of the [`Scrollable`].
pub fn height(mut self, height: Length) -> Self {
self.height = height;
@@ -157,6 +166,10 @@ where
layout: Layout<'_>,
operation: &mut dyn Operation<Message>,
) {
+ let state = tree.state.downcast_mut::<State>();
+
+ operation.scrollable(state, self.id.as_ref().map(|id| &id.0));
+
operation.container(None, &mut |operation| {
self.content.as_widget().operate(
&mut tree.children[0],
@@ -303,6 +316,23 @@ where
}
}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct Id(widget::Id);
+
+impl Id {
+ pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self {
+ Self(widget::Id::new(id))
+ }
+
+ pub fn unique() -> Self {
+ Self(widget::Id::unique())
+ }
+}
+
+pub fn snap_to<Message: 'static>(id: Id, percentage: f32) -> Command<Message> {
+ Command::widget(operation::scrollable::snap_to(id.0, percentage))
+}
+
/// Computes the layout of a [`Scrollable`].
pub fn layout<Renderer>(
renderer: &Renderer,
@@ -790,6 +820,12 @@ impl Default for State {
}
}
+impl operation::Scrollable for State {
+ fn snap_to(&mut self, percentage: f32) {
+ State::snap_to(self, percentage);
+ }
+}
+
/// The local state of a [`Scrollable`].
#[derive(Debug, Clone, Copy)]
enum Offset {