From f15f36cc1b292cea5d93c6a6480104d53d44d770 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Tue, 22 Oct 2019 13:58:59 -0700 Subject: [PATCH] Add ScrollView::scroll_to_important_area --- src/view/scroll/core.rs | 33 +++++++++++++++++---------------- src/views/scroll_view.rs | 6 ++++++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/view/scroll/core.rs b/src/view/scroll/core.rs index 947f557..f3c0522 100644 --- a/src/view/scroll/core.rs +++ b/src/view/scroll/core.rs @@ -337,22 +337,7 @@ impl Core { other => { // The view consumed the event. Maybe something changed? - // Fix offset? - let important = important_area; - - // The furthest top-left we can go - let top_left = (important.bottom_right() + (1, 1)) - .saturating_sub(self.available_size()); - // The furthest bottom-right we can go - let bottom_right = important.top_left(); - - // "top_left < bottom_right" is NOT guaranteed - // if the child is larger than the view. - let offset_min = Vec2::min(top_left, bottom_right); - let offset_max = Vec2::max(top_left, bottom_right); - - self.offset = - self.offset.or_max(offset_min).or_min(offset_max); + self.scroll_to_rect(important_area); other } @@ -554,6 +539,22 @@ impl Core { self.offset = self.offset.or_min(max).or_max(min); } + /// Scrolls until the given rect is in view. + pub fn scroll_to_rect(&mut self, important_area: Rect) { + // The furthest top-left we can go + let top_left = (important_area.bottom_right() + (1, 1)) + .saturating_sub(self.available_size()); + // The furthest bottom-right we can go + let bottom_right = important_area.top_left(); + + // "top_left < bottom_right" is NOT guaranteed + // if the child is larger than the view. + let offset_min = Vec2::min(top_left, bottom_right); + let offset_max = Vec2::max(top_left, bottom_right); + + self.offset = self.offset.or_max(offset_min).or_min(offset_max); + } + /// Scroll until the given point is visible. pub fn scroll_to(&mut self, pos: Vec2) { // The furthest top-left we can go diff --git a/src/views/scroll_view.rs b/src/views/scroll_view.rs index 119ea69..06cdb75 100644 --- a/src/views/scroll_view.rs +++ b/src/views/scroll_view.rs @@ -155,6 +155,12 @@ where self.core.scroll_to_right(); } + /// Programmatically scroll until the child's important area is in view. + pub fn scroll_to_important_area(&mut self) { + let important_area = self.inner.important_area(self.core.last_size()); + self.core.scroll_to_rect(important_area); + } + /// Returns the wrapped view. pub fn into_inner(self) -> V { self.inner