Add documentation to BoxView

This commit is contained in:
Alexandre Bury 2016-07-13 21:30:30 -07:00
parent e166869c51
commit 9390504290
3 changed files with 49 additions and 11 deletions

View File

@ -16,10 +16,7 @@ impl Orientation {
/// (`Horizontal` will return the x value, /// (`Horizontal` will return the x value,
/// and `Vertical` will return the y value.) /// and `Vertical` will return the y value.)
pub fn get(&self, v: &Vec2) -> usize { pub fn get(&self, v: &Vec2) -> usize {
match *self { *v.get(*self)
Orientation::Horizontal => v.x,
Orientation::Vertical => v.y,
}
} }
/// Returns the other orientation. /// Returns the other orientation.

View File

@ -4,9 +4,17 @@ use XY;
use vec::Vec2; use vec::Vec2;
use super::{View, ViewWrapper}; use super::{View, ViewWrapper};
/// `BoxView` is a wrapper around an other view, with a given minimum size. /// Wrapper around another view, with a fixed size.
/// ///
/// # Example /// Each axis can be enabled independantly.
///
/// * If both axis are fixed, the view always asks for this size.
/// * If both axis are left free, the wrapper has no effect and the underlying
/// view is directly queried.
/// * If only one axis is fixed, it will override the size request when
/// querying the wrapped view.
///
/// # Examples
/// ///
/// ``` /// ```
/// # use cursive::view::{BoxView,TextView}; /// # use cursive::view::{BoxView,TextView};
@ -19,7 +27,7 @@ pub struct BoxView<T: View> {
} }
impl<T: View> BoxView<T> { impl<T: View> BoxView<T> {
/// Wraps `view` in a neww `BoxView` with the given size. /// Wraps `view` in a new `BoxView` with the given size.
pub fn fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self { pub fn fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into(); let size = size.into();
@ -60,14 +68,23 @@ impl<T: View> ViewWrapper for BoxView<T> {
fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 { fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 {
if let (Some(w), Some(h)) = self.size.pair() { if let (Some(w), Some(h)) = self.size.pair() {
// If we know everything already, no need to ask
Vec2::new(w, h) Vec2::new(w, h)
} else { } else {
let req = Vec2::new(min(req.x, self.size.x), // If req < self.size in any axis, we're screwed.
min(req.y, self.size.y)); // TODO: handle insufficient space
// (should probably show an error message or a blank canvas)
// From now on, we assume req >= self.size.
// Override the request on the restricted axis
let req = req.zip_map(self.size, min);
// Back in my time, we didn't ask kids for their opinions!
let child_size = self.view.get_min_size(req); let child_size = self.view.get_min_size(req);
Vec2::new(self.size.x.unwrap_or(child_size.x), // This calls unwrap_or on each axis
self.size.y.unwrap_or(child_size.y)) self.size.unwrap_or(child_size)
} }
} }
} }

View File

@ -1,3 +1,5 @@
use orientation::Orientation;
use std::iter; use std::iter;
/// A generic structure with a value for each axis. /// A generic structure with a value for each axis.
@ -34,6 +36,28 @@ impl<T> XY<T> {
pub fn iter(&self) -> iter::Chain<iter::Once<&T>, iter::Once<&T>> { pub fn iter(&self) -> iter::Chain<iter::Once<&T>, iter::Once<&T>> {
iter::once(&self.x).chain(iter::once(&self.y)) iter::once(&self.x).chain(iter::once(&self.y))
} }
/// Returns a reference to the value on the given axis.
pub fn get(&self, o: Orientation) -> &T {
match o {
Orientation::Horizontal => &self.x,
Orientation::Vertical => &self.y,
}
}
/// Returns a new XY by calling `f` on `self` and `other` for each axis.
pub fn zip_map<U,V,F: Fn(T,U) -> V>(self, other: XY<U>, f: F) -> XY<V> {
XY::new(f(self.x, other.x),
f(self.y, other.y))
}
}
impl <T> XY<Option<T>> {
/// Returns a new XY by calling `unwrap_or` on each axis.
pub fn unwrap_or(self, other: XY<T>) -> XY<T> {
self.zip_map(other, |s, o| s.unwrap_or(o))
}
} }
impl<T: Copy> XY<T> { impl<T: Copy> XY<T> {