Add View: Any

This commit is contained in:
Alexandre Bury 2017-12-11 18:54:40 -08:00
parent bbee77f1b7
commit 9b9619aa53
5 changed files with 10 additions and 13 deletions

View File

@ -66,7 +66,7 @@ use vec::Vec2;
use views::IdView; use views::IdView;
/// Main trait defining a view behaviour. /// Main trait defining a view behaviour.
pub trait View { pub trait View: Any {
/// Called when a key was pressed. /// Called when a key was pressed.
/// ///
/// Default implementation just ignores it. /// Default implementation just ignores it.

View File

@ -14,7 +14,7 @@ use view::{Selector, View};
/// `with_view_mut` for you. /// `with_view_mut` for you.
/// ///
/// [`wrap_impl!`]: ../macro.wrap_impl.html /// [`wrap_impl!`]: ../macro.wrap_impl.html
pub trait ViewWrapper { pub trait ViewWrapper: 'static {
/// Type that this view wraps. /// Type that this view wraps.
type V: View + ?Sized; type V: View + ?Sized;
@ -96,7 +96,7 @@ pub trait ViewWrapper {
// Some types easily implement ViewWrapper. // Some types easily implement ViewWrapper.
// This includes Box<T: View> // This includes Box<T: View>
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
impl<U: View + ?Sized, T: Deref<Target = U> + DerefMut> ViewWrapper for T { impl<U: View + ?Sized, T: Deref<Target = U> + DerefMut + 'static> ViewWrapper for T {
type V = U; type V = U;
fn with_view<F, R>(&self, f: F) -> Option<R> fn with_view<F, R>(&self, f: F) -> Option<R>

View File

@ -173,7 +173,7 @@ impl<T> Canvas<T> {
} }
} }
impl<T> View for Canvas<T> { impl<T: 'static> View for Canvas<T> {
fn draw(&self, printer: &Printer) { fn draw(&self, printer: &Printer) {
(self.draw)(&self.state, printer); (self.draw)(&self.state, printer);
} }

View File

@ -1,6 +1,7 @@
use owning_ref::{OwningHandle, RcRef}; use owning_ref::{OwningHandle, RcRef};
use std::any::Any; use std::any::Any;
use std::cell::{RefCell, RefMut}; use std::cell::{RefCell, RefMut};
use std::ops::DerefMut;
use std::rc::Rc; use std::rc::Rc;
use view::{Selector, View, ViewWrapper}; use view::{Selector, View, ViewWrapper};
@ -69,18 +70,14 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
// Whoops! Abort! Undo! // Whoops! Abort! Undo!
self.view = rc; self.view = rc;
Err(self) Err(self)
},
Ok(cell) => {
Ok(cell.into_inner())
} }
Ok(cell) => Ok(cell.into_inner()),
} }
} }
// Some for<'b> weirdness here to please the borrow checker gods... // Some for<'b> weirdness here to please the borrow checker gods...
fn wrap_call_on_any<'a>( fn wrap_call_on_any<'a>(
&mut self, &mut self, selector: &Selector, mut callback: BoxedCallback<'a>
selector: &Selector,
mut callback: BoxedCallback<'a>,
) { ) {
match selector { match selector {
&Selector::Id(id) if id == self.id => callback(self), &Selector::Id(id) if id == self.id => callback(self),
@ -88,7 +85,7 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
self.view self.view
.try_borrow_mut() .try_borrow_mut()
.ok() .ok()
.map(|mut v| v.call_on_any(s, callback)); .map(|mut v| v.deref_mut().call_on_any(s, callback));
} }
} }
} }
@ -99,7 +96,7 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
s => self.view s => self.view
.try_borrow_mut() .try_borrow_mut()
.map_err(|_| ()) .map_err(|_| ())
.and_then(|mut v| v.focus_view(s)), .and_then(|mut v| v.deref_mut().focus_view(s)),
} }
} }
} }

View File

@ -154,7 +154,7 @@ impl<T> RadioButton<T> {
} }
} }
impl<T> View for RadioButton<T> { impl<T: 'static> View for RadioButton<T> {
fn required_size(&mut self, _: Vec2) -> Vec2 { fn required_size(&mut self, _: Vec2) -> Vec2 {
self.req_size() self.req_size()
} }