feat(text_input): add select_on_focus field

This commit is contained in:
Michael Aaron Murphy 2024-06-27 00:20:05 +02:00
parent 7e30695c52
commit ff3e4423f9
No known key found for this signature in database
GPG key ID: B2732D4240C9212C

View file

@ -188,6 +188,7 @@ pub struct TextInput<'a, Message> {
is_secure: bool, is_secure: bool,
is_editable: bool, is_editable: bool,
is_read_only: bool, is_read_only: bool,
select_on_focus: bool,
font: Option<<crate::Renderer as iced_core::text::Renderer>::Font>, font: Option<<crate::Renderer as iced_core::text::Renderer>::Font>,
width: Length, width: Length,
padding: Padding, padding: Padding,
@ -232,6 +233,7 @@ where
is_secure: false, is_secure: false,
is_editable: false, is_editable: false,
is_read_only: false, is_read_only: false,
select_on_focus: false,
font: None, font: None,
width: Length::Fill, width: Length::Fill,
padding: [spacing, spacing, spacing, spacing].into(), padding: [spacing, spacing, spacing, spacing].into(),
@ -300,16 +302,22 @@ where
self self
} }
fn editable(mut self) -> Self { pub fn editable(mut self) -> Self {
self.is_editable = true; self.is_editable = true;
self self
} }
fn editing(mut self, enable: bool) -> Self { pub fn editing(mut self, enable: bool) -> Self {
self.is_read_only = !enable; self.is_read_only = !enable;
self 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 /// Sets the message that should be produced when some text is typed into
/// the [`TextInput`]. /// the [`TextInput`].
/// ///
@ -520,6 +528,7 @@ where
self.is_secure, self.is_secure,
self.is_read_only, self.is_read_only,
self.always_active, self.always_active,
self.select_on_focus,
)) ))
} }
@ -527,7 +536,7 @@ where
let state = tree.state.downcast_mut::<State>(); let state = tree.state.downcast_mut::<State>();
// Unfocus text input if it becomes disabled // 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.last_click = None;
state.is_focused = None; state.is_focused = None;
state.is_pasting = None; state.is_pasting = None;
@ -2405,6 +2414,7 @@ pub struct State {
pub dirty: bool, pub dirty: bool,
pub is_secure: bool, pub is_secure: bool,
pub is_read_only: bool, pub is_read_only: bool,
select_on_focus: bool,
is_focused: Option<Focus>, is_focused: Option<Focus>,
dragging_state: Option<DraggingState>, dragging_state: Option<DraggingState>,
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
@ -2424,7 +2434,7 @@ struct Focus {
impl State { impl State {
/// Creates a new [`State`], representing an unfocused [`TextInput`]. /// 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 { Self {
is_secure, is_secure,
is_read_only, is_read_only,
@ -2435,6 +2445,7 @@ impl State {
now, now,
} }
}), }),
select_on_focus,
..Self::default() ..Self::default()
} }
} }
@ -2473,6 +2484,7 @@ impl State {
helper_text: crate::Paragraph::new(), helper_text: crate::Paragraph::new(),
is_read_only, is_read_only,
is_focused: None, is_focused: None,
select_on_focus: false,
dragging_state: None, dragging_state: None,
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
dnd_offer: DndOfferState::default(), dnd_offer: DndOfferState::default(),
@ -2505,7 +2517,11 @@ impl State {
now, now,
}); });
self.move_cursor_to_end(); if self.select_on_focus {
self.select_all()
} else {
self.move_cursor_to_end();
}
} }
/// Unfocuses the [`TextInput`]. /// Unfocuses the [`TextInput`].