Add ScrollView::scroll_to_important_area

This commit is contained in:
Alexandre Bury 2019-10-22 13:58:59 -07:00
parent 14f2c962af
commit f15f36cc1b
2 changed files with 23 additions and 16 deletions

View File

@ -337,22 +337,7 @@ impl Core {
other => { other => {
// The view consumed the event. Maybe something changed? // The view consumed the event. Maybe something changed?
// Fix offset? self.scroll_to_rect(important_area);
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);
other other
} }
@ -554,6 +539,22 @@ impl Core {
self.offset = self.offset.or_min(max).or_max(min); 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. /// Scroll until the given point is visible.
pub fn scroll_to(&mut self, pos: Vec2) { pub fn scroll_to(&mut self, pos: Vec2) {
// The furthest top-left we can go // The furthest top-left we can go

View File

@ -155,6 +155,12 @@ where
self.core.scroll_to_right(); 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. /// Returns the wrapped view.
pub fn into_inner(self) -> V { pub fn into_inner(self) -> V {
self.inner self.inner