Skip to main content

simple_render/ui/types/
style.rs

1use super::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
4pub struct BorderWidth {
5    pub top: u32,
6    pub right: u32,
7    pub bottom: u32,
8    pub left: u32,
9}
10
11impl BorderWidth {
12    pub const ZERO: Self = Self::all(0);
13
14    pub const fn all(width: u32) -> Self {
15        Self {
16            top: width,
17            right: width,
18            bottom: width,
19            left: width,
20        }
21    }
22
23    pub(in crate::ui) fn is_zero(self) -> bool {
24        self.top == 0 && self.right == 0 && self.bottom == 0 && self.left == 0
25    }
26
27    pub(in crate::ui) fn scaled(self, scale: u32) -> Self {
28        Self {
29            top: self.top.saturating_mul(scale),
30            right: self.right.saturating_mul(scale),
31            bottom: self.bottom.saturating_mul(scale),
32            left: self.left.saturating_mul(scale),
33        }
34    }
35}
36
37#[derive(Clone, Debug, PartialEq, Eq)]
38pub struct Border {
39    pub width: u32,
40    pub widths: BorderWidth,
41    pub color: Paint,
42    pub gradient: GradientDirection,
43}
44
45#[derive(Clone, Debug, PartialEq)]
46pub struct Style {
47    pub background: Option<Paint>,
48    pub border: Option<Border>,
49    pub corner_radius: u32,
50    pub corner_radii: CornerRadius,
51    pub gradient: GradientDirection,
52    pub opacity: f32,
53}
54
55impl Default for Style {
56    fn default() -> Self {
57        Self {
58            background: None,
59            border: None,
60            corner_radius: 0,
61            corner_radii: CornerRadius::ZERO,
62            gradient: GradientDirection::default(),
63            opacity: 1.0,
64        }
65    }
66}
67
68impl Style {
69    pub fn background(color: impl Into<Paint>) -> Self {
70        Self {
71            background: Some(color.into()),
72            border: None,
73            corner_radius: 0,
74            corner_radii: CornerRadius::ZERO,
75            gradient: GradientDirection::default(),
76            opacity: 1.0,
77        }
78    }
79}