cosmic/theme/
mod.rs

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
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

//! Contains the [`Theme`] type and its widget stylesheet implementations.

#[cfg(feature = "xdg-portal")]
pub mod portal;
pub mod style;
use cosmic_theme::ThemeMode;
pub use style::*;

use cosmic_config::config_subscription;
use cosmic_config::CosmicConfigEntry;
use cosmic_theme::Component;
use cosmic_theme::LayeredTheme;
use iced_futures::Subscription;
use iced_runtime::{Appearance, DefaultStyle};

use std::sync::{Arc, Mutex};

#[cfg(feature = "dbus-config")]
use cosmic_config::dbus;

pub type CosmicColor = ::palette::rgb::Srgba;
pub type CosmicComponent = cosmic_theme::Component;
pub type CosmicTheme = cosmic_theme::Theme;

lazy_static::lazy_static! {
    pub static ref COSMIC_DARK: CosmicTheme = CosmicTheme::dark_default();
    pub static ref COSMIC_HC_DARK: CosmicTheme = CosmicTheme::high_contrast_dark_default();
    pub static ref COSMIC_LIGHT: CosmicTheme = CosmicTheme::light_default();
    pub static ref COSMIC_HC_LIGHT: CosmicTheme = CosmicTheme::high_contrast_light_default();
    pub static ref TRANSPARENT_COMPONENT: Component = Component {
        base: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        hover: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        pressed: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        selected: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        selected_text: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        focus: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        disabled: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        on: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        on_disabled: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        divider: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        border: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
        disabled_border: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
    };
}

pub(crate) static THEME: Mutex<Theme> = Mutex::new(Theme {
    theme_type: ThemeType::Dark,
    layer: cosmic_theme::Layer::Background,
});

/// Currently-defined theme.
#[allow(clippy::missing_panics_doc)]
pub fn active() -> Theme {
    THEME.lock().unwrap().clone()
}

/// Currently-defined theme type.
#[allow(clippy::missing_panics_doc)]
pub fn active_type() -> ThemeType {
    THEME.lock().unwrap().theme_type.clone()
}

/// Whether the active theme has a dark preference.
#[must_use]
pub fn is_dark() -> bool {
    active_type().is_dark()
}

/// Whether the active theme is high contrast.
#[must_use]
pub fn is_high_contrast() -> bool {
    active_type().is_high_contrast()
}

/// Watches for changes to the system's theme preference.
pub fn subscription(is_dark: bool) -> Subscription<crate::theme::Theme> {
    config_subscription::<_, crate::cosmic_theme::Theme>(
        (
            std::any::TypeId::of::<crate::cosmic_theme::Theme>(),
            is_dark,
        ),
        if is_dark {
            cosmic_theme::DARK_THEME_ID
        } else {
            cosmic_theme::LIGHT_THEME_ID
        }
        .into(),
        crate::cosmic_theme::Theme::VERSION,
    )
    .map(|res| {
        for err in res.errors {
            tracing::error!("{:?}", err);
        }

        Theme::system(Arc::new(res.config))
    })
}

pub fn system_dark() -> Theme {
    let Ok(helper) = crate::cosmic_theme::Theme::dark_config() else {
        return Theme::dark();
    };

    let t = crate::cosmic_theme::Theme::get_entry(&helper).unwrap_or_else(|(errors, theme)| {
        for err in errors {
            tracing::error!("{:?}", err);
        }
        theme
    });

    Theme::system(Arc::new(t))
}

pub fn system_light() -> Theme {
    let Ok(helper) = crate::cosmic_theme::Theme::light_config() else {
        return Theme::light();
    };

    let t = crate::cosmic_theme::Theme::get_entry(&helper).unwrap_or_else(|(errors, theme)| {
        for err in errors {
            tracing::error!("{:?}", err);
        }
        theme
    });

    Theme::system(Arc::new(t))
}

/// Loads the preferred system theme from `cosmic-config`.
pub fn system_preference() -> Theme {
    let Ok(mode_config) = ThemeMode::config() else {
        return Theme::dark();
    };

    let Ok(is_dark) = ThemeMode::is_dark(&mode_config) else {
        return Theme::dark();
    };
    if is_dark {
        system_dark()
    } else {
        system_light()
    }
}

#[must_use]
#[derive(Debug, Clone, PartialEq, Default)]
pub enum ThemeType {
    #[default]
    Dark,
    Light,
    HighContrastDark,
    HighContrastLight,
    Custom(Arc<CosmicTheme>),
    System {
        prefer_dark: Option<bool>,
        theme: Arc<CosmicTheme>,
    },
}

impl ThemeType {
    /// Whether the theme has a dark preference.
    #[must_use]
    pub fn is_dark(&self) -> bool {
        match self {
            Self::Dark | Self::HighContrastDark => true,
            Self::Light | Self::HighContrastLight => false,
            Self::Custom(theme) | Self::System { theme, .. } => theme.is_dark,
        }
    }

    /// Whether the theme has a high contrast.
    #[must_use]
    pub fn is_high_contrast(&self) -> bool {
        match self {
            Self::Dark | Self::Light => false,
            Self::HighContrastDark | Self::HighContrastLight => true,
            Self::Custom(theme) | Self::System { theme, .. } => theme.is_high_contrast,
        }
    }

    /// Prefer dark or light theme.
    /// If `None`, the system preference is used.
    pub fn prefer_dark(&mut self, new_prefer_dark: Option<bool>) {
        if let Self::System { prefer_dark, .. } = self {
            *prefer_dark = new_prefer_dark;
        }
    }
}

#[must_use]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Theme {
    pub theme_type: ThemeType,
    pub layer: cosmic_theme::Layer,
}

impl Theme {
    pub fn cosmic(&self) -> &cosmic_theme::Theme {
        match self.theme_type {
            ThemeType::Dark => &COSMIC_DARK,
            ThemeType::Light => &COSMIC_LIGHT,
            ThemeType::HighContrastDark => &COSMIC_HC_DARK,
            ThemeType::HighContrastLight => &COSMIC_HC_LIGHT,
            ThemeType::Custom(ref t) | ThemeType::System { theme: ref t, .. } => t.as_ref(),
        }
    }

    pub fn dark() -> Self {
        Self {
            theme_type: ThemeType::Dark,
            ..Default::default()
        }
    }

    pub fn light() -> Self {
        Self {
            theme_type: ThemeType::Light,
            ..Default::default()
        }
    }

    pub fn dark_hc() -> Self {
        Self {
            theme_type: ThemeType::HighContrastDark,
            ..Default::default()
        }
    }

    pub fn light_hc() -> Self {
        Self {
            theme_type: ThemeType::HighContrastLight,
            ..Default::default()
        }
    }

    pub fn custom(theme: Arc<CosmicTheme>) -> Self {
        Self {
            theme_type: ThemeType::Custom(theme),
            ..Default::default()
        }
    }

    pub fn system(theme: Arc<CosmicTheme>) -> Self {
        Self {
            theme_type: ThemeType::System {
                theme,
                prefer_dark: None,
            },
            ..Default::default()
        }
    }

    /// get current container
    /// can be used in a component that is intended to be a child of a `CosmicContainer`
    pub fn current_container(&self) -> &cosmic_theme::Container {
        match self.layer {
            cosmic_theme::Layer::Background => &self.cosmic().background,
            cosmic_theme::Layer::Primary => &self.cosmic().primary,
            cosmic_theme::Layer::Secondary => &self.cosmic().secondary,
        }
    }

    /// set the theme
    pub fn set_theme(&mut self, theme: ThemeType) {
        self.theme_type = theme;
    }
}

impl LayeredTheme for Theme {
    fn set_layer(&mut self, layer: cosmic_theme::Layer) {
        self.layer = layer;
    }
}

impl DefaultStyle for Theme {
    fn default_style(&self) -> Appearance {
        let cosmic = self.cosmic();
        Appearance {
            icon_color: cosmic.bg_color().into(),
            background_color: cosmic.bg_color().into(),
            text_color: cosmic.on_bg_color().into(),
        }
    }
}