From ff3e4423f9359349ae3ab79e7850c7f72d6031f4 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Thu, 27 Jun 2024 00:20:05 +0200 Subject: [PATCH] feat(text_input): add `select_on_focus` field --- src/widget/text_input/input.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/widget/text_input/input.rs b/src/widget/text_input/input.rs index f088c490..916a7e3d 100644 --- a/src/widget/text_input/input.rs +++ b/src/widget/text_input/input.rs @@ -188,6 +188,7 @@ pub struct TextInput<'a, Message> { is_secure: bool, is_editable: bool, is_read_only: bool, + select_on_focus: bool, font: Option<::Font>, width: Length, padding: Padding, @@ -232,6 +233,7 @@ where is_secure: false, is_editable: false, is_read_only: false, + select_on_focus: false, font: None, width: Length::Fill, padding: [spacing, spacing, spacing, spacing].into(), @@ -300,16 +302,22 @@ where self } - fn editable(mut self) -> Self { + pub fn editable(mut self) -> Self { self.is_editable = true; self } - fn editing(mut self, enable: bool) -> Self { + pub fn editing(mut self, enable: bool) -> Self { self.is_read_only = !enable; self } + /// Selects all text when the text input is focused + pub fn select_on_focus(mut self, select_on_focus: bool) -> Self { + self.select_on_focus = select_on_focus; + self + } + /// Sets the message that should be produced when some text is typed into /// the [`TextInput`]. /// @@ -520,6 +528,7 @@ where self.is_secure, self.is_read_only, self.always_active, + self.select_on_focus, )) } @@ -527,7 +536,7 @@ where let state = tree.state.downcast_mut::(); // Unfocus text input if it becomes disabled - if self.on_input.is_none() || state.is_read_only { + if self.on_input.is_none() { state.last_click = None; state.is_focused = None; state.is_pasting = None; @@ -2405,6 +2414,7 @@ pub struct State { pub dirty: bool, pub is_secure: bool, pub is_read_only: bool, + select_on_focus: bool, is_focused: Option, dragging_state: Option, #[cfg(feature = "wayland")] @@ -2424,7 +2434,7 @@ struct Focus { impl State { /// Creates a new [`State`], representing an unfocused [`TextInput`]. - pub fn new(is_secure: bool, is_read_only: bool, always_active: bool) -> Self { + pub fn new(is_secure: bool, is_read_only: bool, always_active: bool, select_on_focus: bool) -> Self { Self { is_secure, is_read_only, @@ -2435,6 +2445,7 @@ impl State { now, } }), + select_on_focus, ..Self::default() } } @@ -2473,6 +2484,7 @@ impl State { helper_text: crate::Paragraph::new(), is_read_only, is_focused: None, + select_on_focus: false, dragging_state: None, #[cfg(feature = "wayland")] dnd_offer: DndOfferState::default(), @@ -2505,7 +2517,11 @@ impl State { now, }); - self.move_cursor_to_end(); + if self.select_on_focus { + self.select_all() + } else { + self.move_cursor_to_end(); + } } /// Unfocuses the [`TextInput`].