Skip to main content

cursive_tree/view/
tree.rs

1use super::{
2    super::{backend::*, model::*},
3    actions::*,
4};
5
6use {cursive::*, event::*};
7
8//
9// TreeView
10//
11
12/// Tree view.
13///
14/// Nodes can be leaves (no children) or branches (potentially have children) and can have custom
15/// data (`DataT`) attached to them. Their representation can be stylized with multiple colors and
16/// effects.
17///
18/// Event handling (configurable):
19///
20/// * Up/Down keys: move selection
21/// * PgUp/PgDown keys: move selection by 10 (configurable)
22/// * Left/Right keys: collapse/expand selected branch node
23/// * Enter key: toggle selected branch node collapse/expand
24/// * "-"/"+" keys: collapse/expand selected branch and all children recursively
25/// * "<"/">" keys: collapse/expand all nodes
26/// * Mouse click on node: select
27/// * Mouse click to the left of the node: toggle branch collapse/expand
28///
29/// The view's data and behavior are backed by a [TreeBackend]. The nodes are stored and managed by
30/// a [TreeModel]. The model can be populated in advance and can also fetch data from the backend
31/// on demand, e.g. when a branch node is expanded.
32pub struct TreeView<BackendT>
33where
34    BackendT: TreeBackend,
35{
36    /// Tree model.
37    pub model: TreeModel<BackendT>,
38
39    pub(crate) selected_row: Option<usize>,
40    pub(crate) page: usize,
41    pub(crate) debug: bool,
42    pub(crate) actions: Actions,
43}
44
45impl<BackendT> TreeView<BackendT>
46where
47    BackendT: TreeBackend,
48{
49    /// Page size for
50    /// [SelectUpPage](Action::SelectUpPage)/[SelectDownPage](Action::SelectDownPage).
51    pub fn page(&self) -> usize {
52        self.page
53    }
54
55    /// Set page size for
56    /// [SelectUpPage](Action::SelectUpPage)/[SelectDownPage](Action::SelectDownPage).
57    pub fn set_page(&mut self, page: usize) {
58        self.page = page;
59    }
60
61    /// Set page size for
62    /// [SelectUpPage](Action::SelectUpPage)/[SelectDownPage](Action::SelectDownPage).
63    ///
64    /// Chainable.
65    pub fn with_page(self, page: usize) -> Self {
66        self.with(|self_| self_.set_page(page))
67    }
68
69    /// Debug mode.
70    pub fn debug(&self) -> bool {
71        self.debug
72    }
73
74    /// Set debug mode.
75    pub fn set_debug(&mut self, debug: bool) {
76        self.debug = debug;
77    }
78
79    /// Set debug mode.
80    ///
81    /// Chainable.
82    pub fn with_debug(self, debug: bool) -> Self {
83        self.with(|self_| self_.set_debug(debug))
84    }
85
86    /// Action map.
87    pub fn actions(&self) -> &Actions {
88        &self.actions
89    }
90
91    /// Action map.
92    pub fn actions_mut(&mut self) -> &mut Actions {
93        &mut self.actions
94    }
95
96    /// Set action map.
97    pub fn set_actions(&mut self, actions: Actions) {
98        self.actions = actions;
99    }
100
101    /// Set action map.
102    ///
103    /// Chainable.
104    pub fn with_actions(self, actions: Actions) -> Self {
105        self.with(|self_| self_.set_actions(actions))
106    }
107
108    /// Set action.
109    pub fn set_action(&mut self, action: Action, event: Event) {
110        self.actions.insert(event, action);
111    }
112
113    /// Set action.
114    ///
115    /// Chainable.
116    pub fn with_action(self, action: Action, event: Event) -> Self {
117        self.with(|self_| self_.set_action(action, event))
118    }
119
120    /// Remove action.
121    ///
122    /// Return true if removed.
123    pub fn remove_action(&mut self, action: Action) -> bool {
124        action.remove(&mut self.actions)
125    }
126
127    /// Remove action.
128    ///
129    /// Chainable.
130    pub fn without_action(self, action: Action) -> Self {
131        self.with(|self_| _ = self_.remove_action(action))
132    }
133}
134
135impl<BackendT> From<TreeModel<BackendT>> for TreeView<BackendT>
136where
137    BackendT: TreeBackend,
138{
139    fn from(model: TreeModel<BackendT>) -> Self {
140        Self { model, selected_row: None, page: 10, debug: false, actions: Action::defaults() }
141    }
142}