cursive/src/menu.rs

115 lines
3.0 KiB
Rust
Raw Normal View History

//! Module to build menus.
2016-06-28 04:55:46 +00:00
use Cursive;
use std::rc::Rc;
use event::Callback;
/// Root of a menu tree.
2016-06-28 04:55:46 +00:00
#[derive(Default)]
pub struct MenuTree {
/// Menu items
2016-06-28 04:55:46 +00:00
pub children: Vec<MenuItem>,
}
/// Node in the menu tree.
2016-06-28 04:55:46 +00:00
pub enum MenuItem {
/// Actionnable button with a label.
Leaf(String, Callback),
/// Sub-menu with a label.
Subtree(String, Rc<MenuTree>),
/// Delimiter without a label.
2016-06-28 04:55:46 +00:00
Delimiter,
}
2016-07-02 08:01:09 +00:00
impl MenuItem {
/// 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,
}
}
/// Returns `true` if `self` is a delimiter.
pub fn is_delimiter(&self) -> bool {
match *self {
MenuItem::Delimiter => true,
_ => false,
}
}
/// Returns `true` if `self` is a subtree.
pub fn is_subtree(&self) -> bool {
match *self {
2016-07-10 02:05:51 +00:00
MenuItem::Subtree(_, _) => true,
_ => false,
}
}
2016-07-02 08:01:09 +00:00
}
2016-06-28 04:55:46 +00:00
impl MenuTree {
/// Creates a new, empty tree.
2016-06-28 04:55:46 +00:00
pub fn new() -> Self {
Self::default()
}
/// Returns the number of children, including delimiters.
2016-07-02 08:01:09 +00:00
pub fn len(&self) -> usize {
self.children.len()
}
/// Remove every children from this tree.
2016-06-28 04:55:46 +00:00
pub fn clear(&mut self) {
self.children.clear();
}
/// 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()
}
/// Adds a delimiter to the end of this tree.
pub fn add_delimiter(&mut self) {
self.children.push(MenuItem::Delimiter);
2016-06-28 04:55:46 +00:00
}
/// Adds a delimiter to the end of this tree - chainable variant.
pub fn delimiter(self) -> Self {
self.with(|menu| menu.add_delimiter())
}
/// 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
.push(MenuItem::Leaf(title.to_string(), Rc::new(cb)));
2016-06-28 04:55:46 +00:00
}
/// Adds a actionnable leaf to the end of this tree - chainable variant.
pub fn leaf<F: 'static + Fn(&mut Cursive)>(self, title: &str, cb: F) -> Self {
self.with(|menu| menu.add_leaf(title, cb))
}
/// Adds a submenu to the end of this tree.
pub fn add_subtree(&mut self, title: &str, tree: MenuTree) {
self.children
.push(MenuItem::Subtree(title.to_string(), Rc::new(tree)));
}
/// Adds a submenu to the end of this tree - chainable variant.
pub fn subtree(self, title: &str, tree: MenuTree) -> Self {
self.with(|menu| menu.add_subtree(title, tree))
}
/// Calls the given closure on `self`.
///
/// Useful in a function chain.
pub fn with<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
f(&mut self);
2016-06-28 04:55:46 +00:00
self
}
}