summaryrefslogtreecommitdiffstats
path: root/src/tree
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2021-08-03 20:49:25 +0200
committerCanop <cano.petrole@gmail.com>2021-08-04 13:25:29 +0200
commitc68e7f3bd12ad14214e074d90c1640d8b2536801 (patch)
tree797741ea2fae240960a9aa03ce3dadb79cb29856 /src/tree
parent3123abc98b2f2e8e52da3ecce96c690341e75c11 (diff)
improve scrolling behaviors
Especially move the selection when you're at one end and you try to scroll the blocked way. Fix #419
Diffstat (limited to 'src/tree')
-rw-r--r--src/tree/tree.rs22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/tree/tree.rs b/src/tree/tree.rs
index 445c56b..253eeb1 100644
--- a/src/tree/tree.rs
+++ b/src/tree/tree.rs
@@ -180,9 +180,27 @@ impl Tree {
}
}
- pub fn try_scroll(&mut self, dy: i32, page_height: i32) {
- self.scroll = (self.scroll + dy).max(0).min(self.lines.len() as i32 - 5);
+ /// scroll the desired amount and return true, or return false if it's
+ /// already at end or the tree fits the page
+ pub fn try_scroll(&mut self, dy: i32, page_height: i32) -> bool {
+ let lines_len = self.lines.len() as i32;
+ if lines_len <= page_height {
+ return false;
+ }
+ if dy < 0 { // scroll up
+ if self.scroll == 0 {
+ return false;
+ }
+ self.scroll = (self.scroll + dy).max(0);
+ } else { // scroll down
+ let max = lines_len - page_height;
+ if self.scroll >= max {
+ return false;
+ }
+ self.scroll = (self.scroll + dy).min(max);
+ }
self.select_visible_line(page_height);
+ true
}
/// try to select a line (works if y+scroll falls on a selectable line)