Add manual scroll methods to scroll::Core

This commit is contained in:
Alexandre Bury 2019-03-12 15:42:32 -07:00
parent ea7b5dbae2
commit 3a0b49088b

View File

@ -498,15 +498,43 @@ impl Core {
self.with(|s| s.set_scroll_x(enabled)) self.with(|s| s.set_scroll_x(enabled))
} }
/// Try to keep the given `rect` in view.
pub fn keep_in_view(&mut self, rect: Rect) {
let min = rect.bottom_right().saturating_sub(self.last_size);
let max = rect.top_left();
let (min, max) = (Vec2::min(min, max), Vec2::max(min, max));
self.offset = self.offset.or_min(max).or_max(min);
}
/// Scroll until the given point is visible.
pub fn scroll_to(&mut self, pos: Vec2) {
// The furthest top-left we can go
let min = pos.saturating_sub(self.last_size);
// How far to the bottom-right we can go
let max = pos;
self.offset = self.offset.or_min(max).or_max(min);
}
/// Scroll until the given column is visible. /// Scroll until the given column is visible.
pub fn scroll_to_x(&mut self, x: usize) { pub fn scroll_to_x(&mut self, x: usize) {
if x > self.offset.x + self.last_size.x { if x >= self.offset.x + self.last_size.x {
self.offset.x = 1 + x - self.last_size.x; self.offset.x = 1 + x - self.last_size.x;
} else if x < self.offset.x { } else if x < self.offset.x {
self.offset.x = x; self.offset.x = x;
} }
} }
/// Scroll until the given row is visible.
pub fn scroll_to_y(&mut self, y: usize) {
if y >= self.offset.y + self.last_size.y {
self.offset.y = 1 + y - self.last_size.y;
} else if y < self.offset.y {
self.offset.y = y;
}
}
/// Programmatically scroll to the top of the view. /// Programmatically scroll to the top of the view.
pub fn scroll_to_top(&mut self) { pub fn scroll_to_top(&mut self) {
let curr_x = self.offset.x; let curr_x = self.offset.x;