cursive/src/view/box_view.rs

51 lines
1.2 KiB
Rust
Raw Normal View History

2016-06-28 05:10:59 +00:00
use vec::{ToVec2, Vec2};
use super::{DimensionRequest, SizeRequest, View, ViewWrapper};
2016-06-28 05:40:11 +00:00
/// `BoxView` is a wrapper around an other view, with a given minimum size.
pub struct BoxView<T: View> {
size: Vec2,
view: T,
}
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
///
/// # Example
///
/// ```
2015-06-04 18:40:35 +00:00
/// # use cursive::view::{BoxView,TextView};
/// // 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!"));
/// ```
pub fn new<S: ToVec2>(size: S, view: T) -> Self {
BoxView {
size: size.to_vec2(),
view: view,
}
}
}
2016-06-25 23:36:22 +00:00
impl<T: View> ViewWrapper for BoxView<T> {
wrap_impl!(&self.view);
fn wrap_get_min_size(&self, mut req: SizeRequest) -> Vec2 {
2016-03-15 22:37:57 +00:00
if self.size.x > 0 {
req.w = DimensionRequest::AtMost(self.size.x);
}
if self.size.y > 0 {
req.h = DimensionRequest::AtMost(self.size.y);
}
let mut size = self.view.get_min_size(req);
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;
}
size
}
}