Simplified examples

This commit is contained in:
Alexandre Bury 2015-07-30 14:51:12 +02:00
parent ac85a1f288
commit fa4b52ee81
2 changed files with 12 additions and 9 deletions

View File

@ -12,13 +12,16 @@ fn main() {
.title("Enter your name") .title("Enter your name")
.button("Ok", |s| { .button("Ok", |s| {
// When the button is clicked, read the text and print it in a new dialog. // When the button is clicked, read the text and print it in a new dialog.
let content = { let name = s.find_id::<EditView>("edit").unwrap().get_content().to_string();
let name = s.find_id::<EditView>("edit").unwrap().get_content(); if name.is_empty() {
format!("Hello {}!", name) s.add_layer(Dialog::new(TextView::new("Please enter a name!"))
}; .dismiss_button("Ok"));
s.pop_layer(); } else {
s.add_layer(Dialog::new(TextView::new(&content)) let content = format!("Hello {}!", name);
.button("Quit", |s| s.quit())); s.pop_layer();
s.add_layer(Dialog::new(TextView::new(&content))
.button("Quit", |s| s.quit()));
}
})); }));
siv.run(); siv.run();

View File

@ -1,14 +1,14 @@
extern crate cursive; extern crate cursive;
use cursive::Cursive; use cursive::Cursive;
use cursive::view::{IdView,TextView,Dialog,Selector,KeyEventView}; use cursive::view::{IdView,TextView,Dialog,KeyEventView};
fn show_popup(siv: &mut Cursive) { fn show_popup(siv: &mut Cursive) {
siv.add_layer(Dialog::new(TextView::new("Tak!")) siv.add_layer(Dialog::new(TextView::new("Tak!"))
.button("Change", |s| { .button("Change", |s| {
// Look for a view tagged "text". We _know_ it's there, so unwrap it. // Look for a view tagged "text". We _know_ it's there, so unwrap it.
let view = s.find::<TextView>(&Selector::Id("text")).unwrap(); let view = s.find_id::<TextView>("text").unwrap();
let content: String = view.get_content().chars().rev().collect(); let content: String = view.get_content().chars().rev().collect();
view.set_content(&content); view.set_content(&content);
}) })