Skip to main content

cursive_tree/view/
view.rs

1use 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; // TODO: keep the selected node?
67                    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; // TODO: keep the selected node?
84                    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                Action::ToggleDebug => {
95                    self.debug = !self.debug;
96                }
97            },
98
99            None => {
100                match event {
101                    Event::Mouse { offset, position, event: MouseEvent::Press(_) } => {
102                        self.selected_row = if let Some(position) = position.checked_sub(offset)
103                            && let Some(node) = self.model.at_row(position.y)
104                        {
105                            if node.kind.is_branch() && (position.x < node.depth * 2 + 2) {
106                                // If we click to the left of the label then toggle state
107                                let context = self.model.context.clone();
108                                if let Some(path) = self.model.path(node)
109                                    && let Some(node) = self.model.at_path_mut(path)
110                                {
111                                    return Self::on_toggle_branch_state(node, context);
112                                } else {
113                                    None
114                                }
115                            } else {
116                                // Otherwise just select
117                                Some(position.y)
118                            }
119                        } else {
120                            None
121                        };
122                    }
123
124                    _ => return EventResult::Ignored,
125                }
126            }
127        }
128
129        self.on_selection_changed(last_selected_row)
130    }
131
132    fn draw(&self, printer: &Printer) {
133        let mut start = Vec2::default();
134        for node in self.model.iter(true) {
135            if start.y >= printer.offset.y + printer.size.y {
136                // We're beyond the print area
137                break;
138            }
139
140            // Start printing only when we're within the print area
141            if start.y >= printer.content_offset.y {
142                // Symbol
143
144                start.x = node.depth * 2;
145                match node.symbol(self.model.context.clone()) {
146                    Symbol::SpannedString(symbol) => printer.print_styled(start, &symbol),
147                    Symbol::String(symbol) => printer.print(start, symbol),
148                };
149                start.x += 2;
150
151                // Label
152
153                let mut label = &node.label;
154
155                let debug_label = if self.debug
156                    && let Some(path) = self.model.path(node)
157                {
158                    let path: Vec<_> = path.into_iter().map(|i| i.to_string()).collect();
159                    let mut label = label.clone();
160                    label.append(format!(" {}", path.join(".")));
161                    Some(label)
162                } else {
163                    None
164                };
165
166                if let Some(debug_label) = &debug_label {
167                    label = debug_label;
168                }
169
170                let highlight = if self.is_selected(start.y) {
171                    Some(if printer.focused { PaletteStyle::Highlight } else { PaletteStyle::HighlightInactive })
172                } else {
173                    None
174                };
175
176                let print = |printer: &Printer| printer.print_styled(start, label);
177
178                match highlight {
179                    Some(highlight) => printer.with_style(highlight, print),
180                    None => print(printer),
181                }
182            }
183
184            start.y += node.label_size.y;
185        }
186    }
187}