Fix selection clipping out of bounds in TextEditor

This commit is contained in:
Héctor Ramón Jiménez 2023-09-22 05:50:31 +02:00
parent 68d49459ce
commit 70e49df428
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -406,20 +406,30 @@ where
style.text_color, style.text_color,
); );
let translation = Vector::new(
bounds.x + self.padding.left,
bounds.y + self.padding.top,
);
if state.is_focused { if state.is_focused {
match internal.editor.cursor() { match internal.editor.cursor() {
Cursor::Caret(position) => { Cursor::Caret(position) => {
let position = position + translation;
if bounds.contains(position) {
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
bounds: Rectangle { bounds: Rectangle {
x: position.x + bounds.x + self.padding.left, x: position.x,
y: position.y + bounds.y + self.padding.top, y: position.y,
width: 1.0, width: 1.0,
height: self height: self
.line_height .line_height
.to_absolute(self.text_size.unwrap_or_else( .to_absolute(
self.text_size.unwrap_or_else(
|| renderer.default_size(), || renderer.default_size(),
)) ),
)
.into(), .into(),
}, },
border_radius: 0.0.into(), border_radius: 0.0.into(),
@ -429,15 +439,14 @@ where
theme.value_color(&self.style), theme.value_color(&self.style),
); );
} }
}
Cursor::Selection(ranges) => { Cursor::Selection(ranges) => {
for range in ranges { for range in ranges.into_iter().filter_map(|range| {
bounds.intersection(&(range + translation))
}) {
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
bounds: range bounds: range,
+ Vector::new(
bounds.x + self.padding.left,
bounds.y + self.padding.top,
),
border_radius: 0.0.into(), border_radius: 0.0.into(),
border_width: 0.0, border_width: 0.0,
border_color: Color::TRANSPARENT, border_color: Color::TRANSPARENT,