2016-07-02 07:47:38 +00:00
|
|
|
use std::cmp;
|
|
|
|
|
2016-06-28 05:10:59 +00:00
|
|
|
use vec::{ToVec2, Vec2};
|
2016-07-02 07:47:38 +00:00
|
|
|
use super::{View, ViewWrapper};
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2016-06-28 05:40:11 +00:00
|
|
|
/// `BoxView` is a wrapper around an other view, with a given minimum size.
|
2015-06-08 03:58:10 +00:00
|
|
|
pub struct BoxView<T: View> {
|
2015-05-15 18:58:47 +00:00
|
|
|
size: Vec2,
|
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-06-28 05:40:11 +00:00
|
|
|
/// Creates a new `BoxView` with the given minimum size and content
|
2015-05-15 00:41:17 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2015-06-04 18:40:35 +00:00
|
|
|
/// # use cursive::view::{BoxView,TextView};
|
2015-05-15 00:41:17 +00:00
|
|
|
/// // Creates a 20x4 BoxView with a TextView content.
|
2015-06-04 18:40:35 +00:00
|
|
|
/// let view = BoxView::new((20,4), TextView::new("Hello!"));
|
2015-05-15 00:41:17 +00:00
|
|
|
/// ```
|
2015-06-08 03:58:10 +00:00
|
|
|
pub fn new<S: ToVec2>(size: S, view: T) -> Self {
|
2015-05-15 00:41:17 +00:00
|
|
|
BoxView {
|
2015-05-15 18:58:47 +00:00
|
|
|
size: size.to_vec2(),
|
2015-06-08 03:58:10 +00:00
|
|
|
view: view,
|
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-10 01:23:58 +00:00
|
|
|
fn wrap_get_min_size(&mut self, mut req: Vec2) -> Vec2 {
|
2016-03-15 22:37:57 +00:00
|
|
|
if self.size.x > 0 {
|
2016-07-02 07:47:38 +00:00
|
|
|
req.x = cmp::min(self.size.x, req.x);
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
|
|
|
if self.size.y > 0 {
|
2016-07-02 07:47:38 +00:00
|
|
|
req.y = cmp::min(self.size.y, req.y);
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
2015-06-08 19:23:36 +00:00
|
|
|
|
|
|
|
let mut size = self.view.get_min_size(req);
|
|
|
|
|
2016-07-02 07:47:38 +00:00
|
|
|
// Did he think he got to decide?
|
|
|
|
// Of course we have the last word here.
|
2016-03-15 22:37:57 +00:00
|
|
|
if self.size.x > 0 {
|
|
|
|
size.x = self.size.x;
|
|
|
|
}
|
|
|
|
if self.size.y > 0 {
|
|
|
|
size.y = self.size.y;
|
|
|
|
}
|
2015-06-08 19:23:36 +00:00
|
|
|
|
|
|
|
size
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|