From f962c6ce0671b4b0b0e683148353fc3a3d1fc3c3 Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Mon, 15 Apr 2024 08:41:29 -0400 Subject: Allow checking whether a TextEditor is focused --- widget/src/text_editor.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 92cdb251..5b0b1b79 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -319,7 +319,9 @@ where } } -struct State { +/// The state of a [`TextEditor`]. +#[derive(Debug)] +pub struct State { is_focused: bool, last_click: Option, drag_click: Option, @@ -329,6 +331,13 @@ struct State { highlighter_format_address: usize, } +impl State { + /// Returns whether the [`TextEditor`] is currently focused or not. + pub fn is_focused(&self) -> bool { + self.is_focused + } +} + impl<'a, Highlighter, Message, Theme, Renderer> Widget for TextEditor<'a, Highlighter, Message, Theme, Renderer> where -- cgit From 73088a6fc1567f8bd557fdf479e16b395032b019 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 26 Apr 2024 15:17:35 +0200 Subject: Fix out of bounds caret in `TextEditor` in some circumstances --- widget/src/text_editor.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 5b0b1b79..7c0b98ea 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -569,23 +569,27 @@ where if state.is_focused { match internal.editor.cursor() { Cursor::Caret(position) => { - let position = position + translation; + let cursor = + Rectangle::new( + position + translation, + Size::new( + 1.0, + self.line_height + .to_absolute(self.text_size.unwrap_or_else( + || renderer.default_size(), + )) + .into(), + ), + ); - if bounds.contains(position) { + if let Some(clipped_cursor) = bounds.intersection(&cursor) { renderer.fill_quad( renderer::Quad { bounds: Rectangle { - x: position.x.floor(), - y: position.y, - width: 1.0, - height: self - .line_height - .to_absolute( - self.text_size.unwrap_or_else( - || renderer.default_size(), - ), - ) - .into(), + x: clipped_cursor.x.floor(), + y: clipped_cursor.y, + width: clipped_cursor.width, + height: clipped_cursor.height, }, ..renderer::Quad::default() }, -- cgit