1use super::tokens::{
39 color::*,
40 radius::RadiusScale,
41 spacing::SpacingValue,
42 typography::Typography,
43};
44use serde::{Deserialize, Serialize};
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
50#[serde(rename_all = "lowercase")]
51pub enum ThemeVariant {
52 #[default]
54 Dark,
55 Light,
57 Auto,
59}
60
61impl ThemeVariant {
62 #[must_use]
64 pub const fn name(&self) -> &'static str {
65 match self {
66 Self::Dark => "dark",
67 Self::Light => "light",
68 Self::Auto => "auto",
69 }
70 }
71
72 #[must_use]
74 pub fn from_name(name: &str) -> Option<Self> {
75 match name.to_lowercase().as_str() {
76 "dark" => Some(Self::Dark),
77 "light" => Some(Self::Light),
78 "auto" => Some(Self::Auto),
79 _ => None,
80 }
81 }
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
88pub struct ColorPalette {
89 pub background: ColorToken<Background>,
91 pub surface: ColorToken<Surface>,
93 pub surface_hover: ColorToken<SurfaceHover>,
95 pub foreground: ColorToken<Foreground>,
97 pub foreground_dim: ColorToken<ForegroundDim>,
99 pub accent: ColorToken<Accent>,
101 pub error: ColorToken<Error>,
103 pub warning: ColorToken<Warning>,
105 pub success: ColorToken<Success>,
107 pub info: ColorToken<Info>,
109}
110
111impl ColorPalette {
112 #[must_use]
114 pub fn dark() -> Self {
115 Self {
116 background: ColorToken::from_hex("#1e1e2e").expect("valid hex"),
117 surface: ColorToken::from_hex("#313244").expect("valid hex"),
118 surface_hover: ColorToken::from_hex("#45475a").expect("valid hex"),
119 foreground: ColorToken::from_hex("#cdd6f4").expect("valid hex"),
120 foreground_dim: ColorToken::from_hex("#a6adc8").expect("valid hex"),
121 accent: ColorToken::from_hex("#89b4fa").expect("valid hex"),
122 error: ColorToken::from_hex("#f38ba8").expect("valid hex"),
123 warning: ColorToken::from_hex("#f9e2af").expect("valid hex"),
124 success: ColorToken::from_hex("#a6e3a1").expect("valid hex"),
125 info: ColorToken::from_hex("#89dceb").expect("valid hex"),
126 }
127 }
128
129 #[must_use]
131 pub fn light() -> Self {
132 Self {
133 background: ColorToken::from_hex("#eff1f5").expect("valid hex"),
134 surface: ColorToken::from_hex("#e6e9ef").expect("valid hex"),
135 surface_hover: ColorToken::from_hex("#dce0e8").expect("valid hex"),
136 foreground: ColorToken::from_hex("#4c4f69").expect("valid hex"),
137 foreground_dim: ColorToken::from_hex("#6c6f85").expect("valid hex"),
138 accent: ColorToken::from_hex("#1e66f5").expect("valid hex"),
139 error: ColorToken::from_hex("#d20f39").expect("valid hex"),
140 warning: ColorToken::from_hex("#df8e1d").expect("valid hex"),
141 success: ColorToken::from_hex("#40a02b").expect("valid hex"),
142 info: ColorToken::from_hex("#04a5e5").expect("valid hex"),
143 }
144 }
145}
146
147impl Default for ColorPalette {
148 fn default() -> Self {
149 Self::dark()
150 }
151}
152
153#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
155pub struct BarConfig {
156 #[serde(default = "default_bar_height")]
158 pub height: u16,
159 #[serde(default)]
161 pub position: BarPosition,
162 #[serde(default)]
164 pub margin: u16,
165}
166
167fn default_bar_height() -> u16 {
168 40
169}
170
171impl Default for BarConfig {
172 fn default() -> Self {
173 Self {
174 height: 40,
175 position: BarPosition::Top,
176 margin: 0,
177 }
178 }
179}
180
181#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
183#[serde(rename_all = "lowercase")]
184pub enum BarPosition {
185 #[default]
187 Top,
188 Bottom,
190}
191
192impl BarPosition {
193 #[must_use]
195 pub const fn name(&self) -> &'static str {
196 match self {
197 Self::Top => "top",
198 Self::Bottom => "bottom",
199 }
200 }
201}
202
203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
247pub struct Theme {
248 #[serde(default)]
250 pub variant: ThemeVariant,
251 #[serde(default)]
253 pub colors: ColorPalette,
254 #[serde(default)]
256 pub typography: Typography,
257 #[serde(default)]
259 pub radius: RadiusScale,
260 #[serde(default)]
262 pub bar: BarConfig,
263}
264
265impl Theme {
266 #[must_use]
279 pub fn dark() -> Self {
280 Self {
281 variant: ThemeVariant::Dark,
282 colors: ColorPalette::dark(),
283 typography: Typography::default(),
284 radius: RadiusScale::default(),
285 bar: BarConfig::default(),
286 }
287 }
288
289 #[must_use]
300 pub fn light() -> Self {
301 Self {
302 variant: ThemeVariant::Light,
303 colors: ColorPalette::light(),
304 typography: Typography::default(),
305 radius: RadiusScale::default(),
306 bar: BarConfig::default(),
307 }
308 }
309
310 #[must_use]
314 pub const fn module_spacing(&self) -> u16 {
315 SpacingValue::Lg.pixels()
316 }
317
318 #[must_use]
320 pub const fn button_spacing(&self) -> u16 {
321 SpacingValue::Sm.pixels()
322 }
323}
324
325impl Default for Theme {
326 fn default() -> Self {
327 Self::dark()
328 }
329}
330
331#[cfg(test)]
332mod tests {
333 use super::*;
334
335 #[test]
336 fn theme_dark_default() {
337 let theme = Theme::dark();
338 assert_eq!(theme.variant, ThemeVariant::Dark);
339 assert_eq!(theme.bar.height, 40);
340 }
341
342 #[test]
343 fn theme_light() {
344 let theme = Theme::light();
345 assert_eq!(theme.variant, ThemeVariant::Light);
346 }
347
348 #[test]
349 fn theme_serialization_roundtrip() {
350 let original = Theme::dark();
351 let json = serde_json::to_string_pretty(&original).unwrap();
352 let restored: Theme = serde_json::from_str(&json).unwrap();
353 assert_eq!(original.variant, restored.variant);
354 assert_eq!(original.bar.height, restored.bar.height);
355 }
356
357 #[test]
358 fn theme_variant_from_name() {
359 assert_eq!(ThemeVariant::from_name("dark"), Some(ThemeVariant::Dark));
360 assert_eq!(ThemeVariant::from_name("LIGHT"), Some(ThemeVariant::Light));
361 assert_eq!(ThemeVariant::from_name("invalid"), None);
362 }
363}