Add some convenient ID methods

This commit is contained in:
Alexandre Bury 2015-07-28 15:57:52 +02:00
parent 25a298a2eb
commit 36fbc1ce5b
4 changed files with 15 additions and 5 deletions

View File

@ -1,19 +1,19 @@
extern crate cursive;
use cursive::{Cursive};
use cursive::view::{Dialog,IdView,EditView,Selector,TextView};
use cursive::view::{Dialog,EditView,TextView};
fn main() {
let mut siv = Cursive::new();
// Create a dialog with an edit text and a button.
siv.add_layer(Dialog::new(IdView::new("edit", EditView::new().min_length(20)))
siv.add_layer(Dialog::new(EditView::new().min_length(20).with_id("edit"))
.padding((1,1,1,0))
.title("Enter your name")
.button("Ok", |s| {
// When the button is clicked, read the text and print it in a new dialog.
let content = {
let name = s.find::<EditView>(&Selector::Id("edit")).unwrap().get_content();
let name = s.find_id::<EditView>("edit").unwrap().get_content();
format!("Hello {}!", name)
};
s.pop_layer();

View File

@ -168,6 +168,11 @@ impl Cursive {
}
}
/// Convenient method to use `find` with a `Selector::Id`.
pub fn find_id<V: View + Any>(&mut self, id: &str) -> Option<&mut V> {
self.find(&Selector::Id(id))
}
/// Adds a global callback, triggered on the given key press when no view catches it.
pub fn add_global_callback<F,E: ToEvent>(&mut self, event: E, cb: F)
where F: Fn(&mut Cursive) + 'static

View File

@ -2,7 +2,7 @@ use ncurses;
use theme::ColorPair;
use vec::Vec2;
use view::{View,SizeRequest};
use view::{View,IdView,SizeRequest};
use event::*;
use printer::Printer;
@ -46,6 +46,11 @@ impl EditView {
self
}
/// Wraps this view into an IdView with the given id.
pub fn with_id(self, label: &str) -> IdView<Self> {
IdView::new(label, self)
}
}
fn remove_char(s: &mut String, cursor: usize) {

View File

@ -29,12 +29,12 @@ use printer::Printer;
pub use self::request::{DimensionRequest,SizeRequest};
pub use self::scroll::ScrollBase;
pub use self::id_view::IdView;
pub use self::box_view::BoxView;
pub use self::button::Button;
pub use self::dialog::Dialog;
pub use self::edit_view::EditView;
pub use self::full_view::FullView;
pub use self::id_view::IdView;
pub use self::key_event_view::KeyEventView;
pub use self::linear_layout::LinearLayout;
pub use self::view_path::ViewPath;