Use layout_runs for hit detection
This commit is contained in:
parent
b6bd72335c
commit
0336e0c5e9
1 changed files with 69 additions and 82 deletions
|
|
@ -762,38 +762,23 @@ impl<'a> TextBuffer<'a> {
|
||||||
let font_size = self.metrics.font_size;
|
let font_size = self.metrics.font_size;
|
||||||
let line_height = self.metrics.line_height;
|
let line_height = self.metrics.line_height;
|
||||||
|
|
||||||
let mut line_y = font_size;
|
let mut new_cursor = self.cursor;
|
||||||
let mut layout_i = 0;
|
|
||||||
for (line_i, line) in self.lines.iter().enumerate() {
|
|
||||||
let layout = match line.layout_opt.as_ref() {
|
|
||||||
Some(some) => some,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
for layout_line in layout {
|
for run in self.layout_runs() {
|
||||||
let scrolled = layout_i < self.scroll;
|
let line_y = run.line_y;
|
||||||
layout_i += 1;
|
|
||||||
|
|
||||||
if scrolled {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if line_y > self.height {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if mouse_y >= line_y - font_size
|
if mouse_y >= line_y - font_size
|
||||||
&& mouse_y < line_y - font_size + line_height
|
&& mouse_y < line_y - font_size + line_height
|
||||||
{
|
{
|
||||||
let mut new_cursor_glyph = layout_line.glyphs.len();
|
let mut new_cursor_glyph = run.glyphs.len();
|
||||||
let mut new_cursor_char = 0;
|
let mut new_cursor_char = 0;
|
||||||
'hit: for (glyph_i, glyph) in layout_line.glyphs.iter().enumerate() {
|
'hit: for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
|
||||||
if mouse_x >= glyph.x as i32
|
if mouse_x >= glyph.x as i32
|
||||||
&& mouse_x <= (glyph.x + glyph.w) as i32
|
&& mouse_x <= (glyph.x + glyph.w) as i32
|
||||||
{
|
{
|
||||||
new_cursor_glyph = glyph_i;
|
new_cursor_glyph = glyph_i;
|
||||||
|
|
||||||
let cluster = &line.text[glyph.start..glyph.end];
|
let cluster = &run.text[glyph.start..glyph.end];
|
||||||
let total = cluster.grapheme_indices(true).count();
|
let total = cluster.grapheme_indices(true).count();
|
||||||
let mut egc_x = glyph.x;
|
let mut egc_x = glyph.x;
|
||||||
let egc_w = glyph.w / (total as f32);
|
let egc_w = glyph.w / (total as f32);
|
||||||
|
|
@ -822,26 +807,24 @@ impl<'a> TextBuffer<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut new_cursor = TextCursor::new(TextLineIndex::new(line_i), 0);
|
new_cursor.line = run.line_i;
|
||||||
|
new_cursor.index = 0;
|
||||||
|
|
||||||
match layout_line.glyphs.get(new_cursor_glyph) {
|
match run.glyphs.get(new_cursor_glyph) {
|
||||||
Some(glyph) => {
|
Some(glyph) => {
|
||||||
// Position at glyph
|
// Position at glyph
|
||||||
new_cursor.index = glyph.start + new_cursor_char;
|
new_cursor.index = glyph.start + new_cursor_char;
|
||||||
},
|
},
|
||||||
None => if let Some(glyph) = layout_line.glyphs.last() {
|
None => if let Some(glyph) = run.glyphs.last() {
|
||||||
// Position at end of line
|
// Position at end of line
|
||||||
new_cursor.index = glyph.end;
|
new_cursor.index = glyph.end;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if new_cursor != self.cursor {
|
if new_cursor != self.cursor {
|
||||||
self.cursor = new_cursor;
|
if let Some(glyph) = run.glyphs.get(new_cursor_glyph) {
|
||||||
self.redraw = true;
|
|
||||||
|
|
||||||
if let Some(glyph) = layout_line.glyphs.get(new_cursor_glyph) {
|
|
||||||
let font_opt = self.font_matches.get_font(&glyph.cache_key.font_id);
|
let font_opt = self.font_matches.get_font(&glyph.cache_key.font_id);
|
||||||
let text_glyph = &line.text[glyph.start..glyph.end];
|
let text_glyph = &run.text[glyph.start..glyph.end];
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"{}, {}: '{}' ('{}'): '{}' ({:?})",
|
"{}, {}: '{}' ('{}'): '{}' ({:?})",
|
||||||
self.cursor.line.get(),
|
self.cursor.line.get(),
|
||||||
|
|
@ -853,10 +836,14 @@ impl<'a> TextBuffer<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
line_y += line_height;
|
if new_cursor != self.cursor {
|
||||||
}
|
self.cursor = new_cursor;
|
||||||
|
self.redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let duration = instant.elapsed();
|
let duration = instant.elapsed();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue