Add with_id_mut and find_id_mut convenient methods.

This commit is contained in:
Alexandre Bury 2017-02-08 15:20:41 -08:00
parent 1de7dcd819
commit 1b8d109e94
2 changed files with 24 additions and 2 deletions

View File

@ -427,6 +427,18 @@ impl Cursive {
self.find(&view::Selector::Id(id), callback) self.find(&view::Selector::Id(id), callback)
} }
/// Convenient method to find a view wrapped in [`RefCellView`].
///
/// This looks for a `RefCellView<V>` with the given ID, and return
/// a mutable reference to the wrapped view.
///
/// [`RefCellView`]: views/struct.RefCellView.html
pub fn find_id_mut<V>(&mut self, id: &str) -> Option<views::ViewRef<V>>
where V: View + Any
{
self.find_id(id, views::RefCellView::<V>::get_mut)
}
/// Adds a global callback. /// Adds a global callback.
/// ///
/// Will be triggered on the given key press when no view catches it. /// Will be triggered on the given key press when no view catches it.

View File

@ -1,16 +1,26 @@
use view::View; use view::View;
use views::IdView; use views::{IdView, RefCellView};
/// Makes a view wrappable in an [`IdView`]. /// Makes a view wrappable in an [`IdView`].
/// ///
/// [`IdView`]: ../views/struct.IdView.html /// [`IdView`]: ../views/struct.IdView.html
pub trait Identifiable: View + Sized { pub trait Identifiable: View + Sized {
/// Wraps this view into an IdView with the given id. /// Wraps this view into an `IdView` with the given id.
/// ///
/// This is just a shortcut for `IdView::new(id, self)` /// This is just a shortcut for `IdView::new(id, self)`
fn with_id(self, id: &str) -> IdView<Self> { fn with_id(self, id: &str) -> IdView<Self> {
IdView::new(id, self) IdView::new(id, self)
} }
/// Wraps this view into both a [`RefCellView`] and an `IdView`.
///
/// This allows to call [`Cursive::find_id_mut`].
///
/// [`RefCellView`]: ../views/struct.RefCellView.html
/// [`Cursive::find_id_mut`]: ../struct.Cursive.html#method.find_id_mut
fn with_id_mut(self, id: &str) -> IdView<RefCellView<Self>> {
RefCellView::new(self).with_id(id)
}
} }
/// Any `View` implements this trait. /// Any `View` implements this trait.