//! The second layer of the theme: the colours gpui-component's widgets read, //! written from the Voelin palette. gpui-component's `ThemeColor` is shadcn's //! vocabulary (about 135 colours: a background, a primary, a muted, one set //! for the list, one for the table, one for each kind of button); the //! client's tokens map onto it as docs/ui.md ("The GPUI front end") lists. //! What has no counterpart stays as the registered theme of the mode has it //! (charts' bullish and bearish colours, the cyan and magenta of the base //! palette). //! //! gpui-component's "accent" is a hover background, not a brand colour: the //! client's `accent` is its `primary`. use gpui_kit::Hsla; use gpui_kit::component::ThemeColor; use super::tokens::{Palette, Rgba}; /// A palette colour as GPUI's. pub fn hsla(color: Rgba) -> Hsla { gpui_kit::rgba(color.0).into() } /// Of `base`, the colours a status button needs: hover, active, and a text /// that reads on it (white or the window's ink, whichever contrasts more). struct Status { base: Hsla, hover: Hsla, active: Hsla, foreground: Hsla, } fn status(base: Rgba, ink: Rgba, dark: bool) -> Status { let toward_hover = if dark { Rgba(0xffff_ffff) } else { Rgba(0x0000_00ff) }; Status { base: hsla(base), hover: hsla(base.mix(toward_hover, 0.14)), active: hsla(base.mix(Rgba(0x0000_00ff), 0.18)), foreground: hsla(base.readable(ink)), } } /// Write the palette's colours into `colors`, over what the registered theme /// of the mode left there. Every field that gpui-component's themes derive /// from another (the buttons fall back to `primary`, `danger`, ...) is written /// too, so that none keeps the default theme's blue. pub fn write(p: &Palette, dark: bool, colors: &mut ThemeColor) { let c = colors; let h = hsla; let clear = Hsla::transparent_black(); // The window and its text. c.background = h(p.bg_app); c.foreground = h(p.text); c.border = h(p.border); c.input = h(p.border_strong); c.ring = h(p.glow); c.overlay = h(p.scrim); c.window_border = h(p.border); c.caret = h(p.text); c.selection = h(p.accent_soft); c.drag_border = h(p.accent); c.drop_target = h(p.accent_soft); // Links: one colour for all three states, as the client has one. c.link = h(p.accent_text); c.link_hover = h(p.accent_text); c.link_active = h(p.accent_text); // The brand colour, and gpui-component's hover "accent". c.primary = h(p.accent); c.primary_hover = h(p.accent_hover); c.primary_active = h(p.accent_pressed); c.primary_foreground = h(p.on_accent); c.accent = h(p.surface_3); c.accent_foreground = h(p.text); // Cards, inputs, menus, popovers. c.secondary = h(p.surface_2); c.secondary_hover = h(p.surface_3); c.secondary_active = h(p.surface_4); c.secondary_foreground = h(p.text); c.muted = h(p.surface_2); c.muted_foreground = h(p.text_muted); c.popover = h(p.surface_3); c.popover_foreground = h(p.text); c.group_box = h(p.surface_2); c.group_box_foreground = h(p.text); c.accordion = h(p.surface_2); c.description_list_label = h(p.surface_2); c.description_list_label_foreground = h(p.text_secondary); // The status colours, with the text that reads on each. let [danger, success, warning, info] = [p.danger, p.success, p.warning, p.info].map(|base| status(base, p.bg_app, dark)); (c.danger, c.danger_hover, c.danger_active, c.danger_foreground) = (danger.base, danger.hover, danger.active, danger.foreground); (c.success, c.success_hover, c.success_active, c.success_foreground) = (success.base, success.hover, success.active, success.foreground); (c.warning, c.warning_hover, c.warning_active, c.warning_foreground) = (warning.base, warning.hover, warning.active, warning.foreground); (c.info, c.info_hover, c.info_active, c.info_foreground) = (info.base, info.hover, info.active, info.foreground); // Lists and tables: rows on the panel, hovered rows raised, the selected // row the soft accent with the accent's edge. c.list = h(p.surface); c.list_hover = h(p.surface_3); c.list_active = h(p.accent_soft); c.list_active_border = h(p.accent); c.list_even = h(p.surface); c.list_head = h(p.surface_2); c.table = h(p.surface); c.table_hover = h(p.surface_3); c.table_active = h(p.accent_soft); c.table_active_border = h(p.accent); c.table_even = h(p.surface); c.table_head = h(p.surface_2); c.table_head_foreground = h(p.text_secondary); c.table_foot = h(p.surface_2); c.table_foot_foreground = h(p.text_secondary); c.table_row_border = h(p.border); // The panels around the content: the sidebar, the title and status bars. c.sidebar = h(p.surface); c.sidebar_foreground = h(p.text); c.sidebar_border = h(p.border); c.sidebar_accent = h(p.surface_3); c.sidebar_accent_foreground = h(p.text); c.sidebar_primary = h(p.accent); c.sidebar_primary_foreground = h(p.on_accent); c.title_bar = h(p.surface); c.title_bar_border = h(p.border); c.status_bar = h(p.surface); c.status_bar_border = h(p.border); // Small widgets. c.scrollbar = clear; c.scrollbar_thumb = h(p.border_strong); c.scrollbar_thumb_hover = h(p.text_muted); c.slider_bar = h(p.accent); c.slider_thumb = h(p.on_accent); c.switch = h(p.surface_4); c.switch_thumb = h(p.on_accent); c.progress_bar = h(p.accent); c.skeleton = h(p.surface_3); c.tab = clear; c.tab_active = h(p.surface_2); c.tab_active_foreground = h(p.text); c.tab_bar = h(p.surface); c.tab_bar_segmented = h(p.surface_2); c.tab_foreground = h(p.text_secondary); // The base palette and the charts: the client's state colours. c.red = h(p.danger); c.red_light = h(p.danger_soft); c.green = h(p.success); c.green_light = h(p.success_soft); c.yellow = h(p.warning); c.yellow_light = h(p.warning_soft); c.blue = h(p.info); c.blue_light = h(p.accent_soft); c.chart_1 = h(p.accent); c.chart_2 = h(p.success); c.chart_3 = h(p.warning); c.chart_4 = h(p.danger); c.chart_5 = h(p.info); // The buttons: what a theme file leaves out they take from the colours // above, so they are written from them here. c.button = c.secondary; c.button_hover = c.secondary_hover; c.button_active = c.secondary_active; c.button_foreground = c.secondary_foreground; c.button_primary = c.primary; c.button_primary_hover = c.primary_hover; c.button_primary_active = c.primary_active; c.button_primary_foreground = c.primary_foreground; c.button_secondary = c.secondary; c.button_secondary_hover = c.secondary_hover; c.button_secondary_active = c.secondary_active; c.button_secondary_foreground = c.secondary_foreground; c.button_danger = c.danger; c.button_danger_hover = c.danger_hover; c.button_danger_active = c.danger_active; c.button_danger_foreground = c.danger_foreground; c.button_success = c.success; c.button_success_hover = c.success_hover; c.button_success_active = c.success_active; c.button_success_foreground = c.success_foreground; c.button_warning = c.warning; c.button_warning_hover = c.warning_hover; c.button_warning_active = c.warning_active; c.button_warning_foreground = c.warning_foreground; c.button_info = c.info; c.button_info_hover = c.info_hover; c.button_info_active = c.info_active; c.button_info_foreground = c.info_foreground; } #[cfg(test)] mod tests { use super::*; fn written(dark: bool) -> (Palette, ThemeColor) { let palette = if dark { Palette::DARK } else { Palette::LIGHT }; let mut colors = ThemeColor::default(); write(&palette, dark, &mut colors); (palette, colors) } #[test] fn the_client_tokens_land_where_the_table_says() { for dark in [true, false] { let (p, c) = written(dark); assert_eq!(c.background, hsla(p.bg_app)); assert_eq!(c.foreground, hsla(p.text)); assert_eq!(c.primary, hsla(p.accent)); assert_eq!(c.primary_hover, hsla(p.accent_hover)); assert_eq!(c.primary_active, hsla(p.accent_pressed)); assert_eq!(c.primary_foreground, hsla(p.on_accent)); assert_eq!( c.accent, hsla(p.surface_3), "gpui-component's accent is a hover background" ); assert_eq!(c.secondary, hsla(p.surface_2)); assert_eq!(c.secondary_active, hsla(p.surface_4)); assert_eq!(c.border, hsla(p.border)); assert_eq!(c.input, hsla(p.border_strong)); assert_eq!(c.ring, hsla(p.glow)); assert_eq!(c.overlay, hsla(p.scrim)); assert_eq!(c.list_active, hsla(p.accent_soft)); assert_eq!(c.link, hsla(p.accent_text)); assert_eq!(c.muted_foreground, hsla(p.text_muted)); assert_eq!(c.sidebar, hsla(p.surface)); assert_eq!(c.danger, hsla(p.danger)); assert_eq!(c.success, hsla(p.success)); } } #[test] fn the_buttons_follow_the_colours_they_fall_back_to() { let (_, c) = written(true); assert_eq!(c.button_primary, c.primary); assert_eq!(c.button_primary_hover, c.primary_hover); assert_eq!(c.button_danger_foreground, c.danger_foreground); assert_eq!(c.button, c.secondary); assert_eq!(c.button_info_active, c.info_active); } #[test] fn the_text_on_a_status_colour_is_the_one_that_reads_better() { for (p, dark) in [(Palette::DARK, true), (Palette::LIGHT, false)] { for base in [p.danger, p.success, p.warning, p.info] { let s = status(base, p.bg_app, dark); let chosen = base.readable(p.bg_app); let other = if chosen == Rgba(0xffff_ffff) { p.bg_app } else { Rgba(0xffff_ffff) }; assert!(base.contrast(chosen) >= base.contrast(other), "{base} on {chosen}"); assert_eq!(s.foreground, hsla(chosen)); assert_ne!(s.hover, s.base); assert_ne!(s.active, s.base); } } } #[test] fn mixing_and_contrast_do_what_they_say() { let (black, white) = (Rgba(0x0000_00ff), Rgba(0xffff_ffff)); assert_eq!(black.mix(white, 0.0), black); assert_eq!(black.mix(white, 1.0), white); assert_eq!(Rgba(0x1020_30c0).mix(white, 0.0), Rgba(0x1020_30c0), "opacity stays"); assert!((black.contrast(white) - 21.0).abs() < 0.01); assert_eq!(black.readable(black), white); assert_eq!(Rgba(0xf5b3_01ff).readable(Rgba(0x060a_18ff)), Rgba(0x060a_18ff)); assert_eq!(Rgba(0x12ab_34ff).to_string(), "#12ab34"); assert_eq!(Rgba(0x12ab_3480).to_string(), "#12ab3480"); } }