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
use With;
use Cursive;
use std::rc::Rc;
use event::Callback;
#[derive(Default)]
pub struct MenuTree {
pub children: Vec<MenuItem>,
}
pub enum MenuItem {
Leaf(String, Callback),
Subtree(String, Rc<MenuTree>),
Delimiter,
}
impl MenuItem {
pub fn label(&self) -> &str {
match *self {
MenuItem::Delimiter => "",
MenuItem::Leaf(ref label, _) |
MenuItem::Subtree(ref label, _) => label,
}
}
pub fn is_delimiter(&self) -> bool {
match *self {
MenuItem::Delimiter => true,
_ => false,
}
}
pub fn is_subtree(&self) -> bool {
match *self {
MenuItem::Subtree(_, _) => true,
_ => false,
}
}
}
impl MenuTree {
pub fn new() -> Self {
Self::default()
}
pub fn len(&self) -> usize {
self.children.len()
}
pub fn clear(&mut self) {
self.children.clear();
}
pub fn is_empty(&self) -> bool {
self.children.is_empty()
}
pub fn add_delimiter(&mut self) {
self.children.push(MenuItem::Delimiter);
}
pub fn delimiter(self) -> Self {
self.with(|menu| menu.add_delimiter())
}
pub fn add_leaf<F: 'static + Fn(&mut Cursive)>(&mut self, title: &str,
cb: F) {
self.children
.push(MenuItem::Leaf(title.to_string(), Callback::from_fn(cb)));
}
pub fn leaf<F>(self, title: &str, cb: F) -> Self
where F: 'static + Fn(&mut Cursive)
{
self.with(|menu| menu.add_leaf(title, cb))
}
pub fn add_subtree(&mut self, title: &str, tree: MenuTree) {
self.children
.push(MenuItem::Subtree(title.to_string(), Rc::new(tree)));
}
pub fn subtree(self, title: &str, tree: MenuTree) -> Self {
self.with(|menu| menu.add_subtree(title, tree))
}
}