feat: find_id can now find views declared with with_id_mut

This commit is contained in:
Alexandre Bury 2017-02-26 15:52:42 -08:00
parent b50d2f077f
commit cb3adc5baf
2 changed files with 20 additions and 5 deletions

View File

@ -65,9 +65,10 @@ use Printer;
use direction::Direction;
use event::{Event, EventResult};
use std::any::Any;
use vec::Vec2;
use views::RefCellView;
use std::any::Any;
/// Main trait defining a view behaviour.
pub trait View {
@ -122,8 +123,7 @@ pub trait View {
/// Returns None if the path doesn't lead to a view.
///
/// Default implementation always return `None`.
fn find_any<'a>(&mut self, _: &Selector,
_: Box<FnMut(&mut Any) + 'a>) {
fn find_any<'a>(&mut self, _: &Selector, _: Box<FnMut(&mut Any) + 'a>) {
// TODO: FnMut -> FnOnce once it works
}
@ -176,8 +176,11 @@ impl<T: View> Finder for T {
let mut callback = Some(callback);
let callback = |v: &mut Any| if let Some(callback) =
callback.take() {
if let Some(v) = v.downcast_mut::<V>() {
*result_ref = Some(callback(v));
if v.is::<V>() {
*result_ref = v.downcast_mut::<V>().map(|v| callback(v));
} else if v.is::<RefCellView<V>>() {
*result_ref = v.downcast_mut::<RefCellView<V>>()
.and_then(|v| v.with_view_mut(callback));
}
};
self.find_any(sel, Box::new(callback));

View File

@ -53,6 +53,11 @@ impl TextView {
}
}
/// Creates a new empty `TextView`.
pub fn empty() -> Self {
TextView::new("")
}
/// Enable or disable the view's scrolling capabilities.
///
/// When disabled, the view will never attempt to scroll
@ -98,6 +103,13 @@ impl TextView {
self
}
/// Replace the text in this view.
///
/// Chainable variant.
pub fn content<S: Into<String>>(self, content: S) -> Self {
self.with(|s| s.set_content(content))
}
/// Replace the text in this view.
pub fn set_content<S: Into<String>>(&mut self, content: S) {
let content = content.into();