From 62e6b5c64590dc99c3f78b42688e6912d8536fc0 Mon Sep 17 00:00:00 2001 From: fkDead Date: Wed, 11 Nov 2026 20:20:49 +0100 Subject: [PATCH] capri-ui: add theme module --- crates/capri-ui/src/theme.rs | 148 +++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 crates/capri-ui/src/theme.rs diff --git a/crates/capri-ui/src/theme.rs b/crates/capri-ui/src/theme.rs new file mode 100644 index 0000000..1115213 --- /dev/null +++ b/crates/capri-ui/src/theme.rs @@ -0,0 +1,148 @@ +//! Capri look: cyan signal, pink rail, acid highlights on dark translucent panels. +//! The active palette is process-global so every window (companion, settings, overlay) shares it. +use std::sync::Arc; + +use eframe::egui::{self, Color32, CornerRadius, FontData, FontDefinitions, FontFamily, Frame, Margin, Stroke}; +use parking_lot::RwLock; + +use crate::settings::{Palette, Theme}; + +pub const ACID: Color32 = Color32::from_rgb(0xf4, 0xed, 0x75); +pub const TEXT: Color32 = Color32::from_rgb(0xf5, 0xf9, 0xfa); +pub const MUTED: Color32 = Color32::from_rgb(0xd5, 0xe4, 0xe7); +pub const NOTICE: Color32 = Color32::from_rgb(0xff, 0xe0, 0xe6); +pub const RED: Color32 = Color32::from_rgb(0xff, 0x56, 0x79); +pub const VIOLET: Color32 = Color32::from_rgb(0xa2, 0x95, 0xff); +pub const SETTINGS_BG: Color32 = Color32::from_rgb(0x07, 0x13, 0x1c); + +pub const fn alpha(r: u8, g: u8, b: u8, a: u8) -> Color32 { + Color32::from_rgba_unmultiplied_const(r, g, b, a) +} + +fn with_alpha(c: Color32, a: u8) -> Color32 { + Color32::from_rgba_unmultiplied(c.r(), c.g(), c.b(), a) +} + +static PALETTE: RwLock> = RwLock::new(None); + +pub fn palette() -> Palette { + PALETTE.read().unwrap_or_else(|| Theme::default().palette()) +} +pub fn cyan() -> Color32 { + palette().cyan +} +pub fn pink() -> Color32 { + palette().pink +} + +const EXO2: &[u8] = include_bytes!("../../../assets/fonts/exo2/Exo2-Variable.ttf"); + +/// System fallbacks (fontconfig paths on Arch): Exo 2 covers Latin and Cyrillic only. +fn fallback_files(locale: &str) -> Vec<&'static str> { + let script = match locale { + "ar" | "fa" | "ur" => "noto/NotoSansArabic-Regular.ttf", + "he" => "noto/NotoSansHebrew-Regular.ttf", + "hi" | "mr" => "noto/NotoSansDevanagari-Regular.ttf", + "bn" => "noto/NotoSansBengali-Regular.ttf", + "ta" => "noto/NotoSansTamil-Regular.ttf", + "te" => "noto/NotoSansTelugu-Regular.ttf", + "gu" => "noto/NotoSansGujarati-Regular.ttf", + "pa" => "noto/NotoSansGurmukhi-Regular.ttf", + "th" => "noto/NotoSansThai-Regular.ttf", + "zh" | "ja" | "ko" => "noto-cjk/NotoSansCJK-Regular.ttc", + _ => "", + }; + ["noto/NotoSans-Regular.ttf", script, "TTF/DejaVuSans.ttf", "noto/NotoSansSymbols2-Regular.ttf"].into_iter().filter(|f| !f.is_empty()).collect() +} + +/// Installs Exo 2 plus the fallbacks the interface language needs. +// TODO: fixed fontconfig paths and no text shaping (Arabic letters stay unjoined); use fontconfig and a shaping renderer. +pub fn install_fonts(ctx: &egui::Context, locale: &str) { + let mut fonts = FontDefinitions::default(); + fonts.font_data.insert("exo2".into(), Arc::new(FontData::from_static(EXO2))); + let mut chain = vec!["exo2".to_string()]; + for file in fallback_files(locale) { + if let Ok(bytes) = std::fs::read(format!("/usr/share/fonts/{file}")) { + fonts.font_data.insert(file.into(), Arc::new(FontData::from_owned(bytes))); + chain.push(file.into()); + } + } + let proportional = fonts.families.get_mut(&FontFamily::Proportional).unwrap(); + for (i, name) in chain.iter().enumerate() { + proportional.insert(i, name.clone()); + } + fonts.families.get_mut(&FontFamily::Monospace).unwrap().extend(chain[1..].iter().cloned()); + ctx.set_fonts(fonts); +} + +/// Applies the theme to the shared palette and to egui's widget visuals. +pub fn apply(ctx: &egui::Context, theme: &Theme) { + let p = theme.palette(); + *PALETTE.write() = Some(p); + let mut visuals = egui::Visuals::dark(); + let (window, extreme) = if p.capri { (alpha(0x08, 0x14, 0x1b, 0xf7), alpha(0x07, 0x11, 0x1b, 0xf5)) } else { (with_alpha(p.background, 0xf7), with_alpha(p.background, 0xf5)) }; + visuals.panel_fill = Color32::TRANSPARENT; + visuals.window_fill = window; + visuals.override_text_color = Some(if p.capri { TEXT } else { p.text }); + visuals.selection.bg_fill = with_alpha(p.cyan, 0x50); + visuals.selection.stroke = Stroke::new(1.0, p.cyan); + visuals.extreme_bg_color = extreme; + visuals.hyperlink_color = p.cyan; + for w in [&mut visuals.widgets.inactive, &mut visuals.widgets.hovered, &mut visuals.widgets.active, &mut visuals.widgets.noninteractive] { + w.corner_radius = CornerRadius::same(2); + } + let raised = if p.capri { alpha(0x10, 0x2a, 0x33, 0xf0) } else { with_alpha(p.panel, 0xf0) }; + visuals.widgets.inactive.weak_bg_fill = raised; + visuals.widgets.inactive.bg_fill = raised; + visuals.widgets.inactive.bg_stroke = Stroke::new(1.0, with_alpha(p.cyan, 0x90)); + visuals.widgets.hovered.weak_bg_fill = if p.capri { alpha(0x1b, 0x41, 0x4c, 0xf5) } else { p.panel }; + visuals.widgets.hovered.bg_stroke = Stroke::new(1.0, p.cyan); + visuals.widgets.active.bg_stroke = Stroke::new(1.0, Color32::from_rgb(0xbe, 0xff, 0xf2)); + visuals.widgets.noninteractive.bg_stroke = Stroke::new(1.0, with_alpha(p.border, 0xc0)); + ctx.set_visuals(visuals); +} + +pub fn header_frame() -> Frame { + Frame::new().fill(alpha(0x08, 0x14, 0x1b, 0xe8)).stroke(Stroke::new(1.0, with_alpha(cyan(), 0xbd))).inner_margin(Margin::symmetric(10, 6)) +} + +pub fn composer_frame() -> Frame { + Frame::new() + .fill(alpha(0x0e, 0x1e, 0x27, 0xf5)) + .stroke(Stroke::new(1.0, with_alpha(cyan(), 0xd4))) + .inner_margin(Margin { left: 12, right: 8, top: 8, bottom: 8 }) +} + +pub fn reply_frame() -> Frame { + Frame::new() + .fill(alpha(0x0c, 0x12, 0x1a, 0xee)) + .stroke(Stroke::new(1.0, with_alpha(cyan(), 0xbd))) + .inner_margin(Margin { left: 14, right: 10, top: 8, bottom: 8 }) + .outer_margin(Margin::symmetric(8, 8)) +} + +pub fn panel_frame() -> Frame { + Frame::new() + .fill(alpha(0x08, 0x14, 0x1b, 0xf7)) + .stroke(Stroke::new(1.0, with_alpha(cyan(), 0xf2))) + .inner_margin(Margin::same(10)) + .outer_margin(Margin::same(6)) +} + +/// Error notice card; `warning` uses the acid frame. +pub fn notice_frame(warning: bool) -> Frame { + let (fill, stroke) = if warning { (alpha(0x21, 0x1e, 0x15, 0xf5), ACID) } else { (alpha(0x10, 0x18, 0x22, 0xf5), RED) }; + Frame::new().fill(fill).stroke(Stroke::new(1.0, stroke)).inner_margin(Margin::same(8)).outer_margin(Margin::symmetric(8, 4)) +} + +pub fn approval_frame() -> Frame { + Frame::new() + .fill(Color32::from_rgb(0x09, 0x1a, 0x21)) + .stroke(Stroke::new(1.5, Color32::from_rgb(0xf5, 0xe9, 0x55))) + .inner_margin(Margin::same(10)) + .outer_margin(Margin::symmetric(8, 4)) +} + +pub fn with(c: Color32, a: u8) -> Color32 { + with_alpha(c, a) +}