2016-06-28 04:55:46 +00:00
|
|
|
use Cursive;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use event::Callback;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct MenuTree {
|
|
|
|
pub children: Vec<MenuItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum MenuItem {
|
|
|
|
Leaf(String, Rc<Callback>),
|
|
|
|
Subtree(String, Box<MenuTree>),
|
|
|
|
Delimiter,
|
|
|
|
}
|
|
|
|
|
2016-07-02 08:01:09 +00:00
|
|
|
impl MenuItem {
|
|
|
|
pub fn label(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
MenuItem::Delimiter => "",
|
|
|
|
MenuItem::Leaf(ref label, _) |
|
|
|
|
MenuItem::Subtree(ref label, _) => label,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 04:55:46 +00:00
|
|
|
impl MenuTree {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2016-07-02 08:01:09 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.children.len()
|
|
|
|
}
|
|
|
|
|
2016-06-28 04:55:46 +00:00
|
|
|
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(mut self) -> Self {
|
|
|
|
self.add_delimiter();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-06-30 00:36:20 +00:00
|
|
|
pub fn leaf<F: 'static + Fn(&mut Cursive)>(&mut self, title: &str, cb: F) -> &mut Self {
|
2016-06-28 05:10:59 +00:00
|
|
|
self.children
|
|
|
|
.push(MenuItem::Leaf(title.to_string(), Rc::new(Box::new(cb))));
|
2016-06-28 04:55:46 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_subtree(&mut self, title: &str, tree: MenuTree) -> &mut Self {
|
|
|
|
self.children
|
|
|
|
.push(MenuItem::Subtree(title.to_string(), Box::new(tree)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-06-28 08:10:48 +00:00
|
|
|
pub fn new_subtree(&mut self, title: &str) -> &mut MenuTree {
|
2016-06-28 04:55:46 +00:00
|
|
|
self.add_subtree(title, MenuTree::new());
|
2016-06-28 05:40:11 +00:00
|
|
|
match *self.children.last_mut().unwrap() {
|
2016-06-28 08:10:48 +00:00
|
|
|
MenuItem::Subtree(_, ref mut tree) => &mut *tree,
|
2016-06-28 04:55:46 +00:00
|
|
|
_ => panic!("??"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subtree(mut self, title: &str, tree: MenuTree) -> Self {
|
|
|
|
self.add_subtree(title, tree);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|