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 {
|
2016-07-02 22:02:42 +00:00
|
|
|
Leaf(String, Callback),
|
|
|
|
Subtree(String, Rc<MenuTree>),
|
2016-06-28 04:55:46 +00:00
|
|
|
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-07-02 22:02:42 +00:00
|
|
|
|
|
|
|
pub fn is_delimiter(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
MenuItem::Delimiter => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2016-07-03 02:37:38 +00:00
|
|
|
|
|
|
|
pub fn is_subtree(&self) -> bool {
|
|
|
|
match *self {
|
2016-07-10 02:05:51 +00:00
|
|
|
MenuItem::Subtree(_, _) => true,
|
2016-07-03 02:37:38 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2016-07-03 03:44:27 +00:00
|
|
|
pub fn add_delimiter(&mut self) {
|
2016-07-02 22:02:42 +00:00
|
|
|
self.children.push(MenuItem::Delimiter);
|
2016-06-28 04:55:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 03:44:27 +00:00
|
|
|
pub fn delimiter(self) -> Self {
|
|
|
|
self.with(|menu| menu.add_delimiter())
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
pub fn add_leaf<F: 'static + Fn(&mut Cursive)>(&mut self, title: &str,
|
|
|
|
cb: F) {
|
2016-06-28 04:55:46 +00:00
|
|
|
self.children
|
2016-07-02 22:02:42 +00:00
|
|
|
.push(MenuItem::Leaf(title.to_string(), Rc::new(cb)));
|
2016-06-28 04:55:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 03:44:27 +00:00
|
|
|
pub fn leaf<F: 'static + Fn(&mut Cursive)>(self, title: &str, cb: F) -> Self {
|
|
|
|
self.with(|menu| menu.add_leaf(title, cb))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_subtree(&mut self, title: &str, tree: MenuTree) {
|
2016-07-02 22:02:42 +00:00
|
|
|
self.children
|
|
|
|
.push(MenuItem::Subtree(title.to_string(), Rc::new(tree)));
|
2016-07-03 03:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subtree(self, title: &str, tree: MenuTree) -> Self {
|
|
|
|
self.with(|menu| menu.add_subtree(title, tree))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
|
|
|
|
f(&mut self);
|
2016-06-28 04:55:46 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|