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

View File

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

View File

@ -34,6 +34,16 @@ pub trait ViewWrapper {
where
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.
fn wrap_draw(&self, printer: &Printer) {
self.with_view(|v| v.draw(printer));
@ -64,7 +74,9 @@ pub trait ViewWrapper {
/// Wraps the `find` method.
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));
}
@ -125,7 +137,9 @@ impl<T: ViewWrapper> View for T {
}
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)
}
@ -176,5 +190,9 @@ macro_rules! wrap_impl {
{
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))
}
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...
fn wrap_call_on_any<'a>(
&mut self, selector: &Selector, mut callback: BoxedCallback<'a>
&mut self,
selector: &Selector,
mut callback: BoxedCallback<'a>,
) {
match selector {
&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`].
///
/// [`StackView`]: struct.StackView.html
#[derive(Debug)]
pub struct Layer<T: View> {
view: T,
}

View File

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

View File

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