11//! Navigate an endless amount of content with a scrollbar.
22use crate :: {
33 column,
4- input:: { mouse, ButtonState } ,
4+ input:: { mouse, touch , ButtonState } ,
55 layout, Align , Clipboard , Column , Element , Event , Hasher , Layout , Length ,
66 Point , Rectangle , Size , Widget ,
77} ;
@@ -175,6 +175,22 @@ where
175175 }
176176 }
177177 }
178+ Event :: Touch ( touch:: Touch :: Started { .. } ) => {
179+ self . state . scroll_box_touched_at = Some ( cursor_position) ;
180+ }
181+ Event :: Touch ( touch:: Touch :: Moved { .. } ) => {
182+ if let Some ( scroll_box_touched_at) =
183+ self . state . scroll_box_touched_at
184+ {
185+ let delta = cursor_position. y - scroll_box_touched_at. y ;
186+ self . state . scroll ( delta, bounds, content_bounds) ;
187+ self . state . scroll_box_touched_at =
188+ Some ( cursor_position) ;
189+ }
190+ }
191+ Event :: Touch ( touch:: Touch :: Ended { .. } ) => {
192+ self . state . scroll_box_touched_at = None ;
193+ }
178194 _ => { }
179195 }
180196 }
@@ -191,10 +207,23 @@ where
191207 Event :: Mouse ( mouse:: Event :: Input {
192208 button : mouse:: Button :: Left ,
193209 state : ButtonState :: Released ,
194- } ) => {
210+ } )
211+ | Event :: Touch ( touch:: Touch :: Ended { .. } ) => {
195212 self . state . scroller_grabbed_at = None ;
196213 }
197- Event :: Mouse ( mouse:: Event :: CursorMoved { .. } ) => {
214+ Event :: Mouse ( mouse:: Event :: Input {
215+ button : mouse:: Button :: Left ,
216+ state : ButtonState :: Pressed ,
217+ } )
218+ | Event :: Touch ( touch:: Touch :: Started { .. } ) => {
219+ self . state . scroll_to (
220+ cursor_position. y / ( bounds. y + bounds. height ) ,
221+ bounds,
222+ content_bounds,
223+ ) ;
224+ }
225+ Event :: Mouse ( mouse:: Event :: CursorMoved { .. } )
226+ | Event :: Touch ( touch:: Touch :: Moved { .. } ) => {
198227 if let ( Some ( scrollbar) , Some ( scroller_grabbed_at) ) =
199228 ( scrollbar, self . state . scroller_grabbed_at )
200229 {
@@ -215,7 +244,8 @@ where
215244 Event :: Mouse ( mouse:: Event :: Input {
216245 button : mouse:: Button :: Left ,
217246 state : ButtonState :: Pressed ,
218- } ) => {
247+ } )
248+ | Event :: Touch ( touch:: Touch :: Started { .. } ) => {
219249 if let Some ( scrollbar) = scrollbar {
220250 if let Some ( scroller_grabbed_at) =
221251 scrollbar. grab_scroller ( cursor_position)
@@ -326,6 +356,7 @@ where
326356#[ derive( Debug , Clone , Copy , Default ) ]
327357pub struct State {
328358 scroller_grabbed_at : Option < f32 > ,
359+ scroll_box_touched_at : Option < Point > ,
329360 offset : f32 ,
330361}
331362
@@ -391,6 +422,11 @@ impl State {
391422 pub fn is_scroller_grabbed ( & self ) -> bool {
392423 self . scroller_grabbed_at . is_some ( )
393424 }
425+
426+ /// Returns whether the scroll box is currently touched or not.
427+ pub fn is_scroll_box_touched ( & self ) -> bool {
428+ self . scroll_box_touched_at . is_some ( )
429+ }
394430}
395431
396432/// The scrollbar of a [`Scrollable`].
0 commit comments