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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use XY;
use vec::Vec2;
use view::{SizeConstraint, View, ViewWrapper};
pub struct BoxView<T: View> {
size: XY<SizeConstraint>,
squishable: bool,
view: T,
}
impl<T: View> BoxView<T> {
pub fn new(width: SizeConstraint, height: SizeConstraint, view: T) -> Self {
BoxView {
size: (width, height).into(),
squishable: false,
view: view,
}
}
pub fn squishable(mut self) -> Self {
self.squishable = true;
self
}
pub fn with_fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into();
BoxView::new(SizeConstraint::Fixed(size.x),
SizeConstraint::Fixed(size.y),
view)
}
pub fn with_fixed_width(width: usize, view: T) -> Self {
BoxView::new(SizeConstraint::Fixed(width), SizeConstraint::Free, view)
}
pub fn with_fixed_height(height: usize, view: T) -> Self {
BoxView::new(SizeConstraint::Free, SizeConstraint::Fixed(height), view)
}
pub fn with_full_screen(view: T) -> Self {
BoxView::new(SizeConstraint::Full, SizeConstraint::Full, view)
}
pub fn with_full_width(view: T) -> Self {
BoxView::new(SizeConstraint::Full, SizeConstraint::Free, view)
}
pub fn with_full_height(view: T) -> Self {
BoxView::new(SizeConstraint::Free, SizeConstraint::Full, view)
}
pub fn with_max_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into();
BoxView::new(SizeConstraint::AtMost(size.x),
SizeConstraint::AtMost(size.y),
view)
}
pub fn with_max_width(max_width: usize, view: T) -> Self {
BoxView::new(SizeConstraint::AtMost(max_width),
SizeConstraint::Free,
view)
}
pub fn with_max_height(max_height: usize, view: T) -> Self {
BoxView::new(SizeConstraint::Free,
SizeConstraint::AtMost(max_height),
view)
}
pub fn with_min_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into();
BoxView::new(SizeConstraint::AtLeast(size.x),
SizeConstraint::AtLeast(size.y),
view)
}
pub fn with_min_width(min_width: usize, view: T) -> Self {
BoxView::new(SizeConstraint::AtLeast(min_width),
SizeConstraint::Free,
view)
}
pub fn with_min_height(min_height: usize, view: T) -> Self {
BoxView::new(SizeConstraint::Free,
SizeConstraint::AtLeast(min_height),
view)
}
}
impl<T: View> ViewWrapper for BoxView<T> {
wrap_impl!(&self.view);
fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 {
let req = self.size.zip_map(req, SizeConstraint::available);
let child_size = self.view.get_min_size(req);
let result = self.size
.zip_map(child_size.zip(req), SizeConstraint::result);
if self.squishable {
let respect_req = result.zip_map(req, |res, req| res <= req);
result.zip_map(respect_req.zip(child_size),
|res, (respect, child)| if respect {
res
} else {
child
})
} else {
result
}
}
}