libcosmic/src/theme/mod.rs

243 lines
6.7 KiB
Rust
Raw Normal View History

// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Contains the [`Theme`] type and its widget stylesheet implementations.
2023-08-15 10:51:59 +02:00
pub mod style;
use cosmic_theme::ThemeMode;
pub use style::*;
2023-06-12 12:08:14 -04:00
use cosmic_config::config_subscription;
use cosmic_config::CosmicConfigEntry;
2022-11-17 20:49:20 -05:00
use cosmic_theme::Component;
2023-02-27 17:42:17 -05:00
use cosmic_theme::LayeredTheme;
2023-06-12 12:08:14 -04:00
use iced_futures::Subscription;
use std::cell::RefCell;
use std::sync::Arc;
#[cfg(feature = "dbus-config")]
use cosmic_config::dbus;
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
pub type CosmicColor = ::palette::rgb::Srgba;
pub type CosmicComponent = cosmic_theme::Component;
pub type CosmicTheme = cosmic_theme::Theme;
lazy_static::lazy_static! {
2023-08-03 19:30:08 -04:00
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 {
2022-11-17 20:49:20 -05:00
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),
2023-02-27 17:42:17 -05:00
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),
2022-11-17 20:49:20 -05:00
};
}
thread_local! {
pub(crate) static THEME: RefCell<Theme> = RefCell::new(Theme { theme_type: ThemeType::Dark, layer: cosmic_theme::Layer::Background });
}
/// Currently-defined theme.
pub fn active() -> Theme {
THEME.with(|theme| theme.borrow().clone())
}
/// Currently-defined theme type.
pub fn active_type() -> ThemeType {
THEME.with(|theme| theme.borrow().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(),
2024-02-26 18:41:58 -05:00
crate::cosmic_theme::Theme::VERSION,
)
2024-01-18 19:01:11 -05:00
.map(|res| {
for err in res.errors {
tracing::error!("{:?}", err);
}
Theme::system(Arc::new(res.config))
})
}
/// 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();
};
let helper = if is_dark {
crate::cosmic_theme::Theme::dark_config()
} else {
crate::cosmic_theme::Theme::light_config()
};
let Ok(helper) = helper 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))
}
#[must_use]
2023-06-09 17:13:01 -04:00
#[derive(Debug, Clone, PartialEq, Default)]
2023-02-27 19:56:53 -05:00
pub enum ThemeType {
#[default]
Dark,
Light,
HighContrastDark,
HighContrastLight,
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
Custom(Arc<CosmicTheme>),
System(Arc<CosmicTheme>),
2023-02-27 17:42:17 -05:00
}
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,
}
}
}
#[must_use]
2023-06-09 17:13:01 -04:00
#[derive(Debug, Clone, PartialEq, Default)]
2023-02-27 19:56:53 -05:00
pub struct Theme {
pub theme_type: ThemeType,
pub layer: cosmic_theme::Layer,
}
impl Theme {
pub fn cosmic(&self) -> &cosmic_theme::Theme {
2023-02-27 19:56:53 -05:00
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(ref t) => t.as_ref(),
2023-02-27 19:56:53 -05:00
}
2023-02-27 17:42:17 -05:00
}
2023-02-27 19:56:53 -05:00
pub fn dark() -> Self {
Self {
theme_type: ThemeType::Dark,
..Default::default()
}
2023-02-27 17:42:17 -05:00
}
2023-02-27 19:56:53 -05:00
pub fn light() -> Self {
Self {
theme_type: ThemeType::Light,
..Default::default()
}
2023-02-27 17:42:17 -05:00
}
2023-02-27 19:56:53 -05:00
pub fn dark_hc() -> Self {
Self {
theme_type: ThemeType::HighContrastDark,
..Default::default()
}
2023-02-27 17:42:17 -05:00
}
2023-02-27 19:56:53 -05:00
pub fn light_hc() -> Self {
2023-02-27 17:42:17 -05:00
Self {
2023-02-27 19:56:53 -05:00
theme_type: ThemeType::HighContrastLight,
..Default::default()
2023-02-27 17:42:17 -05:00
}
}
Cosmic advanced text (#103) * wip: update to use cosmic-advanced-text * use cosmic-advanced-text branch of iced * fix: line height and spacing for segmented button and update to get svg fix * fix: spin button styling & spacing * update iced to fix segmented button border radius * feat: example improvements * feat: helper for loading fonts * feat: add focus style to button * fix: slider height and iced fixed * feat: hash icon width and height * cleanup * update ci * refactor: always use lazy feature of iced * update iced * update iced * cleanup & update iced * update iced: new slider & tiny-skia quad updates * update iced: fixes for tiny-skia quad rendering with edge case border radius * re-export iced_runtime & iced_widget * merge master * udpate iced * update iced * update iced * update iced * fix: make rectangle_tracker subscription only return update if there is some * feat: derive macro for loading a cosmic-config * feat (cosmic-config): iced subscription * fix (example): update to rectangle tracker subscription * fix (cosmic-config) * refactor(cosmic-config-derive): add support for types with generic parameters * fix (cosmic-config): feature gate updates for subscription helpers * feat: support for custom & system themes + move cosmic-theme to libcosmic * feat: sorta hacky way of creating header bars for libcosmic + update iced to get support for resizable windows in iced-sctk * update iced * update and reexport sctk * fix: applet border radius * feat (cosmic-theme): add id and name methods * fix(cosmic-theme): reexport palette from cosmic-theme * fix(cosmic-config-derive): allow use with reexported cosmic-config * feat: update iced with fix and refactor applet env vars * update iced
2023-05-30 12:03:15 -04:00
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),
..Default::default()
}
}
2023-02-27 19:56:53 -05:00
/// get current container
/// can be used in a component that is intended to be a child of a `CosmicContainer`
2023-02-27 17:42:17 -05:00
#[must_use]
pub fn current_container(&self) -> &cosmic_theme::Container {
2023-02-27 19:56:53 -05:00
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;
}
}
2023-02-27 17:42:17 -05:00
impl LayeredTheme for Theme {
fn set_layer(&mut self, layer: cosmic_theme::Layer) {
2023-02-27 19:56:53 -05:00
self.layer = layer;
}
}