Implement is_focused selector and unify selector API

This commit is contained in:
Leonie Theobald 2024-11-05 14:06:36 +01:00 committed by Héctor Ramón Jiménez
parent 645643bfd6
commit 1ae3b5e96a
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 48 additions and 24 deletions

View file

@ -146,3 +146,28 @@ where
pub fn id(id: impl Into<widget::Id>) -> impl Selector<Output = Target> {
id.into()
}
/// Returns a [`Selector`] that matches widgets that are currently focused.
pub fn is_focused() -> impl Selector<Output = Target> {
struct IsFocused;
impl Selector for IsFocused {
type Output = Target;
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output> {
if let Candidate::Focusable { state, .. } = candidate
&& state.is_focused()
{
Some(Target::from(candidate))
} else {
None
}
}
fn description(&self) -> String {
"is focused".to_string()
}
}
IsFocused
}