diff options
Diffstat (limited to 'src/text.rs')
| -rw-r--r-- | src/text.rs | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/src/text.rs b/src/text.rs index 5e1356f..648238f 100644 --- a/src/text.rs +++ b/src/text.rs @@ -178,6 +178,7 @@ pub struct State { dragging: Option<Dragging>, last_click: Option<mouse::Click>, keyboard_modifiers: keyboard::Modifiers, + visual_lines_bounds: Vec<core::Rectangle>, } /// The type of dragging selection. @@ -204,10 +205,31 @@ impl State { }; let bounded_y = point.y.max(bounds.y).min(bounds.y + bounds.height); let bounded_point = Point::new(bounded_x, bounded_y); - let relative_point = + let mut relative_point = bounded_point - core::Vector::new(bounds.x, bounds.y); let buffer = self.paragraph.buffer(); + let line_height = buffer.metrics().line_height; + let visual_line = (relative_point.y / line_height).floor() as usize; + let visual_line_start_offset = self + .visual_lines_bounds + .get(visual_line) + .map(|r| r.x) + .unwrap_or_default(); + let visual_line_end = self + .visual_lines_bounds + .get(visual_line) + .map(|r| r.x + r.width) + .unwrap_or_default(); + + if relative_point.x < visual_line_start_offset { + relative_point.x = visual_line_start_offset; + } + + if relative_point.x > visual_line_end { + relative_point.x = visual_line_end; + } + let cursor = buffer.hit(relative_point.x, relative_point.y)?; let value = buffer.lines[cursor.line].text(); @@ -269,17 +291,39 @@ impl State { if self.content != text.content { text.content.clone_into(&mut self.content); self.paragraph = Paragraph::with_text(text); + self.update_visual_bounds(); return; } match self.paragraph.compare(text.with_content(())) { text::Difference::None => {} - text::Difference::Bounds => self.paragraph.resize(text.bounds), + text::Difference::Bounds => { + self.paragraph.resize(text.bounds); + self.update_visual_bounds(); + } text::Difference::Shape => { self.paragraph = Paragraph::with_text(text); + self.update_visual_bounds(); } } } + + fn update_visual_bounds(&mut self) { + let buffer = self.paragraph.buffer(); + let line_height = buffer.metrics().line_height; + self.visual_lines_bounds = buffer + .lines + .iter() + .flat_map(|line| highlight_line(line, 0, line.text().len())) + .enumerate() + .map(|(visual_line, (x, width))| core::Rectangle { + x, + width, + y: visual_line as f32 * line_height - buffer.scroll().vertical, + height: line_height, + }) + .collect(); + } } impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> @@ -789,10 +833,16 @@ fn highlight_line( let range = start.max(from)..end.min(to); + let x_offset = visual_line + .glyphs + .first() + .map(|glyph| glyph.x) + .unwrap_or_default(); + if range.is_empty() { - (0.0, 0.0) + (x_offset, 0.0) } else if range.start == start && range.end == end { - (0.0, visual_line.w) + (x_offset, visual_line.w) } else { let first_glyph = visual_line .glyphs @@ -810,7 +860,7 @@ fn highlight_line( .map(|glyph| glyph.w) .sum(); - (x, width) + (x_offset + x, width) } }) } |
