Files
gpui-native-kit/crates/theme/src/system.rs
T

143 lines
4.7 KiB
Rust

//! The normalized snapshot a platform adapter produces.
use gpui::{Hsla, Pixels, px};
use crate::ThemeVariant;
/// Accessibility preferences read from the OS, forwarded into resolution.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct AccessibilityPreferences {
pub high_contrast: bool,
pub reduce_motion: bool,
pub reduce_transparency: bool,
}
/// Which adapter produced a [`SystemTheme`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(not(any(target_os = "linux", target_os = "windows")), allow(dead_code))]
pub enum SourceKind {
#[default]
Fallback,
Macos,
Omarchy,
Kde,
Gnome,
Windows,
}
impl SourceKind {
pub fn label(self) -> &'static str {
match self {
Self::Fallback => "neutral fallback",
Self::Macos => "macOS (NSColor)",
Self::Omarchy => "Omarchy (colors.toml)",
Self::Kde => "KDE (kdeglobals)",
Self::Gnome => "GNOME (portal)",
Self::Windows => "Windows 11 (UISettings)",
}
}
/// Overlay scrollbars hide while idle on macOS, GNOME, and Omarchy.
///
/// Windows and KDE keep theirs drawn.
pub fn scrollbar_autohide(self) -> bool {
matches!(self, Self::Macos | Self::Omarchy | Self::Gnome)
}
/// The base spacing unit:
///
/// - GNOME lays out on a 6px grid
/// - Other platform on 4px grid
pub fn spacing_unit(self) -> Pixels {
match self {
Self::Gnome | Self::Omarchy => px(6.),
_ => px(4.),
}
}
}
/// A partial palette with optional colors.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct PartialPalette {
pub background: Option<Hsla>,
pub foreground: Option<Hsla>,
pub surface: Option<Hsla>,
pub surface_foreground: Option<Hsla>,
pub primary_foreground: Option<Hsla>,
pub accent_foreground: Option<Hsla>,
pub secondary: Option<Hsla>,
pub secondary_foreground: Option<Hsla>,
pub muted: Option<Hsla>,
/// No platform states a hover wash, presets override it.
pub muted_hover: Option<Hsla>,
pub muted_foreground: Option<Hsla>,
pub destructive: Option<Hsla>,
/// No platform states severity colors, presets override them.
pub success: Option<Hsla>,
pub warning: Option<Hsla>,
pub info: Option<Hsla>,
pub border: Option<Hsla>,
pub input: Option<Hsla>,
pub ring: Option<Hsla>,
pub selection: Option<Hsla>,
/// No platform states a scrim color; preset and user files override it.
pub overlay: Option<Hsla>,
/// No platform states a scrollbar thumb color; presets override it.
pub scrollbar_thumb: Option<Hsla>,
}
/// A normalized snapshot of the running system's theme.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SystemTheme {
pub variant: ThemeVariant,
pub accent: Option<Hsla>,
pub palette: PartialPalette,
/// The platform's stated control corner radius, if it has one.
pub control_radius: Option<Pixels>,
/// The platform's stated menu/popover radius, if it has one.
pub overlay_radius: Option<Pixels>,
/// The platform's stated checkbox radius, if it has one.
pub sm_radius: Option<Pixels>,
/// The platform's stated control height, if it has one.
pub control_height: Option<Pixels>,
/// The platform's stated text-field height, if it has one.
pub input_height: Option<Pixels>,
/// The platform's stated menu row height, if it has one.
pub menu_item_height: Option<Pixels>,
/// The platform's stated switch track height, if it has one.
pub switch_height: Option<Pixels>,
/// The platform's stated switch track width, if it has one.
pub switch_width: Option<Pixels>,
/// The platform's stated switch knob size, if it has one.
pub switch_knob: Option<Pixels>,
/// The platform's native titlebar height, if it states one.
pub titlebar_height: Option<Pixels>,
/// The platform's native root font size, if it has one.
pub root_font_size: Option<Pixels>,
pub accessibility: AccessibilityPreferences,
pub source: SourceKind,
}
impl SystemTheme {
pub fn fallback(variant: ThemeVariant) -> Self {
Self {
variant,
accent: None,
palette: PartialPalette::default(),
control_radius: None,
overlay_radius: None,
sm_radius: None,
control_height: None,
input_height: None,
menu_item_height: None,
switch_height: None,
switch_width: None,
switch_knob: None,
titlebar_height: None,
root_font_size: None,
accessibility: AccessibilityPreferences::default(),
source: SourceKind::Fallback,
}
}
}