iced_core/widget/operation/
search_id.rs

1//! Search for widgets with the target Id.
2
3use super::Operation;
4use crate::{id::Id, widget::operation::Outcome, Rectangle};
5
6/// Produces an [`Operation`] that searches for the Id
7pub fn search_id(target: Id) -> impl Operation<Id> {
8    struct Find {
9        found: bool,
10        target: Id,
11    }
12
13    impl Operation<Id> for Find {
14        fn custom(&mut self, _state: &mut dyn std::any::Any, id: Option<&Id>) {
15            if Some(&self.target) == id {
16                self.found = true;
17            }
18        }
19
20        fn container(
21            &mut self,
22            _id: Option<&Id>,
23            _bounds: Rectangle,
24            operate_on_children: &mut dyn FnMut(&mut dyn Operation<Id>),
25        ) {
26            operate_on_children(self);
27        }
28
29        fn finish(&self) -> Outcome<Id> {
30            if self.found {
31                Outcome::Some(self.target.clone())
32            } else {
33                Outcome::None
34            }
35        }
36    }
37
38    Find {
39        found: false,
40        target,
41    }
42}