Partially implement selection

This commit is contained in:
Jeremy Soller 2022-10-21 09:49:16 -06:00
parent 1e4d951d74
commit 3ed1e938f5
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE

View file

@ -645,7 +645,18 @@ impl<'a> TextBuffer<'a> {
return; return;
} }
/*TODO let cursor_glyph_opt = |cursor: &TextCursor| -> Option<usize> {
let mut glyph_i_opt = None;
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
if cursor.start == glyph.start {
glyph_i_opt = Some(glyph_i);
} else if cursor.start == glyph.end {
glyph_i_opt = Some(glyph_i + 1);
}
}
glyph_i_opt
};
// Highlight selection (TODO: HIGHLIGHT COLOR!) // Highlight selection (TODO: HIGHLIGHT COLOR!)
if let Some(select) = self.select_opt { if let Some(select) = self.select_opt {
let (start, end) = if select.line < self.cursor.line { let (start, end) = if select.line < self.cursor.line {
@ -654,43 +665,43 @@ impl<'a> TextBuffer<'a> {
(self.cursor, select) (self.cursor, select)
} else { } else {
/* select.line == self.cursor.line */ /* select.line == self.cursor.line */
if select.glyph < self.cursor.glyph { if select.start < self.cursor.start {
(select, self.cursor) (select, self.cursor)
} else { } else {
/* select.glyph >= self.cursor.glyph */ /* select.start >= self.cursor.start */
(self.cursor, select) (self.cursor, select)
} }
}; };
if line_i_scrolled >= start.line && line_i_scrolled <= end.line { if line_i >= start.line.get() && line_i <= end.line.get() {
let start_glyph = if start.line == line_i_scrolled { let start_glyph = if start.line.get() == line_i {
start.glyph cursor_glyph_opt(&start).unwrap_or(0)
} else { } else {
0 0
}; };
let end_glyph = if end.line == line_i_scrolled { let end_glyph = if end.line.get() == line_i {
end.glyph cursor_glyph_opt(&end).unwrap_or(layout_line.glyphs.len() + 1)
} else { } else {
line.glyphs.len() + 1 layout_line.glyphs.len() + 1
}; };
if end_glyph > start_glyph { if end_glyph > start_glyph {
let (left_x, right_x) = if line.rtl { let (left_x, right_x) = if shape.rtl {
( (
line.glyphs.get(end_glyph - 1).map_or(0, |glyph| { layout_line.glyphs.get(end_glyph - 1).map_or(0, |glyph| {
glyph.x as i32 glyph.x as i32
}), }),
line.glyphs.get(start_glyph).map_or(self.width, |glyph| { layout_line.glyphs.get(start_glyph).map_or(self.width, |glyph| {
(glyph.x + glyph.w) as i32 (glyph.x + glyph.w) as i32
}), }),
) )
} else { } else {
( (
line.glyphs.get(start_glyph).map_or(0, |glyph| { layout_line.glyphs.get(start_glyph).map_or(0, |glyph| {
glyph.x as i32 glyph.x as i32
}), }),
line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| { layout_line.glyphs.get(end_glyph - 1).map_or(self.width, |glyph| {
(glyph.x + glyph.w) as i32 (glyph.x + glyph.w) as i32
}), }),
) )
@ -706,29 +717,33 @@ impl<'a> TextBuffer<'a> {
} }
} }
} }
*/
// Draw cursor // Draw cursor
if self.cursor.line.get() == line_i { if self.cursor.line.get() == line_i {
println!("FOUND CURSOR LINE {}", line_i); if let Some(cursor_glyph) = cursor_glyph_opt(&self.cursor) {
let x = match layout_line.glyphs.get(cursor_glyph) {
let mut cursor_glyph_opt = None; Some(glyph) => {
for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() { // Start of detected glyph
if self.cursor.start == glyph.start { if shape.rtl {
cursor_glyph_opt = Some((glyph_i, false)); (glyph.x + glyph.w) as i32
} else if self.cursor.start == glyph.end { } else {
cursor_glyph_opt = Some((glyph_i, true)); glyph.x as i32
} }
} },
None => match layout_line.glyphs.last() {
if let Some((cursor_glyph, cursor_after)) = cursor_glyph_opt { Some(glyph) => {
println!("FOUND CURSOR GLYPH {}, {}", cursor_glyph, cursor_after); // End of last glyph
if shape.rtl {
let glyph = &layout_line.glyphs[cursor_glyph]; glyph.x as i32
let x = if shape.rtl != cursor_after { } else {
(glyph.x + glyph.w) as i32 (glyph.x + glyph.w) as i32
} else { }
glyph.x as i32 },
None => {
// Start of empty line
0
}
}
}; };
println!("x: {}", x); println!("x: {}", x);