clay_layout/elements/containers/
border.rs

1use crate::{
2    bindings::*,
3    color::Color,
4    elements::{CornerRadius, ElementConfigType},
5    mem, TypedConfig,
6};
7
8#[derive(Debug, Clone, Copy)]
9pub struct Border {
10    width: u32,
11    color: Color,
12}
13
14impl Border {
15    pub fn new(width: u32, color: Color) -> Self {
16        Self { width, color }
17    }
18}
19
20impl Default for Border {
21    fn default() -> Self {
22        Self {
23            width: 0,
24            color: Color::rgba(0., 0., 0., 0.),
25        }
26    }
27}
28
29impl From<Clay_Border> for Border {
30    fn from(value: Clay_Border) -> Self {
31        Self {
32            width: value.width,
33            color: value.color.into(),
34        }
35    }
36}
37impl From<Border> for Clay_Border {
38    fn from(value: Border) -> Self {
39        Self {
40            width: value.width,
41            color: value.color.into(),
42        }
43    }
44}
45
46#[derive(Debug, Clone, Copy)]
47pub struct BorderContainer {
48    pub left: Border,
49    pub right: Border,
50    pub top: Border,
51    pub bottom: Border,
52    pub between_childs: Border,
53    pub corner_radius: CornerRadius,
54}
55
56impl BorderContainer {
57    pub fn new() -> Self {
58        Self::default()
59    }
60
61    pub fn left(&mut self, width: u32, color: Color) -> &mut Self {
62        self.left = Border::new(width, color);
63        self
64    }
65    pub fn right(&mut self, width: u32, color: Color) -> &mut Self {
66        self.right = Border::new(width, color);
67        self
68    }
69    pub fn top(&mut self, width: u32, color: Color) -> &mut Self {
70        self.top = Border::new(width, color);
71        self
72    }
73    pub fn bottom(&mut self, width: u32, color: Color) -> &mut Self {
74        self.bottom = Border::new(width, color);
75        self
76    }
77    pub fn between_childs(&mut self, width: u32, color: Color) -> &mut Self {
78        self.between_childs = Border::new(width, color);
79        self
80    }
81    pub fn all_directions(&mut self, width: u32, color: Color) -> &mut Self {
82        self.left(width, color)
83            .right(width, color)
84            .top(width, color)
85            .bottom(width, color)
86    }
87    pub fn all(&mut self, width: u32, color: Color) -> &mut Self {
88        self.all_directions(width, color)
89            .between_childs(width, color)
90    }
91
92    pub fn corner_radius(&mut self, corner_radius: CornerRadius) -> &mut Self {
93        self.corner_radius = corner_radius;
94        self
95    }
96
97    pub fn end(&self) -> TypedConfig {
98        let memory = unsafe { Clay__StoreBorderElementConfig((*self).into()) };
99
100        TypedConfig {
101            config_memory: memory as _,
102            id: mem::zeroed_init(),
103            config_type: ElementConfigType::BorderContainer as _,
104        }
105    }
106}
107
108impl Default for BorderContainer {
109    fn default() -> Self {
110        Self {
111            left: Border::default(),
112            right: Border::default(),
113            top: Border::default(),
114            bottom: Border::default(),
115            between_childs: Border::default(),
116            corner_radius: CornerRadius::All(0.),
117        }
118    }
119}
120
121impl From<Clay_BorderElementConfig> for BorderContainer {
122    fn from(value: Clay_BorderElementConfig) -> Self {
123        Self {
124            left: value.left.into(),
125            right: value.right.into(),
126            top: value.top.into(),
127            bottom: value.bottom.into(),
128            between_childs: value.betweenChildren.into(),
129            corner_radius: value.cornerRadius.into(),
130        }
131    }
132}
133impl From<BorderContainer> for Clay_BorderElementConfig {
134    fn from(value: BorderContainer) -> Self {
135        Self {
136            left: value.left.into(),
137            right: value.right.into(),
138            top: value.top.into(),
139            bottom: value.bottom.into(),
140            betweenChildren: value.between_childs.into(),
141            cornerRadius: value.corner_radius.into(),
142        }
143    }
144}