cursive/examples/menubar.rs

80 lines
3.1 KiB
Rust
Raw Permalink Normal View History

extern crate cursive;
use cursive::Cursive;
use cursive::event::Key;
2016-10-09 22:56:03 +00:00
use cursive::menu::MenuTree;
use cursive::traits::*;
2016-10-09 22:56:03 +00:00
use cursive::views::Dialog;
2017-10-12 23:38:55 +00:00
use std::sync::atomic::{AtomicUsize, Ordering};
fn main() {
let mut siv = Cursive::new();
// We'll use a counter to name new files.
let counter = AtomicUsize::new(1);
2016-10-09 22:56:03 +00:00
// The menubar is a list of (label, menu tree) pairs.
siv.menubar()
2016-10-09 22:56:03 +00:00
// We add a new "File" tree
.add_subtree("File",
MenuTree::new()
2016-10-09 22:56:03 +00:00
// Trees are made of leaves, with are directly actionable...
.leaf("New", move |s| {
// Here we use the counter to add an entry
// in the list of "Recent" items.
let i = counter.fetch_add(1, Ordering::Relaxed);
let filename = format!("New {}", i);
s.menubar().find_subtree("File").unwrap()
.find_subtree("Recent").unwrap()
.insert_leaf(0, filename, |_| ());
s.add_layer(Dialog::info("New file!"));
})
2016-10-09 22:56:03 +00:00
// ... and of sub-trees, which open up when selected.
.subtree("Recent",
2016-10-09 22:56:03 +00:00
// The `.with()` method can help when running loops
// within builder patterns.
MenuTree::new().with(|tree| {
for i in 1..100 {
2016-10-09 22:56:03 +00:00
// We don't actually do anything here,
// but you could!
tree.add_leaf(format!("Item {}", i), |_| ())
}
}))
2016-10-09 22:56:03 +00:00
// Delimiter are simple lines between items,
// and cannot be selected.
.delimiter()
.with(|tree| {
for i in 1..10 {
tree.add_leaf(format!("Option {}", i), |_| ());
}
})
.delimiter()
.leaf("Quit", |s| s.quit()))
.add_subtree("Help",
MenuTree::new()
.subtree("Help",
MenuTree::new()
.leaf("General", |s| {
s.add_layer(Dialog::info("Help message!"))
})
.leaf("Online", |s| {
2016-10-09 22:56:03 +00:00
let text = "Google it yourself!\n\
Kids, these days...";
s.add_layer(Dialog::info(text))
}))
.leaf("About",
|s| s.add_layer(Dialog::info("Cursive v0.0.0"))));
2017-10-15 04:18:50 +00:00
// When `autohide` is on (default), the menu only appears when active.
2016-10-09 22:56:03 +00:00
// Turning it off will leave the menu always visible.
// siv.set_autohide_menu(false);
siv.add_global_callback(Key::Esc, |s| s.select_menubar());
2016-10-09 22:56:03 +00:00
siv.add_layer(Dialog::text("Hit <Esc> to show the menu!"));
siv.run();
}