use gpui_kit::component::color_picker::ColorPickerState;
use gpui_kit::component::{Theme, try_parse_color};
use gpui_kit::{App, Entity, Hsla, Rgba, Window};
use subspace_dashboard::Tokens;
pub(super) const GROUPS: &[(&str, &[(&str, &str)])] = &[
(
"Surfaces",
&[
("background", "Page background"),
("muted.background", "Panel background"),
("popover.background", "Dialogs and menus"),
("title_bar.background", "Title bar"),
("sidebar.background", "Sidebar"),
("list.background", "List background"),
("secondary.background", "Secondary background"),
("group_box.background", "Group box"),
],
),
(
"Tabs",
&[
("tab_bar.background", "Tab strip"),
("tab.background", "Tab"),
("tab.active.background", "Current tab"),
("tab.foreground", "Tab text"),
("tab.active.foreground", "Current tab text"),
],
),
(
"Status bar",
&[
("status_bar.background", "Status bar"),
("status_bar.border", "Status bar edge"),
],
),
(
"Inputs",
&[("input.border", "Input border and dark fill")],
),
(
"Lines",
&[
("border", "Separator"),
("title_bar.border", "Title bar edge"),
("sidebar.border", "Sidebar edge"),
("list.hover.background", "Row hover"),
],
),
(
"Text",
&[
("foreground", "Body text"),
("caret", "Strong text"),
("muted.foreground", "Muted text"),
],
),
(
"Actions and selection",
&[
("primary.background", "Primary action"),
("primary.foreground", "Primary text"),
("accent.background", "Selected background"),
("accent.foreground", "Selected text"),
("ring", "Focus indicator"),
],
),
(
"Status",
&[
("danger.background", "Danger"),
("warning.background", "Warning"),
("success.background", "Success"),
("info.background", "Information"),
],
),
(
"Glass",
&[
("glass", "Glass fill"),
("glass-rim", "Glass rim"),
("glass-shadow", "Glass shadow"),
],
),
(
"Flat buttons",
&[("button-hover", "Button hover"), ("button-on", "Button on")],
),
];
pub(super) struct ColorField {
pub key: &'static str,
pub label: &'static str,
pub picker: Entity<ColorPickerState>,
pub synced: String,
}
impl ColorField {
pub(super) fn show(&mut self, value: String, window: &mut Window, cx: &mut App) {
let color = try_parse_color(&value).ok();
self.synced = color.map_or(value, hex);
self.picker.update(cx, |picker, cx| match color {
Some(color) => picker.set_value(color, window, cx),
None => picker.clear_value(window, cx),
});
}
pub(super) fn value(&self, cx: &App) -> String {
self.picker.read(cx).value().map(hex).unwrap_or_default()
}
}
pub(super) fn keys() -> impl Iterator<Item = (&'static str, &'static str)> {
GROUPS.iter().flat_map(|(_, fields)| fields.iter().copied())
}
pub(super) fn current(key: &str, cx: &App) -> String {
current_color(key, Theme::global(cx), &Tokens::current(cx))
.map(hex)
.unwrap_or_default()
}
const CHANNEL_MAX: f32 = 255.;
const OPAQUE: f32 = 1.;
pub(super) fn hex(color: Hsla) -> String {
let rgba = Rgba::from(color);
let channel = |value: f32| (value * CHANNEL_MAX).round() as u8;
let rgb = format!(
"#{:02x}{:02x}{:02x}",
channel(rgba.r),
channel(rgba.g),
channel(rgba.b)
);
if rgba.a < OPAQUE {
format!("{rgb}{:02x}", channel(rgba.a))
} else {
rgb
}
}
fn current_color(key: &str, theme: &Theme, tokens: &Tokens) -> Option<Hsla> {
let color = match key {
"background" => theme.background,
"foreground" => theme.foreground,
"muted.background" => theme.muted,
"muted.foreground" => theme.muted_foreground,
"popover.background" => theme.popover,
"title_bar.background" => theme.title_bar,
"title_bar.border" => theme.title_bar_border,
"sidebar.background" => theme.sidebar,
"sidebar.border" => theme.sidebar_border,
"list.hover.background" => theme.list_hover,
"list.background" => theme.colors.list,
"secondary.background" => theme.secondary,
"group_box.background" => theme.group_box,
"tab_bar.background" => theme.tab_bar,
"tab.background" => theme.tab,
"tab.active.background" => theme.tab_active,
"tab.foreground" => theme.tab_foreground,
"tab.active.foreground" => theme.tab_active_foreground,
"status_bar.background" => theme.status_bar,
"status_bar.border" => theme.status_bar_border,
"input.border" => theme.input,
"border" => theme.border,
"caret" => theme.caret,
"ring" => theme.ring,
"primary.background" => theme.primary,
"primary.foreground" => theme.primary_foreground,
"accent.background" => theme.accent,
"accent.foreground" => theme.accent_foreground,
"danger.background" => theme.danger,
"warning.background" => theme.warning,
"success.background" => theme.success,
"info.background" => theme.info,
"glass" => tokens.glass,
"glass-rim" => tokens.glass_rim,
"glass-shadow" => tokens.glass_shadow,
"button-hover" => tokens.button_hover,
"button-on" => tokens.button_on,
_ => return None,
};
Some(color)
}
#[cfg(test)]
mod tests {
use super::{current_color, keys};
use crate::theme::ThemeDocument;
use gpui_kit::component::Theme;
use subspace_dashboard::Tokens;
#[test]
fn hex_round_trips_every_written_colour() {
use gpui_kit::component::try_parse_color;
for value in [
"#123456",
"#0969da",
"#ffffff",
"#000000",
"#4c8dff52",
"#00000000",
] {
assert_eq!(super::hex(try_parse_color(value).unwrap()), value);
}
}
#[test]
fn every_field_is_a_theme_colour_with_a_current_value() {
let theme = Theme::default();
let tokens = Tokens::one_dark();
let mut document = ThemeDocument::parse(crate::theme::THEME_JSON).unwrap();
for (key, label) in keys() {
assert!(current_color(key, &theme, &tokens).is_some(), "{label}");
document.set_color(key, "#123456").unwrap();
assert_eq!(document.color(key).as_deref(), Some("#123456"), "{label}");
}
}
}