Implement some From traits for text_input::Id

This commit is contained in:
Héctor Ramón Jiménez 2024-09-18 20:30:14 +02:00
parent f897442831
commit 1448c5bfa5
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 38 additions and 23 deletions

View file

@ -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<Id>) -> Self {
self.id = Some(id.into());
self
}
@ -1226,38 +1226,53 @@ impl From<Id> for widget::Id {
}
}
impl From<&'static str> for Id {
fn from(id: &'static str) -> Self {
Self::new(id)
}
}
impl From<String> for Id {
fn from(id: String) -> Self {
Self::new(id)
}
}
/// Produces a [`Task`] that focuses the [`TextInput`] with the given [`Id`].
pub fn focus<T>(id: Id) -> Task<T> {
task::effect(Action::widget(operation::focusable::focus(id.0)))
pub fn focus<T>(id: impl Into<Id>) -> Task<T> {
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<T>(id: Id) -> Task<T> {
pub fn move_cursor_to_end<T>(id: impl Into<Id>) -> Task<T> {
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<T>(id: Id) -> Task<T> {
pub fn move_cursor_to_front<T>(id: impl Into<Id>) -> Task<T> {
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<T>(id: Id, position: usize) -> Task<T> {
pub fn move_cursor_to<T>(id: impl Into<Id>, position: usize) -> Task<T> {
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<T>(id: Id) -> Task<T> {
task::effect(Action::widget(operation::text_input::select_all(id.0)))
pub fn select_all<T>(id: impl Into<Id>) -> Task<T> {
task::effect(Action::widget(operation::text_input::select_all(
id.into().0,
)))
}
/// The state of a [`TextInput`].