summaryrefslogtreecommitdiff
path: root/crates/material_theme/src/lib.rs
blob: 0df5b1cffe871f2061e631cb871e07b886358836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use std::sync::LazyLock;

use iced_widget::core::theme::{Base, Style};
use iced_widget::core::{Color, color};

pub mod button;
pub mod checkbox;
pub mod combo_box;
pub mod container;
#[cfg(feature = "dialog")]
pub mod dialog;
#[cfg(feature = "markdown")]
pub mod markdown;
pub mod menu;
pub mod pick_list;
pub mod progress_bar;
#[cfg(feature = "qr_code")]
pub mod qr_code;
pub mod radio;
pub mod rule;
pub mod scrollable;
pub mod slider;
#[cfg(feature = "svg")]
pub mod svg;
pub mod text;
pub mod text_input;
pub mod toggler;
pub mod utils;

pub static DARK: LazyLock<Theme> =
    LazyLock::new(|| Theme::new("Dark", ColorScheme::DARK));
pub static LIGHT: LazyLock<Theme> =
    LazyLock::new(|| Theme::new("Light", ColorScheme::LIGHT));

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Theme {
    pub name: String,
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub colorscheme: ColorScheme,
}

impl Theme {
    pub fn new(name: impl Into<String>, colorscheme: ColorScheme) -> Self {
        Self {
            name: name.into(),
            colorscheme,
        }
    }
}

impl Clone for Theme {
    fn clone(&self) -> Self {
        Self {
            name: self.name.clone(),
            colorscheme: self.colorscheme,
        }
    }

    fn clone_from(&mut self, source: &Self) {
        self.name = source.name.clone();
        self.colorscheme = source.colorscheme;
    }
}

impl Default for Theme {
    fn default() -> Self {
        static DEFAULT: LazyLock<Theme> = LazyLock::new(|| {
            match dark_light::detect().unwrap_or(dark_light::Mode::Unspecified)
            {
                dark_light::Mode::Dark | dark_light::Mode::Unspecified => {
                    DARK.clone()
                }
                dark_light::Mode::Light => LIGHT.clone(),
            }
        });

        DEFAULT.clone()
    }
}

impl std::fmt::Display for Theme {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl Base for Theme {
    fn base(&self) -> Style {
        Style {
            background_color: self.colorscheme.surface.color,
            text_color: self.colorscheme.surface.on_surface,
        }
    }

    fn palette(&self) -> Option<iced_widget::theme::Palette> {
        // TODO: create a Palette
        None
    }
}

#[cfg(feature = "animate")]
impl iced_anim::Animate for Theme {
    fn components() -> usize {
        ColorScheme::components()
    }

    fn update(&mut self, components: &mut impl Iterator<Item = f32>) {
        let mut colors = self.colorscheme;
        colors.update(components);

        *self = Theme::new("Animating Theme", colors);
    }

    fn distance_to(&self, end: &Self) -> Vec<f32> {
        self.colorscheme.distance_to(&end.colorscheme)
    }

    fn lerp(&mut self, start: &Self, end: &Self, progress: f32) {
        let mut colors = self.colorscheme;
        colors.lerp(&start.colorscheme, &end.colorscheme, progress);

        *self = Theme::new("Animating Theme", colors);
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColorScheme {
    pub primary: Primary,
    pub secondary: Secondary,
    pub tertiary: Tertiary,
    pub error: Error,
    pub surface: Surface,
    pub inverse: Inverse,
    pub outline: Outline,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub shadow: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub scrim: Color,
}

macro_rules! from_argb {
    ($hex:expr) => {{
        let hex = $hex as u32;

        let a = ((hex & 0xff000000) >> 24) as f32 / 255.0;
        let r = (hex & 0x00ff0000) >> 16;
        let g = (hex & 0x0000ff00) >> 8;
        let b = (hex & 0x000000ff);

        color!(r as u8, g as u8, b as u8, a)
    }};
}

impl ColorScheme {
    const DARK: Self = Self {
        primary: Primary {
            color: from_argb!(0xff9bd4a1),
            on_primary: from_argb!(0xff003916),
            primary_container: from_argb!(0xff1b5129),
            on_primary_container: from_argb!(0xffb6f1bb),
        },
        secondary: Secondary {
            color: from_argb!(0xffb8ccb6),
            on_secondary: from_argb!(0xff233425),
            secondary_container: from_argb!(0xff394b3a),
            on_secondary_container: from_argb!(0xffd3e8d1),
        },
        tertiary: Tertiary {
            color: from_argb!(0xffa1ced7),
            on_tertiary: from_argb!(0xff00363e),
            tertiary_container: from_argb!(0xff1f4d55),
            on_tertiary_container: from_argb!(0xffbdeaf4),
        },
        error: Error {
            color: from_argb!(0xffffb4ab),
            on_error: from_argb!(0xff690005),
            error_container: from_argb!(0xff93000a),
            on_error_container: from_argb!(0xffffdad6),
        },
        surface: Surface {
            color: from_argb!(0xff101510),
            on_surface: from_argb!(0xffe0e4dc),
            on_surface_variant: from_argb!(0xffc1c9be),
            surface_container: SurfaceContainer {
                lowest: from_argb!(0xff0b0f0b),
                low: from_argb!(0xff181d18),
                base: from_argb!(0xff1c211c),
                high: from_argb!(0xff262b26),
                highest: from_argb!(0xff313631),
            },
        },
        inverse: Inverse {
            inverse_surface: from_argb!(0xffe0e4dc),
            inverse_on_surface: from_argb!(0xff2d322c),
            inverse_primary: from_argb!(0xff34693f),
        },
        outline: Outline {
            color: from_argb!(0xff8b9389),
            variant: from_argb!(0xff414941),
        },
        shadow: from_argb!(0xff000000),
        scrim: from_argb!(0x4d000000),
    };

    const LIGHT: Self = Self {
        primary: Primary {
            color: from_argb!(0xff34693f),
            on_primary: from_argb!(0xffffffff),
            primary_container: from_argb!(0xffb6f1bb),
            on_primary_container: from_argb!(0xff1b5129),
        },
        secondary: Secondary {
            color: from_argb!(0xff516351),
            on_secondary: from_argb!(0xffffffff),
            secondary_container: from_argb!(0xffd3e8d1),
            on_secondary_container: from_argb!(0xff394b3a),
        },
        tertiary: Tertiary {
            color: from_argb!(0xff39656d),
            on_tertiary: from_argb!(0xffffffff),
            tertiary_container: from_argb!(0xffbdeaf4),
            on_tertiary_container: from_argb!(0xff1f4d55),
        },
        error: Error {
            color: from_argb!(0xffba1a1a),
            on_error: from_argb!(0xffffffff),
            error_container: from_argb!(0xffffdad6),
            on_error_container: from_argb!(0xff93000a),
        },
        surface: Surface {
            color: from_argb!(0xfff7fbf2),
            on_surface: from_argb!(0xff181d18),
            on_surface_variant: from_argb!(0xff414941),
            surface_container: SurfaceContainer {
                lowest: from_argb!(0xffffffff),
                low: from_argb!(0xfff1f5ed),
                base: from_argb!(0xffebefe7),
                high: from_argb!(0xffe5e9e1),
                highest: from_argb!(0xffe0e4dc),
            },
        },
        inverse: Inverse {
            inverse_surface: from_argb!(0xff2d322c),
            inverse_on_surface: from_argb!(0xffeef2ea),
            inverse_primary: from_argb!(0xff9bd4a1),
        },
        outline: Outline {
            color: from_argb!(0xff727970),
            variant: from_argb!(0xffc1c9be),
        },
        shadow: from_argb!(0xff000000),
        scrim: from_argb!(0x4d000000),
    };
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Primary {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub color: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_primary: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub primary_container: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_primary_container: Color,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Secondary {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub color: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_secondary: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub secondary_container: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_secondary_container: Color,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Tertiary {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub color: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_tertiary: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub tertiary_container: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_tertiary_container: Color,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Error {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub color: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_error: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub error_container: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_error_container: Color,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Surface {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub color: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_surface: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub on_surface_variant: Color,
    pub surface_container: SurfaceContainer,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SurfaceContainer {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub lowest: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub low: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub base: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub high: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub highest: Color,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Inverse {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub inverse_surface: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub inverse_on_surface: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub inverse_primary: Color,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "animate", derive(iced_anim::Animate))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Outline {
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub color: Color,
    #[cfg_attr(feature = "serde", serde(with = "color_serde"))]
    pub variant: Color,
}

#[cfg(feature = "serde")]
mod color_serde {
    use iced_widget::core::Color;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    use super::utils::{color_to_argb, parse_argb};

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Color, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(String::deserialize(deserializer)
            .map(|hex| parse_argb(&hex))?
            .unwrap_or(Color::TRANSPARENT))
    }

    pub fn serialize<S>(color: &Color, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        color_to_argb(*color).serialize(serializer)
    }
}