From 1448c5bfa5d0977e54670bb8c640ba186bb13167 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 18 Sep 2024 20:30:14 +0200 Subject: Implement some `From` traits for `text_input::Id` --- widget/src/text_input.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'widget') diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index d5ede524..3032dd13 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -114,8 +114,8 @@ where } /// Sets the [`Id`] of the [`TextInput`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); + pub fn id(mut self, id: impl Into) -> Self { + self.id = Some(id.into()); self } @@ -1226,38 +1226,53 @@ impl From for widget::Id { } } +impl From<&'static str> for Id { + fn from(id: &'static str) -> Self { + Self::new(id) + } +} + +impl From for Id { + fn from(id: String) -> Self { + Self::new(id) + } +} + /// Produces a [`Task`] that focuses the [`TextInput`] with the given [`Id`]. -pub fn focus(id: Id) -> Task { - task::effect(Action::widget(operation::focusable::focus(id.0))) +pub fn focus(id: impl Into) -> Task { + task::effect(Action::widget(operation::focusable::focus(id.into().0))) } /// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the /// end. -pub fn move_cursor_to_end(id: Id) -> Task { +pub fn move_cursor_to_end(id: impl Into) -> Task { task::effect(Action::widget(operation::text_input::move_cursor_to_end( - id.0, + id.into().0, ))) } /// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the /// front. -pub fn move_cursor_to_front(id: Id) -> Task { +pub fn move_cursor_to_front(id: impl Into) -> Task { task::effect(Action::widget(operation::text_input::move_cursor_to_front( - id.0, + id.into().0, ))) } /// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the /// provided position. -pub fn move_cursor_to(id: Id, position: usize) -> Task { +pub fn move_cursor_to(id: impl Into, position: usize) -> Task { task::effect(Action::widget(operation::text_input::move_cursor_to( - id.0, position, + id.into().0, + position, ))) } /// Produces a [`Task`] that selects all the content of the [`TextInput`] with the given [`Id`]. -pub fn select_all(id: Id) -> Task { - task::effect(Action::widget(operation::text_input::select_all(id.0))) +pub fn select_all(id: impl Into) -> Task { + task::effect(Action::widget(operation::text_input::select_all( + id.into().0, + ))) } /// The state of a [`TextInput`]. -- cgit