diff options
author | 2023-09-22 05:50:31 +0200 | |
---|---|---|
committer | 2023-09-22 05:50:31 +0200 | |
commit | 70e49df4289b925d24f92ce5c91ef2b03dbc54e3 (patch) | |
tree | cec1de33f870477b5493b241ff11b753b1fce43c /widget/src/text_editor.rs | |
parent | 68d49459ce0e8b28e56b71970cb26e66ac1b01b4 (diff) | |
download | iced-70e49df4289b925d24f92ce5c91ef2b03dbc54e3.tar.gz iced-70e49df4289b925d24f92ce5c91ef2b03dbc54e3.tar.bz2 iced-70e49df4289b925d24f92ce5c91ef2b03dbc54e3.zip |
Fix selection clipping out of bounds in `TextEditor`
Diffstat (limited to 'widget/src/text_editor.rs')
-rw-r--r-- | widget/src/text_editor.rs | 57 |
1 files changed, 33 insertions, 24 deletions
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index e8187b9c..c142c22d 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -406,38 +406,47 @@ where style.text_color, ); + let translation = Vector::new( + bounds.x + self.padding.left, + bounds.y + self.padding.top, + ); + if state.is_focused { match internal.editor.cursor() { Cursor::Caret(position) => { - renderer.fill_quad( - renderer::Quad { - bounds: Rectangle { - x: position.x + bounds.x + self.padding.left, - y: position.y + bounds.y + self.padding.top, - width: 1.0, - height: self - .line_height - .to_absolute(self.text_size.unwrap_or_else( - || renderer.default_size(), - )) - .into(), + let position = position + translation; + + if bounds.contains(position) { + renderer.fill_quad( + renderer::Quad { + bounds: Rectangle { + x: position.x, + y: position.y, + width: 1.0, + height: self + .line_height + .to_absolute( + self.text_size.unwrap_or_else( + || renderer.default_size(), + ), + ) + .into(), + }, + border_radius: 0.0.into(), + border_width: 0.0, + border_color: Color::TRANSPARENT, }, - border_radius: 0.0.into(), - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - theme.value_color(&self.style), - ); + theme.value_color(&self.style), + ); + } } Cursor::Selection(ranges) => { - for range in ranges { + for range in ranges.into_iter().filter_map(|range| { + bounds.intersection(&(range + translation)) + }) { renderer.fill_quad( renderer::Quad { - bounds: range - + Vector::new( - bounds.x + self.padding.left, - bounds.y + self.padding.top, - ), + bounds: range, border_radius: 0.0.into(), border_width: 0.0, border_color: Color::TRANSPARENT, |