Skip to main content

zenthra_core/
style.rs

1// crates/zenthra-core/src/style.rs
2
3/// Spacing on all four sides.
4#[derive(Debug, Clone, Copy, PartialEq, Default)]
5pub struct EdgeInsets {
6    pub top: f32,
7    pub right: f32,
8    pub bottom: f32,
9    pub left: f32,
10}
11
12impl EdgeInsets {
13    pub const ZERO: Self = Self {
14        top: 0.0,
15        right: 0.0,
16        bottom: 0.0,
17        left: 0.0,
18    };
19
20    pub fn all(v: f32) -> Self {
21        Self {
22            top: v,
23            right: v,
24            bottom: v,
25            left: v,
26        }
27    }
28
29    pub fn symmetric(vertical: f32, horizontal: f32) -> Self {
30        Self {
31            top: vertical,
32            bottom: vertical,
33            left: horizontal,
34            right: horizontal,
35        }
36    }
37
38    pub fn horizontal(&self) -> f32 {
39        self.left + self.right
40    }
41    pub fn vertical(&self) -> f32 {
42        self.top + self.bottom
43    }
44}
45
46impl From<f32> for EdgeInsets {
47    fn from(v: f32) -> Self {
48        Self::all(v)
49    }
50}
51
52impl From<f64> for EdgeInsets {
53    fn from(v: f64) -> Self {
54        Self::all(v as f32)
55    }
56}
57
58impl From<i32> for EdgeInsets {
59    fn from(v: i32) -> Self {
60        Self::all(v as f32)
61    }
62}
63
64/// Per-corner border radius.
65#[derive(Debug, Clone, Copy, PartialEq, Default)]
66pub struct BorderRadius {
67    pub top_left: f32,
68    pub top_right: f32,
69    pub bottom_right: f32,
70    pub bottom_left: f32,
71}
72
73impl BorderRadius {
74    pub const ZERO: Self = Self {
75        top_left: 0.0,
76        top_right: 0.0,
77        bottom_right: 0.0,
78        bottom_left: 0.0,
79    };
80
81    pub fn all(r: f32) -> Self {
82        Self {
83            top_left: r,
84            top_right: r,
85            bottom_right: r,
86            bottom_left: r,
87        }
88    }
89
90    pub fn to_array(self) -> [f32; 4] {
91        [
92            self.top_left,
93            self.top_right,
94            self.bottom_right,
95            self.bottom_left,
96        ]
97    }
98}
99
100/// A unified alignment enum for both horizontal and vertical axes.
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
102pub enum Align {
103    #[default]
104    Top,
105    Bottom,
106    Left,
107    Right,
108    Center,
109    SpaceBetween,
110    SpaceAround,
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
114pub enum BorderAlignment {
115    #[default]
116    Inside,
117    Center,
118    Outside,
119}
120
121pub mod blur {
122    #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
123    pub enum Type {
124        #[default]
125        Normal,
126        Frosted,
127        Glassmorphism,
128        OpaqueGlass,
129    }
130}
131
132#[derive(Debug, Clone, Copy, PartialEq)]
133pub enum Filter {
134    Blur(f32, blur::Type),
135    Opacity(f32),
136    Saturate(f32),
137    Brightness(f32),
138    Contrast(f32),
139}
140
141#[derive(Debug, Clone, PartialEq, Default)]
142pub struct BackdropFilter {
143    pub filters: Vec<Filter>,
144}
145
146impl BackdropFilter {
147    pub fn new() -> Self {
148        Self { filters: Vec::new() }
149    }
150
151    pub fn blur(mut self, radius: f32, kind: blur::Type) -> Self {
152        self.filters.push(Filter::Blur(radius, kind));
153        self
154    }
155
156    pub fn opacity(mut self, factor: f32) -> Self {
157        self.filters.push(Filter::Opacity(factor));
158        self
159    }
160
161    pub fn saturate(mut self, factor: f32) -> Self {
162        self.filters.push(Filter::Saturate(factor));
163        self
164    }
165
166    pub fn brightness(mut self, factor: f32) -> Self {
167        self.filters.push(Filter::Brightness(factor));
168        self
169    }
170
171    pub fn contrast(mut self, factor: f32) -> Self {
172        self.filters.push(Filter::Contrast(factor));
173        self
174    }
175}