Improvements for enter and cursor positioning

This commit is contained in:
Jeremy Soller 2022-10-21 11:51:04 -06:00
parent 784215e490
commit 88b78e059c
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE

View file

@ -432,8 +432,14 @@ impl<'a> TextBuffer<'a> {
self.shape_until_cursor(CursorScroll::Bottom); self.shape_until_cursor(CursorScroll::Bottom);
}, },
TextAction::Enter => { TextAction::Enter => {
let new_line = {
let line = &mut self.lines[self.cursor.line.get()];
line.reset();
line.text.split_off(self.cursor.index)
};
let next_line = self.cursor.line.get() + 1; let next_line = self.cursor.line.get() + 1;
self.lines.insert(next_line, TextBufferLine::new(String::new())); self.lines.insert(next_line, TextBufferLine::new(new_line));
self.cursor.line = TextLineIndex::new(next_line); self.cursor.line = TextLineIndex::new(next_line);
self.cursor.index = 0; self.cursor.index = 0;
@ -630,15 +636,24 @@ impl<'a> TextBuffer<'a> {
} }
let cursor_glyph_opt = |cursor: &TextCursor| -> Option<usize> { let cursor_glyph_opt = |cursor: &TextCursor| -> Option<usize> {
let mut glyph_i_opt = None; if cursor.line.get() == line_i {
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() { for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
if cursor.index == glyph.start { if cursor.index == glyph.start {
glyph_i_opt = Some(glyph_i); return Some(glyph_i);
} else if cursor.index == glyph.end { }
glyph_i_opt = Some(glyph_i + 1); }
match layout_line.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return Some(layout_line.glyphs.len());
}
},
None => {
return Some(0);
}
} }
} }
glyph_i_opt None
}; };
// Highlight selection (TODO: HIGHLIGHT COLOR!) // Highlight selection (TODO: HIGHLIGHT COLOR!)
@ -736,43 +751,41 @@ impl<'a> TextBuffer<'a> {
} }
// Draw cursor // Draw cursor
if self.cursor.line.get() == line_i { if let Some(cursor_glyph) = cursor_glyph_opt(&self.cursor) {
if let Some(cursor_glyph) = cursor_glyph_opt(&self.cursor) { let x = match layout_line.glyphs.get(cursor_glyph) {
let x = match layout_line.glyphs.get(cursor_glyph) { Some(glyph) => {
// Start of detected glyph
if shape.rtl {
(glyph.x + glyph.w) as i32
} else {
glyph.x as i32
}
},
None => match layout_line.glyphs.last() {
Some(glyph) => { Some(glyph) => {
// Start of detected glyph // End of last glyph
if shape.rtl { if shape.rtl {
(glyph.x + glyph.w) as i32
} else {
glyph.x as i32 glyph.x as i32
} else {
(glyph.x + glyph.w) as i32
} }
}, },
None => match layout_line.glyphs.last() { None => {
Some(glyph) => { // Start of empty line
// End of last glyph 0
if shape.rtl {
glyph.x as i32
} else {
(glyph.x + glyph.w) as i32
}
},
None => {
// Start of empty line
0
}
} }
}; }
};
println!("x: {}", x); println!("x: {}", x);
f( f(
x, x,
line_y - font_size, line_y - font_size,
1, 1,
line_height as u32, line_height as u32,
color, color,
); );
}
} }
layout_line.draw(color, |x, y, color| { layout_line.draw(color, |x, y, color| {