mod customize;
pub use customize::{contrast_checks, on_color, ratio_text};
use super::row::{Handler, ghost_button};
use crate::{
icons::CarbonIcon,
t3::craft::{focus_ring_offset, mono, rem, sans},
t3::press::{hover, inset_frame, track_hover},
theme::color,
tokens::{self, ColorRole},
};
use gpui_kit::component::{
Sizable as _, Size,
color_picker::{ColorPicker, ColorPickerState},
};
use gpui_kit::{
App, AppContext as _, Div, ElementId, Entity, Hsla, InteractiveElement as _, IntoElement,
ParentElement, RenderOnce, Rgba, SharedString, StatefulInteractiveElement as _, Styled,
TestSupportExt as _, Window, canvas, div, prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
pub const MIN_CONTRAST: f32 = 4.5;
const TILE_MIN: f32 = 176.;
const GRID_GAP: f32 = 8.;
const CHIP: f32 = 28.;
#[derive(Clone, Debug, PartialEq)]
pub struct ThemeToken {
pub name: SharedString,
pub value: Hsla,
}
impl ThemeToken {
pub fn new(name: impl Into<SharedString>, value: impl Into<Hsla>) -> Self {
Self {
name: name.into(),
value: value.into(),
}
}
pub fn hex(name: impl Into<SharedString>, hex: &str) -> Option<Self> {
parse_hex(hex).map(|value| Self::new(name, value))
}
}
pub fn parse_hex(hex: &str) -> Option<Hsla> {
Rgba::try_from(hex.trim()).ok().map(Hsla::from)
}
pub fn to_hex(value: Hsla) -> String {
let c = value.to_rgb();
let b = |v: f32| (v.clamp(0., 1.) * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
fn luminance(value: Hsla) -> f32 {
let c = value.to_rgb();
let lin = |v: f32| {
if v <= 0.040_45 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
};
0.2126 * lin(c.r) + 0.7152 * lin(c.g) + 0.0722 * lin(c.b)
}
pub fn contrast_ratio(a: Hsla, b: Hsla) -> f32 {
let (la, lb) = (luminance(a), luminance(b));
let (hi, lo) = if la >= lb { (la, lb) } else { (lb, la) };
(hi + 0.05) / (lo + 0.05)
}
pub fn vscode_token(key: &str) -> Option<&'static str> {
Some(match key {
"editor.background" => "background",
"sideBar.background" | "editorWidget.background" => "layer-01",
"editor.foreground" | "foreground" => "text-primary",
"descriptionForeground" => "text-secondary",
"button.background" | "textLink.foreground" => "interactive",
"focusBorder" => "focus",
"terminal.ansiRed" | "errorForeground" => "support-error",
"terminal.ansiGreen" => "support-success",
"terminal.ansiYellow" => "support-warning",
"terminal.ansiBlue" => "support-info",
_ => return None,
})
}
pub fn import_vscode<'a>(
base: &[ThemeToken],
colors: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Vec<ThemeToken> {
let mut out = base.to_vec();
let mut set = vec![false; out.len()];
for (key, hex) in colors {
let (Some(token), Some(value)) = (vscode_token(key), parse_hex(hex)) else {
continue;
};
if let Some(i) = out.iter().position(|t| t.name == token)
&& !set[i]
{
out[i].value = value;
set[i] = true;
}
}
out
}
pub fn grid_columns(width: f32) -> usize {
(((width + GRID_GAP) / (TILE_MIN + GRID_GAP)).floor() as usize).max(1)
}
#[derive(Clone, Debug)]
pub struct ThemeEditorTexts {
pub import: SharedString,
pub export: SharedString,
pub preview_title: SharedString,
pub preview_body: SharedString,
pub preview_primary: SharedString,
pub contrast: SharedString,
pub contrast_text: SharedString,
pub contrast_secondary: SharedString,
pub contrast_button: SharedString,
pub tokens_title: SharedString,
pub tokens_description: SharedString,
pub picker: [SharedString; 4],
}
impl Default for ThemeEditorTexts {
fn default() -> Self {
Self {
import: "Import VS Code theme".into(),
export: "Export".into(),
preview_title: "Preview".into(),
preview_body: "Secondary text on layer.".into(),
preview_primary: "Primary".into(),
contrast: "Contrast".into(),
contrast_text: "Text on background".into(),
contrast_secondary: "Secondary text on layer".into(),
contrast_button: "Button text on interactive".into(),
tokens_title: "Tokens".into(),
tokens_description: "Each colour of the theme, as gpui-kit’s theme file names it. \
Pick a swatch or type a hex."
.into(),
picker: [
"Saturation and brightness".into(),
"Hue".into(),
"Hex colour".into(),
"Swatches".into(),
],
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ThemeEditorLayout {
#[default]
Grid,
Customize,
}
#[derive(IntoElement)]
pub struct ThemeEditor {
id: ElementId,
name: SharedString,
tokens: Vec<ThemeToken>,
texts: ThemeEditorTexts,
on_import: Option<Handler>,
on_export: Option<Handler>,
layout: ThemeEditorLayout,
primary: Vec<usize>,
on_change: Option<customize::ChangeHandler>,
}
impl ThemeEditor {
pub fn new(id: impl Into<ElementId>, tokens: impl IntoIterator<Item = ThemeToken>) -> Self {
Self {
id: id.into(),
name: "Custom theme".into(),
tokens: tokens.into_iter().collect(),
texts: ThemeEditorTexts::default(),
on_import: None,
on_export: None,
layout: ThemeEditorLayout::Grid,
primary: Vec::new(),
on_change: None,
}
}
pub fn layout(mut self, layout: ThemeEditorLayout) -> Self {
self.layout = layout;
self
}
pub fn primary(mut self, indices: impl IntoIterator<Item = usize>) -> Self {
self.primary = indices.into_iter().collect();
self
}
pub fn on_change(mut self, f: impl Fn(usize, Hsla, &mut Window, &mut App) + 'static) -> Self {
self.on_change = Some(Rc::new(f));
self
}
pub fn name(mut self, name: impl Into<SharedString>) -> Self {
self.name = name.into();
self
}
pub fn texts(mut self, texts: ThemeEditorTexts) -> Self {
self.texts = texts;
self
}
pub fn on_import(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_import = Some(Rc::new(f));
self
}
pub fn on_export(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_export = Some(Rc::new(f));
self
}
pub fn values(id: impl Into<ElementId>, window: &mut Window, cx: &mut App) -> Vec<Hsla> {
let id = id.into();
let pickers = window.use_keyed_state((id, "pickers"), cx, |_, _| Vec::new());
pickers
.read(cx)
.iter()
.map(|p: &Entity<ColorPickerState>| p.read(cx).value().unwrap_or_default())
.collect()
}
}
fn lookup(tokens: &[ThemeToken], values: &[Hsla], name: &str, fallback: Hsla) -> Hsla {
tokens
.iter()
.position(|t| t.name == name)
.and_then(|i| values.get(i).copied())
.unwrap_or(fallback)
}
fn preview(tokens: &[ThemeToken], values: &[Hsla], texts: &ThemeEditorTexts, ink: Hsla) -> Div {
let get = |name: &str, role: ColorRole| lookup(tokens, values, name, color(role));
div()
.p(tokens::SPACING_04)
.bg(get("background", ColorRole::Background))
.text_color(get("text-primary", ColorRole::TextPrimary))
.shadow(inset_frame(color(ColorRole::BorderSubtle01)))
.child(
div()
.flex()
.flex_col()
.items_start()
.p(rem(12.))
.bg(get("layer-01", ColorRole::Layer01))
.child(sans(div(), 14., 400).child(texts.preview_title.clone()))
.child(
div()
.mt(rem(4.))
.mb(rem(8.))
.text_color(get("text-secondary", ColorRole::TextSecondary))
.child(texts.preview_body.clone()),
)
.child(
div()
.px(rem(12.))
.py(rem(4.))
.bg(get("interactive", ColorRole::Interactive))
.text_color(ink)
.child(texts.preview_primary.clone()),
),
)
}
impl RenderOnce for ThemeEditor {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
if self.layout == ThemeEditorLayout::Customize {
return self.render_customize(window, cx);
}
let id = self.id.clone();
let tokens = self.tokens.clone();
let pickers = window.use_keyed_state((id.clone(), "pickers"), cx, |_, _| {
Vec::<Entity<ColorPickerState>>::new()
});
if pickers.read(cx).len() != tokens.len() {
let made: Vec<_> = tokens
.iter()
.map(|t| {
let v = t.value;
cx.new(|cx| ColorPickerState::new(window, cx).default_value(v))
})
.collect();
pickers.update(cx, |p, _| *p = made);
}
let pickers: Vec<_> = pickers.read(cx).clone();
let values: Vec<Hsla> = pickers
.iter()
.zip(&tokens)
.map(|(p, t)| p.read(cx).value().unwrap_or(t.value))
.collect();
let width = window.use_keyed_state((id.clone(), "width"), cx, |_, _| 0f32);
let cols = grid_columns(*width.read(cx));
let measure = {
let width = width.clone();
canvas(
move |bounds, window, cx| {
let w =
bounds.size.width.as_f32() * tokens::REM_PX / window.rem_size().as_f32();
if (*width.read(cx) - w).abs() > 0.5 {
crate::measure::store(&width, w, window, cx);
}
},
|_, _, _, _| {},
)
.absolute()
.top_0()
.left_0()
.size_full()
};
let chip = rem(CHIP).to_pixels(window.rem_size());
let mut tiles: Vec<_> = Vec::new();
for (i, (t, picker)) in tokens.iter().zip(&pickers).enumerate() {
let tile_id: ElementId = (id.clone(), SharedString::from(format!("sw-{i}"))).into();
let (over, store) = hover((tile_id.clone(), "hover"), window, cx);
let focus = gpui_kit::Focusable::focus_handle(picker.read(cx), cx);
let ring = focus.is_focused(window) && crate::focus::focus_visible_mode(window);
let open = picker.clone();
let open_focus = focus.clone();
tiles.push(
div()
.id(tile_id.clone())
.flex()
.flex_1()
.min_w_0()
.items_center()
.gap(tokens::SPACING_03)
.p(tokens::SPACING_02)
.cursor_pointer()
.bg(color(if over {
ColorRole::LayerHover01
} else {
ColorRole::Layer01
}))
.on_hover(track_hover(store))
.child(
div()
.relative()
.flex_none()
.size(rem(CHIP))
.child(
ColorPicker::new(picker)
.with_size(Size::Size(chip))
.accessibility_label(t.name.clone()),
)
.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.shadow(inset_frame(color(ColorRole::BorderSubtle01))),
)
.when(ring, |this| this.child(focus_ring_offset(3.))),
)
.child(
div()
.id((tile_id, "text"))
.test_support()
.flex()
.flex_col()
.min_w_0()
.on_click(move |_, window, cx| {
open_focus.focus(window, cx);
open.update(cx, |s, cx| s.set_open(true, cx));
})
.child(
mono(div(), 12., 400)
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(t.name.clone()),
)
.child(
mono(div(), 12., 400)
.text_color(color(ColorRole::TextSecondary))
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(to_hex(values[i])),
),
)
.into_any_element(),
);
}
let n = tiles.len();
let mut rows = Vec::new();
let mut it = tiles.into_iter();
for _ in 0..n.div_ceil(cols) {
let mut row = div().flex().gap(px(GRID_GAP)).w_full();
for _ in 0..cols {
row = match it.next() {
Some(tile) => row.child(tile),
None => row.child(div().flex_1().min_w_0()),
};
}
rows.push(row);
}
let fg = color(ColorRole::TextPrimary);
sans(div(), 14., 400)
.id(id.clone())
.flex()
.flex_col()
.gap(tokens::SPACING_04)
.w_full()
.text_color(fg)
.child(
div()
.flex()
.items_center()
.gap(tokens::SPACING_02)
.child(
sans(div(), 14., 600)
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(self.name.clone()),
)
.child(div().flex_1())
.child(ghost_button(
(id.clone(), "import"),
self.texts.import.clone(),
Some(CarbonIcon::DocumentImport),
self.on_import.clone(),
))
.child(ghost_button(
(id.clone(), "export"),
self.texts.export.clone(),
Some(CarbonIcon::Download),
self.on_export.clone(),
)),
)
.child(
div()
.relative()
.flex()
.flex_col()
.gap(px(GRID_GAP))
.w_full()
.child(measure)
.children(rows),
)
.child(preview(
&tokens,
&values,
&self.texts,
color(ColorRole::TextOnColor),
))
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hex_round_trips() {
for hex in [
"#0b0d12", "#161a22", "#e6e9ef", "#5b8cff", "#ffffff", "#000000",
] {
assert_eq!(to_hex(parse_hex(hex).unwrap()), hex);
}
assert_eq!(to_hex(parse_hex("#fff").unwrap()), "#ffffff");
assert!(parse_hex("blue").is_none());
}
#[test]
fn contrast_matches_wcag() {
let black = parse_hex("#000000").unwrap();
let white = parse_hex("#ffffff").unwrap();
assert!((contrast_ratio(black, white) - 21.).abs() < 0.01);
assert!((contrast_ratio(white, white) - 1.).abs() < 0.001);
let bg = parse_hex("#0b0d12").unwrap();
let fg = parse_hex("#e6e9ef").unwrap();
assert!(contrast_ratio(fg, bg) >= MIN_CONTRAST);
assert!(contrast_ratio(parse_hex("#999999").unwrap(), white) < MIN_CONTRAST);
}
#[test]
fn vscode_import_maps_known_keys_and_keeps_the_rest() {
let base = vec![
ThemeToken::hex("background", "#ffffff").unwrap(),
ThemeToken::hex("text-primary", "#161616").unwrap(),
ThemeToken::hex("layer-01", "#f4f4f4").unwrap(),
ThemeToken::hex("focus", "#0f62fe").unwrap(),
];
let out = import_vscode(
&base,
[
("editor.background", "#1e1e1e"),
("editor.foreground", "#d4d4d4"),
("foreground", "#cccccc"),
("focusBorder", "#007fd4"),
("activityBar.background", "#333333"),
],
);
assert_eq!(to_hex(out[0].value), "#1e1e1e");
assert_eq!(to_hex(out[1].value), "#d4d4d4");
assert_eq!(out[2], base[2]);
assert_eq!(to_hex(out[3].value), "#007fd4");
assert_eq!(vscode_token("terminal.ansiRed"), Some("support-error"));
assert_eq!(vscode_token("activityBar.background"), None);
}
#[test]
fn customize_checks_the_three_pairs() {
let midnight: Vec<ThemeToken> = [
("background", "#0b1220"),
("layer-01", "#121a2b"),
("text-primary", "#e6edf3"),
("text-secondary", "#9aa7b8"),
("interactive", "#6ea8fe"),
]
.into_iter()
.map(|(n, h)| ThemeToken::hex(n, h).unwrap())
.collect();
let [text, secondary, button] = contrast_checks(&midnight);
assert_eq!(ratio_text(text), "15.8:1");
assert!(secondary >= MIN_CONTRAST, "{secondary}");
assert_eq!(ratio_text(21.), "21.0:1");
assert!(button >= MIN_CONTRAST, "{button}");
assert_eq!(to_hex(on_color(&midnight)), "#0b1220");
let mut white_on = midnight.clone();
white_on.push(ThemeToken::hex("text-on-color", "#ffffff").unwrap());
assert_eq!(to_hex(on_color(&white_on)), "#ffffff");
assert!(contrast_checks(&white_on)[2] < MIN_CONTRAST);
}
#[test]
fn grid_fills_11rem_tracks() {
assert_eq!(grid_columns(0.), 1);
assert_eq!(grid_columns(175.), 1);
assert_eq!(grid_columns(176. * 2. + 8.), 2);
assert_eq!(grid_columns(760.), 4);
}
}