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, Copy, Debug, Default, PartialEq, Eq)]
46pub enum AntiAlias {
47    #[default]
48    On,
49    Off,
50}
51
52#[derive(Clone, Debug, PartialEq)]
53pub struct Style {
54    pub background: Option<Paint>,
55    pub border: Option<Border>,
56    pub corner_radius: u32,
57    pub corner_radii: CornerRadius,
58    pub gradient: GradientDirection,
59    pub opacity: f32,
60    pub transform: PaintTransform,
61    pub anti_alias: AntiAlias,
62}
63
64impl Default for Style {
65    fn default() -> Self {
66        Self {
67            background: None,
68            border: None,
69            corner_radius: 0,
70            corner_radii: CornerRadius::ZERO,
71            gradient: GradientDirection::default(),
72            opacity: 1.0,
73            transform: PaintTransform::IDENTITY,
74            anti_alias: AntiAlias::On,
75        }
76    }
77}
78
79impl Style {
80    pub fn background(color: impl Into<Paint>) -> Self {
81        Self {
82            background: Some(color.into()),
83            border: None,
84            corner_radius: 0,
85            corner_radii: CornerRadius::ZERO,
86            gradient: GradientDirection::default(),
87            opacity: 1.0,
88            transform: PaintTransform::IDENTITY,
89            anti_alias: AntiAlias::On,
90        }
91    }
92}