Fix scrolling with cursor

This commit is contained in:
Jeremy Soller 2022-10-25 20:49:15 -06:00
parent c031944b45
commit e53956cb2d
4 changed files with 29 additions and 24 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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();

View file

@ -246,6 +246,7 @@ pub struct TextBuffer<'a> {
scroll: i32,
cursor: TextCursor,
select_opt: Option<TextCursor>,
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)