Skip to main content

freya_core/
tree_layout_adapter.rs

1use rustc_hash::FxHashMap;
2use torin::{
3    node::Node,
4    prelude::{
5        Direction,
6        TreeAdapter,
7    },
8    size::Size,
9};
10
11use crate::node_id::NodeId;
12
13pub struct TreeAdapterFreya<'a> {
14    pub layout_nodes: &'a FxHashMap<NodeId, Node>,
15    pub parents: &'a FxHashMap<NodeId, NodeId>,
16    pub children: &'a FxHashMap<NodeId, Vec<NodeId>>,
17    pub heights: &'a FxHashMap<NodeId, u16>,
18}
19
20impl TreeAdapter<NodeId> for TreeAdapterFreya<'_> {
21    fn root_id(&self) -> NodeId {
22        NodeId::ROOT
23    }
24
25    fn read_node<R>(
26        &self,
27        node_id: &NodeId,
28        reader: impl FnOnce(&Node, &[NodeId]) -> R,
29    ) -> Option<R> {
30        let children = self.children.get(node_id).map_or(&[][..], Vec::as_slice);
31
32        if *node_id == NodeId::ROOT {
33            let root = Node::from_size_and_direction(Size::Fill, Size::Fill, Direction::Vertical);
34            return Some(reader(&root, children));
35        }
36
37        self.layout_nodes
38            .get(node_id)
39            .map(|layout_node| reader(layout_node, children))
40    }
41
42    fn height(&self, node_id: &NodeId) -> Option<u16> {
43        self.heights.get(node_id).copied()
44    }
45
46    fn parent_of(&self, node_id: &NodeId) -> Option<NodeId> {
47        self.parents.get(node_id).copied()
48    }
49}