2021-02-05 21:05:14 +00:00
|
|
|
use cursive::{event::Key, menu, traits::*, views::Dialog};
|
2017-10-12 23:38:55 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2017-01-23 23:42:36 +00:00
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// This examples shows how to configure and use a menubar at the top of the
|
|
|
|
// application.
|
|
|
|
|
2016-06-28 08:10:48 +00:00
|
|
|
fn main() {
|
2020-04-15 19:20:50 +00:00
|
|
|
let mut siv = cursive::default();
|
2016-06-28 08:10:48 +00:00
|
|
|
|
2017-01-23 23:42:36 +00:00
|
|
|
// 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.
|
2016-07-02 22:02:42 +00:00
|
|
|
siv.menubar()
|
2016-10-09 22:56:03 +00:00
|
|
|
// We add a new "File" tree
|
2019-03-01 00:04:14 +00:00
|
|
|
.add_subtree(
|
|
|
|
"File",
|
2021-02-05 21:05:14 +00:00
|
|
|
menu::Tree::new()
|
2019-03-01 00:04:14 +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, |_| ());
|
2017-01-23 23:42:36 +00:00
|
|
|
|
2019-03-01 00:04:14 +00:00
|
|
|
s.add_layer(Dialog::info("New file!"));
|
|
|
|
})
|
|
|
|
// ... and of sub-trees, which open up when selected.
|
|
|
|
.subtree(
|
|
|
|
"Recent",
|
|
|
|
// The `.with()` method can help when running loops
|
|
|
|
// within builder patterns.
|
2021-02-05 21:05:14 +00:00
|
|
|
menu::Tree::new().with(|tree| {
|
2019-03-01 00:04:14 +00:00
|
|
|
for i in 1..100 {
|
|
|
|
// We don't actually do anything here,
|
|
|
|
// but you could!
|
2021-02-05 21:05:14 +00:00
|
|
|
tree.add_item(menu::Item::leaf(format!("Item {}", i), |_| ()).with(|s| {
|
|
|
|
if i % 5 == 0 { s.disable(); }
|
|
|
|
}))
|
2019-03-01 00:04:14 +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), |_| ());
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.add_subtree(
|
|
|
|
"Help",
|
2021-02-05 21:05:14 +00:00
|
|
|
menu::Tree::new()
|
2019-03-01 00:04:14 +00:00
|
|
|
.subtree(
|
|
|
|
"Help",
|
2021-02-05 21:05:14 +00:00
|
|
|
menu::Tree::new()
|
2019-03-01 00:04:14 +00:00
|
|
|
.leaf("General", |s| {
|
|
|
|
s.add_layer(Dialog::info("Help message!"))
|
|
|
|
})
|
|
|
|
.leaf("Online", |s| {
|
|
|
|
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"))
|
|
|
|
}),
|
|
|
|
)
|
2018-01-09 13:37:40 +00:00
|
|
|
.add_delimiter()
|
|
|
|
.add_leaf("Quit", |s| s.quit());
|
2016-06-28 08:10:48 +00:00
|
|
|
|
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.
|
2018-01-09 14:17:49 +00:00
|
|
|
// Try uncommenting this line!
|
2016-10-09 22:56:03 +00:00
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
// siv.set_autohide_menu(false);
|
2016-06-28 08:10:48 +00:00
|
|
|
|
2016-07-03 02:37:38 +00:00
|
|
|
siv.add_global_callback(Key::Esc, |s| s.select_menubar());
|
2016-07-02 22:02:42 +00:00
|
|
|
|
2016-10-09 22:56:03 +00:00
|
|
|
siv.add_layer(Dialog::text("Hit <Esc> to show the menu!"));
|
2016-06-28 08:10:48 +00:00
|
|
|
|
|
|
|
siv.run();
|
|
|
|
}
|