From 1b8d109e9478b6f98bceeaea2a0923dba4e95932 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 8 Feb 2017 15:20:41 -0800 Subject: [PATCH] Add `with_id_mut` and `find_id_mut` convenient methods. --- src/lib.rs | 12 ++++++++++++ src/view/identifiable.rs | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cda5d79..bd0d636 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -427,6 +427,18 @@ impl Cursive { self.find(&view::Selector::Id(id), callback) } + /// Convenient method to find a view wrapped in [`RefCellView`]. + /// + /// This looks for a `RefCellView` with the given ID, and return + /// a mutable reference to the wrapped view. + /// + /// [`RefCellView`]: views/struct.RefCellView.html + pub fn find_id_mut(&mut self, id: &str) -> Option> + where V: View + Any + { + self.find_id(id, views::RefCellView::::get_mut) + } + /// Adds a global callback. /// /// Will be triggered on the given key press when no view catches it. diff --git a/src/view/identifiable.rs b/src/view/identifiable.rs index 162ab15..90fdbb8 100644 --- a/src/view/identifiable.rs +++ b/src/view/identifiable.rs @@ -1,16 +1,26 @@ use view::View; -use views::IdView; +use views::{IdView, RefCellView}; /// Makes a view wrappable in an [`IdView`]. /// /// [`IdView`]: ../views/struct.IdView.html 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)` fn with_id(self, id: &str) -> IdView { 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::new(self).with_id(id) + } } /// Any `View` implements this trait.