2015-05-23 17:33:29 +00:00
|
|
|
extern crate cursive;
|
|
|
|
|
2016-09-29 05:45:27 +00:00
|
|
|
use cursive::Cursive;
|
2017-06-12 23:39:12 +00:00
|
|
|
use cursive::views::{Dialog, OnEventView, TextView};
|
2016-09-29 05:45:27 +00:00
|
|
|
use cursive::view::{Offset, Position};
|
|
|
|
use cursive::traits::*;
|
2015-05-23 23:46:38 +00:00
|
|
|
|
|
|
|
fn show_popup(siv: &mut Cursive) {
|
|
|
|
|
2016-07-02 02:19:43 +00:00
|
|
|
// Let's center the popup horizontally, but offset it down a few rows
|
|
|
|
siv.screen_mut()
|
|
|
|
.add_layer_at(Position::new(Offset::Center, Offset::Parent(3)),
|
2016-10-02 22:15:30 +00:00
|
|
|
Dialog::around(TextView::new("Tak!"))
|
2016-07-02 02:19:43 +00:00
|
|
|
.button("Change", |s| {
|
|
|
|
// Look for a view tagged "text". We _know_ it's there, so unwrap it.
|
2017-03-26 01:22:14 +00:00
|
|
|
s.call_on_id("text", |view: &mut TextView| {
|
2017-02-07 02:18:17 +00:00
|
|
|
let content: String = view.get_content().chars().rev().collect();
|
|
|
|
view.set_content(content);
|
|
|
|
});
|
2016-07-02 02:19:43 +00:00
|
|
|
})
|
|
|
|
.dismiss_button("Ok"));
|
2015-05-23 23:46:38 +00:00
|
|
|
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut siv = Cursive::new();
|
|
|
|
|
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
|
|
|
|
2015-05-23 23:46:38 +00:00
|
|
|
// Let's wrap the view to give it a recognizable ID, so we can look for it.
|
|
|
|
// 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-06-12 23:39:12 +00:00
|
|
|
siv.add_layer(OnEventView::new(TextView::new(content).with_id("text"))
|
|
|
|
.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();
|
|
|
|
}
|