cursive/examples/list_view.rs

82 lines
3.1 KiB
Rust
Raw Normal View History

use cursive::traits::*;
2018-06-11 06:29:10 +00:00
use cursive::views::{
Checkbox, Dialog, EditView, LinearLayout, ListView, SelectView, TextView,
};
use cursive::Cursive;
2018-01-16 02:55:27 +00:00
// This example uses a ListView.
//
// ListView can be used to build forms, with a list of inputs.
fn main() {
let mut siv = Cursive::default();
2017-10-12 23:38:55 +00:00
siv.add_layer(
Dialog::new()
.title("Please fill out this form")
.button("Ok", |s| s.quit())
.content(
ListView::new()
2018-01-16 02:55:27 +00:00
// Each child is a single-line view with a label
2017-10-12 23:38:55 +00:00
.child("Name", EditView::new().fixed_width(10))
.child(
"Receive spam?",
2017-12-30 22:03:42 +00:00
Checkbox::new().on_change(|s, checked| {
2018-01-16 02:55:27 +00:00
// Enable/Disable the next field depending on this checkbox
2017-12-30 22:03:42 +00:00
for name in &["email1", "email2"] {
2017-10-12 23:38:55 +00:00
s.call_on_id(name, |view: &mut EditView| {
view.set_enabled(checked)
});
if checked {
s.focus_id("email1").unwrap();
}
2017-12-30 22:03:42 +00:00
}
}),
2017-10-12 23:38:55 +00:00
)
.child(
"Email",
2018-01-16 02:55:27 +00:00
// Each child must have a height of 1 line,
// but we can still combine multiple views!
2017-10-12 23:38:55 +00:00
LinearLayout::horizontal()
.child(
EditView::new()
.disabled()
.with_id("email1")
.fixed_width(15),
)
.child(TextView::new("@"))
.child(
EditView::new()
.disabled()
.with_id("email2")
.fixed_width(10),
),
)
2018-01-16 02:55:27 +00:00
// Delimiter currently are just a blank line
2017-10-12 23:38:55 +00:00
.delimiter()
.child(
"Age",
2018-01-16 02:55:27 +00:00
// Popup-mode SelectView are small enough to fit here
2017-10-12 23:38:55 +00:00
SelectView::new()
.popup()
.item_str("0-18")
.item_str("19-30")
.item_str("31-40")
.item_str("41+"),
)
2017-12-30 22:03:42 +00:00
.with(|list| {
2018-01-16 02:55:27 +00:00
// We can also add children procedurally
2017-12-30 22:03:42 +00:00
for i in 0..50 {
list.add_child(
&format!("Item {}", i),
EditView::new(),
);
}
})
.scrollable(),
2017-10-12 23:38:55 +00:00
),
);
siv.run();
}