diff options
Diffstat (limited to 'native/src/widget/scrollable.rs')
-rw-r--r-- | native/src/widget/scrollable.rs | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index ec9746d4..2a658bcc 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -1,7 +1,7 @@ //! Navigate an endless amount of content with a scrollbar. use crate::{ column, - input::{mouse, ButtonState}, + input::{mouse, touch, ButtonState}, layout, Align, Clipboard, Column, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; @@ -175,6 +175,22 @@ where } } } + Event::Touch(touch::Touch::Started { .. }) => { + self.state.scroll_box_touched_at = Some(cursor_position); + } + Event::Touch(touch::Touch::Moved { .. }) => { + if let Some(scroll_box_touched_at) = + self.state.scroll_box_touched_at + { + let delta = cursor_position.y - scroll_box_touched_at.y; + self.state.scroll(delta, bounds, content_bounds); + self.state.scroll_box_touched_at = + Some(cursor_position); + } + } + Event::Touch(touch::Touch::Ended { .. }) => { + self.state.scroll_box_touched_at = None; + } _ => {} } } @@ -191,10 +207,23 @@ where Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Released, - }) => { + }) + | Event::Touch(touch::Touch::Ended { .. }) => { self.state.scroller_grabbed_at = None; } - Event::Mouse(mouse::Event::CursorMoved { .. }) => { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) + | Event::Touch(touch::Touch::Started { .. }) => { + self.state.scroll_to( + cursor_position.y / (bounds.y + bounds.height), + bounds, + content_bounds, + ); + } + Event::Mouse(mouse::Event::CursorMoved { .. }) + | Event::Touch(touch::Touch::Moved { .. }) => { if let (Some(scrollbar), Some(scroller_grabbed_at)) = (scrollbar, self.state.scroller_grabbed_at) { @@ -215,7 +244,8 @@ where Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - }) => { + }) + | Event::Touch(touch::Touch::Started { .. }) => { if let Some(scrollbar) = scrollbar { if let Some(scroller_grabbed_at) = scrollbar.grab_scroller(cursor_position) @@ -326,6 +356,7 @@ where #[derive(Debug, Clone, Copy, Default)] pub struct State { scroller_grabbed_at: Option<f32>, + scroll_box_touched_at: Option<Point>, offset: f32, } @@ -391,6 +422,11 @@ impl State { pub fn is_scroller_grabbed(&self) -> bool { self.scroller_grabbed_at.is_some() } + + /// Returns whether the scroll box is currently touched or not. + pub fn is_scroll_box_touched(&self) -> bool { + self.scroll_box_touched_at.is_some() + } } /// The scrollbar of a [`Scrollable`]. |