Merge pull request #2890 from Remmirad/pub_text_field_select_range

Make `text_input::State` `select_range` public
This commit is contained in:
Héctor 2025-11-25 10:49:12 +01:00 committed by GitHub
commit f8663cf0a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 0 deletions

View file

@ -21,6 +21,8 @@ pub trait TextInput {
/// Selects all the content of the text input.
fn select_all(&mut self);
/// Selects the given content range of the text input.
fn select_range(&mut self, start: usize, end: usize);
}
/// Produces an [`Operation`] that moves the cursor of the widget with the given [`Id`] to the
@ -142,3 +144,38 @@ pub fn select_all<T>(target: Id) -> impl Operation<T> {
MoveCursor { target }
}
/// Produces an [`Operation`] that selects the given content range of the widget with the given [`Id`].
pub fn select_range<T>(
target: Id,
start: usize,
end: usize,
) -> impl Operation<T> {
struct SelectRange {
target: Id,
start: usize,
end: usize,
}
impl<T> Operation<T> for SelectRange {
fn text_input(
&mut self,
id: Option<&Id>,
_bounds: Rectangle,
state: &mut dyn TextInput,
) {
match id {
Some(id) if id == &self.target => {
state.select_range(self.start, self.end);
}
_ => {}
}
}
fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
operate(self);
}
}
SelectRange { target, start, end }
}

View file

@ -86,3 +86,12 @@ pub fn move_cursor_to<T>(id: impl Into<Id>, position: usize) -> Task<T> {
pub fn select_all<T>(id: impl Into<Id>) -> Task<T> {
task::effect(Action::widget(operation::text_input::select_all(id.into())))
}
/// Selects the given content range of the widget with the given [`Id`].
pub fn select_range<T>(id: impl Into<Id>, start: usize, end: usize) -> Task<T> {
task::effect(Action::widget(operation::text_input::select_range(
id.into(),
start,
end,
)))
}

View file

@ -1534,6 +1534,11 @@ impl<P: text::Paragraph> State<P> {
pub fn select_all(&mut self) {
self.cursor.select_range(0, usize::MAX);
}
/// Selects the given range of the content of the [`TextInput`].
pub fn select_range(&mut self, start: usize, end: usize) {
self.cursor.select_range(start, end);
}
}
impl<P: text::Paragraph> operation::Focusable for State<P> {
@ -1574,6 +1579,10 @@ impl<P: text::Paragraph> operation::TextInput for State<P> {
fn select_all(&mut self) {
State::select_all(self);
}
fn select_range(&mut self, start: usize, end: usize) {
State::select_range(self, start, end);
}
}
fn offset<P: text::Paragraph>(