Skip to main content

freya_components/theming/
component_themes.rs

1use std::{
2    any::Any,
3    fmt,
4};
5
6use freya_core::{
7    integration::FxHashMap,
8    prelude::*,
9};
10
11use crate::theming::themes::light_theme;
12
13pub struct Theme {
14    pub name: &'static str,
15    pub colors: ColorsSheet,
16    themes: FxHashMap<&'static str, Box<dyn Any>>,
17}
18
19impl Theme {
20    pub fn new(name: &'static str, colors: ColorsSheet) -> Self {
21        Self {
22            name,
23            colors,
24            themes: FxHashMap::default(),
25        }
26    }
27
28    /// Get a component theme by key.
29    pub fn get<T: 'static>(&self, key: &str) -> Option<&T> {
30        self.themes.get(key).and_then(|v| v.downcast_ref())
31    }
32
33    /// Set a component theme by key.
34    pub fn set<T: 'static>(&mut self, key: &'static str, val: T) {
35        self.themes.insert(key, Box::new(val));
36    }
37}
38
39impl fmt::Debug for Theme {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.debug_struct("Theme")
42            .field("name", &self.name)
43            .field("colors", &self.colors)
44            .field("themes", &format!("({} entries)", self.themes.len()))
45            .finish()
46    }
47}
48
49impl PartialEq for Theme {
50    fn eq(&self, other: &Self) -> bool {
51        self.name == other.name && self.colors == other.colors
52    }
53}
54
55impl Default for Theme {
56    fn default() -> Self {
57        light_theme()
58    }
59}
60
61#[derive(Clone, Debug, PartialEq, Eq)]
62pub struct ColorsSheet {
63    // Brand & Accent
64    pub primary: Color,
65    pub secondary: Color,
66    pub tertiary: Color,
67
68    // Status / Semantic colors
69    pub success: Color,
70    pub warning: Color,
71    pub error: Color,
72    pub info: Color,
73
74    // Surfaces / Backgrounds
75    pub background: Color,
76    pub surface_primary: Color,
77    pub surface_secondary: Color,
78    pub surface_tertiary: Color,
79    pub surface_inverse: Color,
80    pub surface_inverse_secondary: Color,
81    pub surface_inverse_tertiary: Color,
82
83    // Borders
84    pub border: Color,
85    pub border_focus: Color,
86    pub border_disabled: Color,
87
88    // Text / Content
89    pub text_primary: Color,
90    pub text_secondary: Color,
91    pub text_placeholder: Color,
92    pub text_inverse: Color,
93    pub text_highlight: Color,
94
95    // States / Interaction
96    pub hover: Color,
97    pub focus: Color,
98    pub active: Color,
99    pub disabled: Color,
100
101    // Utility
102    pub overlay: Color,
103    pub shadow: Color,
104}