iced_core/
theme.rs

1//! Use the built-in theme and styles.
2pub mod palette;
3
4pub use palette::Palette;
5
6use crate::Color;
7
8use std::borrow::Cow;
9use std::fmt;
10use std::sync::Arc;
11
12/// A built-in theme.
13#[derive(Debug, Clone, PartialEq)]
14pub enum Theme {
15    /// The built-in light variant.
16    Light,
17    /// The built-in dark variant.
18    Dark,
19    /// The built-in Dracula variant.
20    Dracula,
21    /// The built-in Nord variant.
22    Nord,
23    /// The built-in Solarized Light variant.
24    SolarizedLight,
25    /// The built-in Solarized Dark variant.
26    SolarizedDark,
27    /// The built-in Gruvbox Light variant.
28    GruvboxLight,
29    /// The built-in Gruvbox Dark variant.
30    GruvboxDark,
31    /// The built-in Catppuccin Latte variant.
32    CatppuccinLatte,
33    /// The built-in Catppuccin Frappé variant.
34    CatppuccinFrappe,
35    /// The built-in Catppuccin Macchiato variant.
36    CatppuccinMacchiato,
37    /// The built-in Catppuccin Mocha variant.
38    CatppuccinMocha,
39    /// The built-in Tokyo Night variant.
40    TokyoNight,
41    /// The built-in Tokyo Night Storm variant.
42    TokyoNightStorm,
43    /// The built-in Tokyo Night Light variant.
44    TokyoNightLight,
45    /// The built-in Kanagawa Wave variant.
46    KanagawaWave,
47    /// The built-in Kanagawa Dragon variant.
48    KanagawaDragon,
49    /// The built-in Kanagawa Lotus variant.
50    KanagawaLotus,
51    /// The built-in Moonfly variant.
52    Moonfly,
53    /// The built-in Nightfly variant.
54    Nightfly,
55    /// The built-in Oxocarbon variant.
56    Oxocarbon,
57    /// The built-in Ferra variant:
58    Ferra,
59    /// A [`Theme`] that uses a [`Custom`] palette.
60    Custom(Arc<Custom>),
61}
62
63impl Theme {
64    /// A list with all the defined themes.
65    pub const ALL: &'static [Self] = &[
66        Self::Light,
67        Self::Dark,
68        Self::Dracula,
69        Self::Nord,
70        Self::SolarizedLight,
71        Self::SolarizedDark,
72        Self::GruvboxLight,
73        Self::GruvboxDark,
74        Self::CatppuccinLatte,
75        Self::CatppuccinFrappe,
76        Self::CatppuccinMacchiato,
77        Self::CatppuccinMocha,
78        Self::TokyoNight,
79        Self::TokyoNightStorm,
80        Self::TokyoNightLight,
81        Self::KanagawaWave,
82        Self::KanagawaDragon,
83        Self::KanagawaLotus,
84        Self::Moonfly,
85        Self::Nightfly,
86        Self::Oxocarbon,
87        Self::Ferra,
88    ];
89
90    /// Creates a new custom [`Theme`] from the given [`Palette`].
91    pub fn custom(
92        name: impl Into<Cow<'static, str>>,
93        palette: Palette,
94    ) -> Self {
95        Self::custom_with_fn(name, palette, palette::Extended::generate)
96    }
97
98    /// Creates a new custom [`Theme`] from the given [`Palette`], with
99    /// a custom generator of a [`palette::Extended`].
100    pub fn custom_with_fn(
101        name: impl Into<Cow<'static, str>>,
102        palette: Palette,
103        generate: impl FnOnce(Palette) -> palette::Extended,
104    ) -> Self {
105        Self::Custom(Arc::new(Custom::with_fn(name, palette, generate)))
106    }
107
108    /// Returns the [`Palette`] of the [`Theme`].
109    pub fn palette(&self) -> Palette {
110        match self {
111            Self::Light => Palette::LIGHT,
112            Self::Dark => Palette::DARK,
113            Self::Dracula => Palette::DRACULA,
114            Self::Nord => Palette::NORD,
115            Self::SolarizedLight => Palette::SOLARIZED_LIGHT,
116            Self::SolarizedDark => Palette::SOLARIZED_DARK,
117            Self::GruvboxLight => Palette::GRUVBOX_LIGHT,
118            Self::GruvboxDark => Palette::GRUVBOX_DARK,
119            Self::CatppuccinLatte => Palette::CATPPUCCIN_LATTE,
120            Self::CatppuccinFrappe => Palette::CATPPUCCIN_FRAPPE,
121            Self::CatppuccinMacchiato => Palette::CATPPUCCIN_MACCHIATO,
122            Self::CatppuccinMocha => Palette::CATPPUCCIN_MOCHA,
123            Self::TokyoNight => Palette::TOKYO_NIGHT,
124            Self::TokyoNightStorm => Palette::TOKYO_NIGHT_STORM,
125            Self::TokyoNightLight => Palette::TOKYO_NIGHT_LIGHT,
126            Self::KanagawaWave => Palette::KANAGAWA_WAVE,
127            Self::KanagawaDragon => Palette::KANAGAWA_DRAGON,
128            Self::KanagawaLotus => Palette::KANAGAWA_LOTUS,
129            Self::Moonfly => Palette::MOONFLY,
130            Self::Nightfly => Palette::NIGHTFLY,
131            Self::Oxocarbon => Palette::OXOCARBON,
132            Self::Ferra => Palette::FERRA,
133            Self::Custom(custom) => custom.palette,
134        }
135    }
136
137    /// Returns the [`palette::Extended`] of the [`Theme`].
138    pub fn extended_palette(&self) -> &palette::Extended {
139        match self {
140            Self::Light => &palette::EXTENDED_LIGHT,
141            Self::Dark => &palette::EXTENDED_DARK,
142            Self::Dracula => &palette::EXTENDED_DRACULA,
143            Self::Nord => &palette::EXTENDED_NORD,
144            Self::SolarizedLight => &palette::EXTENDED_SOLARIZED_LIGHT,
145            Self::SolarizedDark => &palette::EXTENDED_SOLARIZED_DARK,
146            Self::GruvboxLight => &palette::EXTENDED_GRUVBOX_LIGHT,
147            Self::GruvboxDark => &palette::EXTENDED_GRUVBOX_DARK,
148            Self::CatppuccinLatte => &palette::EXTENDED_CATPPUCCIN_LATTE,
149            Self::CatppuccinFrappe => &palette::EXTENDED_CATPPUCCIN_FRAPPE,
150            Self::CatppuccinMacchiato => {
151                &palette::EXTENDED_CATPPUCCIN_MACCHIATO
152            }
153            Self::CatppuccinMocha => &palette::EXTENDED_CATPPUCCIN_MOCHA,
154            Self::TokyoNight => &palette::EXTENDED_TOKYO_NIGHT,
155            Self::TokyoNightStorm => &palette::EXTENDED_TOKYO_NIGHT_STORM,
156            Self::TokyoNightLight => &palette::EXTENDED_TOKYO_NIGHT_LIGHT,
157            Self::KanagawaWave => &palette::EXTENDED_KANAGAWA_WAVE,
158            Self::KanagawaDragon => &palette::EXTENDED_KANAGAWA_DRAGON,
159            Self::KanagawaLotus => &palette::EXTENDED_KANAGAWA_LOTUS,
160            Self::Moonfly => &palette::EXTENDED_MOONFLY,
161            Self::Nightfly => &palette::EXTENDED_NIGHTFLY,
162            Self::Oxocarbon => &palette::EXTENDED_OXOCARBON,
163            Self::Ferra => &palette::EXTENDED_FERRA,
164            Self::Custom(custom) => &custom.extended,
165        }
166    }
167}
168
169impl fmt::Display for Theme {
170    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171        f.write_str(self.name())
172    }
173}
174
175/// A [`Theme`] with a customized [`Palette`].
176#[derive(Debug, Clone, PartialEq)]
177pub struct Custom {
178    name: Cow<'static, str>,
179    palette: Palette,
180    extended: palette::Extended,
181}
182
183impl Custom {
184    /// Creates a [`Custom`] theme from the given [`Palette`].
185    pub fn new(name: String, palette: Palette) -> Self {
186        Self::with_fn(name, palette, palette::Extended::generate)
187    }
188
189    /// Creates a [`Custom`] theme from the given [`Palette`] with
190    /// a custom generator of a [`palette::Extended`].
191    pub fn with_fn(
192        name: impl Into<Cow<'static, str>>,
193        palette: Palette,
194        generate: impl FnOnce(Palette) -> palette::Extended,
195    ) -> Self {
196        Self {
197            name: name.into(),
198            palette,
199            extended: generate(palette),
200        }
201    }
202}
203
204impl fmt::Display for Custom {
205    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206        write!(f, "{}", self.name)
207    }
208}
209
210/// A theme mode, denoting the tone or brightness of a theme.
211#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
212pub enum Mode {
213    /// No specific tone.
214    #[default]
215    None,
216    /// A mode referring to themes with light tones.
217    Light,
218    /// A mode referring to themes with dark tones.
219    Dark,
220}
221
222/// The base style of a theme.
223#[derive(Debug, Clone, Copy, PartialEq)]
224pub struct Style {
225    /// The background [`Color`] of the application.
226    pub background_color: Color,
227
228    /// The default text [`Color`] of the application.
229    pub text_color: Color,
230}
231
232/// The default blank style of a theme.
233pub trait Base {
234    /// Returns the default theme for the preferred [`Mode`].
235    fn default(preference: Mode) -> Self;
236
237    /// Returns the [`Mode`] of the theme.
238    fn mode(&self) -> Mode;
239
240    /// Returns the default base [`Style`] of the theme.
241    fn base(&self) -> Style;
242
243    /// Returns the color [`Palette`] of the theme.
244    ///
245    /// This [`Palette`] may be used by the runtime for
246    /// debugging purposes; like displaying performance
247    /// metrics or devtools.
248    fn palette(&self) -> Option<Palette>;
249
250    /// Returns the unique name of the theme.
251    ///
252    /// This name may be used to efficiently detect theme
253    /// changes in some widgets.
254    fn name(&self) -> &str;
255}
256
257impl Base for Theme {
258    fn default(preference: Mode) -> Self {
259        use std::env;
260        use std::sync::OnceLock;
261
262        static SYSTEM: OnceLock<Option<Theme>> = OnceLock::new();
263
264        let system = SYSTEM.get_or_init(|| {
265            let name = env::var("ICED_THEME").ok()?;
266
267            Theme::ALL
268                .iter()
269                .find(|theme| theme.to_string() == name)
270                .cloned()
271        });
272
273        if let Some(system) = system {
274            return system.clone();
275        }
276
277        match preference {
278            Mode::None | Mode::Light => Self::Light,
279            Mode::Dark => Self::Dark,
280        }
281    }
282
283    fn mode(&self) -> Mode {
284        if self.extended_palette().is_dark {
285            Mode::Dark
286        } else {
287            Mode::Light
288        }
289    }
290
291    fn base(&self) -> Style {
292        default(self)
293    }
294
295    fn palette(&self) -> Option<Palette> {
296        Some(self.palette())
297    }
298
299    fn name(&self) -> &str {
300        match self {
301            Self::Light => "Light",
302            Self::Dark => "Dark",
303            Self::Dracula => "Dracula",
304            Self::Nord => "Nord",
305            Self::SolarizedLight => "Solarized Light",
306            Self::SolarizedDark => "Solarized Dark",
307            Self::GruvboxLight => "Gruvbox Light",
308            Self::GruvboxDark => "Gruvbox Dark",
309            Self::CatppuccinLatte => "Catppuccin Latte",
310            Self::CatppuccinFrappe => "Catppuccin Frappé",
311            Self::CatppuccinMacchiato => "Catppuccin Macchiato",
312            Self::CatppuccinMocha => "Catppuccin Mocha",
313            Self::TokyoNight => "Tokyo Night",
314            Self::TokyoNightStorm => "Tokyo Night Storm",
315            Self::TokyoNightLight => "Tokyo Night Light",
316            Self::KanagawaWave => "Kanagawa Wave",
317            Self::KanagawaDragon => "Kanagawa Dragon",
318            Self::KanagawaLotus => "Kanagawa Lotus",
319            Self::Moonfly => "Moonfly",
320            Self::Nightfly => "Nightfly",
321            Self::Oxocarbon => "Oxocarbon",
322            Self::Ferra => "Ferra",
323            Self::Custom(custom) => &custom.name,
324        }
325    }
326}
327
328/// The default [`Style`] of a built-in [`Theme`].
329pub fn default(theme: &Theme) -> Style {
330    let palette = theme.extended_palette();
331
332    Style {
333        background_color: palette.background.base.color,
334        text_color: palette.background.base.text,
335    }
336}