-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.rs
410 lines (355 loc) · 14.1 KB
/
widget.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::fmt::Debug;
use std::hash::Hash;
use log::{error, warn};
use unicode_width::UnicodeWidthStr;
use crate::config::theme::Theme;
use crate::experiments::screenspace::Screenspace;
use crate::gladius::providers::Providers;
use crate::io::input_event::InputEvent;
use crate::io::input_event::InputEvent::KeyInput;
use crate::io::keys::Keycode;
use crate::io::output::Output;
use crate::io::style::{Effect, TextStyle};
use crate::primitives::arrow::VerticalArrow;
use crate::primitives::tree::tree_node::TreeNode;
use crate::primitives::xy::XY;
use crate::text::buffer_state::BufferState;
use crate::w7e::buffer_state_shared_ref::BufferSharedRef;
use crate::widget::any_msg::{AnyMsg, AsAny};
use crate::widget::widget::{get_new_widget_id, Widget, WID};
use crate::widgets::nested_menu;
use crate::widgets::nested_menu::msg::Msg;
/*
This describes a simple context menu.
For first version, options remain fixed (no adding/deleting).
There is no optimisation here whatsoever. First let it work, second write the test, then optimise.
Let's describe how it should look like:
v ExpandedSubtree1
v ExpandedSubSubtree1
SelectableItem1
SelectableItem2
> NotExpandedSubtree2
SelectableItem3
Actions:
- hitting enter on expanded tree collapses it
- hitting enter on collapsed tree expands it
- hitting enter on selectable item causes a message to be emmited
- arrows navigate, they can jump between expanded subtrees
- query. Query works as in...
fuck this is just another tree_view.
*/
pub const NESTED_MENU_TYPENAME: &'static str = "nested_menu";
pub const NESTED_MENU_FOLDER_CLOSED: &'static str = ">";
pub const NESTED_MENU_FOLDER_OPENED: &'static str = "v";
pub const NESTED_MENU_FOLDER_WIDHT: u16 = 2;
pub struct NestedMenuWidget<Key: Hash + Eq + Debug + Clone, Item: TreeNode<Key>> {
wid: WID,
mapper: Option<Box<dyn Fn(&Item) -> Option<Box<dyn AnyMsg>> + 'static>>,
max_size: XY,
layout_size: Option<XY>,
// key, label
selected_nodes: Vec<(Key, String)>,
// we count selected row only within selected node. Selected node is determined based on
// "root" item and "selected nodes".
selected_row_idx: u16,
root: Item,
query: BufferSharedRef,
}
pub fn get_highlighted_style(theme: &Theme, focused: bool) -> TextStyle {
theme.highlighted(focused)
}
pub fn get_expanded_style(theme: &Theme, focused: bool) -> TextStyle {
let highlighted = theme.highlighted(focused);
let default = theme.default_text(focused);
TextStyle::new(highlighted.foreground, default.background, Effect::None)
}
pub fn get_default_style(theme: &Theme, focused: bool) -> TextStyle {
theme.default_text(focused)
}
impl<Key: Hash + Eq + Debug + Clone, Item: TreeNode<Key>> NestedMenuWidget<Key, Item> {
pub fn new(providers: Providers, root_node: Item, max_size: XY) -> Self {
let query_buffer = BufferState::simplified_single_line().into_bsr();
NestedMenuWidget {
wid: get_new_widget_id(),
mapper: None,
max_size,
layout_size: None,
selected_nodes: Default::default(),
selected_row_idx: 0,
root: root_node,
query: BufferState::simplified_single_line().into_bsr(),
}
}
pub fn with_mapper<M: Fn(&Item) -> Option<Box<dyn AnyMsg>> + 'static>(self, m: M) -> Self {
Self {
mapper: Some(Box::new(m)),
..self
}
}
fn get_subtree(&self) -> Option<Item> {
let mut item = self.root.clone();
for exp in self.selected_nodes.iter() {
// TODO unwritten assumption that there is no duplicate keys
let mut next: Option<Item> = None;
if let Some(subtree) = item.child_iter().find(|item| item.id() == &exp.0) {
next = Some(subtree);
} else {
error!("key {:?} not found", exp);
return None;
};
if let Some(next) = next {
item = next;
} else {
return None; // never reached
}
}
Some(item)
}
fn get_selected_item(&self) -> Option<Item> {
self.get_subtree()
.map(|subtree| subtree.child_iter().skip(self.selected_row_idx as usize).next())
.flatten()
}
fn get_subtree_height(&self) -> u16 {
// TODO overflow
self.get_subtree().map(|item| item.child_iter().count()).unwrap_or(0) as u16
}
}
impl<Key: Hash + Eq + Debug + Clone + 'static, Item: TreeNode<Key> + 'static> Widget for NestedMenuWidget<Key, Item> {
fn id(&self) -> WID {
self.wid
}
fn static_typename() -> &'static str
where
Self: Sized,
{
"NestedMenuWidget"
}
fn typename(&self) -> &'static str {
"NestedMenuWidget"
}
fn full_size(&self) -> XY {
self.max_size
}
fn layout(&mut self, screenspace: Screenspace) {
let actual_size = screenspace.output_size();
self.layout_size = Some(actual_size);
}
fn on_input(&self, input_event: InputEvent) -> Option<Box<dyn AnyMsg>> {
return match input_event {
KeyInput(key) if key == Keycode::Enter.to_key() => Some(Box::new(Msg::Hit)),
KeyInput(key) if key == Keycode::ArrowUp.to_key() => {
if self.selected_row_idx > 0 {
Msg::Arrow(VerticalArrow::Up).someboxed()
} else {
None
}
}
KeyInput(key) if key == Keycode::ArrowDown.to_key() => {
if self.get_subtree_height() > self.selected_row_idx + 1 {
Msg::Arrow(VerticalArrow::Down).someboxed()
} else {
None
}
}
KeyInput(key) if key == Keycode::ArrowRight.to_key() => {
if self.get_selected_item().map(|item| item.is_leaf() == false).unwrap_or(false) {
Msg::Hit.someboxed()
} else {
None
}
}
KeyInput(key) if key == Keycode::ArrowLeft.to_key() => {
if self.selected_nodes.is_empty() == false {
Msg::UnwrapOneLevel.someboxed()
} else {
None
}
}
// KeyInput(key) if key_to_edit_msg(key).is_some() => {
// let msg = key_to_edit_msg(key).unwrap();
//
// let ignore: bool = match msg {
// CommonEditMsg::Char(_) => false,
// CommonEditMsg::Block(_) => true,
// CommonEditMsg::CursorUp { .. } => true,
// CommonEditMsg::CursorDown { .. } => true,
// CommonEditMsg::CursorLeft { .. } => true,
// CommonEditMsg::CursorRight { .. } => true,
// CommonEditMsg::Backspace => false,
// CommonEditMsg::LineBegin { .. } => true,
// CommonEditMsg::LineEnd { .. } => true,
// CommonEditMsg::WordBegin { .. } => true,
// CommonEditMsg::WordEnd { .. } => true,
// CommonEditMsg::PageUp { .. } => true,
// CommonEditMsg::PageDown { .. } => true,
// CommonEditMsg::Delete => true,
// CommonEditMsg::Copy => true,
// CommonEditMsg::Paste => true,
// CommonEditMsg::Undo => true,
// CommonEditMsg::Redo => true,
// CommonEditMsg::DeleteBlock { .. } => true,
// CommonEditMsg::InsertBlock { .. } => true,
// CommonEditMsg::SubstituteBlock { .. } => true,
// CommonEditMsg::Tab => true,
// CommonEditMsg::ShiftTab => true,
// };
//
// if !ignore {
// Msg::QueryEdit(msg).someboxed()
// } else {
// None
// }
// }
_ => None,
};
}
fn update(&mut self, msg: Box<dyn AnyMsg>) -> Option<Box<dyn AnyMsg>> {
let our_msg = msg.as_msg::<nested_menu::msg::Msg>();
if our_msg.is_none() {
warn!("expecetd nested_menu Msg, got {:?}", msg);
return None;
}
return match our_msg.unwrap() {
Msg::Hit => {
if let Some(item) = self.get_selected_item() {
if item.is_leaf() {
if let Some(mapper) = &self.mapper {
(*mapper)(&item)
} else {
error!("selection mapper not set");
None
}
} else {
self.selected_nodes.push((item.id().clone(), item.label().to_string()));
self.selected_row_idx = 0;
None
}
} else {
warn!("did not get selected item!");
None
}
}
Msg::Arrow(arrow) => match arrow {
VerticalArrow::Up => {
if self.selected_row_idx > 0 {
self.selected_row_idx -= 1;
} else {
error!("can't arrow up");
}
None
}
VerticalArrow::Down => {
if self.selected_row_idx + 1 < self.get_subtree_height() {
self.selected_row_idx += 1;
} else {
error!("can't arrow down");
}
None
}
},
Msg::UnwrapOneLevel => {
if self.selected_nodes.is_empty() {
error!("can't unwrap one level");
None
} else {
let last_item = self.selected_nodes.pop().unwrap().0;
let idx = if let Some(subtree) = self.get_subtree() {
let mut idx: u16 = 0;
for item in subtree.child_iter() {
if *item.id() == last_item {
break;
}
idx += 1;
}
idx
} else {
error!("can't figure out selected index from ");
0
};
self.selected_row_idx = idx;
None
}
}
_ => None,
};
}
fn render(&self, theme: &Theme, focused: bool, output: &mut dyn Output) {
let size = crate::unpack_unit_e!(self.layout_size, "render before layout",);
#[cfg(test)]
{
output.emit_metadata(crate::io::output::Metadata {
id: self.id(),
typename: NESTED_MENU_TYPENAME.to_string(),
rect: crate::primitives::rect::Rect::from_zero(size),
focused,
});
}
let item_expanded: u16 = 0;
let mut expanded_items: u16 = 0;
let base_style = get_default_style(theme, focused);
let cursor_style = get_highlighted_style(theme, focused);
let expanded_nodes_style = get_expanded_style(theme, focused);
// drawing the expanded nodes
// TODO overflow
for y in 0..std::cmp::min(self.selected_nodes.len() as u16, output.size().y) {
// ticker
{
let begin_pos = XY::new(y * NESTED_MENU_FOLDER_WIDHT, y);
output.print_at(begin_pos, expanded_nodes_style, NESTED_MENU_FOLDER_OPENED);
// TODO do a limited write
output.print_at(begin_pos + (1, 0), expanded_nodes_style, " ");
}
// the label
{
let begin_pos = XY::new((y + 1) * NESTED_MENU_FOLDER_WIDHT, y);
let text = self.selected_nodes[y as usize].1.as_str();
// TODO do a limited write
output.print_at(begin_pos, expanded_nodes_style, text);
// TODO overflow
// drawing rest of whitespace
for x in (begin_pos.x + text.width() as u16)..size.x {
output.print_at(XY::new(x, begin_pos.y), expanded_nodes_style, " ");
}
}
}
let left_height: u16 = if output.size().y as usize > self.selected_nodes.len() {
output.size().y - (self.selected_nodes.len() as u16)
} else {
0
};
let x_offset: u16 = NESTED_MENU_FOLDER_WIDHT * (self.selected_nodes.len() as u16);
let y_offset: u16 = self.selected_nodes.len() as u16; // TODO overflow
if let Some(subtree) = self.get_subtree() {
let mut idx: u16 = 0;
for item in subtree.child_iter() {
if idx >= left_height {
break;
}
let highlighted = self.selected_row_idx == idx;
let style = if highlighted {
theme.highlighted(focused)
} else {
theme.default_text(focused)
};
if item.is_leaf() == false {
output.print_at(XY::new(x_offset, y_offset + idx), style, "> ");
} else {
output.print_at(XY::new(x_offset, y_offset + idx), style, " ");
}
for x in 0..x_offset {
output.print_at(XY::new(x, y_offset + idx), style, " ");
}
let begin_x = x_offset + 1 * NESTED_MENU_FOLDER_WIDHT;
let text_pos = XY::new(begin_x, y_offset + idx);
output.print_at(text_pos, style, item.label().as_ref());
// coloring until end of size
// TODO overflow
for x in (begin_x + item.label().width() as u16)..size.x {
output.print_at(XY::new(x, text_pos.y), style, " ");
}
idx += 1;
}
}
}
}