//! Arena Tracker's two palettes and the fonts registered once at startup. use std::{borrow::Cow, rc::Rc}; use gpui_kit::component::{ActiveTheme as _, Theme}; use gpui_kit::*; /// DirectWrite exposes Inter's text optical size as its own family. const INTER: &str = "Inter Variable Text"; const UNBOUNDED: &str = "Unbounded"; /// Gold on the logo and on dark portrait overlays in either theme. pub const LOGO_GOLD: u32 = 0xf5b83d; const LIGHT_GOLD: u32 = 0xe3a21a; /// Dark ink for text and icons on a solid gold fill. pub const ON_GOLD: u32 = 0x161b2e; /// Background of a card or statistics tile. pub fn surface(theme: &Theme) -> Hsla { theme.muted } /// Background of the home and champ select headers. pub fn banner_band(theme: &Theme) -> Hsla { theme.title_bar } /// Gold for text on a neutral surface, darker in the light theme. pub fn gold_ink(theme: &Theme) -> Hsla { theme.link } /// The gold glow around a card lifted under the pointer. pub fn gold_glow(theme: &Theme) -> Hsla { if theme.mode.is_dark() { Hsla::from(rgb(LOGO_GOLD)).opacity(0.55) } else { Hsla::from(rgb(LIGHT_GOLD)).opacity(0.45) } } /// The pale gold of a played champion's tick on the ring. pub fn ring_played(theme: &Theme) -> Hsla { gold_soft(theme) } /// The reference's soft gold: a played champion's tick, the light sweeping the window. pub fn gold_soft(theme: &Theme) -> Hsla { if theme.mode.is_dark() { Hsla::from(rgb(LOGO_GOLD)).opacity(0.34) } else { Hsla::from(rgb(0xd4920e)).opacity(0.4) } } /// The faint tick of a never played champion. pub fn ring_never(theme: &Theme) -> Hsla { theme.switch } struct HeadingFamily(SharedString); impl Global for HeadingFamily {} /// Builds a semibold heading with the family selected at startup. pub fn heading(cx: &App) -> Div { let family = cx .try_global::() .map_or_else(|| &cx.theme().font_family, |heading| &heading.0); div() .font_family(family.clone()) .font_weight(FontWeight::SEMIBOLD) } /// Registers the embedded face and checks that the platform resolves the requested weight. fn load_font(cx: &App, bytes: &'static [u8], family: &'static str, weight: FontWeight) -> bool { let text = cx.text_system(); if let Err(error) = text.add_fonts(vec![Cow::Borrowed(bytes)]) { log::error!("Failed to load {family}: {error}"); return false; } // Naming a missing family incurs a fallback lookup on every text run. let mut requested = font(family); requested.weight = weight; text.get_font_for_id(text.resolve_font(&requested)) == Some(requested) } /// Sets up the app palettes and fonts after `gpui_kit::init`. pub fn init(cx: &mut App) { init_with_heading_font(cx, include_bytes!("../assets/Unbounded-SemiBold.ttf")); } fn init_with_heading_font(cx: &mut App, heading_bytes: &'static [u8]) { let inter = load_font( cx, include_bytes!("../assets/InterVariable.ttf"), INTER, FontWeight::NORMAL, ); let heading = if load_font(cx, heading_bytes, UNBOUNDED, FontWeight::SEMIBOLD) { UNBOUNDED.into() } else { cx.theme().font_family.clone() }; cx.set_global(HeadingFamily(heading)); let theme = Theme::global_mut(cx); for config in [&mut theme.light_theme, &mut theme.dark_theme] { let config = Rc::make_mut(config); if inter { config.font_family = Some(INTER.into()); } // Several kit tokens share each palette role. Register them on the saved configs // so changing appearance reapplies the same palette as startup. macro_rules! role { ($dark:expr, $light:expr => $($token:ident),+ ) => {{ let value = if config.mode.is_dark() { $dark } else { $light }; let hex = SharedString::from(format!("#{value:06x}")); $(config.colors.$token = Some(hex.clone());)+ }}; } role!(0x11152a, 0xeef0f7 => background); role!(0x151a33, 0xe5e8f3 => title_bar, table_head, list_head); role!(0x181e38, 0xffffff => table, list, popover, muted, button, list_even, table_even); role!(0x20274a, 0xe4e8f4 => secondary, secondary_hover, accent, button_hover, table_hover, list_hover, tab_bar, tab_bar_segmented); role!(0x283053, 0xd2d7e9 => border, input, title_bar_border, window_border, table_row_border, chart_grid, secondary_active, button_active); role!(0xedeaf7, ON_GOLD => foreground, popover_foreground, accent_foreground, secondary_foreground); role!(0x8b91b1, 0x596082 => muted_foreground, tab_foreground, table_head_foreground, scrollbar_thumb_hover); role!(0x3a4166, 0xc5cbe0 => switch, scrollbar_thumb); role!(LOGO_GOLD, LIGHT_GOLD => primary, tab_active, chart_1, chart_2, chart_3, chart_4, chart_5, slider_bar, progress_bar); role!(LOGO_GOLD, 0x8f5d00 => link, link_hover, link_active, caret, warning, ring, drag_border, list_active_border, table_active_border); role!(0xff7a7a, 0xc62f2f => danger); role!(0x67e8f9, 0x11667b => info); role!(0x4ade80, 0x196638 => success); role!(ON_GOLD, ON_GOLD => primary_foreground, tab_active_foreground); let gold = if config.mode.is_dark() { LOGO_GOLD } else { LIGHT_GOLD }; let tint = |alpha: u8| Some(SharedString::from(format!("#{gold:06x}{alpha:02x}"))); config.colors.selection = tint(0x38); // The kit caps row highlights at 20% opacity. config.colors.list_active = tint(0x33); config.colors.table_active = config.colors.list_active.clone(); // Let the kit derive pressed and hovered primary buttons from our gold. config.colors.primary_active = None; config.colors.primary_hover = None; } Theme::change(theme.mode, None, cx); } #[cfg(test)] mod tests { use super::{INTER, UNBOUNDED, banner_band, gold_ink, heading, surface}; use gpui_kit::component::{Theme, ThemeMode}; use gpui_kit::{FontWeight, Hsla, Styled as _, TestAppContext, font, px}; // GPUI's usual test text system is a no-op. These tests exercise DirectWrite. fn windows_context() -> TestAppContext { let platform = gpui_kit::platform::current_platform(false); TestAppContext::build_with_text_system( gpui::TestDispatcher::new(0), None, platform.text_system(), ) } #[test] fn unreadable_unbounded_keeps_the_system_heading_in_both_themes() { windows_context().update(|cx| { gpui_kit::init(cx); let system = Theme::global(cx).font_family.clone(); super::init_with_heading_font(cx, b"invalid font file"); for mode in [ThemeMode::Dark, ThemeMode::Light] { Theme::change(mode, None, cx); assert_eq!(Theme::global(cx).font_family.as_str(), INTER); assert_eq!(heading(cx).text_style().font_family.as_ref(), Some(&system)); } }); } #[track_caller] fn assert_contrast(ink: Hsla, background: Hsla, minimum: f32) { let [a, b] = [ink, background].map(|color| { let rgb = color.to_rgb(); let [r, g, b] = [rgb.r, rgb.g, rgb.b].map(|v| { if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } }); 0.2126 * r + 0.7152 * g + 0.0722 * b }); let ratio = (a.max(b) + 0.05) / (a.min(b) + 0.05); assert!( ratio >= minimum, "{ink:?} on {background:?}: {ratio:.2}:1 < {minimum}:1" ); } #[test] fn identity_fonts_and_colors_are_used_in_both_themes() { let cx = windows_context(); cx.update(|cx| { gpui_kit::init(cx); super::init(cx); assert_eq!( Theme::global(cx).background, if Theme::global(cx).mode.is_dark() { gpui::rgb(0x11152a) } else { gpui::rgb(0xeef0f7) } .into() ); for mode in [ThemeMode::Light, ThemeMode::Dark] { Theme::change(mode, None, cx); let theme = Theme::global(cx); assert_eq!(theme.font_family.as_str(), INTER); let style = heading(cx).text_style().clone(); let family = style.font_family.unwrap(); assert_eq!(family.as_str(), UNBOUNDED); let mut face = font(family); face.weight = style.font_weight.unwrap(); assert_eq!(face.weight, FontWeight::SEMIBOLD); let text = cx.text_system(); assert_eq!(text.get_font_for_id(text.resolve_font(&face)), Some(face)); macro_rules! colors { ($dark:literal, $light:literal => $($token:ident),+) => {{ let expected: Hsla = gpui::rgb(if mode.is_dark() { $dark } else { $light }).into(); $(assert_eq!(theme.colors.$token, expected, "{} {mode:?}", stringify!($token));)+ }}; } colors!(0x11152a, 0xeef0f7 => background); colors!(0x151a33, 0xe5e8f3 => title_bar, table_head, list_head); colors!(0x181e38, 0xffffff => table, list, popover, muted, button, list_even, table_even); colors!(0x20274a, 0xe4e8f4 => secondary, secondary_hover, accent, button_hover, table_hover, list_hover, tab_bar, tab_bar_segmented); colors!(0x283053, 0xd2d7e9 => border, input, title_bar_border, window_border, table_row_border, chart_grid, secondary_active, button_active); colors!(0xedeaf7, 0x161b2e => foreground, popover_foreground, accent_foreground, secondary_foreground); colors!(0x8b91b1, 0x596082 => muted_foreground, tab_foreground, table_head_foreground, scrollbar_thumb_hover); colors!(0x3a4166, 0xc5cbe0 => switch, scrollbar_thumb); colors!(0xf5b83d, 0xe3a21a => primary, tab_active, chart_1, chart_2, chart_3, chart_4, chart_5, slider_bar, progress_bar); colors!(0xf5b83d, 0x8f5d00 => link, link_hover, link_active, warning, caret, ring, drag_border, list_active_border, table_active_border); colors!(0xff7a7a, 0xc62f2f => danger); colors!(0x67e8f9, 0x11667b => info); colors!(0x4ade80, 0x196638 => success); colors!(0x161b2e, 0x161b2e => primary_foreground, button_primary_foreground, tab_active_foreground); let selection: Hsla = gpui::rgba(if mode.is_dark() { 0xf5b83d38 } else { 0xe3a21a38 }).into(); assert_eq!(theme.selection, selection); // The kit caps selected rows at 20% opacity. assert_eq!(theme.list_active, theme.primary.opacity(0.2)); assert_eq!(theme.table_active, theme.primary.opacity(0.2)); for bg in [theme.background, banner_band(theme), surface(theme), theme.secondary] { for ink in [theme.foreground, theme.muted_foreground, gold_ink(theme)] { assert_contrast(ink, bg, 4.5); } assert_contrast(theme.ring, bg, 3.); } assert_contrast(theme.primary_foreground, theme.primary, 4.5); for ink in [theme.warning, theme.danger, theme.info, theme.success] { // Alert paints its text over a 4% tint of the same colour. assert_contrast(ink, theme.background.blend(ink.opacity(0.04)), 4.5); } } // DirectWrite returns the nearest weight, so only a wider glyph proves a semibold face. let text = cx.text_system(); let [regular, semibold] = [FontWeight::NORMAL, FontWeight::SEMIBOLD].map(|weight| { let mut requested = font(INTER); requested.weight = weight; let id = text.resolve_font(&requested); assert_eq!(text.get_font_for_id(id), Some(requested)); text.advance(id, px(14.), 'm').unwrap().width }); assert!(semibold > regular, "{semibold:?} <= {regular:?}"); }); } }