cursive/examples/refcell_view.rs

41 lines
1.3 KiB
Rust
Raw Permalink Normal View History

2017-02-26 23:53:12 +00:00
extern crate cursive;
use cursive::Cursive;
use cursive::view::{Boxable, Identifiable};
2017-10-12 23:38:55 +00:00
use cursive::views::{Dialog, EditView, LinearLayout, TextView};
2017-02-26 23:53:12 +00:00
fn main() {
let mut siv = Cursive::new();
// Create a dialog with 2 edit fields, and a text view.
// The text view indicates when the 2 fields content match.
2017-10-12 23:38:55 +00:00
siv.add_layer(
Dialog::around(
LinearLayout::vertical()
.child(EditView::new().on_edit(on_edit).with_id("1"))
.child(EditView::new().on_edit(on_edit).with_id("2"))
.child(TextView::new("match").with_id("match"))
.fixed_width(10),
).button("Quit", Cursive::quit),
);
2017-02-26 23:53:12 +00:00
siv.run();
}
// Compare the content of the two edit views,
// and update the TextView accordingly.
//
// We'll ignore the `content` and `cursor` arguments,
// and directly retrieve the content from the `Cursive` root.
fn on_edit(siv: &mut Cursive, _content: &str, _cursor: usize) {
// Get handles for each view.
let edit_1 = siv.find_id::<EditView>("1").unwrap();
let edit_2 = siv.find_id::<EditView>("2").unwrap();
2017-02-26 23:53:12 +00:00
// Directly compare references to edit_1 and edit_2.
let matches = edit_1.get_content() == edit_2.get_content();
2017-10-12 23:38:55 +00:00
siv.call_on_id("match", |v: &mut TextView| {
v.set_content(if matches { "match" } else { "no match" })
});
2017-02-26 23:53:12 +00:00
}