From 44aba47b0e67ce3cf1d5e8a79768e8fe996f5580 Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Fri, 11 Nov 2022 08:44:10 +0800 Subject: Allow converting from widget-specific IDs to generic ID --- native/src/widget/scrollable.rs | 6 ++++++ native/src/widget/text_input.rs | 6 ++++++ 2 files changed, 12 insertions(+) (limited to 'native/src/widget') diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index b257cbe5..32ec6eb3 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -334,6 +334,12 @@ impl Id { } } +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} + /// Produces a [`Command`] that snaps the [`Scrollable`] with the given [`Id`] /// to the provided `percentage`. pub fn snap_to(id: Id, percentage: f32) -> Command { diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 2315b05a..e57f41ea 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -332,6 +332,12 @@ impl Id { } } +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} + /// Produces a [`Command`] that focuses the [`TextInput`] with the given [`Id`]. pub fn focus(id: Id) -> Command { Command::widget(operation::focusable::focus(id.0)) -- cgit From e56c45470c9a00e31d92e4e7d4b1cb1894d4de7d Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Fri, 11 Nov 2022 09:15:35 +0800 Subject: Add widget operation to find currently focused widget --- native/src/widget/operation/focusable.rs | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'native/src/widget') 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() -> impl Operation { 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 { + struct FindFocused { + focused: Option, + } + + impl Operation 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), + ) { + operate_on_children(self) + } + + fn finish(&self) -> Outcome { + if let Some(id) = &self.focused { + Outcome::Some(id.clone()) + } else { + Outcome::None + } + } + } + + FindFocused { focused: None } +} -- cgit