diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index a247ae5..d9102c2 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -157,7 +157,12 @@ where let mut buffer = self.buffer.lock().unwrap(); let mut cache = self.cache.lock().unwrap(); - buffer.shape_until_cursor(); + if buffer.cursor_moved { + buffer.shape_until_cursor(); + buffer.cursor_moved = false; + } else { + buffer.shape_until_scroll(); + } let buffer_x = layout.bounds().x; let buffer_y = layout.bounds().y; diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index 8dc8f8f..939471b 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -73,13 +73,15 @@ fn main() { let mut mouse_y = -1; let mut mouse_left = false; - //Lets do this once and rely on events for the rest. - buffer.shape_until_cursor(); - loop { let mut force_drag = true; - buffer.shape_until_scroll(); + if buffer.cursor_moved { + buffer.shape_until_cursor(); + buffer.cursor_moved = false; + } else { + buffer.shape_until_scroll(); + } if buffer.redraw { let instant = Instant::now(); @@ -203,8 +205,6 @@ fn main() { } else if mouse_y + 5 >= window.height() as i32 { buffer.action(TextAction::Scroll { lines: 3 }); window_async = true; - } else { - buffer.shape_until_cursor() } force_drag = false; @@ -218,8 +218,6 @@ fn main() { x: mouse_x - line_x, y: mouse_y, }); - - buffer.shape_until_cursor() } force_drag = false; } diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index a51d8c5..d38ada5 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -9,7 +9,12 @@ fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut Sw let bg_color = Color::rgb(0x34, 0x34, 0x34); let font_color = Color::rgb(0xFF, 0xFF, 0xFF); - buffer.shape_until_cursor(); + if buffer.cursor_moved { + buffer.shape_until_cursor(); + buffer.cursor_moved = false; + } else { + buffer.shape_until_scroll(); + } if buffer.redraw { let instant = Instant::now(); diff --git a/src/buffer.rs b/src/buffer.rs index a84801f..7b9bf46 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -246,6 +246,7 @@ pub struct TextBuffer<'a> { scroll: i32, cursor: TextCursor, select_opt: Option, + pub cursor_moved: bool, pub redraw: bool, } @@ -266,6 +267,7 @@ impl<'a> TextBuffer<'a> { scroll: 0, cursor: TextCursor::default(), select_opt: None, + cursor_moved: false, redraw: false, }; buffer.set_text(""); @@ -323,20 +325,9 @@ impl<'a> TextBuffer<'a> { self.width ); if line_i == self.cursor.line.get() { - for layout_line in layout { - let mut found = false; - for glyph in layout_line.glyphs.iter() { - if glyph.start <= self.cursor.index { - found = true; - break; - } - } - if found { - layout_i += 1; - } else { - break; - } - } + let layout_cursor = self.layout_cursor(&self.cursor); + layout_i += layout_cursor.layout as i32; + break; } else { layout_i += layout.len() as i32; } @@ -559,6 +550,8 @@ impl<'a> TextBuffer<'a> { /// Perform a [TextAction] on the buffer pub fn action(&mut self, action: TextAction) { + let mut old_cursor = self.cursor; + match action { TextAction::Previous => { let line = &mut self.lines[self.cursor.line.get()]; @@ -786,6 +779,10 @@ impl<'a> TextBuffer<'a> { self.shape_until_scroll(); } } + + if old_cursor != self.cursor { + self.cursor_moved = true; + } } /// Convert x, y position to TextCursor (hit detection)