Skip to main content

ferroui_controls/templates/
template_extensions.rs

1use crate::primitives::TemplatedControl;
2use crate::Control;
3use ferroui_base::{FerroObject, Ref, Visual};
4
5impl TemplatedControl {
6    /// Gets the list of all control descendants that are part of the template of this
7    /// control: the template descendants that are controls.
8    #[deprecated(note = "Use get_template_descendants")]
9    pub fn get_template_children(&self) -> Vec<Ref<Control>> {
10        self.get_template_descendants().into_iter().filter_map(|child| child.cast::<Control>()).collect()
11    }
12
13    /// Gets a visual tree's template descendants: the visual descendants
14    /// whose templated parent is this control.
15    pub fn get_template_descendants(&self) -> Vec<Ref<Visual>> {
16        let templated_parent: Ref<FerroObject> = self.to_ref().upcast();
17        let mut result = Vec::new();
18        collect_template_descendants(self, &templated_parent, &mut result);
19        result
20    }
21}
22
23fn collect_template_descendants(control: &Visual, templated_parent: &Ref<FerroObject>, result: &mut Vec<Ref<Visual>>) {
24    let Some(children) = control.visual_children_snapshot() else { return };
25
26    for child in children.iter() {
27        let child_templated_parent = child.templated_parent();
28
29        if child_templated_parent.as_ref() == Some(templated_parent) {
30            result.push(child.clone());
31        }
32
33        if child_templated_parent.is_some() {
34            collect_template_descendants(child, templated_parent, result);
35        }
36    }
37}