main.rs
1 use cursive::align::*; 2 use cursive::traits::*; 3 use cursive::views::*; 4 use cursive::Cursive; 5 6 fn main() { 7 let mut siv = cursive::default(); 8 siv.add_global_callback('q', |s| s.quit()); 9 10 let elements = LinearLayout::vertical() 11 .child(TextView::new("This is TextView")) 12 .child(Button::new("This is a Button", open_dialog_box)) 13 .child(Button::new("Click me for more - Part 1", part1)) 14 .child(Button::new("Click me for more - Part 2", part2)) 15 .child(Button::new("Exit", |s| s.quit())); 16 17 siv.add_layer(elements); 18 siv.run(); 19 } 20 21 fn open_dialog_box(s: &mut Cursive) { 22 fn back(s: &mut Cursive) { 23 s.pop_layer(); 24 } 25 let dialog = Dialog::new() 26 .button("Back", back) 27 .button("Quit", |s| s.quit()) 28 .title("Dialog Box"); 29 s.add_layer(dialog); 30 } 31 32 fn part1(s: &mut Cursive) { 33 let dialog_elements = Dialog::around( 34 LinearLayout::vertical() 35 .child(TextView::new("Checkbox")) 36 .child(Checkbox::new().checked().with_name("check")) 37 .child(TextView::new("EditView")) 38 .child( 39 EditView::new() 40 .on_submit(callback) 41 .with_name("text") 42 .fixed_width(20), 43 ), 44 ) 45 .title("Part - 1 Widgets") 46 .button("Back", back); 47 s.add_layer(dialog_elements); 48 } 49 50 fn part2(s: &mut Cursive) { 51 let dialog_elements = Dialog::around( 52 LinearLayout::vertical() 53 .child(TextView::new("TextArea")) 54 .child(TextArea::new().content("")) 55 .child(TextView::new("SelectView")) 56 .child(TextView::new("").with_name("text")) 57 .child( 58 SelectView::new() 59 .h_align(HAlign::Center) 60 .item("Point 1", 1) 61 .item("Point 2", 2) 62 .item("Point 3", 3) 63 .on_submit(|s, item| { 64 let chosen = match *item { 65 1 => "Point 1", 66 2 => "Point 2", 67 3 => "Point 3", 68 _ => unreachable!("No such Point"), 69 }; 70 71 s.add_layer(Dialog::info(format!("You chose \"{}\"", chosen))); 72 }), 73 ), 74 ) 75 .title("Part - 2 Widgets") 76 .button("Back", back); 77 78 s.add_layer(dialog_elements); 79 } 80 81 fn callback(s: &mut Cursive, text: &str) { 82 s.add_layer(Dialog::info(format!("You typed \"{}\" in EditView", text))); 83 } 84 85 fn back(s: &mut Cursive) { 86 s.pop_layer(); 87 }