Skip to content

Commit

Permalink
Add manual scroll methods to scroll::Core
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Mar 12, 2019
1 parent ea7b5db commit 3a0b490
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/view/scroll/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,43 @@ impl Core {
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.
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;
} else if x < self.offset.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.
pub fn scroll_to_top(&mut self) {
let curr_x = self.offset.x;
Expand Down

0 comments on commit 3a0b490

Please sign in to comment.