diff options
author | Christian Duerr <contact@christianduerr.com> | 2018-03-11 13:01:06 +0100 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2018-06-02 09:56:50 -0700 |
commit | 0dcb9ca6a871fd6d28787142a134c9190001ac43 (patch) | |
tree | 8f1f4845942c7af562173b9e670ca61664ee49ee | |
parent | 2c7bb9a4d3ce3ead6de4ca6485ca67c44c0bd1c1 (diff) | |
download | alacritty-0dcb9ca6a871fd6d28787142a134c9190001ac43.tar.gz alacritty-0dcb9ca6a871fd6d28787142a134c9190001ac43.zip |
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.
-rw-r--r-- | src/event.rs | 21 | ||||
-rw-r--r-- | src/grid/mod.rs | 58 | ||||
-rw-r--r-- | src/input.rs | 19 | ||||
-rw-r--r-- | src/term/mod.rs | 26 |
4 files changed, 44 insertions, 80 deletions
diff --git a/src/event.rs b/src/event.rs index 4823d824..cf7d1030 100644 --- a/src/event.rs +++ b/src/event.rs @@ -10,6 +10,7 @@ use parking_lot::MutexGuard; use glutin::{self, ModifiersState, Event, ElementState}; use copypasta::{Clipboard, Load, Store}; +use grid::Scroll; use config::{self, Config}; use cli::Options; use display::OnResize; @@ -53,24 +54,8 @@ impl<'a, N: Notify + 'a> input::ActionContext for ActionContext<'a, N> { *self.size_info } - fn scroll(&mut self, count: isize) { - self.terminal.scroll_display(count); - } - - fn reset_scroll(&mut self) { - self.terminal.reset_scroll(); - } - - fn scroll_to_top(&mut self) { - self.terminal.scroll_to_top(); - } - - fn scroll_page_up(&mut self) { - self.terminal.scroll_page_up(); - } - - fn scroll_page_down(&mut self) { - self.terminal.scroll_page_down(); + fn scroll(&mut self, scroll: Scroll) { + self.terminal.scroll_display(scroll); } fn copy_selection(&self, buffer: ::copypasta::Buffer) { 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, } } diff --git a/src/input.rs b/src/input.rs index adce3448..8cf11811 100644 --- a/src/input.rs +++ b/src/input.rs @@ -28,6 +28,7 @@ use copypasta::{Clipboard, Load, Buffer}; use glutin::{ElementState, VirtualKeyCode, MouseButton, TouchPhase, MouseScrollDelta, ModifiersState}; use config; +use grid::Scroll; use event::{ClickState, Mouse}; use index::{Line, Column, Side, Point}; use term::SizeInfo; @@ -65,11 +66,7 @@ pub trait ActionContext { fn last_modifiers(&mut self) -> &mut ModifiersState; fn change_font_size(&mut self, delta: i8); fn reset_font_size(&mut self); - fn scroll(&mut self, count: isize); - fn reset_scroll(&mut self); - fn scroll_to_top(&mut self); - fn scroll_page_up(&mut self); - fn scroll_page_down(&mut self); + fn scroll(&mut self, scroll: Scroll); } /// Describes a state and action to take in that state @@ -254,16 +251,16 @@ impl Action { ctx.reset_font_size(); }, Action::ScrollPageUp => { - ctx.scroll_page_up(); + ctx.scroll(Scroll::PageUp); }, Action::ScrollPageDown => { - ctx.scroll_page_down(); + ctx.scroll(Scroll::PageDown); }, Action::ScrollToTop => { - ctx.scroll_to_top(); + ctx.scroll(Scroll::Top); }, Action::ScrollToBottom => { - ctx.reset_scroll(); + ctx.scroll(Scroll::Bottom); }, } } @@ -527,7 +524,7 @@ impl<'a, A: ActionContext + 'a> Processor<'a, A> { self.ctx.write_to_pty(content); } else { for _ in 0..scroll_multiplier { - self.ctx.scroll(-((code as isize) * 2 - 129)); + self.ctx.scroll(Scroll::Lines(-((code as isize) * 2 - 129))); } } } @@ -597,7 +594,7 @@ impl<'a, A: ActionContext + 'a> Processor<'a, A> { /// Process a received character pub fn received_char(&mut self, c: char) { if !*self.ctx.suppress_chars() { - self.ctx.reset_scroll(); + self.ctx.scroll(Scroll::Bottom); self.ctx.clear_selection(); let utf8_len = c.len_utf8(); diff --git a/src/term/mod.rs b/src/term/mod.rs index 8e973b0b..3c7ef87c 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -24,7 +24,7 @@ use unicode_width::UnicodeWidthChar; use font::{self, Size}; use ansi::{self, Color, NamedColor, Attr, Handler, CharsetIndex, StandardCharset, CursorStyle}; -use grid::{BidirectionalIterator, Grid, Indexed, IndexRegion, DisplayIter}; +use grid::{BidirectionalIterator, Grid, Indexed, IndexRegion, DisplayIter, Scroll}; use index::{self, Point, Column, Line, IndexRange, Contains, RangeInclusive, Linear}; use selection::{self, Selection, Locations}; use config::{Config, VisualBellAnimation}; @@ -824,28 +824,8 @@ impl Term { self.next_title.take() } - pub fn scroll_display(&mut self, count: isize) { - self.grid.scroll_display(count); - self.dirty = true; - } - - pub fn reset_scroll(&mut self) { - self.grid.reset_scroll_display(); - self.dirty = true; - } - - pub fn scroll_to_top(&mut self) { - self.grid.scroll_to_top(); - self.dirty = true; - } - - pub fn scroll_page_up(&mut self) { - self.grid.scroll_page_up(); - self.dirty = true; - } - - pub fn scroll_page_down(&mut self) { - self.grid.scroll_page_down(); + pub fn scroll_display(&mut self, scroll: Scroll) { + self.grid.scroll_display(scroll); self.dirty = true; } |