freya_core/accessibility/
mod.rs

1mod tree;
2use freya_native_core::{
3    node::NodeType,
4    real_dom::NodeImmutable,
5};
6use itertools::Itertools;
7pub use tree::*;
8
9use crate::{
10    dom::DioxusNode,
11    states::AccessibilityNodeState,
12    types::AccessibilityId,
13};
14
15/// Shortcut functions to retrieve Acessibility info from a Dioxus Node
16pub trait NodeAccessibility {
17    fn get_accessibility_id(&self) -> Option<AccessibilityId>;
18
19    /// Return the first text node from this Node
20    fn get_inner_texts(&self) -> Option<String>;
21
22    /// Collect all the AccessibilityIDs from a Node's children
23    fn get_accessibility_children(&self) -> Vec<AccessibilityId>;
24}
25
26impl NodeAccessibility for DioxusNode<'_> {
27    fn get_accessibility_id(&self) -> Option<AccessibilityId> {
28        if self.id() == self.real_dom().root_id() {
29            Some(ACCESSIBILITY_ROOT_ID)
30        } else {
31            let node_accessibility = &*self.get::<AccessibilityNodeState>()?;
32            node_accessibility.a11y_id
33        }
34    }
35
36    /// Return the first text node from this Node
37    fn get_inner_texts(&self) -> Option<String> {
38        let children = self.children();
39        let first_child = children.first()?;
40        let node_type = first_child.node_type();
41        if let NodeType::Text(text) = &*node_type {
42            Some(text.to_owned())
43        } else {
44            None
45        }
46    }
47
48    /// Collect all descendant accessibility node ids
49    fn get_accessibility_children(&self) -> Vec<AccessibilityId> {
50        self.children()
51            .into_iter()
52            .filter_map(|child| child.get_accessibility_id())
53            .collect_vec()
54    }
55}