Scroll on cursor movement

This commit is contained in:
Jeremy Soller 2022-10-18 13:05:36 -06:00
parent 3e04ffdfa4
commit a599d83ca0
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
2 changed files with 24 additions and 7 deletions

View file

@ -73,7 +73,7 @@ where
) { ) {
let buffer = self.buffer.read().unwrap(); let buffer = self.buffer.read().unwrap();
let font_size = buffer.font_size(); let font_size = buffer.font_size();
let line_height = font_size + 8; /*TODO: store somewhere else */ let line_height = buffer.line_height();
let instant = Instant::now(); let instant = Instant::now();

View file

@ -90,7 +90,7 @@ impl<'a> TextBuffer<'a> {
} }
pub fn shape_until_scroll(&mut self) { pub fn shape_until_scroll(&mut self) {
let lines = self.height / self.line_height; let lines = self.lines();
let scroll_end = self.scroll + lines; let scroll_end = self.scroll + lines;
self.shape_until(scroll_end); self.shape_until(scroll_end);
@ -207,6 +207,10 @@ impl<'a> TextBuffer<'a> {
self.height self.height
} }
pub fn lines(&self) -> i32 {
self.height / self.line_height
}
pub fn set_size(&mut self, width: i32, height: i32) { pub fn set_size(&mut self, width: i32, height: i32) {
if width != self.width { if width != self.width {
self.width = width; self.width = width;
@ -255,12 +259,27 @@ impl<'a> TextBuffer<'a> {
if self.cursor.line > 0 { if self.cursor.line > 0 {
self.cursor.line -= 1; self.cursor.line -= 1;
self.redraw = true; self.redraw = true;
let lines = self.lines();
if (self.cursor.line as i32) < self.scroll
|| (self.cursor.line as i32) >= self.scroll + lines
{
self.scroll = self.cursor.line as i32;
}
} }
}, },
TextAction::Down => { TextAction::Down => {
if self.cursor.line + 1 < self.layout_lines.len() { if self.cursor.line < self.layout_lines.len() {
self.cursor.line += 1; self.cursor.line += 1;
self.redraw = true; self.redraw = true;
let lines = self.lines();
if (self.cursor.line as i32) < self.scroll
|| (self.cursor.line as i32) >= self.scroll + lines
{
self.scroll = self.cursor.line as i32 - (lines - 1);
self.shape_until_scroll();
}
} }
}, },
TextAction::Backspace => { TextAction::Backspace => {
@ -287,14 +306,12 @@ impl<'a> TextBuffer<'a> {
} }
}, },
TextAction::PageUp => { TextAction::PageUp => {
let lines = self.height / self.line_height; self.scroll -= self.lines();
self.scroll -= lines;
self.redraw = true; self.redraw = true;
self.shape_until_scroll(); self.shape_until_scroll();
}, },
TextAction::PageDown => { TextAction::PageDown => {
let lines = self.height / self.line_height; self.scroll += self.lines();
self.scroll += lines;
self.redraw = true; self.redraw = true;
self.shape_until_scroll(); self.shape_until_scroll();
}, },