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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::f64::INFINITY;
use crate::shell::kurbo::Size;
use crate::{
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
UpdateCtx, Widget, WidgetId,
};
pub struct SizedBox<T> {
inner: Option<Box<dyn Widget<T>>>,
width: Option<f64>,
height: Option<f64>,
}
impl<T> SizedBox<T> {
pub fn new(inner: impl Widget<T> + 'static) -> Self {
Self {
inner: Some(Box::new(inner)),
width: None,
height: None,
}
}
pub fn empty() -> Self {
Self {
inner: None,
width: None,
height: None,
}
}
pub fn width(mut self, width: f64) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: f64) -> Self {
self.height = Some(height);
self
}
pub fn expand(mut self) -> Self {
self.width = Some(INFINITY);
self.height = Some(INFINITY);
self
}
pub fn expand_width(mut self) -> Self {
self.width = Some(INFINITY);
self
}
pub fn expand_height(mut self) -> Self {
self.height = Some(INFINITY);
self
}
fn child_constraints(&self, bc: &BoxConstraints) -> BoxConstraints {
let (min_width, max_width) = match self.width {
Some(width) => {
let w = width.max(bc.min().width).min(bc.max().width);
(w, w)
}
None => (bc.min().width, bc.max().width),
};
let (min_height, max_height) = match self.height {
Some(height) => {
let h = height.max(bc.min().height).min(bc.max().height);
(h, h)
}
None => (bc.min().height, bc.max().height),
};
BoxConstraints::new(
Size::new(min_width, min_height),
Size::new(max_width, max_height),
)
}
#[cfg(test)]
pub(crate) fn width_and_height(&self) -> (Option<f64>, Option<f64>) {
(self.width, self.height)
}
}
impl<T: Data> Widget<T> for SizedBox<T> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
if let Some(ref mut inner) = self.inner {
inner.event(ctx, event, data, env);
}
}
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) {
if let Some(ref mut inner) = self.inner {
inner.lifecycle(ctx, event, data, env)
}
}
fn update(&mut self, ctx: &mut UpdateCtx, old_data: &T, data: &T, env: &Env) {
if let Some(ref mut inner) = self.inner {
inner.update(ctx, old_data, data, env);
}
}
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
bc.debug_check("SizedBox");
let child_bc = self.child_constraints(bc);
let size = match self.inner.as_mut() {
Some(inner) => inner.layout(ctx, &child_bc, data, env),
None => bc.constrain((self.width.unwrap_or(0.0), self.height.unwrap_or(0.0))),
};
if size.width.is_infinite() {
log::warn!("SizedBox is returning an infinite width.");
}
if size.height.is_infinite() {
log::warn!("SizedBox is returning an infinite height.");
}
size
}
fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
if let Some(ref mut inner) = self.inner {
inner.paint(ctx, data, env);
}
}
fn id(&self) -> Option<WidgetId> {
self.inner.as_ref().and_then(|inner| inner.id())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::widget::Label;
#[test]
fn expand() {
let expand = SizedBox::<()>::new(Label::new("hello!")).expand();
let bc = BoxConstraints::tight(Size::new(400., 400.)).loosen();
let child_bc = expand.child_constraints(&bc);
assert_eq!(child_bc.min(), Size::new(400., 400.,));
}
#[test]
fn no_width() {
let expand = SizedBox::<()>::new(Label::new("hello!")).height(200.);
let bc = BoxConstraints::tight(Size::new(400., 400.)).loosen();
let child_bc = expand.child_constraints(&bc);
assert_eq!(child_bc.min(), Size::new(0., 200.,));
assert_eq!(child_bc.max(), Size::new(400., 200.,));
}
}