summaryrefslogtreecommitdiffstats
path: root/src/grid
diff options
context:
space:
mode:
authorChristian Duerr <contact@christianduerr.com>2018-03-11 13:01:06 +0100
committerJoe Wilm <joe@jwilm.com>2018-06-02 09:56:50 -0700
commit0dcb9ca6a871fd6d28787142a134c9190001ac43 (patch)
tree8f1f4845942c7af562173b9e670ca61664ee49ee /src/grid
parent2c7bb9a4d3ce3ead6de4ca6485ca67c44c0bd1c1 (diff)
Replace scrolling methods with enum
The different scrolling methods added a bunch of boilerplate where the call was just forwarded to the next struct, this has been removed by making the scroll amount into a struct. Now everything is called through one method and the parameter decides how far the viewport should be scrolled.
Diffstat (limited to 'src/grid')
-rw-r--r--src/grid/mod.rs58
1 files changed, 30 insertions, 28 deletions
diff --git a/src/grid/mod.rs b/src/grid/mod.rs
index ac761adc..65bc8382 100644
--- a/src/grid/mod.rs
+++ b/src/grid/mod.rs
@@ -113,6 +113,14 @@ pub struct GridIterator<'a, T: 'a> {
top: usize,
}
+pub enum Scroll {
+ Lines(isize),
+ PageUp,
+ PageDown,
+ Top,
+ Bottom,
+}
+
impl<T: Copy + Clone> Grid<T> {
pub fn new(lines: index::Line, cols: index::Column, scrollback: usize, template: T) -> Grid<T> {
let mut raw = Storage::with_capacity(*lines + scrollback, lines);
@@ -165,34 +173,28 @@ impl<T: Copy + Clone> Grid<T> {
self.line_to_offset(line) + self.display_offset
}
- pub fn scroll_display(&mut self, count: isize) {
- self.display_offset = min(
- max((self.display_offset as isize) + count, 0isize) as usize,
- self.scroll_limit
- );
- }
-
- pub fn reset_scroll_display(&mut self) {
- self.display_offset = 0;
- }
-
- pub fn scroll_to_top(&mut self) {
- self.display_offset = self.scroll_limit;
- }
-
- pub fn scroll_page_up(&mut self) {
- if self.display_offset + self.lines.0 >= self.scroll_limit {
- self.display_offset = self.scroll_limit;
- } else {
- self.display_offset += self.lines.0;
- }
- }
-
- pub fn scroll_page_down(&mut self) {
- if self.display_offset <= self.lines.0 {
- self.display_offset = 0;
- } else {
- self.display_offset -= self.lines.0;
+ pub fn scroll_display(&mut self, scroll: Scroll) {
+ match scroll {
+ Scroll::Lines(count) => {
+ self.display_offset = min(
+ max((self.display_offset as isize) + count, 0isize) as usize,
+ self.scroll_limit
+ );
+ },
+ Scroll::PageUp => {
+ self.display_offset = min(
+ self.display_offset + self.lines.0,
+ self.scroll_limit
+ );
+ },
+ Scroll::PageDown => {
+ self.display_offset -= min(
+ self.display_offset,
+ self.lines.0
+ );
+ },
+ Scroll::Top => self.display_offset = self.scroll_limit,
+ Scroll::Bottom => self.display_offset = 0,
}
}