//! The Voelin theme, in two layers (docs/ui.md, "The GPUI front end"): //! //! 1. [`VoelinTheme`], a GPUI global with every token of //! `crates/voelin-ui/ui/theme.slint`, light and dark ([`tokens`]), which a //! test keeps equal to the Slint file ([`slint_parity`]). The shell's own //! elements (the server rail, presence dots, meters, the backdrop) read it. //! 2. gpui-component's `ThemeColor`, the colours its widgets (inputs, lists, //! menus, dialogs) read, written from the first on every change //! ([`components`]), so that they look like ours. //! //! The mode is the client's `ui.theme`: `system` follows the window's //! appearance (and its changes), `light` and `dark` do not. mod components; #[cfg(test)] mod slint_parity; pub mod tokens; pub use components::hsla; use gpui_kit::component::{Theme, ThemeMode}; use gpui_kit::{App, Background, Global, WindowAppearance, linear_color_stop, linear_gradient, px}; pub use tokens::{Backdrop, FONT_FAMILY, Metrics, Palette, Rgba}; impl Backdrop { /// What GPUI draws: its gradient has two colour stops, so the first and the /// last of the three; the middle one is not drawn. pub fn to_gpui(&self) -> Background { let [first, _, last] = self.stops; linear_gradient( self.angle, linear_color_stop(hsla(first.0), first.1), linear_color_stop(hsla(last.0), last.1), ) } } /// The theme mode setting (`ui.theme` of the Slint front end). #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Choice { /// Follow the window's appearance. System, /// Always light. Light, /// Always dark. Dark, } impl Choice { /// `system`, `light` or `dark`. pub fn parse(text: &str) -> Option { match text { "system" => Some(Self::System), "light" => Some(Self::Light), "dark" => Some(Self::Dark), _ => None, } } /// The setting's value. pub fn name(self) -> &'static str { match self { Self::System => "system", Self::Light => "light", Self::Dark => "dark", } } /// Whether the theme is dark, given whether the window's appearance is. pub fn resolve(self, system_dark: bool) -> bool { match self { Self::System => system_dark, Self::Light => false, Self::Dark => true, } } } /// Whether a window's appearance is a dark one. pub fn is_dark(appearance: WindowAppearance) -> bool { matches!(appearance, WindowAppearance::Dark | WindowAppearance::VibrantDark) } /// Every token of `theme.slint` for the resolved mode and text size; a GPUI /// global. #[derive(Clone, Debug)] pub struct VoelinTheme { choice: Choice, dark: bool, /// The colours of the resolved mode. pub colors: Palette, /// The window's backdrop gradient of the resolved mode. pub backdrop: Backdrop, /// The sizes, radii, type and durations at the text size factor. pub metrics: Metrics, } impl Global for VoelinTheme {} impl VoelinTheme { /// The tokens for a mode (resolved against `system_dark`) and a text size /// factor. pub fn new(choice: Choice, system_dark: bool, font_scale: f32) -> Self { let dark = choice.resolve(system_dark); Self { choice, dark, colors: if dark { Palette::DARK } else { Palette::LIGHT }, backdrop: if dark { Backdrop::DARK } else { Backdrop::LIGHT }, metrics: Metrics::new(font_scale), } } /// The theme mode that was set. pub fn choice(&self) -> Choice { self.choice } /// Whether the colours are the dark ones. pub fn is_dark(&self) -> bool { self.dark } /// Install the theme as the global and write it into gpui-component, which /// `gpui_kit::init` must have set up. `appearance` is the window's (or /// the system's before there is a window). pub fn init(cx: &mut App, choice: Choice, appearance: WindowAppearance, font_scale: f32) { cx.set_global(Self::new(choice, is_dark(appearance), font_scale)); Self::apply(cx); } /// The theme global. pub fn global(cx: &App) -> &Self { cx.global::() } /// Change the mode (a setting changed), given the window's appearance now. pub fn set_choice(cx: &mut App, choice: Choice, appearance: WindowAppearance) { let (scale, current) = { let this = Self::global(cx); (this.metrics.font_scale, this.choice) }; if current == choice && Self::global(cx).dark == choice.resolve(is_dark(appearance)) { return; } cx.set_global(Self::new(choice, is_dark(appearance), scale)); Self::apply(cx); } /// The window's appearance changed; it matters only in `system` mode. pub fn set_system_appearance(cx: &mut App, appearance: WindowAppearance) { let this = Self::global(cx); if this.choice == Choice::System && this.dark != is_dark(appearance) { let (choice, scale) = (this.choice, this.metrics.font_scale); cx.set_global(Self::new(choice, is_dark(appearance), scale)); Self::apply(cx); } } /// The text size factor changed (`ui.font_scale`). pub fn set_font_scale(cx: &mut App, font_scale: f32) { let this = Self::global(cx); if (this.metrics.font_scale - font_scale).abs() < f32::EPSILON { return; } // `dark` is what the mode resolved to: for every mode, resolving it again // against it gives the same. let (choice, dark) = (this.choice, this.dark); cx.set_global(Self::new(choice, dark, font_scale)); Self::apply(cx); } /// Write the global into gpui-component's theme: the mode, the colours of /// the second layer, the font, the base size and the radii. fn apply(cx: &mut App) { let this = Self::global(cx).clone(); // The registered theme of the mode first (it sets whatever the second // layer leaves alone), then our colours over it in a second update, as // `Theme::update` says a mode change must be. Theme::change(if this.dark { ThemeMode::Dark } else { ThemeMode::Light }, None, cx); Theme::update(cx, |theme| { components::write(&this.colors, this.dark, &mut theme.colors); theme.font_family = FONT_FAMILY.into(); theme.font_size = px(this.metrics.font_body); theme.radius = px(this.metrics.radius_sm); theme.radius_lg = px(this.metrics.radius_lg); }); // Where the platform draws the window's frame itself (macOS), it follows // the mode; elsewhere this does nothing. cx.set_window_appearance(match this.choice { Choice::System => None, Choice::Light => Some(WindowAppearance::Light), Choice::Dark => Some(WindowAppearance::Dark), }); } } #[cfg(test)] mod tests { use gpui::TestAppContext; use gpui_kit::component::ActiveTheme as _; use super::*; fn start(cx: &mut TestAppContext, choice: Choice, appearance: WindowAppearance) { cx.update(|cx| { gpui_kit::init(cx); VoelinTheme::init(cx, choice, appearance, 1.0); }); } #[test] fn the_mode_resolves_against_the_window() { assert!(Choice::System.resolve(true) && !Choice::System.resolve(false)); assert!(Choice::Dark.resolve(false) && !Choice::Light.resolve(true)); for choice in [Choice::System, Choice::Light, Choice::Dark] { assert_eq!(Choice::parse(choice.name()), Some(choice)); } assert_eq!(Choice::parse("blue"), None); assert!(is_dark(WindowAppearance::VibrantDark) && !is_dark(WindowAppearance::VibrantLight)); } #[test] fn the_backdrop_draws_the_first_and_the_last_of_its_three_stops() { let b = Backdrop::DARK; let [first, middle, last] = b.stops; assert_eq!( b.to_gpui(), linear_gradient( 160.0, linear_color_stop(hsla(first.0), 0.0), linear_color_stop(hsla(last.0), 1.0) ) ); assert_ne!( b.to_gpui(), linear_gradient( 160.0, linear_color_stop(hsla(first.0), 0.0), linear_color_stop(hsla(middle.0), 1.0) ), "the middle stop is not the end one" ); } #[gpui::test] fn the_tokens_reach_gpui_component(cx: &mut TestAppContext) { start(cx, Choice::Dark, WindowAppearance::Light); cx.update(|cx| { let theme = cx.theme(); assert!(theme.is_dark()); assert_eq!(theme.colors.primary, hsla(Palette::DARK.accent)); assert_eq!(theme.colors.background, hsla(Palette::DARK.bg_app)); assert_eq!(theme.colors.button_primary, hsla(Palette::DARK.accent)); assert_eq!(theme.font_family.as_ref(), "Inter"); assert_eq!(theme.font_size, px(14.0)); assert_eq!(theme.radius, px(6.0)); assert_eq!(theme.radius_lg, px(12.0)); assert_eq!(VoelinTheme::global(cx).colors, Palette::DARK); assert_eq!(VoelinTheme::global(cx).backdrop, Backdrop::DARK); }); } #[gpui::test] fn system_follows_the_window_appearance_and_light_and_dark_do_not(cx: &mut TestAppContext) { start(cx, Choice::System, WindowAppearance::Light); let primary = |cx: &mut TestAppContext| cx.update(|cx| cx.theme().colors.primary); assert_eq!(primary(cx), hsla(Palette::LIGHT.accent)); cx.update(|cx| VoelinTheme::set_system_appearance(cx, WindowAppearance::Dark)); assert_eq!(primary(cx), hsla(Palette::DARK.accent)); cx.update(|cx| VoelinTheme::set_system_appearance(cx, WindowAppearance::VibrantLight)); assert_eq!(primary(cx), hsla(Palette::LIGHT.accent)); cx.update(|cx| VoelinTheme::set_choice(cx, Choice::Dark, WindowAppearance::Light)); assert_eq!(primary(cx), hsla(Palette::DARK.accent)); cx.update(|cx| VoelinTheme::set_system_appearance(cx, WindowAppearance::Light)); assert_eq!(primary(cx), hsla(Palette::DARK.accent), "a dark choice ignores the window"); cx.update(|cx| VoelinTheme::set_choice(cx, Choice::Light, WindowAppearance::Dark)); assert_eq!(primary(cx), hsla(Palette::LIGHT.accent), "so does a light one"); } #[gpui::test] fn the_text_size_scales_the_type_and_the_controls(cx: &mut TestAppContext) { start(cx, Choice::Dark, WindowAppearance::Dark); cx.update(|cx| VoelinTheme::set_font_scale(cx, 1.25)); cx.update(|cx| { let theme = VoelinTheme::global(cx); assert!(theme.is_dark(), "the mode stays"); assert_eq!(theme.metrics.font_body, 17.5); assert_eq!(theme.metrics.control_height, 45.0); assert_eq!(cx.theme().font_size, px(17.5)); }); } }