cursive/examples/refcell_view.rs

43 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2019-11-15 19:41:05 +00:00
use cursive::traits::{Identifiable, Resizable};
use cursive::views::{Dialog, Edit, LinearLayout, Text};
2018-06-11 06:29:10 +00:00
use cursive::Cursive;
2017-02-26 23:53:12 +00:00
2018-01-16 02:55:27 +00:00
// This example shows a way to access multiple views at the same time.
2017-02-26 23:53:12 +00:00
fn main() {
let mut siv = Cursive::default();
2017-02-26 23:53:12 +00:00
// 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()
2019-11-15 19:41:05 +00:00
.child(Edit::new().on_edit(on_edit).with_id("1"))
.child(Edit::new().on_edit(on_edit).with_id("2"))
.child(Text::new("match").with_id("match"))
2017-10-12 23:38:55 +00:00
.fixed_width(10),
2019-03-01 00:04:14 +00:00
)
.button("Quit", Cursive::quit),
2017-10-12 23:38:55 +00:00
);
2017-02-26 23:53:12 +00:00
siv.run();
}
// Compare the content of the two edit views,
2019-11-15 19:41:05 +00:00
// and update the Text accordingly.
2017-02-26 23:53:12 +00:00
//
// 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.
2019-11-15 19:41:05 +00:00
let edit_1 = siv.find_id::<Edit>("1").unwrap();
let edit_2 = siv.find_id::<Edit>("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();
2018-01-16 02:55:27 +00:00
2019-11-15 19:41:05 +00:00
siv.call_on_id("match", |v: &mut Text| {
2018-06-16 20:23:09 +00:00
v.set_content(if matches { "match" } else { "no match" })
2017-10-12 23:38:55 +00:00
});
2017-02-26 23:53:12 +00:00
}