cursive_tree/model/
tree.rs1use super::{super::backend::*, depth::*, iterator::*, kind::*, list::*, node::*, path::*};
2
3use {
4 cursive::{style::*, utils::span::*, *},
5 std::cmp::*,
6};
7
8pub struct TreeModel<BackendT>
14where
15 BackendT: TreeBackend,
16{
17 pub roots: NodeList<BackendT>,
19
20 pub context: BackendT::Context,
22}
23
24impl<BackendT> TreeModel<BackendT>
25where
26 BackendT: TreeBackend,
27{
28 pub fn new(context: BackendT::Context) -> Self {
30 Self { roots: Default::default(), context }
31 }
32
33 pub fn is_empty(&self) -> bool {
35 self.roots.0.is_empty()
36 }
37
38 pub fn iter(&self, only_expanded: bool) -> NodeIterator<'_, BackendT> {
42 self.roots.iter(only_expanded)
43 }
44
45 pub fn at_path(&self, path: NodePath) -> Option<&Node<BackendT>> {
47 self.roots.at_path(path)
48 }
49
50 pub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Node<BackendT>> {
52 self.roots.at_path_mut(path)
53 }
54
55 pub fn at_row(&self, row: usize) -> Option<&Node<BackendT>> {
57 let mut iterator = self.iter(true);
58 let mut current_row: usize = 0;
59 while let Some(node) = iterator.next() {
60 let next_row = current_row + node.label_size.y;
61 if (row >= current_row) && (row < next_row) {
62 return Some(node);
63 }
64 current_row = next_row;
65 }
66 None
67 }
68
69 pub fn path(&self, node: &Node<BackendT>) -> Option<NodePath> {
71 let mut path = Default::default();
72 if self.roots.fill_path(&mut path, node) { Some(path) } else { None }
73 }
74
75 pub fn extents(&self) -> Vec2 {
77 let mut extents = Vec2::default();
78 let mut iterator = self.iter(true);
79 while let Some(node) = iterator.next() {
80 extents.x = max(extents.x, node.depth * 2 + 2 + node.label_size.x);
81 extents.y += node.label_size.y;
82 }
83 extents
84 }
85
86 pub fn add_root<IdT, LabelT>(&mut self, kind: NodeKind, id: IdT, label: LabelT)
88 where
89 IdT: Into<BackendT::ID>,
90 LabelT: Into<SpannedString<Style>>,
91 {
92 self.roots.add(0, kind, id, label);
93 }
94
95 pub fn insert_root<IdT, LabelT>(&mut self, index: usize, kind: NodeKind, id: IdT, label: LabelT)
97 where
98 IdT: Into<BackendT::ID>,
99 LabelT: Into<SpannedString<Style>>,
100 {
101 self.roots.insert(index, 0, kind, id, label);
102 }
103
104 pub fn expand(&mut self, depth: Option<usize>) -> Result<(), BackendT::Error>
112 where
113 BackendT::Context: Clone,
114 {
115 self.roots.expand(depth, self.context.clone())
116 }
117
118 pub fn collapse(&mut self, depth: Option<usize>) {
124 self.roots.collapse(depth);
125 }
126
127 pub fn populate(&mut self, mut depth: Option<usize>) -> Result<(), BackendT::Error>
137 where
138 BackendT::Context: Clone,
139 {
140 if depth.is_zero() {
141 return Ok(());
142 }
143
144 self.roots = BackendT::roots(self.context.clone())?;
145
146 depth.decrease();
147 if !depth.is_zero() {
148 for node in &mut self.roots {
149 node.populate(depth, self.context.clone())?;
150 }
151 }
152
153 Ok(())
154 }
155
156 pub fn clear(&mut self) {
158 self.roots.0.clear();
159 }
160}