From 4d2fa4298ec818e4f796de4f67416b0f175f740a Mon Sep 17 00:00:00 2001
From: alex-ds13 <145657253+alex-ds13@users.noreply.github.com>
Date: Tue, 25 Nov 2025 15:56:51 +0000
Subject: fix: take visual bounds into consideration when text is centered
---
src/text/rich.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 53 insertions(+), 4 deletions(-)
(limited to 'src/text/rich.rs')
diff --git a/src/text/rich.rs b/src/text/rich.rs
index 29c43f5..30a9e81 100644
--- a/src/text/rich.rs
+++ b/src/text/rich.rs
@@ -225,6 +225,7 @@ struct State {
dragging: Option,
last_click: Option,
keyboard_modifiers: keyboard::Modifiers,
+ visual_lines_bounds: Vec,
}
impl State {
@@ -242,10 +243,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();
@@ -302,6 +324,23 @@ impl State {
})
.collect()
}
+
+ 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 Widget
@@ -326,6 +365,7 @@ where
dragging: None,
last_click: None,
keyboard_modifiers: keyboard::Modifiers::default(),
+ visual_lines_bounds: Vec::new(),
})
}
@@ -861,6 +901,7 @@ where
state.paragraph =
Renderer::Paragraph::with_spans(text_with_spans());
state.spans = spans.iter().cloned().map(Span::to_static).collect();
+ state.update_visual_bounds();
} else {
match state.paragraph.compare(core::Text {
content: (),
@@ -877,10 +918,12 @@ where
core::text::Difference::None => {}
core::text::Difference::Bounds => {
state.paragraph.resize(bounds);
+ state.update_visual_bounds();
}
core::text::Difference::Shape => {
state.paragraph =
Renderer::Paragraph::with_spans(text_with_spans());
+ state.update_visual_bounds();
}
}
}
@@ -942,10 +985,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
@@ -963,7 +1012,7 @@ fn highlight_line(
.map(|glyph| glyph.w)
.sum();
- (x, width)
+ (x_offset + x, width)
}
})
}
--
cgit v1.2.3