cursive/examples/edit.rs
Alexandre Bury b63762d441 View::find_any now takes a closure instead of returning reference
Also updated Finder::find and find_id.
2017-02-07 15:49:06 -08:00

40 lines
1.1 KiB
Rust

extern crate cursive;
use cursive::Cursive;
use cursive::traits::*;
use cursive::views::{Dialog, EditView, TextView};
fn main() {
let mut siv = Cursive::new();
// Create a dialog with an edit text and a button.
// The user can either hit the <Ok> button,
// or press Enter on the edit text.
siv.add_layer(Dialog::new()
.title("Enter your name")
.padding((1, 1, 1, 0))
.content(EditView::new()
.on_submit(show_popup)
.with_id("name")
.fixed_width(20))
.button("Ok", |s| {
let name =
s.find_id("name", |view: &mut EditView| view.get_content())
.unwrap();
show_popup(s, &name);
}));
siv.run();
}
fn show_popup(s: &mut Cursive, name: &str) {
if name.is_empty() {
s.add_layer(Dialog::info("Please enter a name!"));
} else {
let content = format!("Hello {}!", name);
s.pop_layer();
s.add_layer(Dialog::around(TextView::new(content))
.button("Quit", |s| s.quit()));
}
}