IdView: skip view if borrowing fails.

Fixes #144
This commit is contained in:
Alexandre Bury 2017-07-03 01:27:55 -07:00
parent fb18ed8f99
commit 66af7fab7c
2 changed files with 36 additions and 22 deletions

View File

@ -114,15 +114,15 @@ pub trait View {
/// Draws the view with the given printer (includes bounds) and focus.
fn draw(&self, printer: &Printer);
/// Finds the view identified by the given selector.
/// Runs a closure on the view identified by the given selector.
///
/// See [`Finder::call_on`] for a nicer interface, implemented for all views.
///
/// [`Finder::call_on`]: trait.Finder.html#method.call_on
///
/// Returns None if the path doesn't lead to a view.
/// If the selector doesn't find a match, the closure will not be run.
///
/// Default implementation always return `None`.
/// Default implementation is a no-op.
fn call_on_any<'a>(&mut self, _: &Selector, _: Box<FnMut(&mut Any) + 'a>) {
// TODO: FnMut -> FnOnce once it works
}
@ -130,6 +130,8 @@ pub trait View {
/// Moves the focus to the view identified by the given selector.
///
/// Returns `Ok(())` if the view was found and selected.
///
/// Default implementation simply returns `Err(())`.
fn focus_view(&mut self, &Selector) -> Result<(), ()> {
Err(())
}

View File

@ -30,14 +30,18 @@ impl<V: View> IdView<V> {
/// Gets mutable access to the inner view.
///
/// This returns a `ViewRef<V>`, which implement `DerefMut<Target = V>`.
///
/// # Panics
///
/// Panics if another reference for this view already exists.
pub fn get_mut(&mut self) -> ViewRef<V> {
// TODO: return a standalone item (not tied to our lifetime)
// that bundles `self.view.clone()` and allow mutable reference to
// the inner view.
let cell_ref = RcRef::new(self.view.clone());
OwningHandle::new(cell_ref,
|x| unsafe { x.as_ref() }.unwrap().borrow_mut())
// The unsafe part here is tied to OwningHandle's limitation.
OwningHandle::new(
cell_ref,
|x| unsafe { x.as_ref() }.unwrap().borrow_mut(),
)
}
}
@ -45,35 +49,43 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
type V = T;
fn with_view<F, R>(&self, f: F) -> Option<R>
where F: FnOnce(&Self::V) -> R
where
F: FnOnce(&Self::V) -> R,
{
self.view
.try_borrow()
.ok()
.map(|v| f(&*v))
self.view.try_borrow().ok().map(|v| f(&*v))
}
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce(&mut Self::V) -> R
where
F: FnOnce(&mut Self::V) -> R,
{
self.view
.try_borrow_mut()
.ok()
.map(|mut v| f(&mut *v))
self.view.try_borrow_mut().ok().map(|mut v| f(&mut *v))
}
fn wrap_call_on_any<'a>(&mut self, selector: &Selector,
mut callback: Box<for<'b> FnMut(&'b mut Any) + 'a>) {
fn wrap_call_on_any<'a>(
&mut self, selector: &Selector,
mut callback: Box<for<'b> FnMut(&'b mut Any) + 'a>
) {
match selector {
&Selector::Id(id) if id == self.id => callback(self),
s => self.view.borrow_mut().call_on_any(s, callback),
s => {
self.view.try_borrow_mut().ok().map(|mut v| {
v.call_on_any(s, callback)
});
}
}
}
fn wrap_focus_view(&mut self, selector: &Selector) -> Result<(), ()> {
match selector {
&Selector::Id(id) if id == self.id => Ok(()),
s => self.view.borrow_mut().focus_view(s),
s => {
self.view.try_borrow_mut().map_err(|_| ()).and_then(
|mut v| {
v.focus_view(s)
},
)
}
}
}
}