2016-07-12 03:26:33 +00:00
|
|
|
//! Module to build menus.
|
2016-07-20 03:28:34 +00:00
|
|
|
//!
|
|
|
|
//! Menus are a way to arrange many actions in groups of more manageable size.
|
|
|
|
//!
|
|
|
|
//! A menu can be seen as a `MenuTree`. It has a list of children:
|
|
|
|
//!
|
|
|
|
//! * Leaf nodes are made of a label and a callback
|
|
|
|
//! * Sub-trees are made of a label, and another `MenuTree`.
|
|
|
|
//! * Delimiters are just there to separate groups of related children.
|
|
|
|
//!
|
|
|
|
//! The [menubar] is the main way to show menus.
|
|
|
|
//!
|
|
|
|
//! [menubar]: ../struct.Cursive.html#method.menubar
|
2016-07-12 03:26:33 +00:00
|
|
|
|
2016-07-16 20:25:21 +00:00
|
|
|
use With;
|
2016-06-28 04:55:46 +00:00
|
|
|
use Cursive;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use event::Callback;
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Root of a menu tree.
|
2016-06-28 04:55:46 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct MenuTree {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Menu items
|
2016-06-28 04:55:46 +00:00
|
|
|
pub children: Vec<MenuItem>,
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Node in the menu tree.
|
2016-06-28 04:55:46 +00:00
|
|
|
pub enum MenuItem {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Actionnable button with a label.
|
2016-07-02 22:02:42 +00:00
|
|
|
Leaf(String, Callback),
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Sub-menu with a label.
|
2016-07-02 22:02:42 +00:00
|
|
|
Subtree(String, Rc<MenuTree>),
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Delimiter without a label.
|
2016-06-28 04:55:46 +00:00
|
|
|
Delimiter,
|
|
|
|
}
|
|
|
|
|
2016-07-02 08:01:09 +00:00
|
|
|
impl MenuItem {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns the label for this item.
|
|
|
|
///
|
|
|
|
/// Returns an empty string if `self` is a delimiter.
|
2016-07-02 08:01:09 +00:00
|
|
|
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
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns `true` if `self` is a delimiter.
|
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
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns `true` if `self` is a subtree.
|
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 {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Creates a new, empty tree.
|
2016-06-28 04:55:46 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns the number of children, including delimiters.
|
2016-07-02 08:01:09 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.children.len()
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Remove every children from this tree.
|
2016-06-28 04:55:46 +00:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.children.clear();
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Returns `true` if this tree has no children.
|
2016-06-28 04:55:46 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.children.is_empty()
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Adds a delimiter to the end of this tree.
|
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-12 03:26:33 +00:00
|
|
|
/// Adds a delimiter to the end of this tree - chainable variant.
|
2016-07-03 03:44:27 +00:00
|
|
|
pub fn delimiter(self) -> Self {
|
|
|
|
self.with(|menu| menu.add_delimiter())
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Adds a actionnable leaf to the end of this tree.
|
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-25 06:00:13 +00:00
|
|
|
.push(MenuItem::Leaf(title.to_string(), Callback::from_fn(cb)));
|
2016-06-28 04:55:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Adds a actionnable leaf to the end of this tree - chainable variant.
|
2016-07-15 03:27:15 +00:00
|
|
|
pub fn leaf<F>(self, title: &str, cb: F) -> Self
|
|
|
|
where F: 'static + Fn(&mut Cursive)
|
|
|
|
{
|
2016-07-03 03:44:27 +00:00
|
|
|
self.with(|menu| menu.add_leaf(title, cb))
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Adds a submenu to the end of this tree.
|
2016-07-03 03:44:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Adds a submenu to the end of this tree - chainable variant.
|
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))
|
|
|
|
}
|
2016-06-28 04:55:46 +00:00
|
|
|
}
|