mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-12 20:23:35 +00:00
Better Menu API
Now offer non-chainable methods as well.
This commit is contained in:
parent
e439b422da
commit
692c310656
@ -14,19 +14,17 @@ fn main() {
|
||||
.add("File",
|
||||
MenuTree::new()
|
||||
.leaf("New", |s| s.add_layer(Dialog::info("New file!")))
|
||||
.subtree("Recent",
|
||||
MenuTree::new()
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ()))
|
||||
.subtree("Recent", MenuTree::new().with(|tree| {
|
||||
for i in 1..6 {
|
||||
tree.add_leaf(&format!("Item {}", i), |_| ())
|
||||
}
|
||||
}))
|
||||
.delimiter()
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ())
|
||||
.leaf("Item 1", |_| ())
|
||||
.with(|tree| {
|
||||
for i in 1..10 {
|
||||
tree.add_leaf(&format!("Option {}", i), |_| ());
|
||||
}
|
||||
})
|
||||
.delimiter()
|
||||
.leaf("Quit", |s| s.quit()))
|
||||
.add("Help",
|
||||
|
24
src/menu.rs
24
src/menu.rs
@ -54,20 +54,34 @@ impl MenuTree {
|
||||
self.children.is_empty()
|
||||
}
|
||||
|
||||
pub fn delimiter(mut self) -> Self {
|
||||
pub fn add_delimiter(&mut self) {
|
||||
self.children.push(MenuItem::Delimiter);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn leaf<F: 'static + Fn(&mut Cursive)>(mut self, title: &str, cb: F) -> Self {
|
||||
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(), Rc::new(cb)));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn subtree(mut self, title: &str, tree: MenuTree) -> Self {
|
||||
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) {
|
||||
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))
|
||||
}
|
||||
|
||||
pub fn with<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
|
||||
f(&mut self);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user