2016-09-29 05:45:27 +00:00
|
|
|
use cursive::traits::*;
|
2017-10-12 23:38:55 +00:00
|
|
|
use cursive::view::{Offset, Position};
|
|
|
|
use cursive::views::{Dialog, OnEventView, TextView};
|
2018-06-11 06:29:10 +00:00
|
|
|
use cursive::Cursive;
|
2015-05-23 23:46:38 +00:00
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// This example modifies a view after creation.
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-04-15 19:20:50 +00:00
|
|
|
let mut siv = cursive::default();
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2016-09-29 05:45:27 +00:00
|
|
|
let content = "Press Q to quit the application.\n\nPress P to open the \
|
|
|
|
popup.";
|
2015-05-23 23:46:38 +00:00
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
siv.add_global_callback('q', |s| s.quit());
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2020-01-06 23:41:51 +00:00
|
|
|
// Let's wrap the view to give it a recognizable name, so we can look for it.
|
2015-05-23 23:46:38 +00:00
|
|
|
// We add the P callback on the textview only (and not globally),
|
|
|
|
// so that we can't call it when the popup is already visible.
|
2017-10-12 23:38:55 +00:00
|
|
|
siv.add_layer(
|
2020-01-06 23:39:30 +00:00
|
|
|
OnEventView::new(TextView::new(content).with_name("text"))
|
2017-10-12 23:38:55 +00:00
|
|
|
.on_event('p', |s| show_popup(s)),
|
|
|
|
);
|
2015-05-23 23:46:38 +00:00
|
|
|
|
2015-05-23 17:33:29 +00:00
|
|
|
siv.run();
|
|
|
|
}
|
2018-01-16 02:55:27 +00:00
|
|
|
|
|
|
|
fn show_popup(siv: &mut Cursive) {
|
|
|
|
// Let's center the popup horizontally, but offset it down a few rows,
|
|
|
|
// so the user can see both the popup and the view underneath.
|
|
|
|
siv.screen_mut().add_layer_at(
|
|
|
|
Position::new(Offset::Center, Offset::Parent(5)),
|
|
|
|
Dialog::around(TextView::new("Tak!"))
|
|
|
|
.button("Change", |s| {
|
|
|
|
// Look for a view tagged "text".
|
|
|
|
// We _know_ it's there, so unwrap it.
|
2020-01-06 23:39:30 +00:00
|
|
|
s.call_on_name("text", |view: &mut TextView| {
|
2018-01-16 02:55:27 +00:00
|
|
|
let content = reverse(view.get_content().source());
|
|
|
|
view.set_content(content);
|
|
|
|
});
|
2019-03-01 00:04:14 +00:00
|
|
|
})
|
|
|
|
.dismiss_button("Ok"),
|
2018-01-16 02:55:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This just reverses each character
|
|
|
|
//
|
|
|
|
// Note: it would be more correct to iterate on graphemes instead.
|
|
|
|
// Check the unicode_segmentation crate!
|
|
|
|
fn reverse(text: &str) -> String {
|
|
|
|
text.chars().rev().collect()
|
|
|
|
}
|