rui/views/
padding.rs

1use crate::*;
2use std::any::Any;
3
4/// Struct for the `padding` modifier.
5pub struct Padding<V> {
6    child: V,
7    padding: f32,
8}
9
10impl<V> View for Padding<V>
11where
12    V: View,
13{
14    fn process(
15        &self,
16        event: &Event,
17        id: ViewId,
18        cx: &mut Context,
19        actions: &mut Vec<Box<dyn Any>>,
20    ) {
21        let off = LocalOffset::new(self.padding, self.padding);
22        self.child
23            .process(&event.offset(-off), id.child(&0), cx, actions);
24    }
25
26    fn draw(&self, id: ViewId, args: &mut DrawArgs) {
27        args.vger.save();
28        args.vger.translate([self.padding, self.padding]);
29        self.child.draw(id.child(&0), args);
30        args.vger.restore();
31    }
32
33    fn layout(&self, id: ViewId, args: &mut LayoutArgs) -> LocalSize {
34        let child_size = self.child.layout(
35            id.child(&0),
36            &mut args.size(args.sz - [2.0 * self.padding, 2.0 * self.padding].into()),
37        );
38        child_size + LocalSize::new(2.0 * self.padding, 2.0 * self.padding)
39    }
40
41    fn dirty(&self, id: ViewId, xform: LocalToWorld, cx: &mut Context) {
42        self.child.dirty(
43            id.child(&0),
44            xform.pre_translate([self.padding, self.padding].into()),
45            cx,
46        );
47    }
48
49    fn hittest(&self, id: ViewId, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
50        self.child.hittest(
51            id.child(&0),
52            pt - LocalOffset::new(self.padding, self.padding),
53            cx,
54        )
55    }
56
57    fn commands(&self, id: ViewId, cx: &mut Context, cmds: &mut Vec<CommandInfo>) {
58        self.child.commands(id.child(&0), cx, cmds)
59    }
60
61    fn gc(&self, id: ViewId, cx: &mut Context, map: &mut Vec<ViewId>) {
62        self.child.gc(id.child(&0), cx, map)
63    }
64
65    fn access(
66        &self,
67        id: ViewId,
68        cx: &mut Context,
69        nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>,
70    ) -> Option<accesskit::NodeId> {
71        self.child.access(id.child(&0), cx, nodes)
72    }
73}
74
75pub enum PaddingParam {
76    Auto,
77    Px(f32),
78}
79pub struct Auto;
80impl From<Auto> for PaddingParam {
81    fn from(_val: Auto) -> Self {
82        PaddingParam::Auto
83    }
84}
85impl From<f32> for PaddingParam {
86    fn from(val: f32) -> Self {
87        PaddingParam::Px(val)
88    }
89}
90
91impl<V> Padding<V>
92where
93    V: View,
94{
95    pub fn new(child: V, param: PaddingParam) -> Self {
96        Self {
97            child,
98            padding: match param {
99                PaddingParam::Auto => 5.0,
100                PaddingParam::Px(px) => px,
101            },
102        }
103    }
104}
105
106impl<V> private::Sealed for Padding<V> {}