use crate::{A11yId, A11yNode, IdEq}; #[derive(Debug, Clone, Default)] /// Accessible tree of nodes pub struct A11yTree { /// The root of the current widget, children of the parent widget or the Window if there is no parent widget root: Vec, /// The children of a widget and its children children: Vec, } impl A11yTree { /// Create a new A11yTree /// XXX if you use this method, you will need to manually add the children of the root nodes pub fn new(root: Vec, children: Vec) -> Self { Self { root, children } } pub fn leaf>(node: accesskit::Node, id: T) -> Self { Self { root: vec![A11yNode::new(node, id)], children: vec![], } } /// Helper for creating an A11y tree with a single root node and some children pub fn node_with_child_tree(mut root: A11yNode, child_tree: Self) -> Self { root.add_children( child_tree.root.iter().map(|n| n.id()).cloned().collect(), ); Self { root: vec![root], children: child_tree .children .into_iter() .chain(child_tree.root) .collect(), } } /// Joins multiple trees into a single tree pub fn join>(trees: T) -> Self { trees.fold(Self::default(), |mut acc, A11yTree { root, children }| { acc.root.extend(root); acc.children.extend(children); acc }) } pub fn root(&self) -> &Vec { &self.root } pub fn children(&self) -> &Vec { &self.children } pub fn root_mut(&mut self) -> &mut Vec { &mut self.root } pub fn children_mut(&mut self) -> &mut Vec { &mut self.children } pub fn contains(&self, id: &A11yId) -> bool { self.root.iter().any(|n| IdEq::eq(n.id(), id)) || self.children.iter().any(|n| IdEq::eq(n.id(), id)) } } impl From for Vec<(accesskit::NodeId, accesskit::Node)> { fn from(tree: A11yTree) -> Vec<(accesskit::NodeId, accesskit::Node)> { tree.root .into_iter() .map(|node| node.into()) .chain(tree.children.into_iter().map(|node| node.into())) .collect() } }