2023-05-30 12:03:15 -04:00
|
|
|
use crate::{
|
2023-08-03 19:30:08 -04:00
|
|
|
steps::steps, Component, Container, CornerRadii, CosmicPalette, CosmicPaletteInner, Spacing,
|
|
|
|
|
DARK_PALETTE, LIGHT_PALETTE, NAME, THEME_DIR,
|
2023-05-30 12:03:15 -04:00
|
|
|
};
|
|
|
|
|
use anyhow::Context;
|
|
|
|
|
use cosmic_config::{Config, ConfigGet, ConfigSet, CosmicConfigEntry};
|
|
|
|
|
use directories::{BaseDirsExt, ProjectDirsExt};
|
2023-08-03 19:30:08 -04:00
|
|
|
use palette::{FromColor, Oklcha, Srgb, Srgba};
|
2023-05-30 12:03:15 -04:00
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
|
|
|
use std::{
|
|
|
|
|
fmt,
|
|
|
|
|
fs::File,
|
|
|
|
|
io::Write,
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
|
|
|
|
|
/// Theme layer type
|
|
|
|
|
pub enum Layer {
|
|
|
|
|
/// Background layer
|
|
|
|
|
#[default]
|
|
|
|
|
Background,
|
|
|
|
|
/// Primary Layer
|
|
|
|
|
Primary,
|
|
|
|
|
/// Secondary Layer
|
|
|
|
|
Secondary,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Cosmic Theme data structure with all colors and its name
|
2023-06-09 17:13:01 -04:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
2023-05-30 12:03:15 -04:00
|
|
|
pub struct Theme<C> {
|
|
|
|
|
/// name of the theme
|
|
|
|
|
pub name: String,
|
|
|
|
|
/// background element colors
|
|
|
|
|
pub background: Container<C>,
|
|
|
|
|
/// primary element colors
|
|
|
|
|
pub primary: Container<C>,
|
|
|
|
|
/// secondary element colors
|
|
|
|
|
pub secondary: Container<C>,
|
|
|
|
|
/// accent element colors
|
|
|
|
|
pub accent: Component<C>,
|
|
|
|
|
/// suggested element colors
|
|
|
|
|
pub success: Component<C>,
|
|
|
|
|
/// destructive element colors
|
|
|
|
|
pub destructive: Component<C>,
|
|
|
|
|
/// warning element colors
|
|
|
|
|
pub warning: Component<C>,
|
|
|
|
|
/// palette
|
|
|
|
|
pub palette: CosmicPaletteInner<C>,
|
2023-08-03 16:23:24 -04:00
|
|
|
/// spacing
|
|
|
|
|
pub spacing: Spacing,
|
|
|
|
|
/// corner radii
|
|
|
|
|
pub corner_radii: CornerRadii,
|
2023-05-30 12:03:15 -04:00
|
|
|
/// is dark
|
|
|
|
|
pub is_dark: bool,
|
|
|
|
|
/// is high contrast
|
|
|
|
|
pub is_high_contrast: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 19:30:08 -04:00
|
|
|
impl CosmicConfigEntry for Theme<Srgba> {
|
2023-05-30 12:03:15 -04:00
|
|
|
fn write_entry(&self, config: &Config) -> Result<(), cosmic_config::Error> {
|
|
|
|
|
let self_ = self.clone();
|
|
|
|
|
// TODO do as transaction
|
|
|
|
|
let tx = config.transaction();
|
|
|
|
|
|
|
|
|
|
tx.set("name", self_.name)?;
|
|
|
|
|
tx.set("background", self_.background)?;
|
|
|
|
|
tx.set("primary", self_.primary)?;
|
|
|
|
|
tx.set("secondary", self_.secondary)?;
|
|
|
|
|
tx.set("accent", self_.accent)?;
|
|
|
|
|
tx.set("success", self_.success)?;
|
|
|
|
|
tx.set("destructive", self_.destructive)?;
|
|
|
|
|
tx.set("warning", self_.warning)?;
|
|
|
|
|
tx.set("palette", self_.palette)?;
|
|
|
|
|
tx.set("is_dark", self_.is_dark)?;
|
|
|
|
|
tx.set("is_high_contrast", self_.is_high_contrast)?;
|
|
|
|
|
|
|
|
|
|
tx.commit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_entry(config: &Config) -> Result<Self, (Vec<cosmic_config::Error>, Self)> {
|
|
|
|
|
let mut default = Self::default();
|
|
|
|
|
let mut errors = Vec::new();
|
|
|
|
|
|
|
|
|
|
match config.get::<String>("name") {
|
|
|
|
|
Ok(name) => default.name = name,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<Container<Srgba>>("background") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(background) => default.background = background,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<Container<Srgba>>("primary") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(primary) => default.primary = primary,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<Container<Srgba>>("secondary") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(secondary) => default.secondary = secondary,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<Component<Srgba>>("accent") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(accent) => default.accent = accent,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<Component<Srgba>>("success") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(success) => default.success = success,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<Component<Srgba>>("destructive") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(destructive) => default.destructive = destructive,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<Component<Srgba>>("warning") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(warning) => default.warning = warning,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
match config.get::<CosmicPaletteInner<Srgba>>("palette") {
|
2023-05-30 12:03:15 -04:00
|
|
|
Ok(palette) => default.palette = palette,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
|
|
|
|
match config.get::<bool>("is_dark") {
|
|
|
|
|
Ok(is_dark) => default.is_dark = is_dark,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
|
|
|
|
match config.get::<bool>("is_high_contrast") {
|
|
|
|
|
Ok(is_high_contrast) => default.is_high_contrast = is_high_contrast,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-08-03 16:23:24 -04:00
|
|
|
match config.get::<Spacing>("spacing") {
|
|
|
|
|
Ok(spacing) => default.spacing = spacing,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
|
|
|
|
match config.get::<CornerRadii>("corner_radii") {
|
|
|
|
|
Ok(corner_radii) => default.corner_radii = corner_radii,
|
|
|
|
|
Err(e) => errors.push(e),
|
|
|
|
|
}
|
2023-05-30 12:03:15 -04:00
|
|
|
|
|
|
|
|
if errors.is_empty() {
|
|
|
|
|
Ok(default)
|
|
|
|
|
} else {
|
|
|
|
|
Err((errors, default))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Theme<Srgba> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::dark_default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Trait for layered themes
|
|
|
|
|
pub trait LayeredTheme {
|
|
|
|
|
/// Set the layer of the theme
|
|
|
|
|
fn set_layer(&mut self, layer: Layer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<C> Theme<C> {
|
|
|
|
|
/// version of the theme
|
2023-06-12 12:08:14 -04:00
|
|
|
pub fn version() -> u64 {
|
2023-05-30 12:03:15 -04:00
|
|
|
1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// id of the theme
|
|
|
|
|
pub fn id() -> &'static str {
|
|
|
|
|
NAME
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<C> Theme<C>
|
|
|
|
|
where
|
|
|
|
|
C: Clone + fmt::Debug + Default + Into<Srgba> + From<Srgba> + Serialize + DeserializeOwned,
|
|
|
|
|
{
|
|
|
|
|
/// Convert the theme to a high-contrast variant
|
|
|
|
|
pub fn to_high_contrast(&self) -> Self {
|
|
|
|
|
todo!();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// save the theme to the theme directory
|
|
|
|
|
pub fn save(&self) -> anyhow::Result<()> {
|
|
|
|
|
let ron_path: PathBuf = [NAME, THEME_DIR].iter().collect();
|
|
|
|
|
let ron_dirs = directories::ProjectDirs::from_path(ron_path)
|
|
|
|
|
.context("Failed to get project directories.")?;
|
|
|
|
|
let ron_name = format!("{}.ron", &self.name);
|
|
|
|
|
|
|
|
|
|
if let Ok(p) = ron_dirs.place_config_file(ron_name) {
|
|
|
|
|
let mut f = File::create(p)?;
|
|
|
|
|
f.write_all(ron::ser::to_string_pretty(self, Default::default())?.as_bytes())?;
|
|
|
|
|
} else {
|
|
|
|
|
anyhow::bail!("Failed to write RON theme.");
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// init the theme directory
|
|
|
|
|
pub fn init() -> anyhow::Result<PathBuf> {
|
|
|
|
|
let ron_path: PathBuf = [NAME, THEME_DIR].iter().collect();
|
|
|
|
|
let base_dirs = directories::BaseDirs::new().context("Failed to get base directories.")?;
|
|
|
|
|
Ok(base_dirs.create_config_directory(ron_path)?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// load a theme by name
|
|
|
|
|
pub fn load_from_name(name: &str) -> anyhow::Result<Self> {
|
|
|
|
|
let ron_path: PathBuf = [NAME, THEME_DIR].iter().collect();
|
|
|
|
|
let ron_dirs = directories::ProjectDirs::from_path(ron_path)
|
|
|
|
|
.context("Failed to get project directories.")?;
|
|
|
|
|
|
|
|
|
|
let ron_name = format!("{}.ron", name);
|
|
|
|
|
if let Some(p) = ron_dirs.find_config_file(ron_name) {
|
|
|
|
|
let f = File::open(p)?;
|
|
|
|
|
Ok(ron::de::from_reader(f)?)
|
|
|
|
|
} else {
|
|
|
|
|
anyhow::bail!("Failed to write RON theme.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// load a theme by path
|
|
|
|
|
pub fn load(p: &dyn AsRef<Path>) -> anyhow::Result<Self> {
|
|
|
|
|
let f = File::open(p)?;
|
|
|
|
|
Ok(ron::de::from_reader(f)?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO convenient getter functions for each named color variable
|
|
|
|
|
/// get @accent_color
|
|
|
|
|
pub fn accent_color(&self) -> Srgba {
|
|
|
|
|
self.accent.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @success_color
|
|
|
|
|
pub fn success_color(&self) -> Srgba {
|
|
|
|
|
self.success.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @destructive_color
|
|
|
|
|
pub fn destructive_color(&self) -> Srgba {
|
|
|
|
|
self.destructive.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @warning_color
|
|
|
|
|
pub fn warning_color(&self) -> Srgba {
|
|
|
|
|
self.warning.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Containers
|
|
|
|
|
/// get @bg_color
|
|
|
|
|
pub fn bg_color(&self) -> Srgba {
|
|
|
|
|
self.background.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @bg_component_color
|
|
|
|
|
pub fn bg_component_color(&self) -> Srgba {
|
|
|
|
|
self.background.component.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @primary_container_color
|
|
|
|
|
pub fn primary_container_color(&self) -> Srgba {
|
|
|
|
|
self.primary.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @primary_component_color
|
|
|
|
|
pub fn primary_component_color(&self) -> Srgba {
|
|
|
|
|
self.primary.component.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @secondary_container_color
|
|
|
|
|
pub fn secondary_container_color(&self) -> Srgba {
|
|
|
|
|
self.secondary.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @secondary_component_color
|
|
|
|
|
pub fn secondary_component_color(&self) -> Srgba {
|
|
|
|
|
self.secondary.component.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Text
|
|
|
|
|
/// get @on_bg_color
|
|
|
|
|
pub fn on_bg_color(&self) -> Srgba {
|
|
|
|
|
self.background.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_bg_component_color
|
|
|
|
|
pub fn on_bg_component_color(&self) -> Srgba {
|
|
|
|
|
self.background.component.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_primary_color
|
|
|
|
|
pub fn on_primary_container_color(&self) -> Srgba {
|
|
|
|
|
self.primary.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_primary_component_color
|
|
|
|
|
pub fn on_primary_component_color(&self) -> Srgba {
|
|
|
|
|
self.primary.component.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_secondary_color
|
|
|
|
|
pub fn on_secondary_container_color(&self) -> Srgba {
|
|
|
|
|
self.secondary.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_secondary_component_color
|
|
|
|
|
pub fn on_secondary_component_color(&self) -> Srgba {
|
|
|
|
|
self.secondary.component.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @accent_text_color
|
|
|
|
|
pub fn accent_text_color(&self) -> Srgba {
|
|
|
|
|
self.accent.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @success_text_color
|
|
|
|
|
pub fn success_text_color(&self) -> Srgba {
|
|
|
|
|
self.success.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @warning_text_color
|
|
|
|
|
pub fn warning_text_color(&self) -> Srgba {
|
|
|
|
|
self.warning.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @destructive_text_color
|
|
|
|
|
pub fn destructive_text_color(&self) -> Srgba {
|
|
|
|
|
self.destructive.base.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_accent_color
|
|
|
|
|
pub fn on_accent_color(&self) -> Srgba {
|
|
|
|
|
self.accent.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_success_color
|
|
|
|
|
pub fn on_success_color(&self) -> Srgba {
|
|
|
|
|
self.success.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @oon_warning_color
|
|
|
|
|
pub fn on_warning_color(&self) -> Srgba {
|
|
|
|
|
self.warning.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @on_destructive_color
|
|
|
|
|
pub fn on_destructive_color(&self) -> Srgba {
|
|
|
|
|
self.destructive.on.clone().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Borders and Dividers
|
|
|
|
|
/// get @bg_divider
|
|
|
|
|
pub fn bg_divider(&self) -> Srgba {
|
|
|
|
|
self.background.divider.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @bg_component_divider
|
|
|
|
|
pub fn bg_component_divider(&self) -> Srgba {
|
|
|
|
|
self.background.component.divider.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @primary_container_divider
|
|
|
|
|
pub fn primary_container_divider(&self) -> Srgba {
|
|
|
|
|
self.primary.divider.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @primary_component_divider
|
|
|
|
|
pub fn primary_component_divider(&self) -> Srgba {
|
|
|
|
|
self.primary.component.divider.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @secondary_container_divider
|
|
|
|
|
pub fn secondary_container_divider(&self) -> Srgba {
|
|
|
|
|
self.secondary.divider.clone().into()
|
|
|
|
|
}
|
|
|
|
|
/// get @secondary_component_divider
|
|
|
|
|
pub fn secondary_component_divider(&self) -> Srgba {
|
|
|
|
|
self.secondary.component.divider.clone().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// get @window_header_bg
|
|
|
|
|
pub fn window_header_bg(&self) -> Srgba {
|
|
|
|
|
self.background.base.clone().into()
|
|
|
|
|
}
|
2023-08-03 16:23:24 -04:00
|
|
|
|
|
|
|
|
/// get @space_none
|
|
|
|
|
pub fn space_none(&self) -> u16 {
|
|
|
|
|
self.spacing.space_none
|
|
|
|
|
}
|
|
|
|
|
/// get @space_xxxs
|
|
|
|
|
pub fn space_xxxs(&self) -> u16 {
|
|
|
|
|
self.spacing.space_xxxs
|
|
|
|
|
}
|
|
|
|
|
/// get @space_xxs
|
|
|
|
|
pub fn space_xxs(&self) -> u16 {
|
|
|
|
|
self.spacing.space_xxs
|
|
|
|
|
}
|
|
|
|
|
/// get @space_xs
|
|
|
|
|
pub fn space_xs(&self) -> u16 {
|
|
|
|
|
self.spacing.space_xs
|
|
|
|
|
}
|
|
|
|
|
/// get @space_s
|
|
|
|
|
pub fn space_s(&self) -> u16 {
|
|
|
|
|
self.spacing.space_s
|
|
|
|
|
}
|
|
|
|
|
/// get @space_m
|
|
|
|
|
pub fn space_m(&self) -> u16 {
|
|
|
|
|
self.spacing.space_m
|
|
|
|
|
}
|
|
|
|
|
/// get @space_l
|
|
|
|
|
pub fn space_l(&self) -> u16 {
|
|
|
|
|
self.spacing.space_l
|
|
|
|
|
}
|
|
|
|
|
/// get @space_xl
|
|
|
|
|
pub fn space_xl(&self) -> u16 {
|
|
|
|
|
self.spacing.space_xl
|
|
|
|
|
}
|
|
|
|
|
/// get @space_xxl
|
|
|
|
|
pub fn space_xxl(&self) -> u16 {
|
|
|
|
|
self.spacing.space_xxl
|
|
|
|
|
}
|
|
|
|
|
/// get @space_xxxl
|
|
|
|
|
pub fn space_xxxl(&self) -> u16 {
|
|
|
|
|
self.spacing.space_xxxl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// get @radius_0
|
|
|
|
|
pub fn radius_0(&self) -> [u16; 4] {
|
|
|
|
|
self.corner_radii.radius_0
|
|
|
|
|
}
|
|
|
|
|
/// get @radius_xs
|
|
|
|
|
pub fn radius_xs(&self) -> [u16; 4] {
|
|
|
|
|
self.corner_radii.radius_xs
|
|
|
|
|
}
|
|
|
|
|
/// get @radius_s
|
|
|
|
|
pub fn radius_s(&self) -> [u16; 4] {
|
|
|
|
|
self.corner_radii.radius_s
|
|
|
|
|
}
|
|
|
|
|
/// get @radius_m
|
|
|
|
|
pub fn radius_m(&self) -> [u16; 4] {
|
|
|
|
|
self.corner_radii.radius_m
|
|
|
|
|
}
|
|
|
|
|
/// get @radius_l
|
|
|
|
|
pub fn radius_l(&self) -> [u16; 4] {
|
|
|
|
|
self.corner_radii.radius_l
|
|
|
|
|
}
|
|
|
|
|
/// get @radius_xl
|
|
|
|
|
pub fn radius_xl(&self) -> [u16; 4] {
|
|
|
|
|
self.corner_radii.radius_xl
|
|
|
|
|
}
|
2023-05-30 12:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-03 19:30:08 -04:00
|
|
|
impl Theme<Srgba> {
|
2023-05-30 12:03:15 -04:00
|
|
|
/// get the built in light theme
|
|
|
|
|
pub fn light_default() -> Self {
|
|
|
|
|
LIGHT_PALETTE.clone().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// get the built in dark theme
|
|
|
|
|
pub fn dark_default() -> Self {
|
|
|
|
|
DARK_PALETTE.clone().into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// get the built in high contrast dark theme
|
|
|
|
|
pub fn high_contrast_dark_default() -> Self {
|
|
|
|
|
CosmicPalette::HighContrastDark(DARK_PALETTE.as_ref().clone()).into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// get the built in high contrast light theme
|
|
|
|
|
pub fn high_contrast_light_default() -> Self {
|
|
|
|
|
CosmicPalette::HighContrastLight(LIGHT_PALETTE.as_ref().clone()).into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 19:30:08 -04:00
|
|
|
impl<C> From<CosmicPalette<C>> for Theme<Srgba>
|
2023-05-30 12:03:15 -04:00
|
|
|
where
|
2023-08-03 19:30:08 -04:00
|
|
|
CosmicPalette<C>: Into<CosmicPalette<Srgba>>,
|
2023-05-30 12:03:15 -04:00
|
|
|
{
|
|
|
|
|
fn from(p: CosmicPalette<C>) -> Self {
|
2023-08-03 19:30:08 -04:00
|
|
|
ThemeBuilder::palette(p.into()).build()
|
2023-08-03 16:23:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Helper for building customized themes
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct ThemeBuilder {
|
|
|
|
|
palette: CosmicPalette<Srgba>,
|
|
|
|
|
spacing: Spacing,
|
|
|
|
|
corner_radii: CornerRadii,
|
|
|
|
|
neutral_tint: Option<Srgb>,
|
|
|
|
|
bg_color: Option<Srgba>,
|
|
|
|
|
primary_container_bg: Option<Srgba>,
|
2023-08-03 19:30:08 -04:00
|
|
|
secondary_container_bg: Option<Srgba>,
|
2023-08-03 16:23:24 -04:00
|
|
|
text_tint: Option<Srgb>,
|
|
|
|
|
accent: Option<Srgb>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for ThemeBuilder {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
palette: DARK_PALETTE.to_owned().into(),
|
|
|
|
|
spacing: Spacing::default(),
|
|
|
|
|
corner_radii: CornerRadii::default(),
|
|
|
|
|
neutral_tint: Default::default(),
|
|
|
|
|
text_tint: Default::default(),
|
|
|
|
|
bg_color: Default::default(),
|
|
|
|
|
primary_container_bg: Default::default(),
|
2023-08-03 19:30:08 -04:00
|
|
|
secondary_container_bg: Default::default(),
|
2023-08-03 16:23:24 -04:00
|
|
|
accent: Default::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ThemeBuilder {
|
|
|
|
|
/// Get a builder that is initialized with the default dark theme
|
|
|
|
|
pub fn dark() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
palette: DARK_PALETTE.to_owned().into(),
|
|
|
|
|
..Default::default()
|
2023-05-30 12:03:15 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 16:23:24 -04:00
|
|
|
|
|
|
|
|
/// Get a builder that is initialized with the default light theme
|
|
|
|
|
pub fn light() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
palette: LIGHT_PALETTE.to_owned().into(),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a builder that is initialized with the default dark high contrast theme
|
|
|
|
|
pub fn dark_high_contrast() -> Self {
|
|
|
|
|
let palette: CosmicPalette<Srgba> = DARK_PALETTE.to_owned().into();
|
|
|
|
|
Self {
|
|
|
|
|
palette: CosmicPalette::HighContrastLight(palette.inner()),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get a builder that is initialized with the default light high contrast theme
|
|
|
|
|
pub fn light_high_contrast() -> Self {
|
|
|
|
|
let palette: CosmicPalette<Srgba> = LIGHT_PALETTE.to_owned().into();
|
|
|
|
|
Self {
|
|
|
|
|
palette: CosmicPalette::HighContrastLight(palette.inner()),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 19:30:08 -04:00
|
|
|
/// Get a builder that is initialized with the provided palette
|
|
|
|
|
pub fn palette(palette: CosmicPalette<Srgba>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
palette,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 16:23:24 -04:00
|
|
|
/// set the spacing of the builder
|
|
|
|
|
pub fn spacing(mut self, spacing: Spacing) -> Self {
|
|
|
|
|
self.spacing = spacing;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// set the corner_radii of the builder
|
|
|
|
|
pub fn corner_radii(mut self, corner_radii: CornerRadii) -> Self {
|
|
|
|
|
self.corner_radii = corner_radii;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// apply a neutral tint to the palette
|
|
|
|
|
pub fn neutral_tint(mut self, tint: Srgb) -> Self {
|
|
|
|
|
self.neutral_tint = Some(tint);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// apply a text tint to the palette
|
|
|
|
|
pub fn text_tint(mut self, tint: Srgb) -> Self {
|
|
|
|
|
self.text_tint = Some(tint);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// apply a background color to the palette
|
|
|
|
|
pub fn bg_color(mut self, c: Srgba) -> Self {
|
|
|
|
|
self.bg_color = Some(c);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// apply a primary container background color to the palette
|
|
|
|
|
pub fn primary_container_bg(mut self, c: Srgba) -> Self {
|
|
|
|
|
self.primary_container_bg = Some(c);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// apply a accent color to the palette
|
|
|
|
|
pub fn accent(mut self, c: Srgb) -> Self {
|
|
|
|
|
self.accent = Some(c);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// build the theme
|
|
|
|
|
pub fn build(self) -> Theme<Srgba> {
|
|
|
|
|
let Self {
|
|
|
|
|
mut palette,
|
|
|
|
|
spacing,
|
|
|
|
|
corner_radii,
|
|
|
|
|
neutral_tint,
|
|
|
|
|
text_tint,
|
|
|
|
|
bg_color,
|
|
|
|
|
primary_container_bg,
|
2023-08-03 19:30:08 -04:00
|
|
|
secondary_container_bg,
|
2023-08-03 16:23:24 -04:00
|
|
|
accent,
|
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
|
|
if let Some(accent) = accent {
|
|
|
|
|
palette.as_mut().accent = accent.into();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 19:30:08 -04:00
|
|
|
// TODO apply the tint customizations
|
|
|
|
|
|
|
|
|
|
let is_dark = palette.is_dark();
|
|
|
|
|
let is_high_contrast = palette.is_high_contrast();
|
2023-08-03 16:23:24 -04:00
|
|
|
|
|
|
|
|
if let Some(accent) = accent {
|
|
|
|
|
palette.as_mut().accent = accent.into();
|
|
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
let p_ref = palette.as_ref();
|
|
|
|
|
|
|
|
|
|
let bg = if let Some(bg_color) = bg_color {
|
|
|
|
|
bg_color
|
|
|
|
|
} else {
|
2023-08-03 19:56:16 -04:00
|
|
|
p_ref.gray_1.clone()
|
2023-08-03 19:30:08 -04:00
|
|
|
};
|
|
|
|
|
let ok_bg = Oklcha::from_color(bg);
|
|
|
|
|
let step_array = steps(ok_bg);
|
|
|
|
|
|
|
|
|
|
let bg_index = color_index(bg, step_array.len());
|
|
|
|
|
let primary_container_bg = if let Some(primary_container_bg_color) = primary_container_bg {
|
|
|
|
|
primary_container_bg_color
|
|
|
|
|
} else {
|
|
|
|
|
get_color(bg_index, 5, &step_array, is_dark, &p_ref.neutral_1)
|
|
|
|
|
};
|
2023-08-03 16:23:24 -04:00
|
|
|
|
2023-08-03 19:30:08 -04:00
|
|
|
let secondary_container_bg = if let Some(secondary_container_bg) = secondary_container_bg {
|
|
|
|
|
secondary_container_bg
|
|
|
|
|
} else {
|
|
|
|
|
get_color(bg_index, 10, &step_array, is_dark, &p_ref.neutral_2)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let bg_component = get_color(bg_index, 8, &step_array, is_dark, &p_ref.neutral_2);
|
|
|
|
|
let on_bg_component = get_text(
|
|
|
|
|
color_index(bg_component, step_array.len()),
|
|
|
|
|
&step_array,
|
|
|
|
|
is_dark,
|
|
|
|
|
&p_ref.neutral_8,
|
|
|
|
|
);
|
|
|
|
|
let bg_component = Component::component(
|
|
|
|
|
bg_component,
|
|
|
|
|
p_ref.neutral_0.to_owned(),
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
on_bg_component,
|
|
|
|
|
is_high_contrast,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let primary_index = color_index(primary_container_bg, step_array.len());
|
|
|
|
|
let primary_component = get_color(primary_index, 6, &step_array, is_dark, &p_ref.neutral_3);
|
|
|
|
|
let on_primary_component = get_text(
|
|
|
|
|
color_index(primary_component, step_array.len()),
|
|
|
|
|
&step_array,
|
|
|
|
|
is_dark,
|
|
|
|
|
&p_ref.neutral_8,
|
|
|
|
|
);
|
|
|
|
|
let primary_component = Component::component(
|
|
|
|
|
primary_component,
|
|
|
|
|
p_ref.neutral_0.to_owned(),
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
on_primary_component,
|
|
|
|
|
is_high_contrast,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let secondary_index = color_index(secondary_container_bg, step_array.len());
|
|
|
|
|
let secondary_component =
|
|
|
|
|
get_color(secondary_index, 3, &step_array, is_dark, &p_ref.neutral_4);
|
|
|
|
|
let on_secondary_component = get_text(
|
|
|
|
|
color_index(secondary_component, step_array.len()),
|
|
|
|
|
&step_array,
|
|
|
|
|
is_dark,
|
|
|
|
|
&p_ref.neutral_10,
|
|
|
|
|
);
|
|
|
|
|
let secondary_component = Component::component(
|
|
|
|
|
secondary_component,
|
|
|
|
|
p_ref.neutral_0.to_owned(),
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
on_secondary_component,
|
|
|
|
|
is_high_contrast,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut theme: Theme<Srgba> = Theme {
|
|
|
|
|
name: palette.name().to_string(),
|
|
|
|
|
background: Container::new(
|
|
|
|
|
bg_component,
|
|
|
|
|
bg,
|
|
|
|
|
get_text(bg_index, &step_array, is_dark, &p_ref.neutral_8),
|
|
|
|
|
),
|
|
|
|
|
primary: Container::new(
|
|
|
|
|
primary_component,
|
|
|
|
|
primary_container_bg,
|
|
|
|
|
get_text(primary_index, &step_array, is_dark, &p_ref.neutral_8),
|
|
|
|
|
),
|
|
|
|
|
secondary: Container::new(
|
|
|
|
|
secondary_component,
|
|
|
|
|
secondary_container_bg,
|
|
|
|
|
get_text(secondary_index, &step_array, is_dark, &p_ref.neutral_8),
|
|
|
|
|
),
|
|
|
|
|
accent: Component::colored_component(
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
p_ref.neutral_0.to_owned(),
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
),
|
|
|
|
|
success: Component::colored_component(
|
|
|
|
|
p_ref.green.to_owned(),
|
|
|
|
|
p_ref.neutral_0.to_owned(),
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
),
|
|
|
|
|
destructive: Component::colored_component(
|
|
|
|
|
p_ref.red.to_owned(),
|
|
|
|
|
p_ref.neutral_0.to_owned(),
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
),
|
|
|
|
|
warning: Component::colored_component(
|
|
|
|
|
p_ref.yellow.to_owned(),
|
|
|
|
|
p_ref.neutral_0.to_owned(),
|
|
|
|
|
p_ref.accent.to_owned(),
|
|
|
|
|
),
|
|
|
|
|
palette: palette.inner(),
|
|
|
|
|
spacing,
|
|
|
|
|
corner_radii,
|
|
|
|
|
is_dark,
|
|
|
|
|
is_high_contrast,
|
|
|
|
|
};
|
2023-08-03 16:23:24 -04:00
|
|
|
theme.spacing = spacing;
|
|
|
|
|
theme.corner_radii = corner_radii;
|
|
|
|
|
theme
|
|
|
|
|
}
|
2023-05-30 12:03:15 -04:00
|
|
|
}
|
2023-08-03 19:30:08 -04:00
|
|
|
|
|
|
|
|
fn get_index(base_index: usize, steps: usize, step_len: usize, is_dark: bool) -> Option<usize> {
|
|
|
|
|
if is_dark {
|
|
|
|
|
base_index.checked_add(steps)
|
|
|
|
|
} else {
|
|
|
|
|
base_index.checked_sub(steps)
|
|
|
|
|
}
|
|
|
|
|
.filter(|i| *i < step_len)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_color(
|
|
|
|
|
base_index: usize,
|
|
|
|
|
steps: usize,
|
|
|
|
|
step_array: &[Srgba; 100],
|
|
|
|
|
is_dark: bool,
|
|
|
|
|
fallback: &Srgba,
|
|
|
|
|
) -> Srgba {
|
|
|
|
|
get_index(base_index, steps, step_array.len(), is_dark)
|
|
|
|
|
.and_then(|i| step_array.get(i).cloned())
|
|
|
|
|
.unwrap_or_else(|| fallback.to_owned())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_text(
|
|
|
|
|
base_index: usize,
|
|
|
|
|
step_array: &[Srgba; 100],
|
|
|
|
|
is_dark: bool,
|
|
|
|
|
fallback: &Srgba,
|
|
|
|
|
) -> Srgba {
|
|
|
|
|
let Some(index) = get_index(base_index, 70, step_array.len(), is_dark).or_else(|| get_index(base_index, 50, step_array.len(), is_dark)) else {
|
|
|
|
|
return fallback.to_owned();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
step_array
|
|
|
|
|
.get(index)
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or_else(|| fallback.to_owned())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn color_index<C>(c: C, array_len: usize) -> usize
|
|
|
|
|
where
|
|
|
|
|
Oklcha: FromColor<C>,
|
|
|
|
|
{
|
|
|
|
|
let c = Oklcha::from_color(c);
|
|
|
|
|
((c.l * array_len as f32).round() as usize).clamp(0, array_len - 1)
|
|
|
|
|
}
|