use crate::{
highlighter::HighlightTheme, list::ListSettings, notification::NotificationSettings,
scroll::ScrollbarMode, sheet::SheetSettings,
};
use gpui::{App, Global, Hsla, IsZero as _, Pixels, SharedString, Window, WindowAppearance, px};
pub use gpui_base::{
ColorTokens, RadiusTokens, SemanticThemeTokens, ShadowTokens, SpacingTokens, TextStyleToken,
TypographyTokens,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{
ops::{Deref, DerefMut},
rc::Rc,
sync::Arc,
time::Duration,
};
mod color;
mod registry;
mod schema;
mod theme_color;
pub use color::*;
pub use registry::*;
pub use schema::*;
pub use theme_color::*;
pub fn init(cx: &mut App) {
registry::init(cx);
Theme::change(ThemeMode::Light, None, cx);
Theme::sync_scrollbar_appearance(cx);
}
pub trait ActiveTheme {
fn theme(&self) -> &Theme;
}
impl ActiveTheme for App {
#[inline(always)]
fn theme(&self) -> &Theme {
Theme::global(self)
}
}
fn default_true() -> bool {
true
}
const RADIUS_FULL: Pixels = px(9999.);
const SCROLLBAR_IDLE: Duration = Duration::from_secs(2);
const SCROLLBAR_ENTER: Duration = Duration::from_millis(300);
const SCROLLBAR_EXIT: Duration = Duration::from_millis(500);
const SCROLLBAR_EXPAND: Duration = Duration::from_millis(300);
fn scrollbar_motion(mode: ScrollbarMode) -> gpui_base::ScrollbarMotion {
gpui_base::ScrollbarMotion::default()
.with_idle(SCROLLBAR_IDLE)
.with_enter(SCROLLBAR_ENTER)
.with_exit(SCROLLBAR_EXIT)
.with_expand(SCROLLBAR_EXPAND)
.with_entrance(gpui_base::ScrollbarEntrance::Fade)
.with_thumb_hover_entrance(match mode {
ScrollbarMode::Scrolling | ScrollbarMode::Always => gpui_base::ScrollbarEntrance::Fade,
ScrollbarMode::Hover => gpui_base::ScrollbarEntrance::SlideAndFade,
})
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Theme {
pub colors: ThemeColor,
#[serde(default)]
pub tokens: ThemeTokens,
pub highlight_theme: Arc<HighlightTheme>,
pub light_theme: Rc<ThemeConfig>,
pub dark_theme: Rc<ThemeConfig>,
pub mode: ThemeMode,
pub font_family: SharedString,
pub font_size: Pixels,
pub mono_font_family: SharedString,
pub mono_font_size: Pixels,
pub radius: Pixels,
pub radius_lg: Pixels,
pub shadow: bool,
#[serde(default = "default_true")]
pub focus_ring: bool,
#[schemars(schema_with = "gpui::hsla_schemar")]
pub transparent: Hsla,
#[serde(alias = "scrollbar_show")]
pub scrollbar_mode: ScrollbarMode,
#[serde(skip)]
pub notification: NotificationSettings,
pub tile_grid_size: Pixels,
pub tile_shadow: bool,
pub tile_radius: Pixels,
pub list: ListSettings,
pub sheet: SheetSettings,
}
impl Default for Theme {
fn default() -> Self {
Self::from(&ThemeColor::default())
}
}
impl Deref for Theme {
type Target = ThemeColor;
fn deref(&self) -> &Self::Target {
&self.colors
}
}
impl DerefMut for Theme {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.colors
}
}
impl Global for Theme {}
impl Theme {
#[inline(always)]
pub fn global(cx: &App) -> &Theme {
cx.global::<Theme>()
}
#[inline(always)]
pub fn global_mut(cx: &mut App) -> &mut Theme {
cx.global_mut::<Theme>()
}
#[inline(always)]
pub fn is_dark(&self) -> bool {
self.mode.is_dark()
}
pub fn theme_name(&self) -> &SharedString {
if self.is_dark() {
&self.dark_theme.name
} else {
&self.light_theme.name
}
}
pub fn sync_system_appearance(window: Option<&mut Window>, cx: &mut App) {
let appearance = window
.as_ref()
.map(|window| window.appearance())
.unwrap_or_else(|| cx.window_appearance());
Self::change(appearance, window, cx);
}
pub fn sync_scrollbar_appearance(cx: &mut App) {
let mode = if cx.should_auto_hide_scrollbars() {
ScrollbarMode::Scrolling
} else {
ScrollbarMode::Hover
};
Self::set_scrollbar_mode(mode, cx);
}
pub fn set_scrollbar_mode(mode: ScrollbarMode, cx: &mut App) {
Theme::global_mut(cx).scrollbar_mode = mode;
let base_theme = gpui_base::Theme::global_mut(cx);
base_theme.scrollbar = base_theme
.scrollbar
.clone()
.with_mode(mode)
.with_motion(scrollbar_motion(mode));
}
pub fn change(mode: impl Into<ThemeMode>, window: Option<&mut Window>, cx: &mut App) {
let mode = mode.into();
if !cx.has_global::<Theme>() {
let mut theme = Theme::default();
theme.light_theme = ThemeRegistry::global(cx).default_light_theme().clone();
theme.dark_theme = ThemeRegistry::global(cx).default_dark_theme().clone();
cx.set_global(theme);
}
let theme = cx.global_mut::<Theme>();
theme.mode = mode;
if mode.is_dark() {
theme.apply_config(&theme.dark_theme.clone());
} else {
theme.apply_config(&theme.light_theme.clone());
}
let base_theme = theme.base_theme();
cx.set_global(base_theme);
if let Some(window) = window {
window.refresh();
}
}
fn base_theme(&self) -> gpui_base::Theme {
gpui_base::Theme {
appearance: if self.mode.is_dark() {
gpui_base::ThemeAppearance::Dark
} else {
gpui_base::ThemeAppearance::Light
},
tokens: self.semantic_tokens(),
scrollbar: gpui_base::ScrollbarTheme::new()
.with_mode(self.scrollbar_mode)
.with_motion(scrollbar_motion(self.scrollbar_mode))
.with_styles(
gpui_base::ScrollbarStyles::default()
.track(|style| style.bg(self.scrollbar))
.track_hover(|style| style.bg(self.scrollbar))
.track_active(|style| style.bg(self.scrollbar).border_color(self.border))
.thumb(|style| style.bg(self.tokens.scrollbar_thumb).radius(self.radius))
.thumb_hover(|style| {
style
.bg(self.tokens.scrollbar_thumb_hover)
.radius(self.radius)
})
.thumb_active(|style| {
style
.bg(self.tokens.scrollbar_thumb_hover)
.radius(self.radius)
}),
),
resizable: gpui_base::ResizableTheme {
handle: Some(self.border),
active_handle: Some(self.drag_border),
},
}
}
pub fn sync_base(cx: &mut App) {
let base_theme = Theme::global(cx).base_theme();
cx.set_global(base_theme);
}
#[inline]
pub fn input_background(&self) -> Hsla {
if self.is_dark() {
self.input.mix_oklab(self.transparent, 0.3)
} else {
self.background
}
}
#[inline]
pub(crate) fn editor_background(&self) -> Hsla {
self.highlight_theme
.style
.editor_background
.unwrap_or_else(|| self.input_background())
}
pub fn semantic_tokens(&self) -> SemanticThemeTokens {
SemanticThemeTokens {
colors: self.color_tokens(),
radius: self.radius_tokens(),
spacing: self.spacing_tokens(),
typography: self.typography_tokens(),
shadow: self.shadow_tokens(),
}
}
pub fn color_tokens(&self) -> ColorTokens {
ColorTokens {
background: self.background,
foreground: self.foreground,
surface: self.popover,
surface_foreground: self.popover_foreground,
primary: self.primary,
primary_foreground: self.primary_foreground,
secondary: self.secondary,
secondary_foreground: self.secondary_foreground,
muted: self.muted,
muted_foreground: self.muted_foreground,
accent: self.accent,
accent_foreground: self.accent_foreground,
destructive: self.danger,
destructive_foreground: self.danger_foreground,
border: self.border,
input: self.input,
ring: self.ring,
}
}
pub fn radius_full(&self) -> Pixels {
if self.radius.is_zero() {
px(0.)
} else {
RADIUS_FULL
}
}
pub fn radius_tokens(&self) -> RadiusTokens {
RadiusTokens {
none: px(0.),
sm: self.radius / 2.,
md: self.radius,
lg: self.radius_lg,
xl: self.radius * 2.,
full: RADIUS_FULL,
}
}
pub fn spacing_tokens(&self) -> SpacingTokens {
SpacingTokens::default()
}
pub fn typography_tokens(&self) -> TypographyTokens {
let mut tokens = TypographyTokens::default();
tokens.sans = self.font_family.clone();
tokens.mono = self.mono_font_family.clone();
tokens.md.size = self.font_size;
tokens.mono_md.size = self.mono_font_size;
tokens
}
pub fn shadow_tokens(&self) -> ShadowTokens {
if self.shadow {
ShadowTokens::elevations({
let mut color = self.transparent;
color.alpha = 0.18;
color
})
} else {
ShadowTokens::default()
}
}
pub fn apply_semantic_tokens(&mut self, tokens: &SemanticThemeTokens) {
let colors = tokens.colors;
self.background = colors.background;
self.foreground = colors.foreground;
self.popover = colors.surface;
self.popover_foreground = colors.surface_foreground;
self.primary = colors.primary;
self.primary_foreground = colors.primary_foreground;
self.secondary = colors.secondary;
self.secondary_foreground = colors.secondary_foreground;
self.muted = colors.muted;
self.muted_foreground = colors.muted_foreground;
self.accent = colors.accent;
self.accent_foreground = colors.accent_foreground;
self.danger = colors.destructive;
self.danger_foreground = colors.destructive_foreground;
self.border = colors.border;
self.input = colors.input;
self.ring = colors.ring;
self.tokens.background = colors.background.into();
self.tokens.popover = colors.surface.into();
self.tokens.primary = colors.primary.into();
self.tokens.secondary = colors.secondary.into();
self.tokens.muted = colors.muted.into();
self.tokens.accent = colors.accent.into();
self.tokens.danger = colors.destructive.into();
self.radius = tokens.radius.md;
self.radius_lg = tokens.radius.lg;
self.font_family = tokens.typography.sans.clone();
self.mono_font_family = tokens.typography.mono.clone();
self.font_size = tokens.typography.md.size;
self.mono_font_size = tokens.typography.mono_md.size;
self.shadow = !tokens.shadow.sm.is_empty()
|| !tokens.shadow.md.is_empty()
|| !tokens.shadow.lg.is_empty();
}
pub fn resolve_semantic_config(&self, config: &SemanticThemeConfig) -> SemanticThemeTokens {
let mut tokens = self.semantic_tokens();
config.apply_to(&mut tokens);
tokens
}
pub fn apply_semantic_config(&mut self, config: &SemanticThemeConfig) -> SemanticThemeTokens {
let tokens = self.resolve_semantic_config(config);
self.apply_semantic_tokens(&tokens);
tokens
}
pub fn apply_semantic_config_str(
&mut self,
content: &str,
) -> anyhow::Result<SemanticThemeTokens> {
let config = serde_json::from_str::<SemanticThemeConfigFile>(content)?;
Ok(self.apply_semantic_config(&config.tokens))
}
}
#[cfg(test)]
mod semantic_token_tests {
use gpui::{Hsla, IsZero as _, px};
use super::{RADIUS_FULL, Theme};
fn with_alpha(mut color: Hsla, alpha: f32) -> Hsla {
color.alpha = alpha;
color
}
#[test]
fn semantic_colors_are_a_live_projection_of_legacy_fields() {
let mut theme = Theme::default();
let primary = with_alpha(Hsla::default(), 0.42);
theme.primary = primary;
assert_eq!(theme.color_tokens().primary, primary);
assert_eq!(theme.semantic_tokens().colors.primary, primary);
}
#[test]
fn applying_semantic_tokens_only_updates_generic_legacy_colors() {
let mut theme = Theme::default();
let component_color = theme.button_primary;
let mut tokens = theme.semantic_tokens();
tokens.colors.primary = with_alpha(Hsla::default(), 0.25);
tokens.colors.destructive = with_alpha(Hsla::default(), 0.75);
tokens.radius.md = px(10.);
theme.apply_semantic_tokens(&tokens);
assert_eq!(theme.primary, tokens.colors.primary);
assert_eq!(theme.tokens.primary.color, tokens.colors.primary);
assert_eq!(theme.danger, tokens.colors.destructive);
assert_eq!(theme.radius, px(10.));
assert_eq!(theme.button_primary, component_color);
}
#[test]
fn square_themes_square_off_pills_and_circles() {
let mut theme = Theme::default();
assert_eq!(theme.radius_full(), RADIUS_FULL);
assert_eq!(theme.radius_tokens().full, RADIUS_FULL);
theme.radius = px(0.);
assert_eq!(theme.radius_full(), px(0.));
}
#[test]
fn base_projection_carries_a_square_radius_to_the_scrollbar() {
let mut theme = Theme::default();
assert!(!theme.base_theme().tokens.radius.md.is_zero());
theme.radius = px(0.);
assert!(theme.base_theme().tokens.radius.md.is_zero());
}
#[test]
fn disabled_legacy_shadows_project_to_empty_elevations() {
let mut theme = Theme::default();
theme.shadow = false;
let shadows = theme.shadow_tokens();
assert!(shadows.sm.is_empty());
assert!(shadows.md.is_empty());
assert!(shadows.lg.is_empty());
}
}
impl From<&ThemeColor> for Theme {
fn from(colors: &ThemeColor) -> Self {
Theme {
mode: ThemeMode::default(),
transparent: gpui::transparent_black(),
font_family: ".SystemUIFont".into(),
font_size: px(16.),
mono_font_family: if cfg!(target_os = "macos") {
"Menlo".into()
} else if cfg!(target_os = "windows") {
"Consolas".into()
} else {
"DejaVu Sans Mono".into()
},
mono_font_size: px(13.),
radius: px(6.),
radius_lg: px(8.),
shadow: true,
focus_ring: true,
scrollbar_mode: ScrollbarMode::default(),
notification: NotificationSettings::default(),
tile_grid_size: px(8.),
tile_shadow: true,
tile_radius: px(0.),
list: ListSettings::default(),
colors: *colors,
tokens: ThemeTokens::from(colors),
light_theme: Rc::new(ThemeConfig::default()),
dark_theme: Rc::new(ThemeConfig::default()),
highlight_theme: HighlightTheme::default_light(),
sheet: SheetSettings::default(),
}
}
}
#[derive(
Debug,
Clone,
Copy,
Default,
PartialEq,
PartialOrd,
Eq,
Ord,
Hash,
Serialize,
Deserialize,
JsonSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum ThemeMode {
#[default]
Light,
Dark,
}
impl ThemeMode {
#[inline(always)]
pub fn is_dark(&self) -> bool {
matches!(self, Self::Dark)
}
pub fn name(&self) -> &'static str {
match self {
ThemeMode::Light => "light",
ThemeMode::Dark => "dark",
}
}
}
impl From<WindowAppearance> for ThemeMode {
fn from(appearance: WindowAppearance) -> Self {
match appearance {
WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light,
}
}
}
#[cfg(test)]
mod base_theme_projection_tests {
use super::*;
use gpui::TestAppContext;
#[gpui::test]
fn base_theme_tracks_initialization_and_mode_changes(cx: &mut TestAppContext) {
cx.update(|cx| {
init(cx);
assert_styled_projection(cx);
Theme::change(ThemeMode::Dark, None, cx);
assert_styled_projection(cx);
Theme::set_scrollbar_mode(ScrollbarMode::Always, cx);
assert_eq!(Theme::global(cx).scrollbar_mode, ScrollbarMode::Always);
assert_eq!(
gpui_base::Theme::global(cx).scrollbar.mode(),
gpui_base::ScrollbarMode::Always
);
assert_styled_projection(cx);
});
}
#[gpui::test]
fn scrollbar_motion_is_owned_here_and_projected_onto_base(cx: &mut TestAppContext) {
cx.update(|cx| {
init(cx);
let bare = gpui_base::ScrollbarMotion::default();
assert_eq!(bare.enter(), Duration::ZERO);
assert_eq!(bare.exit(), Duration::ZERO);
assert_eq!(bare.expand(), Duration::ZERO);
Theme::set_scrollbar_mode(ScrollbarMode::Scrolling, cx);
let motion = gpui_base::Theme::global(cx).scrollbar.motion();
assert_eq!(motion.idle(), SCROLLBAR_IDLE);
assert_eq!(motion.enter(), SCROLLBAR_ENTER);
assert_eq!(motion.exit(), SCROLLBAR_EXIT);
assert_eq!(motion.expand(), SCROLLBAR_EXPAND);
assert_eq!(
motion.entrance(),
gpui_base::ScrollbarEntrance::Fade,
"scroll-revealed scrollbars fade in without sliding"
);
Theme::set_scrollbar_mode(ScrollbarMode::Hover, cx);
let motion = gpui_base::Theme::global(cx).scrollbar.motion();
assert_eq!(motion.entrance(), gpui_base::ScrollbarEntrance::Fade);
assert_eq!(
motion.thumb_hover_entrance(),
gpui_base::ScrollbarEntrance::SlideAndFade
);
});
}
fn assert_styled_projection(cx: &App) {
let theme = Theme::global(cx);
let base = gpui_base::Theme::global(cx);
assert_eq!(base.tokens, theme.semantic_tokens());
assert_eq!(base.scrollbar.mode(), theme.scrollbar_mode);
assert_eq!(
base.scrollbar.motion(),
scrollbar_motion(theme.scrollbar_mode)
);
assert_eq!(base.resizable.handle, Some(theme.border));
assert_eq!(base.resizable.active_handle, Some(theme.drag_border));
}
}