From 03e143f4c3c15fa52bb01f82758fb2344ac5b19b Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 16 Jun 2018 10:10:59 -0700 Subject: [PATCH] Implement scroll_to_top, scroll_to_bottom, scroll_to_left, and scroll_to_right --- src/views/scroll_view.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/views/scroll_view.rs b/src/views/scroll_view.rs index 79a4b47..7b1c29b 100644 --- a/src/views/scroll_view.rs +++ b/src/views/scroll_view.rs @@ -107,6 +107,32 @@ impl ScrollView { pub fn scroll_x(self, enabled: bool) -> Self { self.with(|s| s.set_scroll_x(enabled)) } + + /// Programmatically scroll to the top of the view. + pub fn scroll_to_top(&mut self) { + let curr_x = self.offset.x; + self.set_offset((curr_x,0)); + } + + /// Programmatically scroll to the bottom of the view. + pub fn scroll_to_bottom(&mut self) { + let max_y = self.inner_size.saturating_sub(self.available_size()).y; + let curr_x = self.offset.x; + self.set_offset((curr_x, max_y)); + } + + /// Programmatically scroll to the leftmost side of the view. + pub fn scroll_to_left(&mut self) { + let curr_y = self.offset.y; + self.set_offset((0, curr_y)); + } + + /// Programmatically scroll to the rightmost side of the view. + pub fn scroll_to_right(&mut self) { + let max_x = self.inner_size.saturating_sub(self.available_size()).x; + let curr_y = self.offset.y; + self.set_offset((max_x, curr_y)); + } /// Returns for each axis if we are scrolling. fn is_scrolling(&self) -> XY {