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 }
}