use crate::tokens::{COLOR_TABLES, CarbonTheme, ColorRole, FontFamilyKey, REM_PX};
use gpui_kit::component::{Theme as KitTheme, ThemeColor, ThemeMode, ThemeTokens};
use gpui_kit::{App, Global, Hsla, Rgba, px};
use std::cell::Cell;
thread_local! {
static ACTIVE: Cell<CarbonTheme> = const { Cell::new(CarbonTheme::White) };
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ActiveTheme(pub CarbonTheme);
impl Global for ActiveTheme {}
pub fn current() -> CarbonTheme {
ACTIVE.get()
}
pub fn color(role: ColorRole) -> Hsla {
rgba(role).into()
}
pub fn rgba(role: ColorRole) -> Rgba {
COLOR_TABLES[current() as usize][role as usize]
}
pub fn color_in(theme: CarbonTheme, role: ColorRole) -> Hsla {
role.in_theme(theme)
}
pub fn is_dark() -> bool {
current().is_dark()
}
pub fn set_theme(cx: &mut App, theme: CarbonTheme) {
ACTIVE.set(theme);
cx.set_global(ActiveTheme(theme));
sync_kit_theme(theme, cx);
cx.refresh_windows();
}
pub fn zone(
theme: CarbonTheme,
build: impl FnOnce(&mut gpui_kit::Window, &mut App) -> gpui_kit::AnyElement + 'static,
) -> ThemeZone {
ThemeZone {
theme,
build: Some(Box::new(build)),
child: None,
}
}
type ZoneBuild = Box<dyn FnOnce(&mut gpui_kit::Window, &mut App) -> gpui_kit::AnyElement>;
pub struct ThemeZone {
theme: CarbonTheme,
build: Option<ZoneBuild>,
child: Option<gpui_kit::AnyElement>,
}
impl ThemeZone {
fn scoped<R>(theme: CarbonTheme, f: impl FnOnce() -> R) -> R {
let previous = ACTIVE.replace(theme);
let result = f();
ACTIVE.set(previous);
result
}
}
impl gpui_kit::Element for ThemeZone {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<gpui_kit::ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&gpui_kit::GlobalElementId>,
_: Option<&gpui_kit::InspectorElementId>,
window: &mut gpui_kit::Window,
cx: &mut App,
) -> (gpui_kit::LayoutId, ()) {
let build = self.build.take();
let child = &mut self.child;
let id = Self::scoped(self.theme, || {
let mut element = match build {
Some(build) => build(window, cx),
None => gpui_kit::IntoElement::into_any_element(gpui_kit::Empty),
};
let id = element.request_layout(window, cx);
*child = Some(element);
id
});
(id, ())
}
fn prepaint(
&mut self,
_: Option<&gpui_kit::GlobalElementId>,
_: Option<&gpui_kit::InspectorElementId>,
_: gpui_kit::Bounds<gpui_kit::Pixels>,
_: &mut (),
window: &mut gpui_kit::Window,
cx: &mut App,
) {
if let Some(child) = &mut self.child {
Self::scoped(self.theme, || {
child.prepaint(window, cx);
});
}
}
fn paint(
&mut self,
_: Option<&gpui_kit::GlobalElementId>,
_: Option<&gpui_kit::InspectorElementId>,
_: gpui_kit::Bounds<gpui_kit::Pixels>,
_: &mut (),
_: &mut (),
window: &mut gpui_kit::Window,
cx: &mut App,
) {
if let Some(child) = &mut self.child {
Self::scoped(self.theme, || child.paint(window, cx));
}
}
}
#[cfg(test)]
pub(crate) fn tests_set(theme: CarbonTheme) {
ACTIVE.set(theme);
}
pub(crate) fn init(cx: &mut App) {
let theme = cx
.try_global::<ActiveTheme>()
.map(|t| t.0)
.unwrap_or_default();
set_theme(cx, theme);
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum LayerLevel {
#[default]
One,
Two,
Three,
}
macro_rules! contextual {
($($(#[$doc:meta])* $fn:ident => $one:ident, $two:ident, $three:ident;)+) => {
impl LayerLevel {
$(
$(#[$doc])*
pub const fn $fn(self) -> ColorRole {
match self {
Self::One => ColorRole::$one,
Self::Two => ColorRole::$two,
Self::Three => ColorRole::$three,
}
}
)+
}
};
}
contextual! {
layer => Layer01, Layer02, Layer03;
layer_hover => LayerHover01, LayerHover02, LayerHover03;
layer_active => LayerActive01, LayerActive02, LayerActive03;
layer_selected => LayerSelected01, LayerSelected02, LayerSelected03;
layer_selected_hover => LayerSelectedHover01, LayerSelectedHover02, LayerSelectedHover03;
layer_accent => LayerAccent01, LayerAccent02, LayerAccent03;
layer_accent_hover => LayerAccentHover01, LayerAccentHover02, LayerAccentHover03;
layer_accent_active => LayerAccentActive01, LayerAccentActive02, LayerAccentActive03;
field => Field01, Field02, Field03;
field_hover => FieldHover01, FieldHover02, FieldHover03;
border_subtle => BorderSubtle01, BorderSubtle02, BorderSubtle03;
border_subtle_selected => BorderSubtleSelected01, BorderSubtleSelected02, BorderSubtleSelected03;
border_strong => BorderStrong01, BorderStrong02, BorderStrong03;
border_tile => BorderTile01, BorderTile02, BorderTile03;
layer_background => LayerBackground01, LayerBackground02, LayerBackground03;
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Selection {
pub background: Option<Hsla>,
pub indicator: Option<Hsla>,
pub semibold: bool,
pub focus: Hsla,
}
impl Selection {
pub fn of(current: bool, selected: bool) -> Self {
let background = if selected {
Some(color(ColorRole::StateSelectedBackground))
} else if current {
Some(color(ColorRole::StateCurrentBackground))
} else {
None
};
Self {
background,
indicator: current.then(|| color(ColorRole::StateCurrentIndicator)),
semibold: current,
focus: color(ColorRole::StateFocus),
}
}
}
impl LayerLevel {
pub const fn next(self) -> Self {
match self {
Self::One => Self::Two,
Self::Two | Self::Three => Self::Three,
}
}
}
macro_rules! kit_projection {
($($field:ident => $role:ident $(@ $alpha:expr)?),* $(,)?) => {
fn project_colors(colors: &mut ThemeColor, theme: CarbonTheme) {
$(colors.$field = kit_projection!(@color theme, $role $(, $alpha)?);)*
}
};
(@color $theme:ident, $role:ident) => { ColorRole::$role.in_theme($theme) };
(@color $theme:ident, $role:ident, $alpha:expr) => {{
let color = ColorRole::$role.in_theme($theme);
color.opacity($alpha)
}};
}
kit_projection! {
background => Background,
foreground => TextPrimary,
border => BorderSubtle01,
input => BorderStrong01,
ring => Focus,
caret => TextPrimary,
selection => Highlight,
overlay => Overlay,
window_border => BorderSubtle01,
drag_border => BorderInteractive,
drop_target => Highlight,
primary => ButtonPrimary,
primary_hover => ButtonPrimaryHover,
primary_active => ButtonPrimaryActive,
primary_foreground => TextOnColor,
secondary => Layer01,
secondary_hover => LayerHover01,
secondary_active => LayerActive01,
secondary_foreground => TextPrimary,
muted => Layer01,
muted_foreground => TextSecondary,
accent => LayerHover01,
accent_foreground => TextPrimary,
popover => Layer01,
popover_foreground => TextPrimary,
danger => SupportError,
danger_hover => ButtonDangerHover,
danger_active => ButtonDangerActive,
danger_foreground => TextOnColor,
success => SupportSuccess,
success_hover => SupportSuccess,
success_active => SupportSuccess,
success_foreground => TextOnColor,
warning => SupportWarning,
warning_hover => SupportWarning,
warning_active => SupportWarning,
warning_foreground => TextPrimary,
info => SupportInfo,
info_hover => SupportInfo,
info_active => SupportInfo,
info_foreground => TextOnColor,
button => Field01,
button_hover => FieldHover01,
button_active => LayerActive01,
button_foreground => TextPrimary,
button_primary => ButtonPrimary,
button_primary_hover => ButtonPrimaryHover,
button_primary_active => ButtonPrimaryActive,
button_primary_foreground => TextOnColor,
button_secondary => ButtonSecondary,
button_secondary_hover => ButtonSecondaryHover,
button_secondary_active => ButtonSecondaryActive,
button_secondary_foreground => TextOnColor,
button_danger => ButtonDangerPrimary,
button_danger_hover => ButtonDangerHover,
button_danger_active => ButtonDangerActive,
button_danger_foreground => TextOnColor,
button_info => ButtonPrimary,
button_info_hover => ButtonPrimaryHover,
button_info_active => ButtonPrimaryActive,
button_info_foreground => TextOnColor,
button_success => SupportSuccess,
button_success_hover => SupportSuccess,
button_success_active => SupportSuccess,
button_success_foreground => TextOnColor,
button_warning => SupportWarning,
button_warning_hover => SupportWarning,
button_warning_active => SupportWarning,
button_warning_foreground => TextPrimary,
link => LinkPrimary,
link_hover => LinkPrimaryHover,
link_active => LinkPrimary,
list => Background,
list_even => Background,
list_head => LayerAccent01,
list_hover => LayerHover01,
list_active => LayerSelected01,
list_active_border => BorderInteractive,
list_highlight_foreground => TextPrimary,
table => Layer01,
table_even => Layer01,
table_head => LayerAccent01,
table_head_foreground => TextPrimary,
table_foot => LayerAccent01,
table_foot_foreground => TextPrimary,
table_hover => LayerHover01,
table_active => LayerSelected01,
table_active_border => BorderInteractive,
table_row_border => BorderSubtle01,
scrollbar => Background @ 0.,
scrollbar_thumb => BorderStrong01,
scrollbar_thumb_hover => IconSecondary,
tab => Background @ 0.,
tab_active => Layer01,
tab_active_foreground => TextPrimary,
tab_bar => Background,
tab_bar_segmented => LayerAccent01,
tab_foreground => TextSecondary,
sidebar => Layer01,
sidebar_foreground => TextSecondary,
sidebar_border => BorderSubtle01,
sidebar_accent => LayerHover01,
sidebar_accent_foreground => TextPrimary,
sidebar_primary => Interactive,
sidebar_primary_foreground => TextOnColor,
title_bar => Background,
title_bar_border => BorderSubtle01,
status_bar => Background,
status_bar_border => BorderSubtle01,
accordion => Layer01,
group_box => Layer01,
group_box_foreground => TextPrimary,
description_list_label => LayerAccent01,
description_list_label_foreground => TextSecondary,
skeleton => SkeletonBackground,
progress_bar => Interactive,
slider_bar => Interactive,
slider_thumb => Interactive,
switch => ToggleOff,
switch_thumb => IconOnColor,
chart_1 => Charts1,
chart_2 => Charts2,
chart_3 => Charts3,
chart_4 => Charts4,
chart_5 => Charts5,
chart_bullish => SupportSuccess,
chart_bearish => SupportError,
chart_grid => BorderSubtle01,
red => TagColorRed,
red_light => TagBackgroundRed,
green => TagColorGreen,
green_light => TagBackgroundGreen,
blue => TagColorBlue,
blue_light => TagBackgroundBlue,
yellow => SupportWarning,
yellow_light => NotificationBackgroundWarning,
magenta => TagColorMagenta,
magenta_light => TagBackgroundMagenta,
cyan => TagColorCyan,
cyan_light => TagBackgroundCyan,
}
fn project_motion(motion: &mut gpui_kit::component::MotionTokens) {
use crate::motion::{Curve, Speed};
motion.duration_fast = Speed::Fast01.duration();
motion.duration_normal = Speed::Fast02.duration();
motion.duration_slow = Speed::Moderate02.duration();
motion.easing_enter = Curve::ProductiveEntrance.easing();
motion.easing_exit = Curve::ProductiveExit.easing();
motion.easing_move = Curve::ProductiveStandard.easing();
}
fn write_kit_projection(kit: &mut KitTheme, theme: CarbonTheme) {
project_colors(&mut kit.colors, theme);
kit.tokens = ThemeTokens::from(kit.colors);
kit.font_family = FontFamilyKey::Sans.primary().into();
kit.mono_font_family = FontFamilyKey::Mono.primary().into();
kit.font_size = px(REM_PX);
kit.mono_font_size = px(crate::tokens::TypeStyle::Code01.spec().font_size_px());
kit.radius = px(0.);
kit.radius_lg = px(0.);
kit.window_radius = px(0.);
kit.shadow = true;
kit.focus_ring = true;
project_motion(&mut kit.motion);
}
fn kit_projection_matches(kit: &KitTheme, theme: CarbonTheme) -> bool {
let mut expected = kit.clone();
write_kit_projection(&mut expected, theme);
expected.colors == kit.colors
&& expected.tokens == kit.tokens
&& expected.font_family == kit.font_family
&& expected.font_size == kit.font_size
&& expected.radius == kit.radius
&& kit.mode.is_dark() == theme.is_dark()
}
fn sync_kit_theme(theme: CarbonTheme, cx: &mut App) -> bool {
if cx.try_global::<KitTheme>().is_none() {
return false;
}
if kit_projection_matches(KitTheme::global(cx), theme) {
return false;
}
let mode = if theme.is_dark() {
ThemeMode::Dark
} else {
ThemeMode::Light
};
if KitTheme::global(cx).mode != mode {
KitTheme::update(cx, |kit| kit.mode = mode);
}
KitTheme::update(cx, |kit| write_kit_projection(kit, theme));
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_zone_answers_for_its_theme_and_restores_the_active_one() {
tests_set(CarbonTheme::White);
let inside = ThemeZone::scoped(CarbonTheme::G100, || {
(current(), color(ColorRole::Background))
});
assert_eq!(
inside,
(
CarbonTheme::G100,
ColorRole::Background.in_theme(CarbonTheme::G100)
)
);
assert_eq!(current(), CarbonTheme::White);
}
#[test]
fn layer_levels_map_to_contextual_tokens() {
assert_eq!(LayerLevel::One.layer(), ColorRole::Layer01);
assert_eq!(LayerLevel::Two.field(), ColorRole::Field02);
assert_eq!(LayerLevel::Three.border_strong(), ColorRole::BorderStrong03);
assert_eq!(LayerLevel::One.next(), LayerLevel::Two);
assert_eq!(LayerLevel::Three.next(), LayerLevel::Three);
}
#[test]
fn kit_projection_is_carbon() {
for theme in CarbonTheme::ALL {
let mut kit = KitTheme::default();
write_kit_projection(&mut kit, theme);
assert_eq!(kit.colors.background, ColorRole::Background.in_theme(theme));
assert_eq!(kit.colors.primary, ColorRole::ButtonPrimary.in_theme(theme));
assert_eq!(kit.colors.ring, ColorRole::Focus.in_theme(theme));
assert_eq!(kit.tokens.primary.color, kit.colors.primary);
assert_eq!(kit.colors.scrollbar.a, 0.);
assert_eq!(kit.radius, px(0.));
assert_eq!(kit.font_family.as_ref(), "IBM Plex Sans");
assert_eq!(kit.font_size, px(16.));
assert_eq!(kit.mono_font_size, px(12.));
}
}
#[test]
fn set_theme_updates_context_free_reads() {
tests_set(CarbonTheme::G100);
assert!(is_dark());
assert_eq!(
color(ColorRole::Background),
ColorRole::Background.in_theme(CarbonTheme::G100)
);
tests_set(CarbonTheme::White);
assert!(!is_dark());
}
}