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
use crate::kurbo::{Insets, Point, Rect, Size};
use crate::{
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
UpdateCtx, Widget, WidgetPod,
};
pub struct Padding<T> {
left: f64,
right: f64,
top: f64,
bottom: f64,
child: WidgetPod<T, Box<dyn Widget<T>>>,
}
impl<T> Padding<T> {
pub fn new(insets: impl Into<Insets>, child: impl Widget<T> + 'static) -> Padding<T> {
let insets = insets.into();
Padding {
left: insets.x0,
right: insets.x1,
top: insets.y0,
bottom: insets.y1,
child: WidgetPod::new(child).boxed(),
}
}
}
impl<T: Data> Widget<T> for Padding<T> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
self.child.event(ctx, event, data, env)
}
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) {
self.child.lifecycle(ctx, event, data, env)
}
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &T, data: &T, env: &Env) {
self.child.update(ctx, data, env);
}
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
bc.debug_check("Padding");
let hpad = self.left + self.right;
let vpad = self.top + self.bottom;
let child_bc = bc.shrink((hpad, vpad));
let size = self.child.layout(ctx, &child_bc, data, env);
let origin = Point::new(self.left, self.top);
self.child
.set_layout_rect(ctx, data, env, Rect::from_origin_size(origin, size));
let my_size = Size::new(size.width + hpad, size.height + vpad);
let my_insets = self.child.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) {
self.child.paint(ctx, data, env);
}
}