Files
gpui-native-kit/crates/theme/src/resolve.rs
T
reya b08516211c Unify theme sizing and spacing on Base UI metrics
- Read only mode, accent, accessibility, and native window material from the OS.
- Replace platform palettes, metrics, type ramps, borders, and elevations with unified tokens.
- Align controls, overlays, and data rows with Base UI sizing.
2026-11-16 18:27:44 +07:00

792 lines
31 KiB
Rust

use crate::definition::ThemeDefinition;
use crate::ramps::{AccentShades, Oklch, contrast_foreground, derive_shades, mix};
use crate::system::{SourceKind, SystemTheme};
use crate::{
ColorTokens, CustomTheme, Elevations, MaterialTokens, Motion, Radius, Scrollbar, SizeTable,
Spacing, ThemeVariant, Typography,
};
use gpui::{WindowBackgroundAppearance, px};
/// Derived state shades for the colors that get pressed and hovered.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Ramps {
pub primary: AccentShades,
pub secondary: AccentShades,
pub destructive: AccentShades,
pub success: AccentShades,
pub warning: AccentShades,
pub info: AccentShades,
}
/// Everything a component paints from, after the overlay chain.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ResolvedTheme {
pub variant: ThemeVariant,
pub colors: ColorTokens,
/// The translucent paints over the window's native material.
pub material: MaterialTokens,
pub ramps: Ramps,
pub radius: Radius,
pub spacing: Spacing,
pub elevations: Elevations,
pub motion: Motion,
pub scrollbar: Scrollbar,
/// The height the window's titlebar strips draw at.
pub titlebar_height: gpui::Pixels,
/// The theme's root font size.
pub root_font_size: gpui::Pixels,
/// The monospace family, for code surfaces.
pub mono_font: &'static str,
/// The type ladder, resolved at the root font size.
pub typography: Typography,
pub sizes: SizeTable,
pub accessibility: crate::system::AccessibilityPreferences,
/// The material a window should open with, after accessibility.
pub window_background: WindowBackgroundAppearance,
}
/// Overlay order (later wins):
/// neutral base → system → preset definition → custom theme.
pub(crate) fn resolve(
system: &SystemTheme,
preset: Option<&ThemeDefinition>,
mode_override: Option<ThemeVariant>,
custom: Option<CustomTheme>,
) -> ResolvedTheme {
let custom = custom.unwrap_or_default();
let variant = mode_override
.or(preset.and_then(|def| def.mode()))
.unwrap_or(system.variant);
let base = match variant {
ThemeVariant::Light => ColorTokens::light(),
ThemeVariant::Dark => ColorTokens::dark(),
};
let def_color = |field: fn(&ThemeDefinition) -> Option<gpui::Hsla>| preset.and_then(field);
let accent = def_color(|d| d.accent())
.or(system.accent)
.unwrap_or(base.accent);
let primary = def_color(|d| d.primary()).unwrap_or(accent);
let destructive = def_color(|d| d.destructive()).unwrap_or(base.destructive);
let success = def_color(|d| d.success()).unwrap_or(base.success);
let warning = def_color(|d| d.warning()).unwrap_or(base.warning);
let info = def_color(|d| d.info()).unwrap_or(base.info);
let mut colors = ColorTokens {
background: def_color(|d| d.background()).unwrap_or(base.background),
foreground: def_color(|d| d.foreground()).unwrap_or(base.foreground),
surface: def_color(|d| d.surface()).unwrap_or(base.surface),
surface_foreground: def_color(|d| d.surface_foreground())
.unwrap_or(base.surface_foreground),
sidebar: def_color(|d| d.sidebar()).unwrap_or(base.sidebar),
primary,
primary_foreground: def_color(|d| d.primary_foreground())
.unwrap_or_else(|| contrast_foreground(primary)),
secondary: def_color(|d| d.secondary()).unwrap_or(base.secondary),
secondary_foreground: def_color(|d| d.secondary_foreground())
.unwrap_or(base.secondary_foreground),
muted: def_color(|d| d.muted()).unwrap_or(base.muted),
muted_hover: def_color(|d| d.muted_hover()).unwrap_or(base.muted_hover),
muted_foreground: def_color(|d| d.muted_foreground()).unwrap_or(base.muted_foreground),
accent,
accent_foreground: def_color(|d| d.accent_foreground())
.unwrap_or_else(|| contrast_foreground(accent)),
destructive,
destructive_foreground: def_color(|d| d.destructive_foreground())
.unwrap_or_else(|| contrast_foreground(destructive)),
success,
success_foreground: def_color(|d| d.success_foreground())
.unwrap_or_else(|| contrast_foreground(success)),
warning,
warning_foreground: def_color(|d| d.warning_foreground())
.unwrap_or_else(|| contrast_foreground(warning)),
info,
info_foreground: def_color(|d| d.info_foreground())
.unwrap_or_else(|| contrast_foreground(info)),
border: def_color(|d| d.border()).unwrap_or(base.border),
input: def_color(|d| d.input()).unwrap_or(base.input),
ring: def_color(|d| d.ring()).unwrap_or(accent),
selection: def_color(|d| d.selection()).unwrap_or(base.selection),
overlay: def_color(|d| d.overlay()).unwrap_or(base.overlay),
scrollbar_thumb: def_color(|d| d.scrollbar_thumb()).unwrap_or(base.scrollbar_thumb),
};
// The knob scales the fill over a platform-stated blur.
let transparency = custom
.transparency
.map(|level| level.min(100))
.filter(|_| system.source != SourceKind::Windows && system.window_background.is_some());
// Reduced transparency forces the window opaque.
let window_background = if system.accessibility.reduce_transparency || transparency == Some(0) {
WindowBackgroundAppearance::Opaque
} else {
system
.window_background
.unwrap_or(WindowBackgroundAppearance::Opaque)
};
let accessibility = system.accessibility;
if accessibility.high_contrast {
colors.border = mix(
Oklch::from_hsla(colors.border),
Oklch::from_hsla(colors.foreground),
0.35,
)
.to_hsla();
}
// A custom theme's stated font size wins over the 16px design root.
let root_font_size = custom.root_font_size.unwrap_or(px(16.));
let sizes = SizeTable::with_root_font_size(root_font_size);
let titlebar_height = root_font_size * 2.5;
let mut radius = Radius::default();
if let Some(control) = custom
.control_radius
.or(preset.and_then(|def| def.control_radius()).map(px))
{
radius.control = control;
}
if let Some(overlay) = custom
.overlay_radius
.or(preset.and_then(|def| def.overlay_radius()).map(px))
{
radius.overlay = overlay;
}
if let Some(sm) = custom
.sm_radius
.or(preset.and_then(|def| def.sm_radius()).map(px))
{
radius.sm = sm;
}
let ramps = Ramps {
primary: derive_shades(primary, colors.background, variant == ThemeVariant::Dark),
secondary: derive_shades(
colors.secondary,
colors.background,
variant == ThemeVariant::Dark,
),
destructive: derive_shades(
destructive,
colors.background,
variant == ThemeVariant::Dark,
),
success: derive_shades(success, colors.background, variant == ThemeVariant::Dark),
warning: derive_shades(warning, colors.background, variant == ThemeVariant::Dark),
info: derive_shades(info, colors.background, variant == ThemeVariant::Dark),
};
if def_color(|d| d.selection()).is_none() {
colors.selection = ramps.primary.subtle;
}
let material = MaterialTokens::derive(
&colors,
ramps.primary.base,
variant,
window_background != WindowBackgroundAppearance::Opaque,
transparency,
);
let default_elevations = Elevations::for_variant(variant);
let elevations = Elevations {
popover: custom
.popover_elevation
.unwrap_or(default_elevations.popover),
dialog: custom.dialog_elevation.unwrap_or(default_elevations.dialog),
control: custom
.control_elevation
.unwrap_or(default_elevations.control),
};
ResolvedTheme {
variant,
ramps,
colors,
material,
radius,
spacing: Spacing {
unit: root_font_size * 0.25,
},
elevations,
motion: Motion::default(),
scrollbar: Scrollbar {
autohide: system.source.scrollbar_autohide(),
..Scrollbar::default()
},
titlebar_height,
root_font_size,
mono_font: mono_font(),
typography: Typography::resolve(root_font_size),
sizes,
accessibility,
window_background,
}
}
/// The platform's monospace family, for code surfaces.
fn mono_font() -> &'static str {
if cfg!(target_os = "macos") {
"Menlo"
} else if cfg!(target_os = "windows") {
"Consolas"
} else {
"monospace"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::system::{AccessibilityPreferences, SourceKind};
use crate::{Elevation, Shadow};
use gpui::{Rgba, WindowBackgroundAppearance, rgb};
fn hsla(hex: u32) -> gpui::Hsla {
gpui::Hsla::from(rgb(hex))
}
fn system(variant: ThemeVariant, accent: Option<gpui::Hsla>) -> SystemTheme {
SystemTheme {
variant,
accent,
accessibility: AccessibilityPreferences::default(),
window_background: None,
source: SourceKind::Macos,
}
}
#[test]
fn empty_system_resolves_to_the_neutral_fallback() {
for variant in [ThemeVariant::Light, ThemeVariant::Dark] {
let resolved = resolve(
&SystemTheme {
source: SourceKind::Fallback,
..system(variant, None)
},
None,
None,
None,
);
let base = match variant {
ThemeVariant::Light => ColorTokens::light(),
ThemeVariant::Dark => ColorTokens::dark(),
};
assert_eq!(resolved.colors.background, base.background);
assert_eq!(resolved.colors.accent, base.accent);
assert_eq!(resolved.colors.ring, base.accent);
assert_eq!(resolved.ramps.primary.base, base.primary);
assert_eq!(resolved.ramps.secondary.base, base.secondary);
assert_eq!(resolved.colors.selection, resolved.ramps.primary.subtle);
assert_eq!(resolved.radius.sm, px(4.));
assert_eq!(resolved.radius.control, px(6.));
assert_eq!(resolved.radius.overlay, px(10.));
assert_eq!(resolved.root_font_size, px(16.));
assert_eq!(resolved.sizes.medium.control_height, px(32.));
assert_eq!(resolved.sizes.medium.input_height, px(32.));
assert_eq!(resolved.sizes.medium.text_size, px(14.));
assert_eq!(resolved.sizes.small.control_height, px(28.));
assert_eq!(resolved.sizes.small.text_size, px(12.));
assert_eq!(resolved.sizes.small.avatar_size, px(24.));
assert_eq!(resolved.sizes.medium.avatar_size, px(32.));
assert_eq!(resolved.sizes.large.avatar_size, px(40.));
assert_eq!(resolved.sizes.small.caption_size, px(12.));
assert_eq!(resolved.sizes.medium.caption_size, px(12.));
assert_eq!(resolved.sizes.large.caption_size, px(14.));
assert_eq!(resolved.sizes.medium.dense_padding_x, px(8.));
assert_eq!(resolved.sizes.medium.plate_inset, px(2.));
assert_eq!(resolved.sizes.medium.strip_height, px(32.));
assert_eq!(resolved.sizes.medium.icon_size, px(16.));
assert_eq!(resolved.sizes.medium.indicator_size, px(16.));
assert_eq!(resolved.sizes.medium.menu_item_height, px(32.));
assert_eq!(resolved.sizes.large.control_height, px(40.));
assert_eq!(resolved.sizes.large.text_size, px(16.));
assert_eq!(resolved.sizes.medium.switch_height, px(20.));
assert_eq!(resolved.sizes.medium.switch_width, px(36.));
assert_eq!(resolved.sizes.medium.switch_knob, px(14.));
assert_eq!(resolved.spacing.unit, px(4.));
assert_eq!(resolved.titlebar_height, px(40.));
assert_eq!(resolved.typography.body.size, px(16.));
}
}
#[test]
fn scrollbar_autohide_follows_the_platform_source() {
let mut stated = system(ThemeVariant::Dark, None);
stated.source = SourceKind::Macos;
assert!(resolve(&stated, None, None, None).scrollbar.autohide);
stated.source = SourceKind::Gnome;
assert!(resolve(&stated, None, None, None).scrollbar.autohide);
stated.source = SourceKind::Omarchy;
assert!(resolve(&stated, None, None, None).scrollbar.autohide);
stated.source = SourceKind::Windows;
assert!(!resolve(&stated, None, None, None).scrollbar.autohide);
stated.source = SourceKind::Kde;
assert!(!resolve(&stated, None, None, None).scrollbar.autohide);
stated.source = SourceKind::Fallback;
assert!(!resolve(&stated, None, None, None).scrollbar.autohide);
}
#[test]
fn accent_drives_primary_ring_and_selection() {
let red = hsla(0xdc2626);
let resolved = resolve(&system(ThemeVariant::Light, Some(red)), None, None, None);
assert_eq!(resolved.colors.accent, red);
assert_eq!(resolved.colors.primary, red);
assert_eq!(resolved.colors.ring, red);
assert_eq!(resolved.ramps.primary.base, red);
assert_eq!(resolved.colors.selection, resolved.ramps.primary.subtle);
let base = ColorTokens::light();
assert_eq!(resolved.colors.background, base.background);
assert_eq!(resolved.colors.foreground, base.foreground);
assert_eq!(resolved.colors.border, base.border);
}
#[test]
fn every_source_resolves_the_same_tokens() {
let mut stated = system(ThemeVariant::Dark, None);
stated.accent = Some(hsla(0x7aa2f7));
stated.window_background = Some(WindowBackgroundAppearance::Blurred);
let sources = [
SourceKind::Fallback,
SourceKind::Macos,
SourceKind::Windows,
SourceKind::Gnome,
SourceKind::Kde,
SourceKind::Omarchy,
];
let first = resolve(
&SystemTheme {
source: sources[0],
..stated
},
None,
None,
None,
);
for source in &sources[1..] {
let next = resolve(
&SystemTheme {
source: *source,
..stated
},
None,
None,
None,
);
assert_eq!(next.colors, first.colors, "{source:?}");
assert_eq!(next.radius, first.radius, "{source:?}");
assert_eq!(next.spacing, first.spacing, "{source:?}");
assert_eq!(next.elevations, first.elevations, "{source:?}");
assert_eq!(next.titlebar_height, first.titlebar_height, "{source:?}");
assert_eq!(next.root_font_size, first.root_font_size, "{source:?}");
assert_eq!(next.typography, first.typography, "{source:?}");
assert_eq!(next.sizes, first.sizes, "{source:?}");
}
}
#[test]
fn the_radius_ladder_follows_custom_values_with_preset_priority() {
let resolved = resolve(&system(ThemeVariant::Light, None), None, None, None);
assert_eq!(resolved.radius.sm, px(4.));
assert_eq!(resolved.radius.control, px(6.));
assert_eq!(resolved.radius.overlay, px(10.));
let def = ThemeDefinition::parse_toml("overlay_radius = 12.0\n").unwrap();
let overridden = resolve(&system(ThemeVariant::Light, None), Some(&def), None, None);
assert_eq!(overridden.radius.overlay, px(12.));
assert_eq!(overridden.radius.sm, px(4.));
let custom = CustomTheme {
sm_radius: Some(px(2.)),
control_radius: Some(px(9.)),
..CustomTheme::default()
};
let custom = resolve(
&system(ThemeVariant::Light, None),
Some(&def),
None,
Some(custom),
);
assert_eq!(custom.radius.sm, px(2.));
assert_eq!(custom.radius.control, px(9.));
assert_eq!(custom.radius.overlay, px(12.));
}
#[test]
fn elevations_follow_the_variant() {
let light = resolve(&system(ThemeVariant::Light, None), None, None, None);
assert_eq!(light.elevations.popover.shadows().len(), 2);
assert_eq!(light.elevations.dialog.shadows().len(), 2);
assert_eq!(light.elevations.control.shadows().len(), 1);
let dark = resolve(&system(ThemeVariant::Dark, None), None, None, None);
assert!(dark.elevations.popover.shadows().is_empty());
assert!(dark.elevations.dialog.shadows().is_empty());
assert_eq!(dark.elevations.control.shadows().len(), 1);
}
#[test]
fn the_platforms_window_material_resolves_into_the_theme() {
let mut stated = system(ThemeVariant::Light, None);
stated.window_background = Some(WindowBackgroundAppearance::MicaBackdrop);
let resolved = resolve(&stated, None, None, None);
assert_eq!(
resolved.window_background,
WindowBackgroundAppearance::MicaBackdrop
);
// Material surfaces stay translucent over the stated backdrop.
assert!(resolved.material.background.a < resolved.colors.background.a);
assert!(resolved.material.selection.a < 1.0);
let unstated = resolve(&system(ThemeVariant::Light, None), None, None, None);
assert_eq!(
unstated.window_background,
WindowBackgroundAppearance::Opaque
);
assert_eq!(unstated.material.background, unstated.colors.background);
assert_eq!(unstated.material.hover, unstated.colors.muted);
assert_eq!(unstated.material.selection, unstated.ramps.primary.base);
}
#[test]
fn reduced_transparency_forces_an_opaque_window_and_material() {
let mut stated = system(ThemeVariant::Dark, None);
stated.window_background = Some(WindowBackgroundAppearance::Blurred);
stated.accessibility.reduce_transparency = true;
let resolved = resolve(&stated, None, None, None);
assert_eq!(
resolved.window_background,
WindowBackgroundAppearance::Opaque
);
assert_eq!(resolved.material.background, resolved.colors.background);
assert_eq!(resolved.material.hover, resolved.colors.muted);
assert_eq!(resolved.material.selection, resolved.ramps.primary.base);
}
#[test]
fn a_custom_transparency_level_scales_the_material_fill() {
let mut stated = system(ThemeVariant::Dark, None);
stated.window_background = Some(WindowBackgroundAppearance::Blurred);
let custom = CustomTheme {
transparency: Some(80),
..CustomTheme::default()
};
let resolved = resolve(&stated, None, None, Some(custom));
assert_eq!(
resolved.window_background,
WindowBackgroundAppearance::Blurred
);
assert!((resolved.material.background.a - 0.32).abs() < 1e-6);
// Levels past 100 clamp, bottoming out at the dark floor.
let clamped = resolve(
&stated,
None,
None,
Some(CustomTheme {
transparency: Some(150),
..CustomTheme::default()
}),
);
assert!((clamped.material.background.a - 0.15).abs() < 1e-6);
}
#[test]
fn light_mode_keeps_a_heavier_fill_at_full_transparency() {
let mut light_system = system(ThemeVariant::Light, None);
light_system.window_background = Some(WindowBackgroundAppearance::Blurred);
let mut dark_system = system(ThemeVariant::Dark, None);
dark_system.window_background = Some(WindowBackgroundAppearance::Blurred);
let maxed = CustomTheme {
transparency: Some(100),
..CustomTheme::default()
};
let light = resolve(&light_system, None, None, Some(maxed));
let dark = resolve(&dark_system, None, None, Some(maxed));
assert!((light.material.background.a - 0.45).abs() < 1e-6);
assert!((dark.material.background.a - 0.15).abs() < 1e-6);
}
#[test]
fn transparency_zero_opens_an_opaque_window() {
let mut stated = system(ThemeVariant::Dark, None);
stated.window_background = Some(WindowBackgroundAppearance::Blurred);
let resolved = resolve(
&stated,
None,
None,
Some(CustomTheme {
transparency: Some(0),
..CustomTheme::default()
}),
);
assert_eq!(
resolved.window_background,
WindowBackgroundAppearance::Opaque
);
assert_eq!(resolved.material.background, resolved.colors.background);
assert_eq!(resolved.material.hover, resolved.colors.muted);
assert_eq!(resolved.material.selection, resolved.ramps.primary.base);
}
#[test]
fn the_transparency_knob_skips_windows_and_platforms_without_a_material() {
let mut windows = system(ThemeVariant::Dark, None);
windows.source = SourceKind::Windows;
windows.window_background = Some(WindowBackgroundAppearance::MicaBackdrop);
let resolved_windows = resolve(
&windows,
None,
None,
Some(CustomTheme {
transparency: Some(100),
..CustomTheme::default()
}),
);
assert_eq!(
resolved_windows.window_background,
WindowBackgroundAppearance::MicaBackdrop
);
// The knob's value is ignored on Windows; the default drives the fill.
let unset = resolve(&windows, None, None, None);
assert_eq!(
resolved_windows.material.background,
unset.material.background
);
let mut unstated = system(ThemeVariant::Dark, None);
unstated.window_background = None;
let resolved_unstated = resolve(
&unstated,
None,
None,
Some(CustomTheme {
transparency: Some(100),
..CustomTheme::default()
}),
);
assert_eq!(
resolved_unstated.window_background,
WindowBackgroundAppearance::Opaque
);
assert_eq!(
resolved_unstated.material.background,
resolved_unstated.colors.background
);
}
#[test]
fn overlay_is_translucent_and_overridable() {
let light = resolve(&system(ThemeVariant::Light, None), None, None, None);
let dark = resolve(&system(ThemeVariant::Dark, None), None, None, None);
// Both scrims are translucent black; dark is denser for contrast.
assert!(light.colors.overlay.a > 0.0 && light.colors.overlay.a < 0.5);
assert!(dark.colors.overlay.a > light.colors.overlay.a);
let def = ThemeDefinition::parse_toml("overlay = \"#000000\"").unwrap();
let overridden = resolve(&system(ThemeVariant::Light, None), Some(&def), None, None);
assert_eq!(overridden.colors.overlay, hsla(0x000000));
assert_eq!(overridden.colors.overlay.a, 1.0);
}
#[test]
fn elevations_and_motion_resolve_with_defaults() {
let resolved = resolve(&system(ThemeVariant::Light, None), None, None, None);
// The popover shadow is softer and tighter than the dialog's.
assert!(
resolved.elevations.popover.layers[0].blur_radius
< resolved.elevations.dialog.layers[0].blur_radius
);
assert!(resolved.motion.overlay > std::time::Duration::ZERO);
let shadows = resolved.elevations.popover.shadows();
assert_eq!(shadows.len(), 2);
assert!(!shadows[0].inset);
}
#[test]
fn preset_beats_system_and_the_mode_override_beats_everything() {
let def = ThemeDefinition::parse_toml(
"mode = \"dark\"\nbackground = \"#1a1b26\"\naccent = \"#7aa2f7\"\ncontrol_radius = 12.0\n",
)
.unwrap();
let resolved = resolve(&system(ThemeVariant::Light, None), Some(&def), None, None);
assert_eq!(resolved.variant, ThemeVariant::Dark);
// Compare in RGB space: Hsla round-trips are lossy at low saturation.
let bg = Rgba::from(resolved.colors.background);
assert!((bg.r - 0x1a as f32 / 255.0).abs() < 0.001);
assert!((bg.b - 0x26 as f32 / 255.0).abs() < 0.001);
let accent = Rgba::from(resolved.colors.accent);
assert!((accent.r - 0x7a as f32 / 255.0).abs() < 0.001);
// And the metric override beats the radius default.
assert_eq!(resolved.radius.control, px(12.));
let forced = resolve(
&system(ThemeVariant::Light, None),
Some(&def),
Some(ThemeVariant::Light),
None,
);
assert_eq!(forced.variant, ThemeVariant::Light);
// The override flips the variant (and with it the base roles), but
// the preset's explicit background still wins over the base.
let forced_bg = Rgba::from(forced.colors.background);
assert!((forced_bg.r - 0x1a as f32 / 255.0).abs() < 0.001);
}
#[test]
fn an_accent_only_override_derives_foregrounds_and_ramps() {
let def = ThemeDefinition::parse_toml("mode = \"dark\"\naccent = \"#7aa2f7\"\n").unwrap();
let resolved = resolve(&system(ThemeVariant::Dark, None), Some(&def), None, None);
// The definition sets accent only; primary follows it, and both
// foregrounds derive from their own color.
let accent = Rgba::from(resolved.colors.accent);
assert!((accent.r - 0x7a as f32 / 255.0).abs() < 0.001);
assert_eq!(resolved.colors.primary, resolved.colors.accent);
assert_eq!(resolved.colors.ring, resolved.colors.accent);
assert_eq!(resolved.colors.selection, resolved.ramps.primary.subtle);
assert_eq!(resolved.ramps.primary.base, resolved.colors.primary);
assert_eq!(resolved.ramps.destructive.base, resolved.colors.destructive);
assert_ne!(resolved.ramps.primary.hover, resolved.ramps.primary.base);
assert_ne!(resolved.ramps.primary.subtle, resolved.ramps.primary.base);
}
#[test]
fn system_accent_becomes_primary() {
let resolved = resolve(
&system(ThemeVariant::Dark, Some(hsla(0x7aa2f7))),
None,
None,
None,
);
let primary = Rgba::from(resolved.colors.primary);
assert!((primary.r - 0x7a as f32 / 255.0).abs() < 0.001);
assert_eq!(resolved.ramps.primary.base, resolved.colors.primary);
assert_eq!(
resolved.colors.primary_foreground,
resolved.colors.accent_foreground
);
}
#[test]
fn high_contrast_sharpens_the_border_toward_the_foreground() {
let mut a11y = AccessibilityPreferences::default();
let plain = resolve(&system(ThemeVariant::Dark, None), None, None, None);
a11y.high_contrast = true;
let contrasted = resolve(
&SystemTheme {
accessibility: a11y,
..system(ThemeVariant::Dark, None)
},
None,
None,
None,
);
let plain_border = Oklch::from_hsla(plain.colors.border);
let contrasted_border = Oklch::from_hsla(contrasted.colors.border);
let foreground_l = Oklch::from_hsla(plain.colors.foreground).l;
assert!((contrasted_border.l - foreground_l).abs() < (plain_border.l - foreground_l).abs());
}
#[test]
fn every_resolved_role_is_distinct_enough_to_paint_with() {
for variant in [ThemeVariant::Light, ThemeVariant::Dark] {
let resolved = resolve(&system(variant, None), None, None, None);
assert_ne!(resolved.colors.background, resolved.colors.foreground);
assert_ne!(resolved.colors.border, resolved.colors.background);
assert_ne!(resolved.colors.muted_foreground, resolved.colors.foreground);
assert!(resolved.colors.ring != resolved.colors.background);
}
}
#[test]
fn info_stays_calm_when_the_accent_warms_up() {
// The whole point of the info role: a warm platform accent (GNOME
// red, Omarchy red themes) must not make Info toasts read as Danger.
let red_accent = hsla(0xdc2626);
let resolved = resolve(
&system(ThemeVariant::Dark, Some(red_accent)),
None,
None,
None,
);
assert_eq!(resolved.ramps.primary.base, red_accent);
assert_ne!(resolved.colors.info, resolved.colors.destructive);
assert_ne!(resolved.ramps.info.base, resolved.ramps.destructive.base);
}
#[test]
fn info_follows_the_preset() {
let def = ThemeDefinition::parse_toml("info = \"#0ea5e9\"\n").unwrap();
let resolved = resolve(&system(ThemeVariant::Dark, None), Some(&def), None, None);
assert_eq!(resolved.colors.info, hsla(0x0ea5e9));
assert_eq!(resolved.ramps.info.base, hsla(0x0ea5e9));
}
#[test]
fn a_custom_theme_beats_the_preset_and_defaults() {
let def = ThemeDefinition::parse_toml("overlay_radius = 12.0\n").unwrap();
let popover = Elevation::single(Shadow::black(0., 2., 8., 0.3));
let custom = CustomTheme {
sm_radius: Some(px(2.)),
control_radius: Some(px(4.)),
popover_elevation: Some(popover),
..CustomTheme::default()
};
let resolved = resolve(
&system(ThemeVariant::Light, None),
Some(&def),
None,
Some(custom),
);
assert_eq!(resolved.radius.control, px(4.));
assert_eq!(resolved.radius.sm, px(2.));
// Fields the custom theme leaves unstated keep the resolved values.
assert_eq!(resolved.radius.overlay, px(12.));
assert_eq!(resolved.elevations.popover, popover);
let defaults = Elevations::for_variant(ThemeVariant::Light);
assert_eq!(resolved.elevations.dialog, defaults.dialog);
assert_eq!(resolved.elevations.control, defaults.control);
}
#[test]
fn a_custom_root_font_size_rescales_the_table() {
let custom = CustomTheme {
root_font_size: Some(px(20.)),
..CustomTheme::default()
};
let resolved = resolve(&system(ThemeVariant::Light, None), None, None, Some(custom));
assert_eq!(resolved.root_font_size, px(20.));
assert_eq!(resolved.sizes.medium.control_height, px(40.));
assert_eq!(resolved.sizes.medium.text_size, px(17.5));
assert_eq!(resolved.spacing.unit, px(5.));
assert_eq!(resolved.titlebar_height, px(50.));
assert_eq!(resolved.typography.body.size, px(20.));
assert_eq!(resolved.typography.large_title.size, px(30.));
}
}