2016-07-13 04:01:11 +00:00
|
|
|
use XY;
|
2016-07-12 02:24:00 +00:00
|
|
|
use vec::Vec2;
|
2016-08-04 04:55:41 +00:00
|
|
|
use view::{SizeConstraint, View, ViewWrapper};
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2016-08-04 04:55:41 +00:00
|
|
|
/// Wrapper around another view, with a controlled size.
|
2016-07-12 03:26:33 +00:00
|
|
|
///
|
2016-08-04 04:55:41 +00:00
|
|
|
/// Each axis can independently be set to:
|
2016-07-14 04:30:30 +00:00
|
|
|
///
|
2016-08-04 04:55:41 +00:00
|
|
|
/// * Keep a **fixed** size
|
|
|
|
/// * Use **all** available size
|
|
|
|
/// * Use **at most** a given size
|
|
|
|
/// * Use **at least** a given size
|
|
|
|
/// * Let the wrapped view decide.
|
2016-07-14 04:30:30 +00:00
|
|
|
///
|
|
|
|
/// # 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.
|
2016-08-04 04:55:41 +00:00
|
|
|
/// let view = BoxView::with_fixed_size((20,4), TextView::new("Hello!"));
|
2016-07-12 03:26:33 +00:00
|
|
|
/// ```
|
2015-06-08 03:58:10 +00:00
|
|
|
pub struct BoxView<T: View> {
|
2016-08-04 04:55:41 +00:00
|
|
|
/// Constraint on each axis
|
|
|
|
size: XY<SizeConstraint>,
|
|
|
|
|
|
|
|
/// `true` if the view can be squished.
|
|
|
|
///
|
|
|
|
/// This means if the required size is less than the computed size,
|
|
|
|
/// consider returning a smaller size.
|
|
|
|
/// For instance, try to return the child's desires size.
|
|
|
|
squishable: bool,
|
|
|
|
|
|
|
|
/// The actual view we're wrapping.
|
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-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.
|
2017-10-12 23:38:55 +00:00
|
|
|
pub fn new(
|
2018-01-22 22:37:27 +00:00
|
|
|
width: SizeConstraint, height: SizeConstraint, view: T
|
2017-10-12 23:38:55 +00:00
|
|
|
) -> Self {
|
2015-05-15 00:41:17 +00:00
|
|
|
BoxView {
|
2016-07-13 04:01:11 +00:00
|
|
|
size: (width, height).into(),
|
2016-08-04 04:55:41 +00:00
|
|
|
squishable: false,
|
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-08-04 04:55:41 +00:00
|
|
|
/// Sets `self` to be squishable.
|
|
|
|
///
|
|
|
|
/// A squishable `BoxView` will take a smaller size than it should when
|
|
|
|
/// the available space is too small. In that case, it will allow the
|
|
|
|
/// child view to contract, if it can.
|
|
|
|
///
|
|
|
|
/// More specifically, if the available space is less than the size we
|
|
|
|
/// would normally ask for, return the child size.
|
|
|
|
pub fn squishable(mut self) -> Self {
|
|
|
|
self.squishable = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a new `BoxView` with the given size.
|
|
|
|
pub fn with_fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
|
|
|
let size = size.into();
|
|
|
|
|
2017-10-12 23:38:55 +00:00
|
|
|
BoxView::new(
|
|
|
|
SizeConstraint::Fixed(size.x),
|
|
|
|
SizeConstraint::Fixed(size.y),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps `view` in a new `BoxView` with fixed width.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_fixed_width(width: usize, view: T) -> Self {
|
|
|
|
BoxView::new(SizeConstraint::Fixed(width), SizeConstraint::Free, view)
|
2016-07-12 02:24:00 +00:00
|
|
|
}
|
2016-07-12 03:26:33 +00:00
|
|
|
|
|
|
|
/// Wraps `view` in a new `BoxView` with fixed height.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_fixed_height(height: usize, view: T) -> Self {
|
|
|
|
BoxView::new(SizeConstraint::Free, SizeConstraint::Fixed(height), view)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will take all available space.
|
|
|
|
pub fn with_full_screen(view: T) -> Self {
|
|
|
|
BoxView::new(SizeConstraint::Full, SizeConstraint::Full, view)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will take all available width.
|
|
|
|
pub fn with_full_width(view: T) -> Self {
|
|
|
|
BoxView::new(SizeConstraint::Full, SizeConstraint::Free, view)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will take all available height.
|
|
|
|
pub fn with_full_height(view: T) -> Self {
|
|
|
|
BoxView::new(SizeConstraint::Free, SizeConstraint::Full, view)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will never be bigger than `size`.
|
|
|
|
pub fn with_max_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
|
|
|
let size = size.into();
|
|
|
|
|
2017-10-12 23:38:55 +00:00
|
|
|
BoxView::new(
|
|
|
|
SizeConstraint::AtMost(size.x),
|
|
|
|
SizeConstraint::AtMost(size.y),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will enforce a maximum width.
|
|
|
|
///
|
|
|
|
/// The resulting width will never be more than `max_width`.
|
|
|
|
pub fn with_max_width(max_width: usize, view: T) -> Self {
|
2017-10-12 23:38:55 +00:00
|
|
|
BoxView::new(
|
|
|
|
SizeConstraint::AtMost(max_width),
|
|
|
|
SizeConstraint::Free,
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will enforce a maximum height.
|
|
|
|
///
|
|
|
|
/// The resulting height will never be more than `max_height`.
|
|
|
|
pub fn with_max_height(max_height: usize, view: T) -> Self {
|
2017-10-12 23:38:55 +00:00
|
|
|
BoxView::new(
|
|
|
|
SizeConstraint::Free,
|
|
|
|
SizeConstraint::AtMost(max_height),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will never be smaller than `size`.
|
|
|
|
pub fn with_min_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
|
|
|
let size = size.into();
|
|
|
|
|
2017-10-12 23:38:55 +00:00
|
|
|
BoxView::new(
|
|
|
|
SizeConstraint::AtLeast(size.x),
|
|
|
|
SizeConstraint::AtLeast(size.y),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps `view` in a `BoxView` which will enforce a minimum width.
|
|
|
|
///
|
|
|
|
/// The resulting width will never be less than `min_width`.
|
|
|
|
pub fn with_min_width(min_width: usize, view: T) -> Self {
|
2017-10-12 23:38:55 +00:00
|
|
|
BoxView::new(
|
|
|
|
SizeConstraint::AtLeast(min_width),
|
|
|
|
SizeConstraint::Free,
|
|
|
|
view,
|
|
|
|
)
|
2016-07-12 03:26:33 +00:00
|
|
|
}
|
2016-07-12 02:24:00 +00:00
|
|
|
|
2016-08-04 04:55:41 +00:00
|
|
|
/// Wraps `view` in a `BoxView` which will enforce a minimum height.
|
|
|
|
///
|
|
|
|
/// The resulting height will never be less than `min_height`.
|
|
|
|
pub fn with_min_height(min_height: usize, view: T) -> Self {
|
2017-10-12 23:38:55 +00:00
|
|
|
BoxView::new(
|
|
|
|
SizeConstraint::Free,
|
|
|
|
SizeConstraint::AtLeast(min_height),
|
|
|
|
view,
|
|
|
|
)
|
2016-07-12 02:24:00 +00:00
|
|
|
}
|
2018-01-26 10:39:10 +00:00
|
|
|
|
2018-01-27 09:37:08 +00:00
|
|
|
inner_getters!(self.view: T);
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: View> ViewWrapper for BoxView<T> {
|
2016-09-20 00:11:00 +00:00
|
|
|
wrap_impl!(self.view: T);
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
2016-08-04 04:55:41 +00:00
|
|
|
let req = self.size.zip_map(req, SizeConstraint::available);
|
2017-01-24 06:52:29 +00:00
|
|
|
let child_size = self.view.required_size(req);
|
2017-07-17 16:55:48 +00:00
|
|
|
|
2016-08-04 04:55:41 +00:00
|
|
|
let result = self.size
|
|
|
|
.zip_map(child_size.zip(req), SizeConstraint::result);
|
2016-07-14 04:30:30 +00:00
|
|
|
|
2017-08-23 14:49:09 +00:00
|
|
|
debug!("{:?}", result);
|
2017-07-17 16:55:48 +00:00
|
|
|
|
2016-08-04 04:55:41 +00:00
|
|
|
if self.squishable {
|
|
|
|
// We respect the request if we're less or equal.
|
|
|
|
let respect_req = result.zip_map(req, |res, req| res <= req);
|
2017-10-12 23:38:55 +00:00
|
|
|
result.zip_map(
|
|
|
|
respect_req.zip(child_size),
|
|
|
|
|res, (respect, child)| {
|
|
|
|
if respect {
|
|
|
|
// If we respect the request, keep the result
|
|
|
|
res
|
|
|
|
} else {
|
|
|
|
// Otherwise, take the child as squish attempt.
|
|
|
|
child
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
} else {
|
|
|
|
result
|
2016-07-14 03:52:24 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 18:48:17 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use vec::Vec2;
|
2016-09-23 05:10:14 +00:00
|
|
|
use view::{Boxable, View};
|
2016-09-20 18:48:17 +00:00
|
|
|
use views::DummyView;
|
|
|
|
|
|
|
|
// No need to test `draw()` method as it's directly forwarded.
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn min_size() {
|
|
|
|
let mut min_w = DummyView.full_screen().min_width(5);
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
assert_eq!(Vec2::new(5, 1), min_w.required_size(Vec2::new(1, 1)));
|
|
|
|
assert_eq!(Vec2::new(5, 10), min_w.required_size(Vec2::new(1, 10)));
|
|
|
|
assert_eq!(Vec2::new(10, 1), min_w.required_size(Vec2::new(10, 1)));
|
|
|
|
assert_eq!(Vec2::new(10, 10), min_w.required_size(Vec2::new(10, 10)));
|
2016-09-20 18:48:17 +00:00
|
|
|
|
|
|
|
let mut min_h = DummyView.full_screen().min_height(5);
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
assert_eq!(Vec2::new(1, 5), min_h.required_size(Vec2::new(1, 1)));
|
|
|
|
assert_eq!(Vec2::new(1, 10), min_h.required_size(Vec2::new(1, 10)));
|
|
|
|
assert_eq!(Vec2::new(10, 5), min_h.required_size(Vec2::new(10, 1)));
|
|
|
|
assert_eq!(Vec2::new(10, 10), min_h.required_size(Vec2::new(10, 10)));
|
2016-09-20 18:48:17 +00:00
|
|
|
|
|
|
|
let mut min_s = DummyView.full_screen().min_size((5, 5));
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
assert_eq!(Vec2::new(5, 5), min_s.required_size(Vec2::new(1, 1)));
|
|
|
|
assert_eq!(Vec2::new(5, 10), min_s.required_size(Vec2::new(1, 10)));
|
|
|
|
assert_eq!(Vec2::new(10, 5), min_s.required_size(Vec2::new(10, 1)));
|
|
|
|
assert_eq!(Vec2::new(10, 10), min_s.required_size(Vec2::new(10, 10)));
|
2016-09-20 18:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn max_size() {
|
|
|
|
let mut max_w = DummyView.full_screen().max_width(5);
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
assert_eq!(Vec2::new(1, 1), max_w.required_size(Vec2::new(1, 1)));
|
|
|
|
assert_eq!(Vec2::new(1, 10), max_w.required_size(Vec2::new(1, 10)));
|
|
|
|
assert_eq!(Vec2::new(5, 1), max_w.required_size(Vec2::new(10, 1)));
|
|
|
|
assert_eq!(Vec2::new(5, 10), max_w.required_size(Vec2::new(10, 10)));
|
2016-09-20 18:48:17 +00:00
|
|
|
|
|
|
|
let mut max_h = DummyView.full_screen().max_height(5);
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
assert_eq!(Vec2::new(1, 1), max_h.required_size(Vec2::new(1, 1)));
|
|
|
|
assert_eq!(Vec2::new(1, 5), max_h.required_size(Vec2::new(1, 10)));
|
|
|
|
assert_eq!(Vec2::new(10, 1), max_h.required_size(Vec2::new(10, 1)));
|
|
|
|
assert_eq!(Vec2::new(10, 5), max_h.required_size(Vec2::new(10, 10)));
|
2016-09-20 18:48:17 +00:00
|
|
|
|
|
|
|
let mut max_s = DummyView.full_screen().max_size((5, 5));
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
assert_eq!(Vec2::new(1, 1), max_s.required_size(Vec2::new(1, 1)));
|
|
|
|
assert_eq!(Vec2::new(1, 5), max_s.required_size(Vec2::new(1, 10)));
|
|
|
|
assert_eq!(Vec2::new(5, 1), max_s.required_size(Vec2::new(10, 1)));
|
|
|
|
assert_eq!(Vec2::new(5, 5), max_s.required_size(Vec2::new(10, 10)));
|
2016-09-20 18:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn full_screen() {
|
|
|
|
let mut full = DummyView.full_screen();
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
assert_eq!(Vec2::new(1, 1), full.required_size(Vec2::new(1, 1)));
|
|
|
|
assert_eq!(Vec2::new(1, 10), full.required_size(Vec2::new(1, 10)));
|
|
|
|
assert_eq!(Vec2::new(10, 1), full.required_size(Vec2::new(10, 1)));
|
|
|
|
assert_eq!(Vec2::new(10, 10), full.required_size(Vec2::new(10, 10)));
|
2016-09-20 18:48:17 +00:00
|
|
|
}
|
2018-01-25 12:02:07 +00:00
|
|
|
|
|
|
|
#[test]
|
2018-01-26 10:39:10 +00:00
|
|
|
fn test_get_inner() {
|
|
|
|
use views::TextView;
|
|
|
|
|
|
|
|
let parent = TextView::new("abc").full_screen();
|
|
|
|
let child = parent.get_inner();
|
|
|
|
assert_eq!(child.get_content().source(), "abc");
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_get_inner_mut() {
|
|
|
|
use views::TextView;
|
|
|
|
|
|
|
|
let mut parent = TextView::new("").full_screen();
|
|
|
|
let new_value = "new";
|
|
|
|
let child = parent.get_inner_mut();
|
|
|
|
|
|
|
|
child.set_content(new_value);
|
|
|
|
|
|
|
|
assert_eq!(child.get_content().source(), new_value);
|
2018-01-25 12:02:07 +00:00
|
|
|
}
|
2016-09-20 18:48:17 +00:00
|
|
|
}
|