cursive/src/views/id_view.rs

75 lines
2.1 KiB
Rust
Raw Normal View History

use owning_ref::{RcRef, OwningHandle};
2015-05-23 17:33:29 +00:00
use std::cell::{RefCell, RefMut};
use std::any::Any;
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
/// 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,
}
/// Mutable reference to a view.
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 {
view: Rc::new(RefCell::new(view)),
id: id.into(),
2015-05-23 17:33:29 +00:00
}
}
/// Gets mutable access to the inner view.
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())
}
2015-05-23 17:33:29 +00:00
}
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
{
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
{
self.view
.try_borrow_mut()
.ok()
.map(|mut v| f(&mut *v))
}
2015-05-23 17:33:29 +00:00
fn wrap_find_any<'a>(&mut self, selector: &Selector,
mut callback: Box<for<'b> FnMut(&'b mut Any) + 'a>) {
let result = match selector {
&Selector::Id(id) if id == self.id => callback(self),
s => self.view.borrow_mut().find_any(s, callback),
};
result
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(()),
s => self.view.borrow_mut().focus_view(s),
2017-03-25 21:50:52 +00:00
}
}
2015-05-23 17:33:29 +00:00
}