chargrid_common/
pad_by.rs

1use chargrid_core::*;
2
3pub struct Padding {
4    pub top: u32,
5    pub bottom: u32,
6    pub left: u32,
7    pub right: u32,
8}
9impl Padding {
10    pub fn all(padding: u32) -> Self {
11        Self {
12            top: padding,
13            bottom: padding,
14            left: padding,
15            right: padding,
16        }
17    }
18}
19
20pub struct PadBy<C: Component> {
21    pub component: C,
22    pub padding: Padding,
23}
24
25impl Padding {
26    fn update_ctx<'a>(&self, ctx: Ctx<'a>) -> Ctx<'a> {
27        ctx.add_offset(Coord::new(self.left as i32, self.top as i32))
28            .add_size(Size::new(self.right, self.bottom))
29    }
30    fn size_delta(&self) -> Size {
31        Size::new(self.left + self.right, self.top + self.bottom)
32    }
33}
34
35impl<C: Component> Component for PadBy<C> {
36    type Output = C::Output;
37    type State = C::State;
38    fn render(&self, state: &Self::State, ctx: Ctx, fb: &mut FrameBuffer) {
39        self.component
40            .render(state, self.padding.update_ctx(ctx), fb);
41    }
42    fn update(&mut self, state: &mut Self::State, ctx: Ctx, event: Event) -> Self::Output {
43        self.component
44            .update(state, self.padding.update_ctx(ctx), event)
45    }
46    fn size(&self, state: &Self::State, ctx: Ctx) -> Size {
47        self.component.size(state, ctx) + self.padding.size_delta()
48    }
49}