Skip to main content

cosmic/
icon_theme.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Select the preferred icon theme.
5
6use std::borrow::Cow;
7use std::sync::Mutex;
8
9pub const COSMIC: &str = crate::cosmic_theme::DEFAULT_ICON_THEME;
10
11static OVERRIDE: Mutex<Option<Cow<'static, str>>> = Mutex::new(None);
12
13/// This is the icon theme configured by the active theme unless the
14/// application has set an override with [`set_default`].
15#[must_use]
16#[allow(clippy::missing_panics_doc)]
17#[inline]
18pub fn default() -> String {
19    let override_theme = OVERRIDE.lock().unwrap();
20    if let Some(theme) = override_theme.as_ref() {
21        return theme.to_string();
22    }
23    drop(override_theme);
24
25    let theme = crate::theme::active().cosmic().icon_theme.clone();
26    if theme.is_empty() {
27        COSMIC.to_string()
28    } else {
29        theme
30    }
31}
32
33/// Override the icon theme selected by the active theme.
34///
35/// Passing an empty string clears the override, returning control to the icon
36/// theme configured by the active theme.
37#[allow(clippy::missing_panics_doc)]
38#[cold]
39pub fn set_default(name: impl Into<Cow<'static, str>>) {
40    let name = name.into();
41    *OVERRIDE.lock().unwrap() = if name.is_empty() { None } else { Some(name) };
42}
43
44/// Clear the application icon theme override.
45#[allow(clippy::missing_panics_doc)]
46#[cold]
47pub fn reset_default() {
48    *OVERRIDE.lock().unwrap() = None;
49}