2016-07-02 07:47:38 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2016-07-13 04:01:11 +00:00
|
|
|
use XY;
|
2016-07-12 02:24:00 +00:00
|
|
|
use vec::Vec2;
|
2016-07-28 23:36:01 +00:00
|
|
|
use view::{View, ViewWrapper};
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2016-07-14 04:30:30 +00:00
|
|
|
/// Wrapper around another view, with a fixed size.
|
2016-07-12 03:26:33 +00:00
|
|
|
///
|
2016-07-14 04:30:30 +00:00
|
|
|
/// 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
|
2016-07-12 03:26:33 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2016-07-28 23:36:01 +00:00
|
|
|
/// # use cursive::views::{BoxView,TextView};
|
2016-07-12 03:26:33 +00:00
|
|
|
/// // Creates a 20x4 BoxView with a TextView content.
|
|
|
|
/// let view = BoxView::fixed_size((20,4), TextView::new("Hello!"));
|
|
|
|
/// ```
|
2015-06-08 03:58:10 +00:00
|
|
|
pub struct BoxView<T: View> {
|
2016-07-13 04:01:11 +00:00
|
|
|
size: XY<Option<usize>>,
|
2015-06-08 03:58:10 +00:00
|
|
|
view: T,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: View> BoxView<T> {
|
2016-07-14 04:30:30 +00:00
|
|
|
/// Wraps `view` in a new `BoxView` with the given size.
|
2016-07-12 02:24:00 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Creates a new `BoxView` with the given width and height requirements.
|
|
|
|
///
|
|
|
|
/// `None` values will use the wrapped view's preferences.
|
2016-07-12 02:24:00 +00:00
|
|
|
pub fn new(width: Option<usize>, height: Option<usize>, view: T) -> Self {
|
2015-05-15 00:41:17 +00:00
|
|
|
BoxView {
|
2016-07-13 04:01:11 +00:00
|
|
|
size: (width, height).into(),
|
2015-06-08 03:58:10 +00:00
|
|
|
view: view,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 02:24:00 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps `view` in a new `BoxView` with fixed width.
|
2016-07-12 02:24:00 +00:00
|
|
|
pub fn fixed_width(width: usize, view: T) -> Self {
|
|
|
|
BoxView::new(Some(width), None, view)
|
|
|
|
}
|
2016-07-12 03:26:33 +00:00
|
|
|
|
|
|
|
/// Wraps `view` in a new `BoxView` with fixed height.
|
|
|
|
pub fn fixed_height(height: usize, view: T) -> Self {
|
|
|
|
BoxView::new(None, Some(height), view)
|
|
|
|
}
|
2016-07-12 02:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn min<T: Ord>(a: T, b: Option<T>) -> T {
|
|
|
|
match b {
|
|
|
|
Some(b) => cmp::min(a, b),
|
|
|
|
None => a,
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: View> ViewWrapper for BoxView<T> {
|
2015-06-08 03:58:10 +00:00
|
|
|
wrap_impl!(&self.view);
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2016-07-12 02:24:00 +00:00
|
|
|
fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 {
|
2015-06-08 19:23:36 +00:00
|
|
|
|
2016-07-13 04:01:11 +00:00
|
|
|
if let (Some(w), Some(h)) = self.size.pair() {
|
2016-07-14 04:30:30 +00:00
|
|
|
// If we know everything already, no need to ask
|
2016-07-12 02:24:00 +00:00
|
|
|
Vec2::new(w, h)
|
|
|
|
} else {
|
2016-07-14 04:30:30 +00:00
|
|
|
// 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!
|
2016-07-12 02:24:00 +00:00
|
|
|
let child_size = self.view.get_min_size(req);
|
2015-06-08 19:23:36 +00:00
|
|
|
|
2016-07-14 04:30:30 +00:00
|
|
|
// This calls unwrap_or on each axis
|
|
|
|
self.size.unwrap_or(child_size)
|
2016-07-14 03:52:24 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|