1#[cfg(xdg_portal)]
7pub mod portal;
8pub mod style;
9
10use ::iced::Alignment;
11use cosmic_config::CosmicConfigEntry;
12use cosmic_theme::{Component, LayeredTheme, Spacing, ThemeMode};
13use iced_runtime::{Appearance, DefaultStyle};
14use std::sync::{Arc, LazyLock, Mutex};
15pub use style::*;
16
17pub type CosmicColor = ::palette::rgb::Srgba;
18pub type CosmicComponent = cosmic_theme::Component;
19pub type CosmicTheme = cosmic_theme::Theme;
20
21pub static COSMIC_DARK: LazyLock<CosmicTheme> = LazyLock::new(CosmicTheme::dark_default);
22
23pub static COSMIC_HC_DARK: LazyLock<CosmicTheme> =
24 LazyLock::new(CosmicTheme::high_contrast_dark_default);
25
26pub static COSMIC_LIGHT: LazyLock<CosmicTheme> = LazyLock::new(CosmicTheme::light_default);
27
28pub static COSMIC_HC_LIGHT: LazyLock<CosmicTheme> =
29 LazyLock::new(CosmicTheme::high_contrast_light_default);
30
31pub static TRANSPARENT_COMPONENT: LazyLock<Component> = LazyLock::new(|| Component {
32 base: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
33 hover: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
34 pressed: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
35 selected: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
36 selected_text: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
37 focus: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
38 disabled: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
39 on: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
40 on_disabled: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
41 divider: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
42 border: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
43 disabled_border: CosmicColor::new(0.0, 0.0, 0.0, 0.0),
44});
45
46pub(crate) static THEME: Mutex<Theme> = Mutex::new(Theme {
47 theme_type: ThemeType::Dark,
48 layer: cosmic_theme::Layer::Background,
49 transparent: false,
50 list_item_position: None,
51});
52
53#[inline]
55#[allow(clippy::missing_panics_doc)]
56pub fn active() -> Theme {
57 THEME.lock().unwrap().clone()
58}
59
60#[inline]
62#[allow(clippy::missing_panics_doc)]
63pub fn active_type() -> ThemeType {
64 THEME.lock().unwrap().theme_type.clone()
65}
66
67#[inline]
69pub fn spacing() -> Spacing {
70 active().cosmic().spacing
71}
72
73#[inline]
75#[must_use]
76pub fn is_dark() -> bool {
77 active_type().is_dark()
78}
79
80#[inline]
82#[must_use]
83pub fn is_high_contrast() -> bool {
84 active_type().is_high_contrast()
85}
86
87pub fn system_dark() -> Theme {
88 let Ok(helper) = crate::cosmic_theme::Theme::dark_config() else {
89 return Theme::dark();
90 };
91
92 let t = crate::cosmic_theme::Theme::get_entry(&helper).unwrap_or_else(|(errors, theme)| {
93 for error in errors.into_iter().filter(cosmic_config::Error::is_err) {
94 tracing::error!(?error, "error loading system dark theme");
95 }
96 theme
97 });
98
99 Theme::system(Arc::new(t))
100}
101
102pub fn system_light() -> Theme {
103 let Ok(helper) = crate::cosmic_theme::Theme::light_config() else {
104 return Theme::light();
105 };
106
107 let t = crate::cosmic_theme::Theme::get_entry(&helper).unwrap_or_else(|(errors, theme)| {
108 for error in errors.into_iter().filter(cosmic_config::Error::is_err) {
109 tracing::error!(?error, "error loading system light theme");
110 }
111 theme
112 });
113
114 Theme::system(Arc::new(t))
115}
116
117pub fn system_preference() -> Theme {
119 let Ok(mode_config) = ThemeMode::config() else {
120 return Theme::dark();
121 };
122
123 let Ok(is_dark) = ThemeMode::is_dark(&mode_config) else {
124 return Theme::dark();
125 };
126 if is_dark {
127 system_dark()
128 } else {
129 system_light()
130 }
131}
132
133#[must_use]
134#[derive(Debug, Clone, PartialEq, Default)]
135pub enum ThemeType {
136 #[default]
137 Dark,
138 Light,
139 HighContrastDark,
140 HighContrastLight,
141 Custom(Arc<CosmicTheme>),
142 System {
143 prefer_dark: Option<bool>,
144 theme: Arc<CosmicTheme>,
145 },
146}
147
148impl ThemeType {
149 #[must_use]
151 #[inline]
152 pub fn is_dark(&self) -> bool {
153 match self {
154 Self::Dark | Self::HighContrastDark => true,
155 Self::Light | Self::HighContrastLight => false,
156 Self::Custom(theme) | Self::System { theme, .. } => theme.is_dark,
157 }
158 }
159
160 #[inline]
162 #[must_use]
163 pub fn is_high_contrast(&self) -> bool {
164 match self {
165 Self::Dark | Self::Light => false,
166 Self::HighContrastDark | Self::HighContrastLight => true,
167 Self::Custom(theme) | Self::System { theme, .. } => theme.is_high_contrast,
168 }
169 }
170
171 #[inline]
172 pub fn prefer_dark(&mut self, new_prefer_dark: Option<bool>) {
175 if let Self::System { prefer_dark, .. } = self {
176 *prefer_dark = new_prefer_dark;
177 }
178 }
179}
180
181#[must_use]
182#[derive(Debug, Clone, PartialEq, Default)]
183pub struct Theme {
184 pub theme_type: ThemeType,
185 pub layer: cosmic_theme::Layer,
186 pub transparent: bool,
187 pub list_item_position: Option<(Alignment, usize)>,
189}
190
191impl Theme {
192 #[inline]
193 pub fn cosmic(&self) -> &cosmic_theme::Theme {
194 match self.theme_type {
195 ThemeType::Dark => &COSMIC_DARK,
196 ThemeType::Light => &COSMIC_LIGHT,
197 ThemeType::HighContrastDark => &COSMIC_HC_DARK,
198 ThemeType::HighContrastLight => &COSMIC_HC_LIGHT,
199 ThemeType::Custom(ref t) | ThemeType::System { theme: ref t, .. } => t.as_ref(),
200 }
201 }
202
203 #[inline]
204 pub fn dark() -> Self {
205 Self {
206 theme_type: ThemeType::Dark,
207 ..Default::default()
208 }
209 }
210
211 #[inline]
212 pub fn light() -> Self {
213 Self {
214 theme_type: ThemeType::Light,
215 ..Default::default()
216 }
217 }
218
219 #[inline]
220 pub fn dark_hc() -> Self {
221 Self {
222 theme_type: ThemeType::HighContrastDark,
223 ..Default::default()
224 }
225 }
226
227 #[inline]
228 pub fn light_hc() -> Self {
229 Self {
230 theme_type: ThemeType::HighContrastLight,
231 ..Default::default()
232 }
233 }
234
235 #[inline]
236 pub fn custom(theme: Arc<CosmicTheme>) -> Self {
237 Self {
238 theme_type: ThemeType::Custom(theme),
239 ..Default::default()
240 }
241 }
242
243 #[inline]
244 pub fn system(theme: Arc<CosmicTheme>) -> Self {
245 Self {
246 theme_type: ThemeType::System {
247 theme,
248 prefer_dark: None,
249 },
250 ..Default::default()
251 }
252 }
253
254 #[inline]
255 pub fn current_container(&self) -> &cosmic_theme::Container {
258 match self.layer {
259 cosmic_theme::Layer::Background => self.cosmic().background(self.transparent),
260 cosmic_theme::Layer::Primary => self.cosmic().primary(self.transparent),
261 cosmic_theme::Layer::Secondary => self.cosmic().secondary(self.transparent),
262 }
263 }
264
265 #[inline]
266 pub fn set_theme(&mut self, theme: ThemeType) {
268 self.theme_type = theme;
269 }
270
271 #[inline]
272 pub fn with_list_item_position(&self, position: Option<(Alignment, usize)>) -> Self {
274 let mut new = self.clone();
275 new.list_item_position = position;
276 new
277 }
278}
279
280impl LayeredTheme for Theme {
281 #[inline]
282 fn set_layer(&mut self, layer: cosmic_theme::Layer) {
283 self.layer = layer;
284 }
285}
286
287impl DefaultStyle for Theme {
288 fn default_style(&self) -> Appearance {
289 let cosmic = self.cosmic();
290 Appearance {
291 icon_color: cosmic.on_bg_color().into(),
292 background_color: cosmic.bg_color().into(),
293 text_color: cosmic.on_bg_color().into(),
294 }
295 }
296}