use std::cmp;
use XY;
use vec::Vec2;
use super::{View, ViewWrapper};
pub struct BoxView<T: View> {
size: XY<Option<usize>>,
view: T,
}
impl<T: View> BoxView<T> {
pub fn fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into();
BoxView::new(Some(size.x), Some(size.y), view)
}
pub fn new(width: Option<usize>, height: Option<usize>, view: T) -> Self {
BoxView {
size: (width, height).into(),
view: view,
}
}
pub fn fixed_width(width: usize, view: T) -> Self {
BoxView::new(Some(width), None, view)
}
pub fn fixed_height(height: usize, view: T) -> Self {
BoxView::new(None, Some(height), view)
}
}
fn min<T: Ord>(a: T, b: Option<T>) -> T {
match b {
Some(b) => cmp::min(a, b),
None => a,
}
}
impl<T: View> ViewWrapper for BoxView<T> {
wrap_impl!(&self.view);
fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 {
if let (Some(w), Some(h)) = self.size.pair() {
Vec2::new(w, h)
} else {
let req = req.zip_map(self.size, min);
let child_size = self.view.get_min_size(req);
self.size.unwrap_or(child_size)
}
}
}