Add ViewWrapper::into_inner

And add some derive(Debug)
This commit is contained in:
Alexandre Bury 2017-12-11 17:31:55 -08:00
parent d0c30ded21
commit bbee77f1b7
7 changed files with 47 additions and 4 deletions

View File

@ -1,6 +1,7 @@
//! Tools to control view alignment. //! Tools to control view alignment.
/// Specifies the alignment along both horizontal and vertical directions. /// Specifies the alignment along both horizontal and vertical directions.
#[derive(Debug)]
pub struct Align { pub struct Align {
/// Horizontal alignment policy /// Horizontal alignment policy
pub h: HAlign, pub h: HAlign,
@ -41,6 +42,7 @@ impl Align {
} }
/// Horizontal alignment /// Horizontal alignment
#[derive(Debug)]
pub enum HAlign { pub enum HAlign {
/// Place the element to the left of available space /// Place the element to the left of available space
Left, Left,
@ -51,6 +53,7 @@ pub enum HAlign {
} }
/// Vertical alignment /// Vertical alignment
#[derive(Debug)]
pub enum VAlign { pub enum VAlign {
/// Place the element at the top of available space /// Place the element at the top of available space
Top, Top,

View File

@ -8,7 +8,7 @@ use vec::Vec2;
/// ///
/// You're not supposed to use this directly, /// You're not supposed to use this directly,
/// but it can be helpful if you create your own Views. /// but it can be helpful if you create your own Views.
#[derive(Default)] #[derive(Default, Debug)]
pub struct ScrollBase { pub struct ScrollBase {
/// First line visible /// First line visible
pub start_line: usize, pub start_line: usize,
@ -36,6 +36,7 @@ pub struct ScrollBase {
} }
/// Defines the scrolling behaviour on content or size change /// Defines the scrolling behaviour on content or size change
#[derive(Debug)]
pub enum ScrollStrategy { pub enum ScrollStrategy {
/// Keeps the same row number /// Keeps the same row number
KeepRow, KeepRow,

View File

@ -34,6 +34,16 @@ pub trait ViewWrapper {
where where
F: FnOnce(&mut Self::V) -> R; F: FnOnce(&mut Self::V) -> R;
/// Attempts to retrieve the inner view.
fn into_inner(self) -> Result<Self::V, Self>
where
Self: Sized,
Self::V: Sized,
{
Err(self)
}
/// Wraps the `draw` method. /// Wraps the `draw` method.
fn wrap_draw(&self, printer: &Printer) { fn wrap_draw(&self, printer: &Printer) {
self.with_view(|v| v.draw(printer)); self.with_view(|v| v.draw(printer));
@ -64,7 +74,9 @@ pub trait ViewWrapper {
/// Wraps the `find` method. /// Wraps the `find` method.
fn wrap_call_on_any<'a>( fn wrap_call_on_any<'a>(
&mut self, selector: &Selector, callback: Box<FnMut(&mut Any) + 'a> &mut self,
selector: &Selector,
callback: Box<FnMut(&mut Any) + 'a>,
) { ) {
self.with_view_mut(|v| v.call_on_any(selector, callback)); self.with_view_mut(|v| v.call_on_any(selector, callback));
} }
@ -125,7 +137,9 @@ impl<T: ViewWrapper> View for T {
} }
fn call_on_any<'a>( fn call_on_any<'a>(
&mut self, selector: &Selector, callback: Box<FnMut(&mut Any) + 'a> &mut self,
selector: &Selector,
callback: Box<FnMut(&mut Any) + 'a>,
) { ) {
self.wrap_call_on_any(selector, callback) self.wrap_call_on_any(selector, callback)
} }
@ -176,5 +190,9 @@ macro_rules! wrap_impl {
{ {
Some(f(&mut self.$v)) Some(f(&mut self.$v))
} }
fn into_inner(self) -> Result<Self::V, Self> where Self::V: Sized {
Ok(self.$v)
}
}; };
} }

View File

@ -60,9 +60,27 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
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 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)
},
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, selector: &Selector, mut callback: BoxedCallback<'a> &mut self,
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),

View File

@ -6,6 +6,7 @@ use view::{View, ViewWrapper};
/// Used as layer in the [`StackView`]. /// Used as layer in the [`StackView`].
/// ///
/// [`StackView`]: struct.StackView.html /// [`StackView`]: struct.StackView.html
#[derive(Debug)]
pub struct Layer<T: View> { pub struct Layer<T: View> {
view: T, view: T,
} }

View File

@ -4,6 +4,7 @@ use vec::Vec2;
use view::{View, ViewWrapper}; use view::{View, ViewWrapper};
/// Draws a border around a wrapped view. /// Draws a border around a wrapped view.
#[derive(Debug)]
pub struct Panel<V: View> { pub struct Panel<V: View> {
view: V, view: V,
} }

View File

@ -10,6 +10,7 @@ use vec::Vec2;
use view::{ScrollBase, ScrollStrategy, SizeCache, View}; use view::{ScrollBase, ScrollStrategy, SizeCache, View};
/// A simple view showing a fixed text /// A simple view showing a fixed text
#[derive(Debug)]
pub struct TextView { pub struct TextView {
content: String, content: String,
rows: Vec<Row>, rows: Vec<Row>,