2016-09-29 05:45:27 +00:00
|
|
|
use cursive::traits::*;
|
2017-02-07 02:18:17 +00:00
|
|
|
use cursive::views::{Dialog, EditView, TextView};
|
2018-06-11 06:29:10 +00:00
|
|
|
use cursive::Cursive;
|
2015-05-26 22:46:05 +00:00
|
|
|
|
|
|
|
fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
let mut siv = Cursive::default();
|
2015-05-26 22:46:05 +00:00
|
|
|
|
2015-05-26 23:11:22 +00:00
|
|
|
// Create a dialog with an edit text and a button.
|
2016-07-30 19:58:25 +00:00
|
|
|
// The user can either hit the <Ok> button,
|
|
|
|
// or press Enter on the edit text.
|
2017-10-12 23:38:55 +00:00
|
|
|
siv.add_layer(
|
|
|
|
Dialog::new()
|
|
|
|
.title("Enter your name")
|
2018-01-16 02:55:27 +00:00
|
|
|
// Padding is (left, right, top, bottom)
|
2020-01-10 18:51:48 +00:00
|
|
|
.padding_lrtb(1, 1, 1, 0)
|
2017-10-12 23:38:55 +00:00
|
|
|
.content(
|
|
|
|
EditView::new()
|
2018-06-20 17:28:44 +00:00
|
|
|
// Call `show_popup` when the user presses `Enter`
|
2017-10-12 23:38:55 +00:00
|
|
|
.on_submit(show_popup)
|
2018-06-20 17:28:44 +00:00
|
|
|
// Give the `EditView` a name so we can refer to it later.
|
2020-01-06 23:39:30 +00:00
|
|
|
.with_name("name")
|
2020-01-06 19:51:50 +00:00
|
|
|
// Wrap this in a `ResizedView` with a fixed width.
|
2020-01-06 23:39:30 +00:00
|
|
|
// Do this _after_ `with_name` or the name will point to the
|
2020-01-06 19:51:50 +00:00
|
|
|
// `ResizedView` instead of `EditView`!
|
2017-10-12 23:38:55 +00:00
|
|
|
.fixed_width(20),
|
|
|
|
)
|
|
|
|
.button("Ok", |s| {
|
2018-01-16 02:55:27 +00:00
|
|
|
// This will run the given closure, *ONLY* if a view with the
|
2020-01-06 23:41:51 +00:00
|
|
|
// correct type and the given name is found.
|
2019-03-01 00:04:14 +00:00
|
|
|
let name = s
|
2020-01-06 23:39:30 +00:00
|
|
|
.call_on_name("name", |view: &mut EditView| {
|
2019-03-01 00:04:14 +00:00
|
|
|
// We can return content from the closure!
|
|
|
|
view.get_content()
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-01-16 02:55:27 +00:00
|
|
|
|
|
|
|
// Run the next step
|
2017-10-12 23:38:55 +00:00
|
|
|
show_popup(s, &name);
|
|
|
|
}),
|
|
|
|
);
|
2015-05-26 22:46:05 +00:00
|
|
|
|
|
|
|
siv.run();
|
|
|
|
}
|
2016-07-30 19:58:25 +00:00
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// This will replace the current layer with a new popup.
|
|
|
|
// If the name is empty, we'll show an error message instead.
|
2016-07-30 19:58:25 +00:00
|
|
|
fn show_popup(s: &mut Cursive, name: &str) {
|
|
|
|
if name.is_empty() {
|
2018-06-20 17:28:44 +00:00
|
|
|
// Try again as many times as we need!
|
2016-07-30 19:58:25 +00:00
|
|
|
s.add_layer(Dialog::info("Please enter a name!"));
|
|
|
|
} else {
|
|
|
|
let content = format!("Hello {}!", name);
|
2018-06-20 17:28:44 +00:00
|
|
|
// Remove the initial popup
|
2016-07-30 19:58:25 +00:00
|
|
|
s.pop_layer();
|
2018-06-20 17:28:44 +00:00
|
|
|
// And put a new one instead
|
2017-10-12 23:38:55 +00:00
|
|
|
s.add_layer(
|
|
|
|
Dialog::around(TextView::new(content))
|
|
|
|
.button("Quit", |s| s.quit()),
|
|
|
|
);
|
2016-07-30 19:58:25 +00:00
|
|
|
}
|
|
|
|
}
|