Skip to main content

cursive_tree/model/
node.rs

1use super::{super::backend::*, depth::*, kind::*, list::*, path::*, representation::*, state::*, symbol::*};
2
3use {
4    cursive::*,
5    std::{borrow::*, marker::*},
6};
7
8//
9// Node
10//
11
12/// Tree node.
13pub struct Node<BackendT>
14where
15    BackendT: TreeBackend,
16{
17    /// Depth.
18    pub depth: usize,
19
20    /// Kind.
21    pub kind: NodeKind,
22
23    /// ID.
24    pub id: BackendT::ID,
25
26    /// Representation.
27    pub representation: Representation,
28
29    /// Representation size.
30    ///
31    /// Cached so we only have to calculate it once.
32    ///
33    /// > Note that nodes can ostensibly have heights >1, i.e. they can be multiline, however this
34    /// is *not* currently supported by [TreeView](super::super::TreeView).
35    pub representation_size: Vec2,
36
37    /// State for [Branch](NodeKind::Branch) node.
38    pub branch_state: BranchState,
39
40    /// Children for [Branch](NodeKind::Branch) node.
41    pub children: Option<NodeList<BackendT>>,
42
43    /// Data.
44    pub data: Option<BackendT::Data>,
45
46    backend: PhantomData<BackendT>,
47}
48
49impl<BackendT> Node<BackendT>
50where
51    BackendT: TreeBackend,
52{
53    /// Constructor.
54    pub fn new(depth: usize, kind: NodeKind, id: BackendT::ID, representation: Representation) -> Self {
55        let representation_size = (representation.width(), 1).into();
56        Self {
57            depth,
58            kind,
59            id,
60            representation,
61            representation_size,
62            branch_state: Default::default(),
63            children: None,
64            data: None,
65            backend: Default::default(),
66        }
67    }
68
69    /// Symbol.
70    ///
71    /// Its char count is always 1.
72    pub fn symbol(&self, context: BackendT::Context) -> Symbol<'_> {
73        BackendT::symbol(self, context)
74    }
75
76    /// Get node at path.
77    pub fn at_path(&self, path: NodePath) -> Option<&Self> {
78        if path.is_empty() { Some(self) } else { self.children.as_ref().and_then(|children| children.at_path(path)) }
79    }
80
81    /// Get node at path.
82    pub fn at_path_mut(&mut self, path: NodePath) -> Option<&mut Self> {
83        if path.is_empty() {
84            Some(self)
85        } else {
86            self.children.as_mut().and_then(|children| children.at_path_mut(path))
87        }
88    }
89
90    /// Fill path to node.
91    ///
92    /// Returns true if found.
93    pub fn fill_path(&self, path: &mut NodePath, node: &Self) -> bool {
94        self.children.as_ref().map(|children| children.fill_path(path, node)).unwrap_or_default()
95    }
96
97    /// Fetch the branch's children from backend.
98    ///
99    /// If depth is [None] will populate all depths.
100    ///
101    /// If depth is 0 or not a [Branch](NodeKind::Branch) will do nothing.
102    ///
103    /// Note that populating a node will *not* automatically expand it.
104    pub fn populate(&mut self, mut depth: Option<usize>, context: BackendT::Context) -> Result<(), BackendT::Error>
105    where
106        BackendT::Context: Clone,
107    {
108        if depth.is_zero() || !self.kind.is_branch() {
109            return Ok(());
110        }
111
112        if self.children.is_none() {
113            BackendT::populate(self, context.clone())?;
114        }
115
116        depth.decrease();
117        if !depth.is_zero()
118            && let Some(children) = &mut self.children
119        {
120            for node in children {
121                node.populate(depth, context.clone())?;
122            }
123        }
124
125        Ok(())
126    }
127
128    /// Add a child node.
129    ///
130    /// Will do nothing if not a [Branch](NodeKind::Branch).
131    pub fn add_child(&mut self, kind: NodeKind, id: BackendT::ID, representation: Representation) {
132        if !self.kind.is_branch() {
133            return;
134        }
135
136        if self.children.is_none() {
137            self.children = Some(Default::default());
138        }
139
140        self.children.as_mut().expect("children").add(self.depth + 1, kind, id, representation);
141    }
142
143    /// Inserts a child node.
144    ///
145    /// Will do nothing if not a [Branch](NodeKind::Branch).
146    pub fn insert_child(&mut self, index: usize, kind: NodeKind, id: BackendT::ID, representation: Representation) {
147        if !self.kind.is_branch() {
148            return;
149        }
150
151        if self.children.is_none() {
152            self.children = Some(Default::default());
153        }
154
155        self.children.as_mut().expect("children").insert(index, self.depth + 1, kind, id, representation);
156    }
157
158    /// Expand the branch.
159    ///
160    /// Will return false if not a [Branch](NodeKind::Branch) or already expanded.
161    ///
162    /// Note that this will populate the node from the backend.
163    pub fn expand(&mut self, context: BackendT::Context) -> Result<bool, BackendT::Error>
164    where
165        BackendT::Context: Clone,
166    {
167        if !self.kind.is_branch() || self.branch_state.is_expanded() {
168            return Ok(false);
169        }
170
171        self.populate(Some(1), context)?;
172        self.branch_state = BranchState::Expanded;
173        Ok(true)
174    }
175
176    /// Collapse the branch.
177    ///
178    /// Will return false if not a [Branch](NodeKind::Branch) or already collapsed.
179    pub fn collapse(&mut self) -> bool {
180        if !self.kind.is_branch() || self.branch_state.is_collapsed() {
181            return false;
182        }
183
184        self.branch_state = BranchState::Collapsed;
185        true
186    }
187
188    /// Toggle the branch state.
189    ///
190    /// Expand if collapsed, collapse if expanded.
191    ///
192    /// Will do nothing if not a [Branch](NodeKind::Branch).
193    pub fn toggle_branch_state(&mut self, context: BackendT::Context) -> Result<(), BackendT::Error>
194    where
195        BackendT::Context: Clone,
196    {
197        if !self.kind.is_branch() {
198            return Ok(());
199        }
200
201        _ = match self.branch_state {
202            BranchState::Collapsed => self.expand(context)?,
203            BranchState::Expanded => self.collapse(),
204        };
205
206        Ok(())
207    }
208
209    /// Data.
210    ///
211    /// If not already cached will fetch it from the backend.
212    pub fn data(&mut self, context: BackendT::Context) -> Result<Option<Cow<'_, BackendT::Data>>, BackendT::Error>
213    where
214        BackendT::Data: Clone,
215    {
216        if self.data.is_none() {
217            if let Some((data, cache)) = BackendT::data(self, context)? {
218                if cache {
219                    self.data = Some(data);
220                } else {
221                    return Ok(Some(Cow::Owned(data)));
222                }
223            }
224        }
225
226        Ok(self.data.as_ref().map(Cow::Borrowed))
227    }
228}