cursive_tree/view/
view.rs1use super::{
2 super::{backend::*, model::*},
3 actions::*,
4 tree::*,
5};
6
7use cursive::{direction::*, event::*, theme::*, view::*, *};
8
9const TYPE_NAME: &str = "TreeView";
10
11impl<BackendT> View for TreeView<BackendT>
12where
13 BackendT: 'static + TreeBackend + Send + Sync,
14 BackendT::Context: Clone + Send + Sync,
15 BackendT::Error: Send + Sync,
16 BackendT::ID: Send + Sync,
17 BackendT::Data: Send + Sync,
18{
19 fn type_name(&self) -> &'static str {
20 TYPE_NAME
21 }
22
23 fn take_focus(&mut self, _source: Direction) -> Result<EventResult, CannotFocus> {
24 if self.model.is_empty() { Err(CannotFocus) } else { Ok(EventResult::consumed()) }
25 }
26
27 fn required_size(&mut self, _constraint: Vec2) -> Vec2 {
28 self.model.extents()
29 }
30
31 fn important_area(&self, view_size: Vec2) -> Rect {
32 if let Some(selected_row) = self.selected_row {
33 Rect::from_size((0, selected_row), (view_size.x, 1))
34 } else {
35 Rect::from_size((0, 0), view_size)
36 }
37 }
38
39 fn on_event(&mut self, event: Event) -> EventResult {
40 let last_selected_row = self.selected_row;
41
42 match self.actions.get(&event) {
43 Some(action) => match action {
44 Action::SelectTop => self.select_top_(),
45 Action::SelectUp => self.move_selection_(-1),
46 Action::SelectUpPage => self.move_selection_(-(self.page as isize)),
47 Action::SelectBottom => self.select_bottom_(),
48 Action::SelectDown => self.move_selection_(1),
49 Action::SelectDownPage => self.move_selection_(self.page as isize),
50
51 Action::Expand => {
52 let context = self.model.context.clone();
53 if let Some(node) = self.selected_node_mut() {
54 return Self::on_expand_branch(node, Some(1), context);
55 }
56 }
57
58 Action::ExpandRecursive => {
59 let context = self.model.context.clone();
60 if let Some(node) = self.selected_node_mut() {
61 return Self::on_expand_branch(node, None, context);
62 }
63 }
64
65 Action::ExpandAll => {
66 self.selected_row = None; return self.on_expand_all();
68 }
69
70 Action::Collapse => {
71 if let Some(node) = self.selected_node_mut() {
72 node.collapse(Some(1));
73 }
74 }
75
76 Action::CollapseRecursive => {
77 if let Some(node) = self.selected_node_mut() {
78 node.collapse(None);
79 }
80 }
81
82 Action::CollapseAll => {
83 self.selected_row = None; self.model.collapse(None);
85 }
86
87 Action::ToggleState => {
88 let context = self.model.context.clone();
89 if let Some(node) = self.selected_node_mut() {
90 return Self::on_toggle_branch_state(node, context);
91 }
92 }
93 },
94
95 None => {
96 match event {
97 Event::Mouse { offset, position, event: MouseEvent::Press(_) } => {
98 self.selected_row = if let Some(position) = position.checked_sub(offset)
99 && let Some(node) = self.model.at_row(position.y)
100 {
101 if node.kind.is_branch() && (position.x < node.depth * 2 + 2) {
102 let context = self.model.context.clone();
104 if let Some(path) = self.model.path(node)
105 && let Some(node) = self.model.at_path_mut(path)
106 {
107 return Self::on_toggle_branch_state(node, context);
108 } else {
109 None
110 }
111 } else {
112 Some(position.y)
114 }
115 } else {
116 None
117 };
118 }
119
120 _ => return EventResult::Ignored,
121 }
122 }
123 }
124
125 self.on_selection_changed(last_selected_row)
126 }
127
128 fn draw(&self, printer: &Printer) {
129 let mut start = Vec2::default();
130 for node in self.model.iter(true) {
131 if start.y >= printer.offset.y + printer.size.y {
132 break;
134 }
135
136 if start.y >= printer.content_offset.y {
138 start.x = node.depth * 2;
141 match node.symbol(self.model.context.clone()) {
142 Symbol::SpannedString(symbol) => printer.print_styled(start, &symbol),
143 Symbol::String(symbol) => printer.print(start, symbol),
144 };
145 start.x += 2;
146
147 let mut label = &node.label;
150
151 let debug_label = if self.debug
152 && let Some(path) = self.model.path(node)
153 {
154 let path: Vec<_> = path.into_iter().map(|i| i.to_string()).collect();
155 let mut label = label.clone();
156 label.append(format!(" {}", path.join(".")));
157 Some(label)
158 } else {
159 None
160 };
161
162 if let Some(debug_label) = &debug_label {
163 label = debug_label;
164 }
165
166 let highlight = if self.is_selected(start.y) {
167 Some(if printer.focused { PaletteStyle::Highlight } else { PaletteStyle::HighlightInactive })
168 } else {
169 None
170 };
171
172 let print = |printer: &Printer| printer.print_styled(start, label);
173
174 match highlight {
175 Some(highlight) => printer.with_style(highlight, print),
176 None => print(printer),
177 }
178 }
179
180 start.y += node.label_size.y;
181 }
182 }
183}