use crate::{A11yId, A11yNode, IdEq};
#[derive(Debug, Clone, Default)]
pub struct A11yTree {
root: Vec<A11yNode>,
children: Vec<A11yNode>,
}
impl A11yTree {
pub fn new(root: Vec<A11yNode>, children: Vec<A11yNode>) -> Self {
Self { root, children }
}
pub fn leaf<T: Into<A11yId>>(node: accesskit::NodeBuilder, id: T) -> Self {
Self {
root: vec![A11yNode::new(node, id)],
children: vec![],
}
}
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(),
}
}
pub fn join<T: Iterator<Item = Self>>(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<A11yNode> {
&self.root
}
pub fn children(&self) -> &Vec<A11yNode> {
&self.children
}
pub fn root_mut(&mut self) -> &mut Vec<A11yNode> {
&mut self.root
}
pub fn children_mut(&mut self) -> &mut Vec<A11yNode> {
&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<A11yTree> 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()
}
}