use std::borrow::Cow;
use std::collections::HashMap;
use std::path::Path;
use std::sync::LazyLock;
use crate::{Error, Result, Theme};
const PRESET_ENTRIES: &[(&str, &str)] = &[
("kde-breeze", include_str!("presets/kde-breeze.toml")),
("adwaita", include_str!("presets/adwaita.toml")),
("windows-11", include_str!("presets/windows-11.toml")),
("macos-sonoma", include_str!("presets/macos-sonoma.toml")),
("material", include_str!("presets/material.toml")),
("ios", include_str!("presets/ios.toml")),
(
"catppuccin-latte",
include_str!("presets/catppuccin-latte.toml"),
),
(
"catppuccin-frappe",
include_str!("presets/catppuccin-frappe.toml"),
),
(
"catppuccin-macchiato",
include_str!("presets/catppuccin-macchiato.toml"),
),
(
"catppuccin-mocha",
include_str!("presets/catppuccin-mocha.toml"),
),
("nord", include_str!("presets/nord.toml")),
("dracula", include_str!("presets/dracula.toml")),
("gruvbox", include_str!("presets/gruvbox.toml")),
("solarized", include_str!("presets/solarized.toml")),
("tokyo-night", include_str!("presets/tokyo-night.toml")),
("one-dark", include_str!("presets/one-dark.toml")),
(
"kde-breeze-live",
include_str!("presets/kde-breeze-live.toml"),
),
("adwaita-live", include_str!("presets/adwaita-live.toml")),
(
"macos-sonoma-live",
include_str!("presets/macos-sonoma-live.toml"),
),
(
"windows-11-live",
include_str!("presets/windows-11-live.toml"),
),
];
const PRESET_NAMES: &[&str] = &[
"kde-breeze",
"adwaita",
"windows-11",
"macos-sonoma",
"material",
"ios",
"catppuccin-latte",
"catppuccin-frappe",
"catppuccin-macchiato",
"catppuccin-mocha",
"nord",
"dracula",
"gruvbox",
"solarized",
"tokyo-night",
"one-dark",
];
const PRESET_DISPLAY_NAMES: &[(&str, &str)] = &[
("kde-breeze", "KDE Breeze"),
("kde-breeze-live", "KDE Breeze"),
("adwaita", "Adwaita"),
("adwaita-live", "Adwaita"),
("windows-11", "Windows 11"),
("windows-11-live", "Windows 11"),
("macos-sonoma", "macOS Sonoma"),
("macos-sonoma-live", "macOS Sonoma"),
("material", "Material"),
("ios", "iOS"),
("catppuccin-latte", "Catppuccin Latte"),
("catppuccin-frappe", "Catppuccin Frappe"),
("catppuccin-macchiato", "Catppuccin Macchiato"),
("catppuccin-mocha", "Catppuccin Mocha"),
("nord", "Nord"),
("dracula", "Dracula"),
("gruvbox", "Gruvbox"),
("solarized", "Solarized"),
("tokyo-night", "Tokyo Night"),
("one-dark", "One Dark"),
];
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PresetInfo {
pub key: &'static str,
pub display_name: &'static str,
pub platforms: &'static [&'static str],
pub light_only: bool,
}
const PRESET_INFOS: &[PresetInfo] = &[
PresetInfo {
key: "kde-breeze",
display_name: "KDE Breeze",
platforms: &["linux-kde"],
light_only: false,
},
PresetInfo {
key: "adwaita",
display_name: "Adwaita",
platforms: &["linux"],
light_only: false,
},
PresetInfo {
key: "windows-11",
display_name: "Windows 11",
platforms: &["windows"],
light_only: false,
},
PresetInfo {
key: "macos-sonoma",
display_name: "macOS Sonoma",
platforms: &["macos"],
light_only: false,
},
PresetInfo {
key: "material",
display_name: "Material",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "ios",
display_name: "iOS",
platforms: &["macos", "ios"],
light_only: false,
},
PresetInfo {
key: "catppuccin-latte",
display_name: "Catppuccin Latte",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "catppuccin-frappe",
display_name: "Catppuccin Frappe",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "catppuccin-macchiato",
display_name: "Catppuccin Macchiato",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "catppuccin-mocha",
display_name: "Catppuccin Mocha",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "nord",
display_name: "Nord",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "dracula",
display_name: "Dracula",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "gruvbox",
display_name: "Gruvbox",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "solarized",
display_name: "Solarized",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "tokyo-night",
display_name: "Tokyo Night",
platforms: &[],
light_only: false,
},
PresetInfo {
key: "one-dark",
display_name: "One Dark",
platforms: &[],
light_only: false,
},
];
type Parsed = std::result::Result<Theme, String>;
fn parse(toml_str: &str) -> Parsed {
from_toml(toml_str).map_err(|e| e.to_string())
}
static CACHE: LazyLock<HashMap<&str, Parsed>> = LazyLock::new(|| {
PRESET_ENTRIES
.iter()
.map(|(key, toml_str)| {
let mut result = parse(toml_str);
if let Ok(ref mut theme) = result
&& let Some((_, display_name)) = PRESET_DISPLAY_NAMES.iter().find(|(k, _)| k == key)
{
theme.name = Cow::Borrowed(display_name);
}
(*key, result)
})
.collect()
});
pub(crate) fn preset(name: &str) -> Result<Theme> {
match CACHE.get(name) {
None => Err(Error::UnknownPreset {
name: name.to_string(),
known: PRESET_NAMES,
}),
Some(Ok(theme)) => Ok(theme.clone()),
Some(Err(msg)) => Err(Error::ReaderFailed {
reader: "preset_cache",
source: format!("bundled preset '{name}': {msg}").into(),
}),
}
}
pub(crate) fn list_presets() -> &'static [PresetInfo] {
PRESET_INFOS
}
#[allow(unreachable_code)]
fn detect_platform() -> &'static str {
#[cfg(target_os = "macos")]
{
return "macos";
}
#[cfg(target_os = "windows")]
{
return "windows";
}
#[cfg(target_os = "linux")]
{
if crate::detect::detect_linux_desktop() == crate::detect::LinuxDesktop::Kde {
return "linux-kde";
}
"linux"
}
#[cfg(target_os = "ios")]
{
return "ios";
}
#[cfg(not(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "ios"
)))]
{
"linux"
}
}
pub(crate) fn list_presets_for_platform() -> Vec<PresetInfo> {
let platform = detect_platform();
PRESET_INFOS
.iter()
.filter(|info| {
info.platforms.is_empty() || info.platforms.iter().any(|p| platform.starts_with(p))
})
.cloned()
.collect()
}
pub(crate) fn from_toml(toml_str: &str) -> Result<Theme> {
let theme: Theme = toml::from_str(toml_str)?;
Ok(theme)
}
pub(crate) fn from_file(path: impl AsRef<Path>) -> Result<Theme> {
let contents = std::fs::read_to_string(path)?;
from_toml(&contents)
}
pub(crate) fn to_toml(theme: &Theme) -> Result<String> {
let s = toml::to_string_pretty(theme)?;
Ok(s)
}
#[cfg(test)]
mod documented_sizes;
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn every_bundled_preset_source_lints_clean() {
for (name, source) in PRESET_ENTRIES {
let warnings = crate::Theme::lint_toml(source)
.unwrap_or_else(|e| panic!("preset {name} should lint: {e}"));
assert!(
warnings.is_empty(),
"preset {name}'s source has lint warnings: {warnings:?}"
);
}
assert!(
PRESET_ENTRIES
.iter()
.any(|(_, s)| s.contains("padding_horizontal_px")),
"no bundled preset uses the shorthand any more, so this test no \
longer proves the linter accepts it"
);
}
#[test]
fn preset_unknown_name_returns_unknown_preset() {
let err = preset("nonexistent").unwrap_err();
let Error::UnknownPreset { name, .. } = err else {
return;
};
assert!(name.contains("nonexistent"));
}
#[test]
fn from_toml_minimal_valid() {
let toml_str = r##"
name = "Minimal"
[light.defaults]
accent_color = "#ff0000"
"##;
let theme = from_toml(toml_str).unwrap();
assert_eq!(theme.name, "Minimal");
assert!(theme.light.is_some());
let light = theme.light.unwrap();
assert_eq!(
light.defaults.accent_color,
Some(crate::Rgba::rgb(255, 0, 0))
);
}
#[test]
fn from_toml_invalid_returns_toml_error() {
let err = from_toml("{{{{invalid toml").unwrap_err();
assert!(
matches!(err, Error::Toml(_)),
"expected Toml variant, got: {err:?}"
);
}
#[test]
fn to_toml_produces_valid_round_trip() {
let theme = preset("catppuccin-mocha").unwrap();
let toml_str = to_toml(&theme).unwrap();
let reparsed = from_toml(&toml_str).unwrap();
assert_eq!(reparsed.name, theme.name);
assert!(reparsed.light.is_some());
assert!(reparsed.dark.is_some());
let orig_light = theme.light.as_ref().unwrap();
let new_light = reparsed.light.as_ref().unwrap();
assert_eq!(
orig_light.defaults.accent_color,
new_light.defaults.accent_color
);
}
#[test]
fn from_file_with_tempfile() {
let dir = std::env::temp_dir();
let path = dir.join("native_theme_test_preset.toml");
let toml_str = r##"
name = "File Test"
[light.defaults]
accent_color = "#00ff00"
"##;
std::fs::write(&path, toml_str).unwrap();
let theme = from_file(&path).unwrap();
assert_eq!(theme.name, "File Test");
assert!(theme.light.is_some());
let _ = std::fs::remove_file(&path);
}
#[test]
fn icon_set_native_presets_have_correct_values() {
use crate::IconSet;
let cases: &[(&str, IconSet)] = &[
("windows-11", IconSet::SegoeIcons),
("macos-sonoma", IconSet::SfSymbols),
("ios", IconSet::SfSymbols),
("adwaita", IconSet::Freedesktop),
("kde-breeze", IconSet::Freedesktop),
("material", IconSet::Material),
];
for (name, expected) in cases {
let theme = preset(name).unwrap();
assert_eq!(
theme.icon_set,
Some(*expected),
"preset '{name}' icon_set should be Some({expected:?})"
);
}
}
#[test]
fn icon_set_community_presets_have_lucide() {
let community = &[
"catppuccin-latte",
"catppuccin-frappe",
"catppuccin-macchiato",
"catppuccin-mocha",
"nord",
"dracula",
"gruvbox",
"solarized",
"tokyo-night",
"one-dark",
];
for name in community {
let theme = preset(name).unwrap();
assert_eq!(
theme.icon_set,
Some(crate::IconSet::Lucide),
"preset '{name}' icon_set should be Lucide"
);
}
}
#[test]
fn from_file_nonexistent_returns_error() {
let err = from_file("/tmp/nonexistent_theme_file_12345.toml").unwrap_err();
match err {
Error::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound),
other => panic!("expected Io, got: {other:?}"),
}
}
#[test]
fn preset_names_match_list() {
for info in list_presets() {
assert!(
preset(info.key).is_ok(),
"preset '{}' not loadable",
info.key
);
}
}
#[test]
fn platform_presets_no_derived_fields() {
let platform_presets = &["kde-breeze", "adwaita", "windows-11", "macos-sonoma"];
for name in platform_presets {
let theme = preset(name).unwrap();
for (label, variant_opt) in [
("light", theme.light.as_ref()),
("dark", theme.dark.as_ref()),
] {
let variant = variant_opt.unwrap();
assert!(
variant.button.primary_background.is_none(),
"preset '{name}' {label}.button.primary_background should be None (derived)"
);
assert_eq!(
variant.checkbox.checked_background.is_some(),
*name == "kde-breeze",
"preset '{name}' {label}.checkbox.checked_background: stated only where \
platform-facts does not derive it from the accent"
);
assert!(
variant.slider.fill_color.is_none(),
"preset '{name}' {label}.slider.fill_color should be None (derived)"
);
assert_eq!(
variant.progress_bar.fill_color.is_some(),
*name == "kde-breeze",
"preset '{name}' {label}.progress_bar.fill_color: stated only where \
platform-facts does not derive it from the accent"
);
assert!(
variant.switch.checked_background.is_none(),
"preset '{name}' {label}.switch.checked_background should be None (derived)"
);
}
}
}
#[test]
fn all_presets_resolve_validate() {
for info in list_presets() {
let name = info.key;
let theme = preset(name).unwrap();
if let Some(mut light) = theme.light.clone() {
light.resolve_all();
light.validate().unwrap_or_else(|e| {
panic!("preset {name} light variant failed validation: {e}");
});
}
if let Some(mut dark) = theme.dark.clone() {
dark.resolve_all();
dark.validate().unwrap_or_else(|e| {
panic!("preset {name} dark variant failed validation: {e}");
});
}
}
}
#[test]
fn all_presets_indicator_color_inherits_accent_text_color() {
for info in list_presets() {
let name = info.key;
let theme = preset(name).unwrap();
for (label, variant_opt) in
[("light", theme.light.clone()), ("dark", theme.dark.clone())]
{
let Some(mut variant) = variant_opt else {
continue;
};
if variant.checkbox.indicator_color.is_some() {
continue;
}
variant.resolve_all();
assert_eq!(
variant.checkbox.indicator_color, variant.defaults.accent_text_color,
"preset '{name}' {label}.checkbox.indicator_color should inherit \
defaults.accent_text_color"
);
}
}
}
#[test]
fn resolve_fills_accent_derived_fields() {
let theme = preset("catppuccin-mocha").unwrap();
let mut light = theme.light.clone().unwrap();
assert!(
light.button.primary_background.is_none(),
"primary_background should be None pre-resolve"
);
assert!(
light.checkbox.checked_background.is_none(),
"checkbox.checked_background should be None pre-resolve"
);
assert!(
light.slider.fill_color.is_none(),
"slider.fill should be None pre-resolve"
);
assert!(
light.progress_bar.fill_color.is_none(),
"progress_bar.fill should be None pre-resolve"
);
assert!(
light.switch.checked_background.is_none(),
"switch.checked_background should be None pre-resolve"
);
light.resolve();
let accent = light.defaults.accent_color.unwrap();
assert_eq!(
light.button.primary_background,
Some(accent),
"button.primary_background should match accent"
);
assert_eq!(
light.checkbox.checked_background,
Some(accent),
"checkbox.checked_background should match accent"
);
assert_eq!(
light.slider.fill_color,
Some(accent),
"slider.fill should match accent"
);
assert_eq!(
light.progress_bar.fill_color,
Some(accent),
"progress_bar.fill should match accent"
);
assert_eq!(
light.switch.checked_background,
Some(accent),
"switch.checked_background should match accent"
);
}
#[test]
fn resolve_then_validate_produces_complete_theme() {
let theme = preset("catppuccin-mocha").unwrap();
let mut light = theme.light.clone().unwrap();
light.resolve_all();
let resolved = light.validate().unwrap();
assert_eq!(resolved.defaults.font.family.as_ref(), "Inter");
assert_eq!(resolved.defaults.font.size, 14.0);
assert_eq!(resolved.defaults.font.weight, 400);
assert_eq!(resolved.defaults.line_height, 1.2);
assert_eq!(resolved.defaults.border.corner_radius, 8.0);
assert_eq!(resolved.defaults.focus_ring_width, 2.0);
assert_eq!(resolved.defaults.icon_sizes.toolbar, 24.0);
assert_eq!(resolved.defaults.icon_sizes.toolbar, 24.0);
assert_eq!(
resolved.window.background_color,
resolved.defaults.background_color
);
assert_eq!(theme.icon_set, Some(crate::IconSet::Lucide));
}
#[test]
fn font_subfield_inheritance_integration() {
let theme = preset("catppuccin-mocha").unwrap();
let mut light = theme.light.clone().unwrap();
use crate::model::FontSpec;
use crate::model::font::FontSize;
light.menu.font = Some(FontSpec {
family: None,
size: Some(FontSize::Px(12.0)),
weight: None,
..Default::default()
});
light.resolve_all();
let resolved = light.validate().unwrap();
assert_eq!(
resolved.menu.font.family.as_ref(),
"Inter",
"menu font family should inherit from defaults"
);
assert_eq!(
resolved.menu.font.size, 12.0,
"menu font size should be the explicit value"
);
assert_eq!(
resolved.menu.font.weight, 400,
"menu font weight should inherit from defaults"
);
}
#[test]
fn text_scale_inheritance_integration() {
let theme = preset("catppuccin-mocha").unwrap();
let mut light = theme.light.clone().unwrap();
light.text_scale.caption = None;
light.resolve_all();
let resolved = light.validate().unwrap();
let expected_size = 14.0;
assert!(
(resolved.text_scale.caption.size - expected_size).abs() < 0.01,
"caption size = defaults.font.size, got {}",
resolved.text_scale.caption.size
);
assert_eq!(
resolved.text_scale.caption.weight, 400,
"caption weight from defaults.font.weight"
);
let expected_lh = 1.2 * expected_size;
assert!(
(resolved.text_scale.caption.line_height - expected_lh).abs() < 0.01,
"caption line_height should be line_height_multiplier * caption_size = {expected_lh}, got {}",
resolved.text_scale.caption.line_height
);
}
#[test]
fn live_presets_loadable() {
let live_names = &[
"kde-breeze-live",
"adwaita-live",
"macos-sonoma-live",
"windows-11-live",
];
for name in live_names {
let theme = preset(name)
.unwrap_or_else(|e| panic!("live preset '{name}' failed to parse: {e}"));
assert!(
theme.light.is_some(),
"live preset '{name}' missing light variant"
);
assert!(
theme.dark.is_some(),
"live preset '{name}' missing dark variant"
);
let light = theme.light.as_ref().unwrap();
let dark = theme.dark.as_ref().unwrap();
assert!(
light.defaults.accent_color.is_none(),
"live preset '{name}' light should have no accent"
);
assert!(
light.defaults.background_color.is_none(),
"live preset '{name}' light should have no background"
);
assert!(
light.defaults.text_color.is_none(),
"live preset '{name}' light should have no foreground"
);
assert!(
dark.defaults.accent_color.is_none(),
"live preset '{name}' dark should have no accent"
);
assert!(
dark.defaults.background_color.is_none(),
"live preset '{name}' dark should have no background"
);
assert!(
dark.defaults.text_color.is_none(),
"live preset '{name}' dark should have no foreground"
);
assert!(
light.defaults.font.family.is_none(),
"live preset '{name}' light should have no font family"
);
assert!(
light.defaults.font.size.is_none(),
"live preset '{name}' light should have no font size"
);
assert!(
light.defaults.font.weight.is_none(),
"live preset '{name}' light should have no font weight"
);
assert!(
dark.defaults.font.family.is_none(),
"live preset '{name}' dark should have no font family"
);
assert!(
dark.defaults.font.size.is_none(),
"live preset '{name}' dark should have no font size"
);
assert!(
dark.defaults.font.weight.is_none(),
"live preset '{name}' dark should have no font weight"
);
}
}
#[test]
fn list_presets_for_platform_returns_subset() {
let all = list_presets();
let filtered = list_presets_for_platform();
for info in &filtered {
assert!(
all.iter().any(|a| a.key == info.key),
"filtered preset '{}' not in full list",
info.key
);
}
let community = &[
"catppuccin-latte",
"catppuccin-frappe",
"catppuccin-macchiato",
"catppuccin-mocha",
"nord",
"dracula",
"gruvbox",
"solarized",
"tokyo-night",
"one-dark",
];
for name in community {
assert!(
filtered.iter().any(|info| info.key == *name),
"community preset '{name}' should always be in filtered list"
);
}
assert!(
filtered.iter().any(|info| info.key == "material"),
"material should always be in filtered list"
);
}
#[test]
fn live_presets_fail_validate_standalone() {
let live_names = &[
"kde-breeze-live",
"adwaita-live",
"macos-sonoma-live",
"windows-11-live",
];
for name in live_names {
let theme = preset(name).unwrap();
let mut light = theme.light.clone().unwrap();
light.resolve();
let result = light.validate();
assert!(
result.is_err(),
"live preset '{name}' light should fail validation standalone (missing colors/fonts)"
);
let mut dark = theme.dark.clone().unwrap();
dark.resolve();
let result = dark.validate();
assert!(
result.is_err(),
"live preset '{name}' dark should fail validation standalone (missing colors/fonts)"
);
}
}
}