diff options
author | 2022-11-14 00:47:28 +0100 | |
---|---|---|
committer | 2022-11-14 00:47:28 +0100 | |
commit | 23364bc4a2adb0c9c3be6449d7226784783c4abc (patch) | |
tree | 11df677e27827adb2589dbf409ad9d86bc090efd /native/src/widget/operation | |
parent | cf410a5e0b37c7f88ea33874796f0becbe970628 (diff) | |
parent | e56c45470c9a00e31d92e4e7d4b1cb1894d4de7d (diff) | |
download | iced-23364bc4a2adb0c9c3be6449d7226784783c4abc.tar.gz iced-23364bc4a2adb0c9c3be6449d7226784783c4abc.tar.bz2 iced-23364bc4a2adb0c9c3be6449d7226784783c4abc.zip |
Merge pull request #1526 from mtkennerly/feature/find-focused
Add widget operation to find currently focused widget
Diffstat (limited to 'native/src/widget/operation')
-rw-r--r-- | native/src/widget/operation/focusable.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/native/src/widget/operation/focusable.rs b/native/src/widget/operation/focusable.rs index f17bf178..0067006b 100644 --- a/native/src/widget/operation/focusable.rs +++ b/native/src/widget/operation/focusable.rs @@ -167,3 +167,37 @@ pub fn focus_next<T>() -> impl Operation<T> { count(|count| FocusNext { count, current: 0 }) } + +/// Produces an [`Operation`] that searches for the current focused widget +/// and stores its ID. This ignores widgets that do not have an ID. +pub fn find_focused() -> impl Operation<Id> { + struct FindFocused { + focused: Option<Id>, + } + + impl Operation<Id> for FindFocused { + fn focusable(&mut self, state: &mut dyn Focusable, id: Option<&Id>) { + if state.is_focused() && id.is_some() { + self.focused = id.cloned(); + } + } + + fn container( + &mut self, + _id: Option<&Id>, + operate_on_children: &mut dyn FnMut(&mut dyn Operation<Id>), + ) { + operate_on_children(self) + } + + fn finish(&self) -> Outcome<Id> { + if let Some(id) = &self.focused { + Outcome::Some(id.clone()) + } else { + Outcome::None + } + } + } + + FindFocused { focused: None } +} |