Rename View::find -> View::find_any

And add the `Finder` trait on Views to provide a true `find`.
This commit is contained in:
Alexandre Bury 2016-09-21 11:33:44 -07:00
parent 7d16f70d67
commit 93980a7d0c
8 changed files with 36 additions and 36 deletions

View File

@ -100,6 +100,7 @@ pub use with::With;
pub use printer::Printer; pub use printer::Printer;
use backend::{Backend, NcursesBackend}; use backend::{Backend, NcursesBackend};
use view::Finder;
use std::sync::mpsc; use std::sync::mpsc;
use std::any::Any; use std::any::Any;
@ -320,11 +321,6 @@ impl Cursive {
self.active_screen = screen_id; self.active_screen = screen_id;
} }
fn find_any(&mut self, selector: &view::Selector) -> Option<&mut Any> {
// Internal find method that returns a Any object.
self.screen_mut().find(selector)
}
/// Tries to find the view pointed to by the given path. /// Tries to find the view pointed to by the given path.
/// ///
/// If the view is not found, or if it is not of the asked type, /// If the view is not found, or if it is not of the asked type,
@ -348,7 +344,7 @@ impl Cursive {
/// # } /// # }
/// ``` /// ```
pub fn find<V: View + Any>(&mut self, sel: &view::Selector) -> Option<&mut V> { pub fn find<V: View + Any>(&mut self, sel: &view::Selector) -> Option<&mut V> {
self.find_any(sel).and_then(|b| b.downcast_mut::<V>()) self.screen_mut().find(sel)
} }
/// Convenient method to use `find` with a `view::Selector::Id`. /// Convenient method to use `find` with a `view::Selector::Id`.

View File

@ -118,7 +118,7 @@ pub trait View {
/// Returns None if the path doesn't lead to a view. /// Returns None if the path doesn't lead to a view.
/// ///
/// Default implementation always return `None`. /// Default implementation always return `None`.
fn find(&mut self, &Selector) -> Option<&mut Any> { fn find_any(&mut self, &Selector) -> Option<&mut Any> {
None None
} }
@ -134,6 +134,19 @@ pub trait View {
} }
} }
/// Provides `find<V: View>` to views.
pub trait Finder {
/// Find a view
fn find<V: View + Any>(&mut self, sel: &Selector) -> Option<&mut V>;
}
impl <T: View> Finder for T {
fn find<V: View + Any>(&mut self, sel: &Selector) -> Option<&mut V> {
self.find_any(sel).and_then(|b| b.downcast_mut::<V>())
}
}
/// Selects a single view (if any) in the tree. /// Selects a single view (if any) in the tree.
pub enum Selector<'a> { pub enum Selector<'a> {
/// Selects a view from its ID. /// Selects a view from its ID.

View File

@ -51,8 +51,8 @@ pub trait ViewWrapper {
} }
/// Wraps the `find` method. /// Wraps the `find` method.
fn wrap_find(&mut self, selector: &Selector) -> Option<&mut Any> { fn wrap_find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
self.get_view_mut().find(selector) self.get_view_mut().find_any(selector)
} }
/// Wraps the `needs_relayout` method. /// Wraps the `needs_relayout` method.
@ -82,8 +82,8 @@ impl<T: ViewWrapper> View for T {
self.wrap_take_focus(source) self.wrap_take_focus(source)
} }
fn find(&mut self, selector: &Selector) -> Option<&mut Any> { fn find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
self.wrap_find(selector) self.wrap_find_any(selector)
} }
fn needs_relayout(&self) -> bool { fn needs_relayout(&self) -> bool {

View File

@ -396,7 +396,7 @@ impl View for Dialog {
} }
} }
fn find(&mut self, selector: &Selector) -> Option<&mut Any> { fn find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
self.content.find(selector) self.content.find_any(selector)
} }
} }

View File

@ -21,10 +21,10 @@ impl<T: View> IdView<T> {
impl<T: View + Any> ViewWrapper for IdView<T> { impl<T: View + Any> ViewWrapper for IdView<T> {
wrap_impl!(self.view: T); wrap_impl!(self.view: T);
fn wrap_find(&mut self, selector: &Selector) -> Option<&mut Any> { fn wrap_find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
match selector { match selector {
&Selector::Id(id) if id == self.id => Some(&mut self.view), &Selector::Id(id) if id == self.id => Some(&mut self.view),
s => self.view.find(s), s => self.view.find_any(s),
} }
} }
} }

View File

@ -383,7 +383,10 @@ impl View for LinearLayout {
} }
} }
fn find(&mut self, selector: &Selector) -> Option<&mut Any> { fn find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
self.children.iter_mut().filter_map(|c| c.view.find(selector)).next() self.children
.iter_mut()
.filter_map(|c| c.view.find_any(selector))
.next()
} }
} }

View File

@ -28,16 +28,6 @@ impl Child {
_ => None, _ => None,
} }
} }
// Currently unused
//
// fn is_delimiter(&self) -> bool {
// match *self {
// Child::Row(_, _) => false,
// Child::Delimiter => true,
// }
// }
//
} }
/// Displays a scrollable list of elements. /// Displays a scrollable list of elements.
@ -270,11 +260,11 @@ impl View for ListView {
true true
} }
fn find(&mut self, selector: &Selector) -> Option<&mut Any> { fn find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
self.children self.children
.iter_mut() .iter_mut()
.filter_map(Child::view) .filter_map(Child::view)
.filter_map(|v| v.find(selector)) .filter_map(|v| v.find_any(selector))
.next() .next()
} }
} }

View File

@ -133,12 +133,10 @@ impl View for StackView {
} }
} }
fn find(&mut self, selector: &Selector) -> Option<&mut Any> { fn find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
for layer in &mut self.layers { self.layers
if let Some(any) = layer.view.find(selector) { .iter_mut()
return Some(any); .filter_map(|l| l.view.find_any(selector))
} .next()
}
None
} }
} }