cursive_tree/view/
view.rs1use super::{
2 super::{backend::*, model::*},
3 tree::*,
4};
5
6use cursive::{direction::*, event::*, theme::*, view::*, *};
7
8const TYPE_NAME: &str = "TreeView";
9
10impl<BackendT> View for TreeView<BackendT>
11where
12 BackendT: 'static + TreeBackend + Send + Sync,
13 BackendT::Context: Clone + Send + Sync,
14 BackendT::Error: Send + Sync,
15 BackendT::ID: Send + Sync,
16 BackendT::Data: Send + Sync,
17{
18 fn type_name(&self) -> &'static str {
19 TYPE_NAME
20 }
21
22 fn take_focus(&mut self, _source: Direction) -> Result<EventResult, CannotFocus> {
23 if self.model.is_empty() { Err(CannotFocus) } else { Ok(EventResult::consumed()) }
24 }
25
26 fn required_size(&mut self, _constraint: Vec2) -> Vec2 {
27 self.model.extents()
28 }
29
30 fn important_area(&self, view_size: Vec2) -> Rect {
31 if let Some(selected_row) = self.selected_row {
32 Rect::from_size((0, selected_row), (view_size.x, 1))
33 } else {
34 Rect::from_size((0, 0), view_size)
35 }
36 }
37
38 fn on_event(&mut self, event: Event) -> EventResult {
39 let selected_row = self.selected_row;
40
41 match event {
42 Event::Key(Key::Up) => self.move_selection(-1),
43 Event::Key(Key::Down) => self.move_selection(1),
44 Event::Key(Key::PageUp) => self.move_selection(-(self.page as isize)),
45 Event::Key(Key::PageDown) => self.move_selection(self.page as isize),
46 Event::Key(Key::Home) => self.select_top(),
47 Event::Key(Key::End) => self.select_bottom(),
48
49 Event::Key(Key::Left) => {
50 if let Some(node) = self.selected_node_mut()
51 && node.kind.is_branch()
52 {
53 node.collapse();
54 }
55 }
56
57 Event::Key(Key::Right) => {
58 let context = self.model.context.clone();
59 if let Some(node) = self.selected_node_mut()
60 && node.kind.is_branch()
61 {
62 return Self::on_expand_branch(node, context);
63 }
64 }
65
66 Event::Key(Key::Enter) => {
67 let context = self.model.context.clone();
68 if let Some(node) = self.selected_node_mut()
69 && node.kind.is_branch()
70 {
71 return Self::on_toggle_branch_state(node, context);
72 }
73 }
74
75 Event::Mouse { offset, position, event: MouseEvent::Press(_) } => {
76 self.selected_row = if let Some(position) = position.checked_sub(offset)
77 && let Some(node) = self.model.at_row(position.y)
78 {
79 if node.kind.is_branch() && (position.x < node.depth * 2 + 2) {
80 let context = self.model.context.clone();
82 if let Some(path) = self.model.path(node)
83 && let Some(node) = self.model.at_path_mut(path)
84 {
85 return Self::on_toggle_branch_state(node, context);
86 } else {
87 None
88 }
89 } else {
90 Some(position.y)
92 }
93 } else {
94 None
95 };
96 }
97
98 _ => return EventResult::Ignored,
99 }
100
101 EventResult::Consumed(if self.selected_row != selected_row {
102 Some(Callback::from_fn_once(|cursive| {
104 BackendT::handle_selection_changed(cursive);
105 }))
106 } else {
107 None
108 })
109 }
110
111 fn draw(&self, printer: &Printer) {
112 let mut start = Vec2::default();
113 for node in self.model.iter(true) {
114 if start.y >= printer.offset.y + printer.size.y {
115 break;
117 }
118
119 if start.y >= printer.content_offset.y {
121 start.x = node.depth * 2;
124 match node.symbol(self.model.context.clone()) {
125 Symbol::Representation(symbol) => printer.print_styled(start, &symbol),
126 Symbol::String(symbol) => printer.print(start, symbol),
127 };
128 start.x += 2;
129
130 let mut representation = &node.representation;
133
134 let debug_representation = if self.debug
135 && let Some(path) = self.model.path(node)
136 {
137 let path: Vec<_> = path.into_iter().map(|i| i.to_string()).collect();
138 let mut representation = representation.clone();
139 representation.append(format!(" {}", path.join(".")));
140 Some(representation)
141 } else {
142 None
143 };
144
145 if let Some(debug_representation) = &debug_representation {
146 representation = debug_representation;
147 }
148
149 let highlight = if self.is_selected(start.y) {
150 Some(if printer.focused { PaletteStyle::Highlight } else { PaletteStyle::HighlightInactive })
151 } else {
152 None
153 };
154
155 let print = |printer: &Printer| printer.print_styled(start, representation);
156
157 match highlight {
158 Some(highlight) => printer.with_style(highlight, print),
159 None => print(printer),
160 }
161 }
162
163 start.y += node.representation_size.y;
164 }
165 }
166}