Performance improvements

This commit is contained in:
Jeremy Soller 2022-10-21 12:31:02 -06:00
parent d5fde04d09
commit 50dfd4e6ed
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
4 changed files with 15 additions and 45 deletions

View file

@ -152,6 +152,8 @@ where
); );
} }
buffer.shape_until_cursor();
let buffer_x = layout.bounds().x; let buffer_x = layout.bounds().x;
let buffer_y = layout.bounds().y; let buffer_y = layout.bounds().y;
buffer.draw(text_color_u32, |x, y, w, h, color| { buffer.draw(text_color_u32, |x, y, w, h, color| {

View file

@ -100,6 +100,8 @@ fn main() {
let font_size = buffer.metrics().font_size; let font_size = buffer.metrics().font_size;
let line_height = buffer.metrics().line_height; let line_height = buffer.metrics().line_height;
buffer.shape_until_cursor();
if buffer.redraw { if buffer.redraw {
let instant = Instant::now(); let instant = Instant::now();

View file

@ -9,6 +9,8 @@ fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>) {
let font_size = buffer.metrics().font_size; let font_size = buffer.metrics().font_size;
let line_height = buffer.metrics().line_height; let line_height = buffer.metrics().line_height;
buffer.shape_until_cursor();
if buffer.redraw { if buffer.redraw {
let instant = Instant::now(); let instant = Instant::now();

View file

@ -60,12 +60,6 @@ impl TextCursor {
} }
} }
enum CursorScroll {
None,
Bottom,
Top,
}
/// Index of a text line /// Index of a text line
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub struct TextLineIndex(usize); pub struct TextLineIndex(usize);
@ -220,7 +214,7 @@ impl<'a> TextBuffer<'a> {
total_layout total_layout
} }
fn shape_until_cursor(&mut self, scroll: CursorScroll) { pub fn shape_until_cursor(&mut self) {
let instant = Instant::now(); let instant = Instant::now();
let mut reshaped = 0; let mut reshaped = 0;
@ -265,23 +259,13 @@ impl<'a> TextBuffer<'a> {
} }
let lines = self.lines(); let lines = self.lines();
match scroll { if layout_i < self.scroll {
CursorScroll::None => (), self.scroll = layout_i;
CursorScroll::Bottom => { } else if layout_i >= self.scroll + lines {
if layout_i < self.scroll self.scroll = layout_i - (lines - 1);
|| layout_i >= self.scroll + lines
{
self.scroll = layout_i - (lines - 1);
}
},
CursorScroll::Top => {
if layout_i < self.scroll
|| layout_i >= self.scroll + lines
{
self.scroll = layout_i;
}
}
} }
self.shape_until_scroll();
} }
fn shape_until_scroll(&mut self) { fn shape_until_scroll(&mut self) {
@ -408,13 +392,9 @@ impl<'a> TextBuffer<'a> {
} }
self.cursor.index = prev_index; self.cursor.index = prev_index;
self.shape_until_cursor(CursorScroll::Bottom);
} else if self.cursor.line.get() > 0 { } else if self.cursor.line.get() > 0 {
self.cursor.line = TextLineIndex::new(self.cursor.line.get() - 1); self.cursor.line = TextLineIndex::new(self.cursor.line.get() - 1);
self.cursor.index = self.lines[self.cursor.line.get()].text.len(); self.cursor.index = self.lines[self.cursor.line.get()].text.len();
self.shape_until_cursor(CursorScroll::Bottom);
} }
}, },
TextAction::Next => { TextAction::Next => {
@ -427,13 +407,9 @@ impl<'a> TextBuffer<'a> {
break; break;
} }
} }
self.shape_until_cursor(CursorScroll::Bottom);
} else if self.cursor.line.get() + 1 < self.lines.len() { } else if self.cursor.line.get() + 1 < self.lines.len() {
self.cursor.line = TextLineIndex::new(self.cursor.line.get() + 1); self.cursor.line = TextLineIndex::new(self.cursor.line.get() + 1);
self.cursor.index = 0; self.cursor.index = 0;
self.shape_until_cursor(CursorScroll::Bottom);
} }
}, },
TextAction::Left => { TextAction::Left => {
@ -466,8 +442,8 @@ impl<'a> TextBuffer<'a> {
self.shape_until_scroll(); self.shape_until_scroll();
}, },
TextAction::Insert(character) => if character.is_control() { TextAction::Insert(character) => if character.is_control() && character != '\t' {
// Filter out special chars, use TextAction instead // Filter out special chars (except for tab), use TextAction instead
log::debug!("Refusing to insert control character {:?}", character); log::debug!("Refusing to insert control character {:?}", character);
} else { } else {
let line = &mut self.lines[self.cursor.line.get()]; let line = &mut self.lines[self.cursor.line.get()];
@ -475,8 +451,6 @@ impl<'a> TextBuffer<'a> {
line.text.insert(self.cursor.index, character); line.text.insert(self.cursor.index, character);
self.cursor.index += character.len_utf8(); self.cursor.index += character.len_utf8();
self.shape_until_cursor(CursorScroll::Bottom);
}, },
TextAction::Enter => { TextAction::Enter => {
let new_line = { let new_line = {
@ -490,8 +464,6 @@ impl<'a> TextBuffer<'a> {
self.cursor.line = TextLineIndex::new(next_line); self.cursor.line = TextLineIndex::new(next_line);
self.cursor.index = 0; self.cursor.index = 0;
self.shape_until_cursor(CursorScroll::Bottom);
}, },
TextAction::Backspace => { TextAction::Backspace => {
if self.cursor.index > 0 { if self.cursor.index > 0 {
@ -511,8 +483,6 @@ impl<'a> TextBuffer<'a> {
self.cursor.index = prev_index; self.cursor.index = prev_index;
line.text.remove(self.cursor.index); line.text.remove(self.cursor.index);
self.shape_until_cursor(CursorScroll::Top);
} else if self.cursor.line.get() > 0 { } else if self.cursor.line.get() > 0 {
let mut line_index = self.cursor.line.get(); let mut line_index = self.cursor.line.get();
let old_line = self.lines.remove(line_index); let old_line = self.lines.remove(line_index);
@ -525,8 +495,6 @@ impl<'a> TextBuffer<'a> {
self.cursor.index = line.text.len(); self.cursor.index = line.text.len();
line.text.push_str(&old_line.text); line.text.push_str(&old_line.text);
self.shape_until_cursor(CursorScroll::Top);
} }
}, },
TextAction::Delete => { TextAction::Delete => {
@ -535,8 +503,6 @@ impl<'a> TextBuffer<'a> {
line.reset(); line.reset();
line.text.remove(self.cursor.index); line.text.remove(self.cursor.index);
self.shape_until_cursor(CursorScroll::Bottom);
} else if self.cursor.line.get() + 1 < self.lines.len() { } else if self.cursor.line.get() + 1 < self.lines.len() {
let old_line = self.lines.remove(self.cursor.line.get() + 1); let old_line = self.lines.remove(self.cursor.line.get() + 1);
@ -544,8 +510,6 @@ impl<'a> TextBuffer<'a> {
line.reset(); line.reset();
line.text.push_str(&old_line.text); line.text.push_str(&old_line.text);
self.shape_until_cursor(CursorScroll::Bottom);
} }
}, },
TextAction::Click { x, y } => { TextAction::Click { x, y } => {