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
use super::BackgroundBrush;
use crate::shell::kurbo::{Point, Rect, Size};
use crate::{
BoxConstraints, Color, Data, Env, Event, EventCtx, KeyOrValue, LayoutCtx, LifeCycle,
LifeCycleCtx, PaintCtx, RenderContext, UpdateCtx, Widget, WidgetPod,
};
struct BorderStyle {
width: KeyOrValue<f64>,
color: KeyOrValue<Color>,
}
pub struct Container<T> {
background: Option<BackgroundBrush<T>>,
border: Option<BorderStyle>,
corner_radius: f64,
inner: WidgetPod<T, Box<dyn Widget<T>>>,
}
impl<T: Data> Container<T> {
pub fn new(inner: impl Widget<T> + 'static) -> Self {
Self {
background: None,
border: None,
corner_radius: 0.0,
inner: WidgetPod::new(inner).boxed(),
}
}
pub fn background(mut self, brush: impl Into<BackgroundBrush<T>>) -> Self {
self.background = Some(brush.into());
self
}
pub fn border(
mut self,
color: impl Into<KeyOrValue<Color>>,
width: impl Into<KeyOrValue<f64>>,
) -> Self {
self.border = Some(BorderStyle {
color: color.into(),
width: width.into(),
});
self
}
pub fn rounded(mut self, radius: f64) -> Self {
self.corner_radius = radius;
self
}
#[cfg(test)]
pub(crate) fn background_is_some(&self) -> bool {
self.background.is_some()
}
#[cfg(test)]
pub(crate) fn border_is_some(&self) -> bool {
self.border.is_some()
}
}
impl<T: Data> Widget<T> for Container<T> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
self.inner.event(ctx, event, data, env);
}
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) {
self.inner.lifecycle(ctx, event, data, env)
}
fn update(&mut self, ctx: &mut UpdateCtx, old_data: &T, data: &T, env: &Env) {
if let Some(BackgroundBrush::Painter(p)) = self.background.as_mut() {
p.update(ctx, old_data, data, env);
}
self.inner.update(ctx, data, env);
}
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
bc.debug_check("Container");
let border_width = match &self.border {
Some(border) => border.width.resolve(env),
None => 0.0,
};
let child_bc = bc.shrink((2.0 * border_width, 2.0 * border_width));
let size = self.inner.layout(ctx, &child_bc, data, env);
let origin = Point::new(border_width, border_width);
self.inner
.set_layout_rect(ctx, data, env, Rect::from_origin_size(origin, size));
let my_size = Size::new(
size.width + 2.0 * border_width,
size.height + 2.0 * border_width,
);
let my_insets = self.inner.compute_parent_paint_insets(my_size);
ctx.set_paint_insets(my_insets);
my_size
}
fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
if let Some(background) = self.background.as_mut() {
let panel = ctx.size().to_rounded_rect(self.corner_radius);
ctx.with_save(|ctx| {
ctx.clip(panel);
background.paint(ctx, data, env);
});
}
if let Some(border) = &self.border {
let border_width = border.width.resolve(env);
let border_rect = ctx
.size()
.to_rect()
.inset(border_width / -2.0)
.to_rounded_rect(self.corner_radius);
ctx.stroke(border_rect, &border.color.resolve(env), border_width);
};
self.inner.paint(ctx, data, env);
}
}