Skip to content

Commit

Permalink
Merge pull request #2812 from andymandias/text_input-is_focused
Browse files Browse the repository at this point in the history
`operation::focusable::is_focused` & `text_input::is_focused`
  • Loading branch information
hecrj authored Feb 23, 2025
2 parents fd1cfc0 + 7916a9c commit 34314b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
45 changes: 45 additions & 0 deletions core/src/widget/operation/focusable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,48 @@ pub fn find_focused() -> impl Operation<Id> {

FindFocused { focused: None }
}

/// Produces an [`Operation`] that searches for the focusable widget
/// and stores whether it is focused or not. This ignores widgets that
/// do not have an ID.
pub fn is_focused(target: Id) -> impl Operation<bool> {
struct IsFocused {
target: Id,
is_focused: Option<bool>,
}

impl Operation<bool> for IsFocused {
fn focusable(
&mut self,
id: Option<&Id>,
_bounds: Rectangle,
state: &mut dyn Focusable,
) {
if id.is_some_and(|id| *id == self.target) {
self.is_focused = Some(state.is_focused());
}
}

fn container(
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<bool>),
) {
if self.is_focused.is_some() {
return;
}

operate_on_children(self);
}

fn finish(&self) -> Outcome<bool> {
self.is_focused.map_or(Outcome::None, Outcome::Some)
}
}

IsFocused {
target,
is_focused: None,
}
}
5 changes: 5 additions & 0 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,11 @@ impl From<String> for Id {
}
}

/// Produces a [`Task`] that returns whether the [`TextInput`] with the given [`Id`] is focused or not.
pub fn is_focused(id: impl Into<Id>) -> Task<bool> {
task::widget(operation::focusable::is_focused(id.into().into()))
}

/// Produces a [`Task`] that focuses the [`TextInput`] with the given [`Id`].
pub fn focus<T>(id: impl Into<Id>) -> Task<T> {
task::effect(Action::widget(operation::focusable::focus(id.into().0)))
Expand Down

0 comments on commit 34314b3

Please sign in to comment.