summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor <hector@hecrj.dev>2025-02-23 07:34:30 +0100
committerLibravatar GitHub <noreply@github.com>2025-02-23 07:34:30 +0100
commit34314b3f576fdfffdd82968a685020d4dca47992 (patch)
treea0b1c7a070803606e5b2a0dfb06f2d637ad0540b
parentfd1cfc05eae2d2527fc5fe0a4b30b133283c7095 (diff)
parent7916a9c2272a3d6d89e7545b33e2991657ad5990 (diff)
downloadiced-34314b3f576fdfffdd82968a685020d4dca47992.tar.gz
iced-34314b3f576fdfffdd82968a685020d4dca47992.tar.bz2
iced-34314b3f576fdfffdd82968a685020d4dca47992.zip
Merge pull request #2812 from andymandias/text_input-is_focused
`operation::focusable::is_focused` & `text_input::is_focused`
-rw-r--r--core/src/widget/operation/focusable.rs45
-rw-r--r--widget/src/text_input.rs5
2 files changed, 50 insertions, 0 deletions
diff --git a/core/src/widget/operation/focusable.rs b/core/src/widget/operation/focusable.rs
index 1ee41244..44c9d647 100644
--- a/core/src/widget/operation/focusable.rs
+++ b/core/src/widget/operation/focusable.rs
@@ -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,
+ }
+}
diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs
index 5b6ee9e0..3abead5b 100644
--- a/widget/src/text_input.rs
+++ b/widget/src/text_input.rs
@@ -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)))