mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-10 03:10:41 +00:00
Add documentation to BoxView
This commit is contained in:
parent
e166869c51
commit
9390504290
@ -16,10 +16,7 @@ impl Orientation {
|
||||
/// (`Horizontal` will return the x value,
|
||||
/// and `Vertical` will return the y value.)
|
||||
pub fn get(&self, v: &Vec2) -> usize {
|
||||
match *self {
|
||||
Orientation::Horizontal => v.x,
|
||||
Orientation::Vertical => v.y,
|
||||
}
|
||||
*v.get(*self)
|
||||
}
|
||||
|
||||
/// Returns the other orientation.
|
||||
|
@ -4,9 +4,17 @@ use XY;
|
||||
use vec::Vec2;
|
||||
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};
|
||||
@ -19,7 +27,7 @@ pub struct BoxView<T: View> {
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
|
||||
if let (Some(w), Some(h)) = self.size.pair() {
|
||||
// If we know everything already, no need to ask
|
||||
Vec2::new(w, h)
|
||||
} else {
|
||||
let req = Vec2::new(min(req.x, self.size.x),
|
||||
min(req.y, self.size.y));
|
||||
// If req < self.size in any axis, we're screwed.
|
||||
// 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);
|
||||
|
||||
Vec2::new(self.size.x.unwrap_or(child_size.x),
|
||||
self.size.y.unwrap_or(child_size.y))
|
||||
// This calls unwrap_or on each axis
|
||||
self.size.unwrap_or(child_size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
24
src/xy.rs
24
src/xy.rs
@ -1,3 +1,5 @@
|
||||
use orientation::Orientation;
|
||||
|
||||
use std::iter;
|
||||
|
||||
/// 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>> {
|
||||
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> {
|
||||
|
Loading…
Reference in New Issue
Block a user