mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add ScrollView::scroll_to_important_area
This commit is contained in:
parent
14f2c962af
commit
f15f36cc1b
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user