Files
chandra/host-native/src/theme.rs
T

141 lines
4.3 KiB
Rust

use serde::Deserialize;
use std::path::Path;
#[derive(Debug, Clone, Copy)]
pub struct Rgba {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Rgba {
pub fn from_hex(s: &str) -> anyhow::Result<Self> {
let hex = s.strip_prefix('#').unwrap_or(s);
let bytes = hex.as_bytes();
let parse_byte = |i: usize| -> anyhow::Result<u8> {
let sub = std::str::from_utf8(&bytes[i..i + 2])?;
Ok(u8::from_str_radix(sub, 16)?)
};
match bytes.len() {
6 => Ok(Self {
r: parse_byte(0)?,
g: parse_byte(2)?,
b: parse_byte(4)?,
a: 255,
}),
8 => Ok(Self {
r: parse_byte(0)?,
g: parse_byte(2)?,
b: parse_byte(4)?,
a: parse_byte(6)?,
}),
_ => anyhow::bail!("invalid hex color: {s}"),
}
}
}
#[derive(Debug, Clone)]
pub struct ThemePalette {
pub desktop_top: Rgba,
pub desktop_bottom: Rgba,
pub surface: Rgba,
pub surface_hover: Rgba,
pub surface_popup: Rgba,
pub foreground: Rgba,
pub foreground_muted: Rgba,
pub accent: Rgba,
pub accent_foreground: Rgba,
pub border: Rgba,
}
#[derive(Debug, Clone)]
pub struct ThemeTokens {
pub name: String,
pub palette: ThemePalette,
pub font_size: u32,
pub radius: u32,
pub spacing: u32,
pub panel_height: u32,
}
#[derive(Deserialize)]
struct RawPalette {
#[serde(rename = "desktop-top")]
desktop_top: String,
#[serde(rename = "desktop-bottom")]
desktop_bottom: String,
surface: String,
#[serde(rename = "surface-hover")]
surface_hover: String,
#[serde(rename = "surface-popup")]
surface_popup: String,
foreground: String,
#[serde(rename = "foreground-muted")]
foreground_muted: String,
accent: String,
#[serde(rename = "accent-foreground")]
accent_foreground: String,
border: String,
}
#[derive(Deserialize)]
struct RawTheme {
name: String,
palette: RawPalette,
#[serde(rename = "font-size")]
font_size: u32,
radius: u32,
spacing: u32,
#[serde(rename = "panel-height")]
panel_height: u32,
}
impl ThemeTokens {
pub fn load_file(path: &Path) -> anyhow::Result<Self> {
let text = std::fs::read_to_string(path)?;
let raw: RawTheme = serde_json::from_str(&text)?;
Ok(Self {
name: raw.name,
palette: ThemePalette {
desktop_top: Rgba::from_hex(&raw.palette.desktop_top)?,
desktop_bottom: Rgba::from_hex(&raw.palette.desktop_bottom)?,
surface: Rgba::from_hex(&raw.palette.surface)?,
surface_hover: Rgba::from_hex(&raw.palette.surface_hover)?,
surface_popup: Rgba::from_hex(&raw.palette.surface_popup)?,
foreground: Rgba::from_hex(&raw.palette.foreground)?,
foreground_muted: Rgba::from_hex(&raw.palette.foreground_muted)?,
accent: Rgba::from_hex(&raw.palette.accent)?,
accent_foreground: Rgba::from_hex(&raw.palette.accent_foreground)?,
border: Rgba::from_hex(&raw.palette.border)?,
},
font_size: raw.font_size,
radius: raw.radius,
spacing: raw.spacing,
panel_height: raw.panel_height,
})
}
pub fn moon_night_default() -> Self {
Self {
name: "Moon Night".to_string(),
palette: ThemePalette {
desktop_top: Rgba { r: 0x16, g: 0x21, b: 0x3e, a: 0xff },
desktop_bottom: Rgba { r: 0x0b, g: 0x10, b: 0x20, a: 0xff },
surface: Rgba { r: 0x1f, g: 0x29, b: 0x37, a: 0xf2 },
surface_hover: Rgba { r: 0x37, g: 0x41, b: 0x51, a: 0xff },
surface_popup: Rgba { r: 0x11, g: 0x18, b: 0x27, a: 0xf5 },
foreground: Rgba { r: 0xf3, g: 0xf4, b: 0xf6, a: 0xff },
foreground_muted: Rgba { r: 0x9c, g: 0xa3, b: 0xaf, a: 0xff },
accent: Rgba { r: 0x7c, g: 0x9c, b: 0xff, a: 0xff },
accent_foreground: Rgba { r: 0x0b, g: 0x10, b: 0x20, a: 0xff },
border: Rgba { r: 0x3b, g: 0x46, b: 0x58, a: 0xff },
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
}