1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use vec::{Vec2, ToVec2};
use super::{View, ViewWrapper, SizeRequest, DimensionRequest};

/// BoxView is a wrapper around an other view, with a given minimum size.
pub struct BoxView<T: View> {
    size: Vec2,
    view: T,
}

impl<T: View> BoxView<T> {
    /// Creates a new BoxView with the given minimum size and content
    ///
    /// # Example
    ///
    /// ```
    /// # use cursive::view::{BoxView,TextView};
    /// // Creates a 20x4 BoxView with a TextView content.
    /// 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,
        }
    }
}

impl<T: View> ViewWrapper for BoxView<T> {
    wrap_impl!(&self.view);

    fn wrap_get_min_size(&self, mut req: SizeRequest) -> Vec2 {
        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);

        if self.size.x > 0 {
            size.x = self.size.x;
        }
        if self.size.y > 0 {
            size.y = self.size.y;
        }

        size
    }
}