2017-10-12 23:38:55 +00:00
|
|
|
use owning_ref::{OwningHandle, RcRef};
|
2017-03-27 03:50:50 +00:00
|
|
|
use std::any::Any;
|
2017-03-26 04:56:31 +00:00
|
|
|
use std::cell::{RefCell, RefMut};
|
2017-12-12 02:54:40 +00:00
|
|
|
use std::ops::DerefMut;
|
2017-03-26 04:56:31 +00:00
|
|
|
use std::rc::Rc;
|
2016-06-28 05:10:59 +00:00
|
|
|
use view::{Selector, View, ViewWrapper};
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2017-03-26 04:56:31 +00:00
|
|
|
/// Wrapper around a view to provide interior mutability.
|
|
|
|
pub struct IdView<V: View> {
|
|
|
|
view: Rc<RefCell<V>>,
|
2015-05-23 17:33:29 +00:00
|
|
|
id: String,
|
|
|
|
}
|
|
|
|
|
2017-03-26 04:56:31 +00:00
|
|
|
/// Mutable reference to a view.
|
2017-03-27 03:50:50 +00:00
|
|
|
///
|
|
|
|
/// This behaves like a [`RefMut`], but without being tied to a lifetime.
|
|
|
|
///
|
|
|
|
/// [`RefMut`]: https://doc.rust-lang.org/std/cell/struct.RefMut.html
|
2017-03-26 04:56:31 +00:00
|
|
|
pub type ViewRef<V> = OwningHandle<RcRef<RefCell<V>>, RefMut<'static, V>>;
|
|
|
|
|
|
|
|
impl<V: View> IdView<V> {
|
|
|
|
/// Wraps `view` in a new `IdView`.
|
|
|
|
pub fn new<S: Into<String>>(id: S, view: V) -> Self {
|
2015-05-23 17:33:29 +00:00
|
|
|
IdView {
|
2017-03-26 04:56:31 +00:00
|
|
|
view: Rc::new(RefCell::new(view)),
|
|
|
|
id: id.into(),
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-26 04:56:31 +00:00
|
|
|
|
|
|
|
/// Gets mutable access to the inner view.
|
2017-03-27 03:50:50 +00:00
|
|
|
///
|
|
|
|
/// This returns a `ViewRef<V>`, which implement `DerefMut<Target = V>`.
|
2017-07-03 08:27:55 +00:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if another reference for this view already exists.
|
2017-03-26 04:56:31 +00:00
|
|
|
pub fn get_mut(&mut self) -> ViewRef<V> {
|
2017-10-13 18:22:02 +00:00
|
|
|
let cell_ref = RcRef::new(Rc::clone(&self.view));
|
2017-03-26 04:56:31 +00:00
|
|
|
|
2017-07-07 17:49:51 +00:00
|
|
|
OwningHandle::new_mut(cell_ref)
|
2017-03-26 04:56:31 +00:00
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 22:40:33 +00:00
|
|
|
// Shortcut for a boxed callback (for the wrap_call_on_any method).
|
|
|
|
type BoxedCallback<'a> = Box<for<'b> FnMut(&'b mut Any) + 'a>;
|
|
|
|
|
2017-03-26 04:56:31 +00:00
|
|
|
impl<T: View + 'static> ViewWrapper for IdView<T> {
|
|
|
|
type V = T;
|
|
|
|
|
|
|
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
2017-07-03 08:27:55 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&Self::V) -> R,
|
2017-03-26 04:56:31 +00:00
|
|
|
{
|
2017-07-03 08:27:55 +00:00
|
|
|
self.view.try_borrow().ok().map(|v| f(&*v))
|
2017-03-26 04:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
|
2017-07-03 08:27:55 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self::V) -> R,
|
2017-03-26 04:56:31 +00:00
|
|
|
{
|
2017-07-03 08:27:55 +00:00
|
|
|
self.view.try_borrow_mut().ok().map(|mut v| f(&mut *v))
|
2017-03-26 04:56:31 +00:00
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2017-12-12 01:31:55 +00:00
|
|
|
fn into_inner(mut self) -> Result<Self::V, Self>
|
|
|
|
where
|
|
|
|
Self::V: Sized,
|
|
|
|
{
|
|
|
|
match Rc::try_unwrap(self.view) {
|
|
|
|
Err(rc) => {
|
|
|
|
// Whoops! Abort! Undo!
|
|
|
|
self.view = rc;
|
|
|
|
Err(self)
|
|
|
|
}
|
2017-12-12 02:54:40 +00:00
|
|
|
Ok(cell) => Ok(cell.into_inner()),
|
2017-12-12 01:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:22:02 +00:00
|
|
|
// Some for<'b> weirdness here to please the borrow checker gods...
|
2017-07-03 08:27:55 +00:00
|
|
|
fn wrap_call_on_any<'a>(
|
2017-12-12 02:54:40 +00:00
|
|
|
&mut self, selector: &Selector, mut callback: BoxedCallback<'a>
|
2017-07-03 08:27:55 +00:00
|
|
|
) {
|
2017-03-27 20:27:50 +00:00
|
|
|
match selector {
|
2017-03-26 04:56:31 +00:00
|
|
|
&Selector::Id(id) if id == self.id => callback(self),
|
2017-07-03 08:27:55 +00:00
|
|
|
s => {
|
2017-10-12 23:38:55 +00:00
|
|
|
self.view
|
|
|
|
.try_borrow_mut()
|
|
|
|
.ok()
|
2017-12-12 02:54:40 +00:00
|
|
|
.map(|mut v| v.deref_mut().call_on_any(s, callback));
|
2017-07-03 08:27:55 +00:00
|
|
|
}
|
2017-03-27 20:27:50 +00:00
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
2017-03-25 21:50:52 +00:00
|
|
|
|
|
|
|
fn wrap_focus_view(&mut self, selector: &Selector) -> Result<(), ()> {
|
|
|
|
match selector {
|
|
|
|
&Selector::Id(id) if id == self.id => Ok(()),
|
2017-10-12 23:38:55 +00:00
|
|
|
s => self.view
|
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| ())
|
2017-12-12 02:54:40 +00:00
|
|
|
.and_then(|mut v| v.deref_mut().focus_view(s)),
|
2017-03-25 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|