2019-08-19 22:16:41 +00:00
|
|
|
use crate::printer::Printer;
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::view::{SizeConstraint, View, ViewWrapper};
|
2020-01-06 23:51:38 +00:00
|
|
|
use crate::Vec2;
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::XY;
|
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
|
|
|
///
|
|
|
|
/// ```
|
2020-01-06 19:51:50 +00:00
|
|
|
/// use cursive::views::{ResizedView, TextView};
|
2019-10-07 23:14:35 +00:00
|
|
|
///
|
2020-01-06 19:51:50 +00:00
|
|
|
/// // Creates a 20x4 ResizedView with a TextView content.
|
|
|
|
/// let view = ResizedView::with_fixed_size((20,4), TextView::new("Hello!"));
|
2016-07-12 03:26:33 +00:00
|
|
|
/// ```
|
2019-10-07 23:14:35 +00:00
|
|
|
///
|
|
|
|
/// See also [`Boxable`](crate::view::Boxable) for an easy way to wrap any view.
|
2020-01-06 19:51:50 +00:00
|
|
|
pub struct ResizedView<T: View> {
|
2016-08-04 04:55:41 +00:00
|
|
|
/// Constraint on each axis
|
|
|
|
size: XY<SizeConstraint>,
|
|
|
|
|
2018-08-20 20:30:42 +00:00
|
|
|
/// Set to `true` whenever we change some settings. Means we should re-layout just in case.
|
|
|
|
invalidated: bool,
|
2016-08-04 04:55:41 +00:00
|
|
|
|
|
|
|
/// The actual view we're wrapping.
|
2015-06-08 03:58:10 +00:00
|
|
|
view: T,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
impl<T: View> ResizedView<T> {
|
|
|
|
/// Creates a new `ResizedView` with the given width and height requirements.
|
2016-07-12 03:26:33 +00:00
|
|
|
///
|
|
|
|
/// `None` values will use the wrapped view's preferences.
|
2017-10-12 23:38:55 +00:00
|
|
|
pub fn new(
|
2019-07-30 23:08:05 +00:00
|
|
|
width: SizeConstraint,
|
|
|
|
height: SizeConstraint,
|
|
|
|
view: T,
|
2017-10-12 23:38:55 +00:00
|
|
|
) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView {
|
2016-07-13 04:01:11 +00:00
|
|
|
size: (width, height).into(),
|
2018-08-20 20:30:42 +00:00
|
|
|
invalidated: true,
|
2018-04-10 18:53:25 +00:00
|
|
|
view,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 02:24:00 +00:00
|
|
|
|
2018-01-28 06:12:29 +00:00
|
|
|
/// Sets the size constraints for this view.
|
2018-02-17 01:21:24 +00:00
|
|
|
pub fn set_constraints(
|
2019-07-30 23:08:05 +00:00
|
|
|
&mut self,
|
|
|
|
width: SizeConstraint,
|
|
|
|
height: SizeConstraint,
|
2018-02-17 01:21:24 +00:00
|
|
|
) {
|
2018-01-28 06:12:29 +00:00
|
|
|
self.set_width(width);
|
|
|
|
self.set_height(height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the width constraint for this view.
|
|
|
|
///
|
|
|
|
/// Leaves the height unchanged.
|
|
|
|
pub fn set_width(&mut self, width: SizeConstraint) {
|
|
|
|
self.size.x = width;
|
2018-08-20 20:30:42 +00:00
|
|
|
self.invalidate();
|
2018-01-28 06:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the height constraint for this view.
|
|
|
|
///
|
|
|
|
/// Leaves the width unchanged.
|
|
|
|
pub fn set_height(&mut self, height: SizeConstraint) {
|
|
|
|
self.size.y = height;
|
2018-08-20 20:30:42 +00:00
|
|
|
self.invalidate();
|
2018-01-28 06:12:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a new `ResizedView` with the given size.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
|
|
|
let size = size.into();
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
2017-10-12 23:38:55 +00:00
|
|
|
SizeConstraint::Fixed(size.x),
|
|
|
|
SizeConstraint::Fixed(size.y),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a new `ResizedView` with fixed width.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_fixed_width(width: usize, view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
|
|
|
SizeConstraint::Fixed(width),
|
|
|
|
SizeConstraint::Free,
|
|
|
|
view,
|
|
|
|
)
|
2016-07-12 02:24:00 +00:00
|
|
|
}
|
2016-07-12 03:26:33 +00:00
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a new `ResizedView` with fixed height.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_fixed_height(height: usize, view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
|
|
|
SizeConstraint::Free,
|
|
|
|
SizeConstraint::Fixed(height),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will take all available space.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_full_screen(view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(SizeConstraint::Full, SizeConstraint::Full, view)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will take all available width.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_full_width(view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(SizeConstraint::Full, SizeConstraint::Free, view)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will take all available height.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_full_height(view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(SizeConstraint::Free, SizeConstraint::Full, view)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will never be bigger than `size`.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_max_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
|
|
|
let size = size.into();
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
2017-10-12 23:38:55 +00:00
|
|
|
SizeConstraint::AtMost(size.x),
|
|
|
|
SizeConstraint::AtMost(size.y),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will enforce a maximum width.
|
2016-08-04 04:55:41 +00:00
|
|
|
///
|
|
|
|
/// The resulting width will never be more than `max_width`.
|
|
|
|
pub fn with_max_width(max_width: usize, view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
2017-10-12 23:38:55 +00:00
|
|
|
SizeConstraint::AtMost(max_width),
|
|
|
|
SizeConstraint::Free,
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will enforce a maximum height.
|
2016-08-04 04:55:41 +00:00
|
|
|
///
|
|
|
|
/// The resulting height will never be more than `max_height`.
|
|
|
|
pub fn with_max_height(max_height: usize, view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
2017-10-12 23:38:55 +00:00
|
|
|
SizeConstraint::Free,
|
|
|
|
SizeConstraint::AtMost(max_height),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will never be smaller than `size`.
|
2016-08-04 04:55:41 +00:00
|
|
|
pub fn with_min_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
|
|
|
let size = size.into();
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
2017-10-12 23:38:55 +00:00
|
|
|
SizeConstraint::AtLeast(size.x),
|
|
|
|
SizeConstraint::AtLeast(size.y),
|
|
|
|
view,
|
|
|
|
)
|
2016-08-04 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will enforce a minimum width.
|
2016-08-04 04:55:41 +00:00
|
|
|
///
|
|
|
|
/// The resulting width will never be less than `min_width`.
|
|
|
|
pub fn with_min_width(min_width: usize, view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
2017-10-12 23:38:55 +00:00
|
|
|
SizeConstraint::AtLeast(min_width),
|
|
|
|
SizeConstraint::Free,
|
|
|
|
view,
|
|
|
|
)
|
2016-07-12 03:26:33 +00:00
|
|
|
}
|
2016-07-12 02:24:00 +00:00
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
/// Wraps `view` in a `ResizedView` which will enforce a minimum height.
|
2016-08-04 04:55:41 +00:00
|
|
|
///
|
|
|
|
/// The resulting height will never be less than `min_height`.
|
|
|
|
pub fn with_min_height(min_height: usize, view: T) -> Self {
|
2020-01-06 19:51:50 +00:00
|
|
|
ResizedView::new(
|
2017-10-12 23:38:55 +00:00
|
|
|
SizeConstraint::Free,
|
|
|
|
SizeConstraint::AtLeast(min_height),
|
|
|
|
view,
|
|
|
|
)
|
2016-07-12 02:24:00 +00:00
|
|
|
}
|
2018-01-26 10:39:10 +00:00
|
|
|
|
2018-08-20 20:30:42 +00:00
|
|
|
/// Should be called anytime something changes.
|
|
|
|
fn invalidate(&mut self) {
|
|
|
|
self.invalidated = true;
|
|
|
|
}
|
|
|
|
|
2018-01-27 09:37:08 +00:00
|
|
|
inner_getters!(self.view: T);
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 19:51:50 +00:00
|
|
|
impl<T: View> ViewWrapper for ResizedView<T> {
|
2016-09-20 00:11:00 +00:00
|
|
|
wrap_impl!(self.view: T);
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2019-08-19 22:16:41 +00:00
|
|
|
fn wrap_draw(&self, printer: &Printer) {
|
|
|
|
self.view.draw(&printer.inner_size(
|
|
|
|
self.size.zip_map(printer.size, |c, s| c.result((s, s))),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
2018-08-20 20:30:42 +00:00
|
|
|
// This is what the child will see as request.
|
2016-08-04 04:55:41 +00:00
|
|
|
let req = self.size.zip_map(req, SizeConstraint::available);
|
2018-08-20 20:30:42 +00:00
|
|
|
|
|
|
|
// This is the size the child would like to have.
|
2019-08-19 22:16:41 +00:00
|
|
|
// Given the constraints of our box.
|
2019-08-27 20:09:13 +00:00
|
|
|
let child_size = self.view.required_size(req);
|
2017-07-17 16:55:48 +00:00
|
|
|
|
2018-08-20 20:30:42 +00:00
|
|
|
// Some of this request will be granted, but maybe not all.
|
2019-08-27 20:29:16 +00:00
|
|
|
self.size
|
|
|
|
.zip_map(child_size.zip(req), SizeConstraint::result)
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2018-08-20 20:30:42 +00:00
|
|
|
|
|
|
|
fn wrap_layout(&mut self, size: Vec2) {
|
|
|
|
self.invalidated = false;
|
2019-08-27 20:29:16 +00:00
|
|
|
self.view
|
|
|
|
.layout(self.size.zip_map(size, |c, s| c.result((s, s))));
|
2018-08-20 20:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_needs_relayout(&self) -> bool {
|
|
|
|
self.invalidated || self.view.needs_relayout()
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2016-09-20 18:48:17 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::view::{Boxable, View};
|
|
|
|
use crate::views::DummyView;
|
2020-01-06 23:51:38 +00:00
|
|
|
use crate::Vec2;
|
2016-09-20 18:48:17 +00:00
|
|
|
|
|
|
|
// 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() {
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::views::TextView;
|
2018-01-26 10:39:10 +00:00
|
|
|
|
|
|
|
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() {
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::views::TextView;
|
2018-01-26 10:39:10 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|