1use std::{rc::Rc, sync::Arc};
2
3use gpui::{Background, BoxShadow, FontWeight, Hsla, SharedString, px};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use crate::highlighter::{HighlightTheme, HighlightThemeStyle};
8
9use super::color::{
10 try_parse_background, try_parse_background_clamped, try_parse_color, try_parse_theme_color,
11};
12use super::{Colorize, SemanticThemeTokens, Theme, ThemeColor, ThemeMode, ThemeToken, ThemeTokens};
13
14trait BlendExt {
15 fn blend(self, other: Hsla) -> Hsla;
16}
17
18impl BlendExt for Hsla {
19 fn blend(self, other: Hsla) -> Hsla {
20 gpui::ColorExt::blend(&self, &other)
21 }
22}
23
24fn try_parse_theme_token(value: &str) -> anyhow::Result<ThemeToken> {
25 Ok(ThemeToken::new(
26 try_parse_theme_color(value)?,
27 try_parse_background(value)?,
28 ))
29}
30
31#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
33#[serde(default)]
34pub struct ThemeSet {
35 pub name: SharedString,
37 pub author: Option<SharedString>,
39 pub url: Option<SharedString>,
41 #[serde(rename = "themes")]
43 pub themes: Vec<ThemeConfig>,
44}
45
46#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
47#[serde(default)]
48pub struct ThemeConfig {
49 pub is_default: bool,
51 pub name: SharedString,
53 pub mode: ThemeMode,
55
56 #[serde(rename = "font.size")]
58 pub font_size: Option<f32>,
59 #[serde(rename = "font.family")]
61 pub font_family: Option<SharedString>,
62 #[serde(rename = "mono_font.family")]
67 pub mono_font_family: Option<SharedString>,
68 #[serde(rename = "mono_font.size")]
70 pub mono_font_size: Option<f32>,
71
72 #[serde(rename = "radius")]
74 pub radius: Option<usize>,
75 #[serde(rename = "radius.lg")]
77 pub radius_lg: Option<usize>,
78 #[serde(rename = "shadow")]
80 pub shadow: Option<bool>,
81
82 pub colors: ThemeConfigColors,
84 pub highlight: Option<HighlightThemeStyle>,
88}
89
90#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
91#[serde(default)]
92pub struct SemanticThemeConfig {
93 pub colors: SemanticColorConfig,
94 pub radius: SemanticRadiusConfig,
95 pub spacing: SemanticSpacingConfig,
96 pub typography: SemanticTypographyConfig,
97 pub shadow: SemanticShadowConfig,
98}
99
100#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
105#[serde(default)]
106pub struct SemanticThemeConfigFile {
107 pub tokens: SemanticThemeConfig,
108}
109
110#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
111#[serde(default)]
112pub struct SemanticColorConfig {
113 pub background: Option<SharedString>,
114 pub foreground: Option<SharedString>,
115 pub surface: Option<SharedString>,
116 pub surface_foreground: Option<SharedString>,
117 pub primary: Option<SharedString>,
118 pub primary_foreground: Option<SharedString>,
119 pub secondary: Option<SharedString>,
120 pub secondary_foreground: Option<SharedString>,
121 pub muted: Option<SharedString>,
122 pub muted_foreground: Option<SharedString>,
123 pub accent: Option<SharedString>,
124 pub accent_foreground: Option<SharedString>,
125 pub destructive: Option<SharedString>,
126 pub destructive_foreground: Option<SharedString>,
127 pub border: Option<SharedString>,
128 pub input: Option<SharedString>,
129 pub ring: Option<SharedString>,
130}
131
132#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
133#[serde(default)]
134pub struct SemanticRadiusConfig {
135 pub none: Option<f32>,
136 pub sm: Option<f32>,
137 pub md: Option<f32>,
138 pub lg: Option<f32>,
139 pub xl: Option<f32>,
140 pub full: Option<f32>,
141}
142
143#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
144#[serde(default)]
145pub struct SemanticSpacingConfig {
146 pub xxs: Option<f32>,
147 pub xs: Option<f32>,
148 pub sm: Option<f32>,
149 pub md: Option<f32>,
150 pub lg: Option<f32>,
151 pub xl: Option<f32>,
152 pub xxl: Option<f32>,
153}
154
155#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
156#[serde(default)]
157pub struct SemanticTextStyleConfig {
158 pub size: Option<f32>,
159 pub line_height: Option<f32>,
160 pub weight: Option<FontWeight>,
161}
162
163#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
164#[serde(default)]
165pub struct SemanticTypographyConfig {
166 pub sans: Option<SharedString>,
167 pub mono: Option<SharedString>,
168 pub xs: SemanticTextStyleConfig,
169 pub sm: SemanticTextStyleConfig,
170 pub md: SemanticTextStyleConfig,
171 pub lg: SemanticTextStyleConfig,
172 pub xl: SemanticTextStyleConfig,
173 pub mono_md: SemanticTextStyleConfig,
174}
175
176#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
177#[serde(default)]
178pub struct SemanticShadowConfig {
179 #[schemars(with = "Option<Vec<serde_json::Value>>")]
180 pub sm: Option<Vec<BoxShadow>>,
181 #[schemars(with = "Option<Vec<serde_json::Value>>")]
182 pub md: Option<Vec<BoxShadow>>,
183 #[schemars(with = "Option<Vec<serde_json::Value>>")]
184 pub lg: Option<Vec<BoxShadow>>,
185}
186
187impl SemanticThemeConfig {
188 pub(crate) fn apply_to(&self, tokens: &mut SemanticThemeTokens) {
189 macro_rules! apply_color {
190 ($field:ident) => {
191 if let Some(value) = &self.colors.$field
192 && let Ok(value) = try_parse_color(value)
193 {
194 tokens.colors.$field = value;
195 }
196 };
197 }
198 apply_color!(background);
199 apply_color!(foreground);
200 apply_color!(surface);
201 apply_color!(surface_foreground);
202 apply_color!(primary);
203 apply_color!(primary_foreground);
204 apply_color!(secondary);
205 apply_color!(secondary_foreground);
206 apply_color!(muted);
207 apply_color!(muted_foreground);
208 apply_color!(accent);
209 apply_color!(accent_foreground);
210 apply_color!(destructive);
211 apply_color!(destructive_foreground);
212 apply_color!(border);
213 apply_color!(input);
214 apply_color!(ring);
215
216 macro_rules! apply_pixels {
217 ($config:expr, $tokens:expr, $($field:ident),+ $(,)?) => {
218 $(if let Some(value) = $config.$field { $tokens.$field = px(value); })+
219 };
220 }
221 apply_pixels!(self.radius, tokens.radius, none, sm, md, lg, xl, full);
222 apply_pixels!(self.spacing, tokens.spacing, xxs, xs, sm, md, lg, xl, xxl);
223
224 if let Some(value) = &self.typography.sans {
225 tokens.typography.sans = value.clone();
226 }
227 if let Some(value) = &self.typography.mono {
228 tokens.typography.mono = value.clone();
229 }
230 apply_text_style(&self.typography.xs, &mut tokens.typography.xs);
231 apply_text_style(&self.typography.sm, &mut tokens.typography.sm);
232 apply_text_style(&self.typography.md, &mut tokens.typography.md);
233 apply_text_style(&self.typography.lg, &mut tokens.typography.lg);
234 apply_text_style(&self.typography.xl, &mut tokens.typography.xl);
235 apply_text_style(&self.typography.mono_md, &mut tokens.typography.mono_md);
236
237 if let Some(value) = &self.shadow.sm {
238 tokens.shadow.sm = value.clone();
239 }
240 if let Some(value) = &self.shadow.md {
241 tokens.shadow.md = value.clone();
242 }
243 if let Some(value) = &self.shadow.lg {
244 tokens.shadow.lg = value.clone();
245 }
246 }
247}
248
249fn apply_text_style(config: &SemanticTextStyleConfig, token: &mut gpui_base::TextStyleToken) {
250 if let Some(value) = config.size {
251 token.size = px(value);
252 }
253 if let Some(value) = config.line_height {
254 token.line_height = px(value);
255 }
256 if let Some(value) = config.weight {
257 token.weight = value;
258 }
259}
260
261#[derive(Debug, Default, Clone, JsonSchema, Serialize, Deserialize)]
262pub struct ThemeConfigColors {
263 #[serde(rename = "accent.background")]
265 pub accent: Option<SharedString>,
266 #[serde(rename = "accent.foreground")]
268 pub accent_foreground: Option<SharedString>,
269 #[serde(rename = "accordion.background")]
271 pub accordion: Option<SharedString>,
272 #[serde(rename = "background")]
274 pub background: Option<SharedString>,
275 #[serde(rename = "border")]
277 pub border: Option<SharedString>,
278 #[serde(rename = "button.background")]
280 pub button: Option<SharedString>,
281 #[serde(rename = "button.active.background")]
283 pub button_active: Option<SharedString>,
284 #[serde(rename = "button.foreground")]
286 pub button_foreground: Option<SharedString>,
287 #[serde(rename = "button.hover.background")]
289 pub button_hover: Option<SharedString>,
290 #[serde(rename = "button.danger.background")]
292 pub button_danger: Option<SharedString>,
293 #[serde(rename = "button.danger.active.background")]
295 pub button_danger_active: Option<SharedString>,
296 #[serde(rename = "button.danger.foreground")]
298 pub button_danger_foreground: Option<SharedString>,
299 #[serde(rename = "button.danger.hover.background")]
301 pub button_danger_hover: Option<SharedString>,
302 #[serde(rename = "button.info.background")]
304 pub button_info: Option<SharedString>,
305 #[serde(rename = "button.info.active.background")]
307 pub button_info_active: Option<SharedString>,
308 #[serde(rename = "button.info.foreground")]
310 pub button_info_foreground: Option<SharedString>,
311 #[serde(rename = "button.info.hover.background")]
313 pub button_info_hover: Option<SharedString>,
314 #[serde(rename = "button.primary.background")]
316 pub button_primary: Option<SharedString>,
317 #[serde(rename = "button.primary.active.background")]
319 pub button_primary_active: Option<SharedString>,
320 #[serde(rename = "button.primary.foreground")]
322 pub button_primary_foreground: Option<SharedString>,
323 #[serde(rename = "button.primary.hover.background")]
325 pub button_primary_hover: Option<SharedString>,
326 #[serde(rename = "button.secondary.background")]
328 pub button_secondary: Option<SharedString>,
329 #[serde(rename = "button.secondary.active.background")]
331 pub button_secondary_active: Option<SharedString>,
332 #[serde(rename = "button.secondary.foreground")]
334 pub button_secondary_foreground: Option<SharedString>,
335 #[serde(rename = "button.secondary.hover.background")]
337 pub button_secondary_hover: Option<SharedString>,
338 #[serde(rename = "button.success.background")]
340 pub button_success: Option<SharedString>,
341 #[serde(rename = "button.success.active.background")]
343 pub button_success_active: Option<SharedString>,
344 #[serde(rename = "button.success.foreground")]
346 pub button_success_foreground: Option<SharedString>,
347 #[serde(rename = "button.success.hover.background")]
349 pub button_success_hover: Option<SharedString>,
350 #[serde(rename = "button.warning.background")]
352 pub button_warning: Option<SharedString>,
353 #[serde(rename = "button.warning.active.background")]
355 pub button_warning_active: Option<SharedString>,
356 #[serde(rename = "button.warning.foreground")]
358 pub button_warning_foreground: Option<SharedString>,
359 #[serde(rename = "button.warning.hover.background")]
361 pub button_warning_hover: Option<SharedString>,
362 #[serde(rename = "group_box.background")]
364 pub group_box: Option<SharedString>,
365 #[serde(rename = "group_box.foreground")]
367 pub group_box_foreground: Option<SharedString>,
368 #[serde(rename = "group_box.title.foreground")]
370 pub group_box_title_foreground: Option<SharedString>,
371 #[serde(rename = "caret")]
373 pub caret: Option<SharedString>,
374 #[serde(rename = "chart.1")]
376 pub chart_1: Option<SharedString>,
377 #[serde(rename = "chart.2")]
379 pub chart_2: Option<SharedString>,
380 #[serde(rename = "chart.3")]
382 pub chart_3: Option<SharedString>,
383 #[serde(rename = "chart.4")]
385 pub chart_4: Option<SharedString>,
386 #[serde(rename = "chart.5")]
388 pub chart_5: Option<SharedString>,
389 #[serde(rename = "chart_bullish")]
391 pub chart_bullish: Option<SharedString>,
392 #[serde(rename = "chart_bearish")]
394 pub chart_bearish: Option<SharedString>,
395 #[serde(rename = "danger.background")]
397 pub danger: Option<SharedString>,
398 #[serde(rename = "danger.active.background")]
400 pub danger_active: Option<SharedString>,
401 #[serde(rename = "danger.foreground")]
403 pub danger_foreground: Option<SharedString>,
404 #[serde(rename = "danger.hover.background")]
406 pub danger_hover: Option<SharedString>,
407 #[serde(rename = "description_list.label.background")]
409 pub description_list_label: Option<SharedString>,
410 #[serde(rename = "description_list.label.foreground")]
412 pub description_list_label_foreground: Option<SharedString>,
413 #[serde(rename = "drag.border")]
415 pub drag_border: Option<SharedString>,
416 #[serde(rename = "drop_target.background")]
418 pub drop_target: Option<SharedString>,
419 #[serde(rename = "foreground")]
421 pub foreground: Option<SharedString>,
422 #[serde(rename = "info.background")]
424 pub info: Option<SharedString>,
425 #[serde(rename = "info.active.background")]
427 pub info_active: Option<SharedString>,
428 #[serde(rename = "info.foreground")]
430 pub info_foreground: Option<SharedString>,
431 #[serde(rename = "info.hover.background")]
433 pub info_hover: Option<SharedString>,
434 #[serde(rename = "input.border")]
436 pub input: Option<SharedString>,
437 #[serde(rename = "link")]
439 pub link: Option<SharedString>,
440 #[serde(rename = "link.active")]
442 pub link_active: Option<SharedString>,
443 #[serde(rename = "link.hover")]
445 pub link_hover: Option<SharedString>,
446 #[serde(rename = "list.background")]
448 pub list: Option<SharedString>,
449 #[serde(rename = "list.active.background")]
451 pub list_active: Option<SharedString>,
452 #[serde(rename = "list.active.border")]
454 pub list_active_border: Option<SharedString>,
455 #[serde(rename = "list.even.background")]
457 pub list_even: Option<SharedString>,
458 #[serde(rename = "list.head.background")]
460 pub list_head: Option<SharedString>,
461 #[serde(rename = "list.hover.background")]
463 pub list_hover: Option<SharedString>,
464 #[serde(rename = "muted.background")]
466 pub muted: Option<SharedString>,
467 #[serde(rename = "muted.foreground")]
469 pub muted_foreground: Option<SharedString>,
470 #[serde(rename = "popover.background")]
472 pub popover: Option<SharedString>,
473 #[serde(rename = "popover.foreground")]
475 pub popover_foreground: Option<SharedString>,
476 #[serde(rename = "primary.background")]
478 pub primary: Option<SharedString>,
479 #[serde(rename = "primary.active.background")]
481 pub primary_active: Option<SharedString>,
482 #[serde(rename = "primary.foreground")]
484 pub primary_foreground: Option<SharedString>,
485 #[serde(rename = "primary.hover.background")]
487 pub primary_hover: Option<SharedString>,
488 #[serde(rename = "progress.bar.background")]
490 pub progress_bar: Option<SharedString>,
491 #[serde(rename = "ring")]
493 pub ring: Option<SharedString>,
494 #[serde(rename = "scrollbar.background")]
496 pub scrollbar: Option<SharedString>,
497 #[serde(rename = "scrollbar.thumb.background")]
499 pub scrollbar_thumb: Option<SharedString>,
500 #[serde(rename = "scrollbar.thumb.hover.background")]
502 pub scrollbar_thumb_hover: Option<SharedString>,
503 #[serde(rename = "secondary.background")]
505 pub secondary: Option<SharedString>,
506 #[serde(rename = "secondary.active.background")]
508 pub secondary_active: Option<SharedString>,
509 #[serde(rename = "secondary.foreground")]
511 pub secondary_foreground: Option<SharedString>,
512 #[serde(rename = "secondary.hover.background")]
514 pub secondary_hover: Option<SharedString>,
515 #[serde(rename = "selection.background")]
517 pub selection: Option<SharedString>,
518 #[serde(rename = "sidebar.background")]
520 pub sidebar: Option<SharedString>,
521 #[serde(rename = "sidebar.accent.background")]
523 pub sidebar_accent: Option<SharedString>,
524 #[serde(rename = "sidebar.accent.foreground")]
526 pub sidebar_accent_foreground: Option<SharedString>,
527 #[serde(rename = "sidebar.border")]
529 pub sidebar_border: Option<SharedString>,
530 #[serde(rename = "sidebar.foreground")]
532 pub sidebar_foreground: Option<SharedString>,
533 #[serde(rename = "sidebar.primary.background")]
535 pub sidebar_primary: Option<SharedString>,
536 #[serde(rename = "sidebar.primary.foreground")]
538 pub sidebar_primary_foreground: Option<SharedString>,
539 #[serde(rename = "skeleton.background")]
541 pub skeleton: Option<SharedString>,
542 #[serde(rename = "slider.background")]
544 pub slider_bar: Option<SharedString>,
545 #[serde(rename = "slider.thumb.background")]
547 pub slider_thumb: Option<SharedString>,
548 #[serde(rename = "success.background")]
550 pub success: Option<SharedString>,
551 #[serde(rename = "success.foreground")]
553 pub success_foreground: Option<SharedString>,
554 #[serde(rename = "success.hover.background")]
556 pub success_hover: Option<SharedString>,
557 #[serde(rename = "success.active.background")]
559 pub success_active: Option<SharedString>,
560 #[serde(rename = "switch.background")]
562 pub switch: Option<SharedString>,
563 #[serde(rename = "switch.thumb.background")]
565 pub switch_thumb: Option<SharedString>,
566 #[serde(rename = "tab.background")]
568 pub tab: Option<SharedString>,
569 #[serde(rename = "tab.active.background")]
571 pub tab_active: Option<SharedString>,
572 #[serde(rename = "tab.active.foreground")]
574 pub tab_active_foreground: Option<SharedString>,
575 #[serde(rename = "tab_bar.background")]
577 pub tab_bar: Option<SharedString>,
578 #[serde(rename = "tab_bar.segmented.background")]
580 pub tab_bar_segmented: Option<SharedString>,
581 #[serde(rename = "tab.foreground")]
583 pub tab_foreground: Option<SharedString>,
584 #[serde(rename = "table.background")]
586 pub table: Option<SharedString>,
587 #[serde(rename = "table.active.background")]
589 pub table_active: Option<SharedString>,
590 #[serde(rename = "table.active.border")]
592 pub table_active_border: Option<SharedString>,
593 #[serde(rename = "table.even.background")]
595 pub table_even: Option<SharedString>,
596 #[serde(rename = "table.head.background")]
598 pub table_head: Option<SharedString>,
599 #[serde(rename = "table.head.foreground")]
601 pub table_head_foreground: Option<SharedString>,
602 #[serde(rename = "table.foot.background")]
604 pub table_foot: Option<SharedString>,
605 #[serde(rename = "table.foot.foreground")]
607 pub table_foot_foreground: Option<SharedString>,
608 #[serde(rename = "table.hover.background")]
610 pub table_hover: Option<SharedString>,
611 #[serde(rename = "table.row.border")]
613 pub table_row_border: Option<SharedString>,
614 #[serde(rename = "title_bar.background")]
616 pub title_bar: Option<SharedString>,
617 #[serde(rename = "title_bar.border")]
619 pub title_bar_border: Option<SharedString>,
620 #[serde(rename = "status_bar.background")]
622 pub status_bar: Option<SharedString>,
623 #[serde(rename = "status_bar.border")]
625 pub status_bar_border: Option<SharedString>,
626 #[serde(rename = "tiles.background")]
628 pub tiles: Option<SharedString>,
629 #[serde(rename = "warning.background")]
631 pub warning: Option<SharedString>,
632 #[serde(rename = "warning.active.background")]
634 pub warning_active: Option<SharedString>,
635 #[serde(rename = "warning.hover.background")]
637 pub warning_hover: Option<SharedString>,
638 #[serde(rename = "warning.foreground")]
640 pub warning_foreground: Option<SharedString>,
641 #[serde(rename = "overlay")]
643 pub overlay: Option<SharedString>,
644 #[serde(rename = "window.border")]
650 pub window_border: Option<SharedString>,
651
652 #[serde(rename = "base.blue")]
654 blue: Option<String>,
655 #[serde(rename = "base.blue.light")]
657 blue_light: Option<String>,
658 #[serde(rename = "base.cyan")]
660 cyan: Option<String>,
661 #[serde(rename = "base.cyan.light")]
663 cyan_light: Option<String>,
664 #[serde(rename = "base.green")]
666 green: Option<String>,
667 #[serde(rename = "base.green.light")]
669 green_light: Option<String>,
670 #[serde(rename = "base.magenta")]
672 magenta: Option<String>,
673 #[serde(rename = "base.magenta.light")]
674 magenta_light: Option<String>,
675 #[serde(rename = "base.red")]
677 red: Option<String>,
678 #[serde(rename = "base.red.light")]
680 red_light: Option<String>,
681 #[serde(rename = "base.yellow")]
683 yellow: Option<String>,
684 #[serde(rename = "base.yellow.light")]
686 yellow_light: Option<String>,
687}
688
689impl ThemeColor {
690 pub(crate) fn apply_config(
692 &mut self,
693 config: &ThemeConfig,
694 default_theme: &ThemeColor,
695 ) -> ThemeTokens {
696 let colors = config.colors.clone();
697 let default_tokens = ThemeTokens::from(default_theme);
698 let mut tokens = default_tokens;
699
700 macro_rules! apply_color {
701 ($config_field:ident) => {
702 if let Some(value) = &colors.$config_field {
703 self.$config_field =
704 try_parse_color(value).unwrap_or(default_theme.$config_field);
705 } else {
706 self.$config_field = default_theme.$config_field;
707 }
708 tokens.$config_field = self.$config_field.into();
709 };
710 ($config_field:ident, fallback = $fallback:expr) => {
712 let fallback: gpui::Hsla = ($fallback).into();
713 if let Some(value) = &colors.$config_field {
714 self.$config_field = try_parse_color(value).unwrap_or(fallback);
715 } else {
716 self.$config_field = fallback;
717 }
718 tokens.$config_field = self.$config_field.into();
719 };
720 }
721
722 macro_rules! apply_background_color {
723 ($config_field:ident) => {
724 let token = if let Some(value) = &colors.$config_field {
725 if let Ok(token) = try_parse_theme_token(&value) {
726 token
727 } else {
728 default_tokens.$config_field
729 }
730 } else {
731 default_tokens.$config_field
732 };
733 self.$config_field = token.color;
734 tokens.$config_field = token;
735 };
736 ($config_field:ident, fallback = $fallback:expr) => {
737 let fallback: ThemeToken = ($fallback).into();
738 let token = if let Some(value) = &colors.$config_field {
739 if let Ok(token) = try_parse_theme_token(&value) {
740 token
741 } else {
742 fallback
743 }
744 } else {
745 fallback
746 };
747 self.$config_field = token.color;
748 tokens.$config_field = token;
749 };
750 }
751
752 apply_background_color!(background);
753
754 apply_color!(red);
756 apply_color!(
757 red_light,
758 fallback = self.background.blend(self.red.opacity(0.8))
759 );
760 apply_color!(green);
761 apply_color!(
762 green_light,
763 fallback = self.background.blend(self.green.opacity(0.8))
764 );
765 apply_color!(blue);
766 apply_color!(
767 blue_light,
768 fallback = self.background.blend(self.blue.opacity(0.8))
769 );
770 apply_color!(magenta);
771 apply_color!(
772 magenta_light,
773 fallback = self.background.blend(self.magenta.opacity(0.8))
774 );
775 apply_color!(yellow);
776 apply_color!(
777 yellow_light,
778 fallback = self.background.blend(self.yellow.opacity(0.8))
779 );
780 apply_color!(cyan);
781 apply_color!(
782 cyan_light,
783 fallback = self.background.blend(self.cyan.opacity(0.8))
784 );
785
786 apply_color!(border);
787 apply_color!(foreground);
788 apply_color!(input, fallback = self.border);
789 apply_background_color!(muted);
790 apply_color!(
791 muted_foreground,
792 fallback = self.muted.blend(self.foreground.opacity(0.7))
793 );
794
795 let active_darken = if config.mode.is_dark() { 0.2 } else { 0.1 };
797 let hover_opacity = 0.9;
798 let transparent = gpui::transparent_black();
799 let button_background = if config.mode.is_dark() {
800 self.input.mix_oklab(transparent, 0.3)
801 } else {
802 self.background
803 };
804 apply_background_color!(button, fallback = button_background);
805 apply_color!(button_foreground, fallback = self.foreground);
806 apply_background_color!(
807 button_hover,
808 fallback = self.input.mix_oklab(transparent, 0.5)
809 );
810 apply_background_color!(
811 button_active,
812 fallback = self.input.mix_oklab(transparent, 0.7)
813 );
814 apply_background_color!(primary);
815 apply_color!(primary_foreground, fallback = self.foreground);
816 apply_background_color!(
817 primary_hover,
818 fallback = self.background.blend(self.primary.opacity(hover_opacity))
819 );
820 apply_background_color!(
821 primary_active,
822 fallback = self.primary.darken(active_darken)
823 );
824 apply_background_color!(button_primary, fallback = tokens.primary);
825 apply_color!(
826 button_primary_foreground,
827 fallback = self.primary_foreground
828 );
829 apply_background_color!(button_primary_hover, fallback = tokens.primary_hover);
830 apply_background_color!(button_primary_active, fallback = tokens.primary_active);
831 apply_background_color!(secondary);
832 apply_color!(secondary_foreground, fallback = self.foreground);
833 apply_background_color!(
834 secondary_hover,
835 fallback = self.background.blend(self.secondary.opacity(hover_opacity))
836 );
837 apply_background_color!(
838 secondary_active,
839 fallback = self.secondary.darken(active_darken)
840 );
841 apply_background_color!(button_secondary, fallback = tokens.secondary);
842 apply_color!(
843 button_secondary_foreground,
844 fallback = self.secondary_foreground
845 );
846 apply_background_color!(button_secondary_hover, fallback = tokens.secondary_hover);
847 apply_background_color!(button_secondary_active, fallback = tokens.secondary_active);
848 apply_background_color!(success, fallback = self.green);
849 apply_color!(success_foreground, fallback = self.primary_foreground);
850 apply_background_color!(
851 success_hover,
852 fallback = self.background.blend(self.success.opacity(hover_opacity))
853 );
854 apply_background_color!(
855 success_active,
856 fallback = self.success.darken(active_darken)
857 );
858 apply_background_color!(
859 button_success,
860 fallback = self.success.mix_oklab(transparent, 0.2)
861 );
862 apply_color!(button_success_foreground, fallback = self.success);
863 apply_background_color!(
864 button_success_hover,
865 fallback = self.success.mix_oklab(transparent, 0.3)
866 );
867 apply_background_color!(
868 button_success_active,
869 fallback = self.success.mix_oklab(transparent, 0.4)
870 );
871 apply_background_color!(info, fallback = self.cyan);
872 apply_color!(info_foreground, fallback = self.primary_foreground);
873 apply_background_color!(
874 info_hover,
875 fallback = self.background.blend(self.info.opacity(hover_opacity))
876 );
877 apply_background_color!(info_active, fallback = self.info.darken(active_darken));
878 apply_background_color!(
879 button_info,
880 fallback = self.info.mix_oklab(transparent, 0.2)
881 );
882 apply_color!(button_info_foreground, fallback = self.info);
883 apply_background_color!(
884 button_info_hover,
885 fallback = self.info.mix_oklab(transparent, 0.3)
886 );
887 apply_background_color!(
888 button_info_active,
889 fallback = self.info.mix_oklab(transparent, 0.4)
890 );
891 apply_background_color!(warning, fallback = self.yellow);
892 apply_color!(warning_foreground, fallback = self.primary_foreground);
893 apply_background_color!(
894 warning_hover,
895 fallback = self.background.blend(self.warning.opacity(0.9))
896 );
897 apply_background_color!(
898 warning_active,
899 fallback = self.background.blend(self.warning.darken(active_darken))
900 );
901 apply_background_color!(
902 button_warning,
903 fallback = self.warning.mix_oklab(transparent, 0.2)
904 );
905 apply_color!(button_warning_foreground, fallback = self.warning);
906 apply_background_color!(
907 button_warning_hover,
908 fallback = self.warning.mix_oklab(transparent, 0.3)
909 );
910 apply_background_color!(
911 button_warning_active,
912 fallback = self.warning.mix_oklab(transparent, 0.4)
913 );
914
915 apply_background_color!(accent, fallback = tokens.secondary);
917 apply_color!(accent_foreground, fallback = self.foreground);
918 apply_background_color!(accordion, fallback = tokens.background);
919 apply_background_color!(
920 group_box,
921 fallback = self
922 .background
923 .blend(
924 self.secondary
925 .opacity(if config.mode.is_dark() { 0.3 } else { 0.4 })
926 )
927 );
928 apply_color!(group_box_foreground, fallback = self.foreground);
929 apply_color!(caret, fallback = self.primary);
930 apply_color!(chart_1, fallback = self.blue.lighten(0.4));
931 apply_color!(chart_2, fallback = self.blue.lighten(0.2));
932 apply_color!(chart_3, fallback = self.blue);
933 apply_color!(chart_4, fallback = self.blue.darken(0.2));
934 apply_color!(chart_5, fallback = self.blue.darken(0.4));
935 apply_color!(chart_bullish, fallback = self.green);
936 apply_color!(chart_bearish, fallback = self.red);
937 apply_background_color!(danger, fallback = self.red);
938 apply_background_color!(danger_active, fallback = self.danger.darken(active_darken));
939 apply_color!(danger_foreground, fallback = self.primary_foreground);
940 apply_background_color!(
941 danger_hover,
942 fallback = self.background.blend(self.danger.opacity(0.9))
943 );
944 apply_background_color!(
945 button_danger,
946 fallback = self.danger.mix_oklab(transparent, 0.2)
947 );
948 apply_color!(button_danger_foreground, fallback = self.danger);
949 apply_background_color!(
950 button_danger_hover,
951 fallback = self.danger.mix_oklab(transparent, 0.3)
952 );
953 apply_background_color!(
954 button_danger_active,
955 fallback = self.danger.mix_oklab(transparent, 0.4)
956 );
957 apply_background_color!(
958 description_list_label,
959 fallback = self.background.blend(self.border.opacity(0.2))
960 );
961 apply_color!(
962 description_list_label_foreground,
963 fallback = self.muted_foreground
964 );
965 apply_color!(drag_border, fallback = self.primary.opacity(0.65));
966 apply_background_color!(drop_target, fallback = self.primary.opacity(0.2));
967 apply_color!(link, fallback = self.primary);
968 apply_color!(link_active, fallback = self.link);
969 apply_color!(link_hover, fallback = self.link);
970 apply_background_color!(list, fallback = tokens.background);
971 apply_background_color!(
972 list_active,
973 fallback = self.background.blend(self.primary.opacity(0.1))
974 );
975 apply_color!(
976 list_active_border,
977 fallback = self.background.blend(self.primary.opacity(0.6))
978 );
979 apply_background_color!(list_even, fallback = tokens.list);
980 apply_background_color!(list_head, fallback = tokens.list);
981 apply_background_color!(list_hover, fallback = self.accent.opacity(0.6));
982 apply_background_color!(popover, fallback = tokens.background);
983 apply_color!(popover_foreground, fallback = self.foreground);
984 apply_background_color!(progress_bar, fallback = tokens.primary);
985 apply_color!(ring, fallback = self.blue);
986 apply_background_color!(scrollbar, fallback = tokens.background);
987 apply_background_color!(scrollbar_thumb, fallback = tokens.accent);
988 apply_background_color!(scrollbar_thumb_hover, fallback = tokens.scrollbar_thumb);
989 apply_background_color!(selection, fallback = tokens.primary);
990 apply_background_color!(
991 sidebar,
992 fallback = self.background.blend(self.border.opacity(0.15))
993 );
994 apply_background_color!(sidebar_accent, fallback = tokens.accent);
995 apply_color!(sidebar_accent_foreground, fallback = self.accent_foreground);
996 apply_color!(sidebar_border, fallback = self.border);
997 apply_color!(sidebar_foreground, fallback = self.foreground);
998 apply_background_color!(sidebar_primary, fallback = tokens.primary);
999 apply_color!(
1000 sidebar_primary_foreground,
1001 fallback = self.primary_foreground
1002 );
1003 apply_background_color!(skeleton, fallback = tokens.secondary);
1004 apply_background_color!(slider_bar, fallback = tokens.primary);
1005 apply_background_color!(slider_thumb, fallback = self.primary_foreground);
1006 apply_background_color!(switch, fallback = tokens.secondary_active);
1007 apply_background_color!(switch_thumb, fallback = tokens.background);
1008 apply_background_color!(tab, fallback = tokens.background);
1009 apply_background_color!(tab_active, fallback = tokens.background);
1010 apply_color!(tab_active_foreground, fallback = self.foreground);
1011 apply_background_color!(tab_bar, fallback = tokens.background);
1012 apply_background_color!(tab_bar_segmented, fallback = tokens.secondary);
1013 apply_color!(tab_foreground, fallback = self.foreground);
1014 apply_background_color!(table, fallback = tokens.list);
1015 apply_background_color!(table_active, fallback = tokens.list_active);
1016 apply_color!(table_active_border, fallback = self.list_active_border);
1017 apply_background_color!(table_even, fallback = tokens.list_even);
1018 apply_background_color!(table_head, fallback = tokens.list_head);
1019 apply_color!(table_head_foreground, fallback = self.muted_foreground);
1020 apply_background_color!(table_foot, fallback = tokens.list_head);
1021 apply_color!(table_foot_foreground, fallback = self.muted_foreground);
1022 apply_background_color!(table_hover, fallback = tokens.list_hover);
1023 apply_color!(table_row_border, fallback = self.border);
1024 apply_background_color!(title_bar, fallback = tokens.background);
1025 apply_color!(title_bar_border, fallback = self.border);
1026 apply_background_color!(status_bar, fallback = tokens.title_bar);
1027 apply_color!(status_bar_border, fallback = self.title_bar_border);
1028 apply_background_color!(tiles, fallback = tokens.background);
1029 apply_background_color!(overlay);
1030 apply_color!(window_border, fallback = self.border);
1031
1032 let clamp_alpha = |raw: Option<&str>, color: Hsla, background: Background, max: f32| {
1036 let base = color.alpha;
1037 let target = base.min(max);
1038 let mut color = color;
1039 color.alpha = target;
1040 let background = raw
1041 .and_then(|value| try_parse_background_clamped(value, max).ok())
1042 .unwrap_or_else(|| {
1043 let factor = if base > 0. { target / base } else { 1. };
1044 background.opacity(factor)
1045 });
1046 (color, ThemeToken::new(color, background))
1047 };
1048
1049 (self.list_active, tokens.list_active) = clamp_alpha(
1050 colors.list_active.as_deref(),
1051 self.list_active,
1052 tokens.list_active.background,
1053 0.2,
1054 );
1055 (self.table_active, tokens.table_active) = clamp_alpha(
1056 colors.table_active.as_deref(),
1057 self.table_active,
1058 tokens.table_active.background,
1059 0.2,
1060 );
1061 (self.selection, tokens.selection) = clamp_alpha(
1062 colors.selection.as_deref(),
1063 self.selection,
1064 tokens.selection.background,
1065 0.3,
1066 );
1067
1068 tokens
1069 }
1070}
1071
1072impl Theme {
1073 pub fn apply_config(&mut self, config: &Rc<ThemeConfig>) {
1075 if config.mode.is_dark() {
1076 self.dark_theme = config.clone();
1077 } else {
1078 self.light_theme = config.clone();
1079 }
1080 if let Some(style) = &config.highlight {
1081 let highlight_theme = Arc::new(HighlightTheme {
1082 name: config.name.to_string(),
1083 appearance: config.mode,
1084 style: style.clone(),
1085 });
1086 self.highlight_theme = highlight_theme.clone();
1087 }
1088
1089 let default_colors = if config.mode.is_dark() {
1090 ThemeColor::dark()
1091 } else {
1092 ThemeColor::light()
1093 };
1094
1095 if let Some(font_size) = config.font_size {
1096 self.font_size = px(font_size);
1097 }
1098 if let Some(font_family) = &config.font_family {
1099 self.font_family = font_family.clone();
1100 }
1101 if let Some(mono_font_family) = &config.mono_font_family {
1102 self.mono_font_family = mono_font_family.clone();
1103 }
1104 if let Some(mono_font_size) = config.mono_font_size {
1105 self.mono_font_size = px(mono_font_size);
1106 }
1107 if let Some(radius) = config.radius {
1108 self.radius = px(radius as f32);
1109 }
1110 if let Some(radius_lg) = config.radius_lg {
1111 self.radius_lg = px(radius_lg as f32);
1112 }
1113 if let Some(shadow) = config.shadow {
1114 self.shadow = shadow;
1115 }
1116
1117 self.tokens = self.colors.apply_config(&config, &default_colors);
1118 self.mode = config.mode;
1119 }
1120}
1121
1122#[cfg(test)]
1123mod tests {
1124 use gpui::{Hsla, linear_color_stop, linear_gradient, px};
1125
1126 use crate::{Theme, ThemeConfig, ThemeMode, ThemeSet, try_parse_color};
1127
1128 fn with_alpha(mut color: Hsla, alpha: f32) -> Hsla {
1129 color.alpha = alpha;
1130 color
1131 }
1132
1133 #[test]
1134 fn test_semantic_theme_config_parses_and_roundtrips() {
1135 let value = serde_json::json!({
1136 "name": "Semantic",
1137 "mode": "dark",
1138 "tokens": {
1139 "colors": {
1140 "surface": "#111827",
1141 "surface_foreground": "#f9fafb",
1142 "primary": "#2563eb",
1143 "destructive": "#dc2626"
1144 },
1145 "radius": { "sm": 2.0, "md": 6.0, "lg": 10.0 },
1146 "spacing": { "xs": 4.0, "md": 12.0, "xl": 24.0 },
1147 "typography": {
1148 "sans": "Inter",
1149 "md": { "size": 15.0, "line_height": 22.0 }
1150 }
1151 }
1152 });
1153 let config: super::SemanticThemeConfigFile = serde_json::from_value(value).unwrap();
1154 let serialized = serde_json::to_string(&config).unwrap();
1155 let reparsed: super::SemanticThemeConfigFile = serde_json::from_str(&serialized).unwrap();
1156 let semantic = reparsed.tokens;
1157
1158 assert_eq!(semantic.colors.surface.as_deref(), Some("#111827"));
1159 assert_eq!(semantic.colors.destructive.as_deref(), Some("#dc2626"));
1160 assert_eq!(semantic.radius.lg, Some(10.0));
1161 assert_eq!(semantic.spacing.xl, Some(24.0));
1162 assert_eq!(semantic.typography.sans.as_deref(), Some("Inter"));
1163 assert_eq!(semantic.typography.md.line_height, Some(22.0));
1164
1165 let mut theme = Theme::default();
1166 let resolved = theme.apply_semantic_config_str(&serialized).unwrap();
1167 assert_eq!(theme.primary, try_parse_color("#2563eb").unwrap());
1168 assert_eq!(resolved.spacing.xl, px(24.));
1169 assert_eq!(resolved.typography.md.line_height, px(22.));
1170 }
1171
1172 #[test]
1173 fn test_semantic_tokens_override_legacy_generic_fields_only() {
1174 let config = serde_json::from_value::<super::SemanticThemeConfigFile>(serde_json::json!({
1175 "tokens": {
1176 "colors": { "primary": "#2563eb", "destructive": "#b91c1c" },
1177 "spacing": { "md": 14.0 },
1178 "typography": { "md": { "size": 15.0 } }
1179 }
1180 }))
1181 .unwrap();
1182 let mut theme = Theme::default();
1183 let component_color = theme.button_primary;
1184 let resolved = theme.apply_semantic_config(&config.tokens);
1185
1186 assert_eq!(theme.primary, try_parse_color("#2563eb").unwrap());
1187 assert_eq!(theme.danger, try_parse_color("#b91c1c").unwrap());
1188 assert_eq!(theme.button_primary, component_color);
1189 assert_eq!(resolved.spacing.md, px(14.));
1190 assert_eq!(resolved.typography.md.size, px(15.));
1191 }
1192
1193 #[test]
1194 fn test_legacy_config_without_semantic_tokens_is_unchanged() {
1195 let config = serde_json::from_value::<ThemeConfig>(serde_json::json!({
1196 "name": "Legacy",
1197 "mode": "light",
1198 "radius": 7,
1199 "colors": { "primary.background": "#7c3aed" }
1200 }))
1201 .unwrap();
1202 let mut theme = Theme::default();
1203 theme.apply_config(&std::rc::Rc::new(config));
1204 assert_eq!(theme.primary, try_parse_color("#7c3aed").unwrap());
1205 assert_eq!(theme.radius, px(7.));
1206 assert_eq!(theme.semantic_tokens().spacing, Default::default());
1207 }
1208
1209 #[test]
1210 fn test_apply_config_preserves_gradient_background_and_solid_color_fallback() {
1211 let config = serde_json::from_value::<ThemeConfig>(serde_json::json!({
1212 "name": "Gradient",
1213 "mode": "light",
1214 "colors": {
1215 "primary.background": "linear-gradient(135deg, #4F46E5, #06B6D4)",
1216 "button.primary.hover.background": "linear-gradient(to right, red-500 25%, blue-600 75%)"
1217 }
1218 }))
1219 .unwrap();
1220
1221 let mut theme = Theme::default();
1222 theme.apply_config(&std::rc::Rc::new(config));
1223
1224 let primary_from = try_parse_color("#4F46E5").unwrap();
1225 let primary_to = try_parse_color("#06B6D4").unwrap();
1226 assert_eq!(theme.primary, primary_from);
1227 assert_eq!(theme.tokens.primary.color, primary_from);
1228 assert_eq!(
1229 theme.tokens.primary.background,
1230 linear_gradient(
1231 135.,
1232 linear_color_stop(primary_from, 0.),
1233 linear_color_stop(primary_to, 1.)
1234 )
1235 );
1236 assert_eq!(
1237 theme.tokens.button_primary.background,
1238 theme.tokens.primary.background
1239 );
1240 assert_eq!(
1241 theme.tokens.button_primary_hover.background,
1242 linear_gradient(
1243 90.,
1244 linear_color_stop(crate::red_500(), 0.25),
1245 linear_color_stop(crate::blue_600(), 0.75)
1246 )
1247 );
1248 assert_eq!(theme.mode, ThemeMode::Light);
1249 }
1250
1251 #[test]
1252 fn test_aurora_theme_parses_gradient_backgrounds() {
1253 let theme_set =
1254 serde_json::from_str::<ThemeSet>(include_str!("../../../../themes/aurora.json"))
1255 .unwrap();
1256 assert_eq!(theme_set.themes.len(), 1);
1257 assert!(theme_set.themes.iter().all(|theme| !theme.mode.is_dark()));
1258
1259 let light = theme_set
1260 .themes
1261 .iter()
1262 .find(|theme| theme.name.as_ref() == "Aurora Light")
1263 .unwrap();
1264 let mut theme = Theme::default();
1265 theme.apply_config(&std::rc::Rc::new(light.clone()));
1266
1267 assert_ne!(
1268 theme.tokens.button_primary.background,
1269 theme.button_primary.into()
1270 );
1271 assert_eq!(theme.tokens.background.background, theme.background.into());
1272 assert_eq!(theme.button_primary, try_parse_color("#1E293B").unwrap());
1273 assert_eq!(theme.background, try_parse_color("#FFFFFF").unwrap());
1274 assert_ne!(
1275 theme.tokens.progress_bar.background,
1276 theme.progress_bar.into()
1277 );
1278 assert_ne!(
1279 theme.tokens.scrollbar_thumb.background,
1280 theme.scrollbar_thumb.into()
1281 );
1282 assert_ne!(theme.tokens.switch.background, theme.switch.into());
1283 assert_ne!(
1284 theme.tokens.switch_thumb.background,
1285 theme.switch_thumb.into()
1286 );
1287 assert_ne!(theme.tokens.title_bar.background, theme.title_bar.into());
1288 assert_ne!(theme.tokens.status_bar.background, theme.status_bar.into());
1289 }
1290
1291 #[test]
1292 fn test_apply_config_clamps_highlight_alpha_per_gradient_stop() {
1293 let config = serde_json::from_value::<ThemeConfig>(serde_json::json!({
1294 "name": "Highlight",
1295 "mode": "light",
1296 "colors": {
1297 "list.active.background": "#3b82f6",
1299 "table.active.background": "linear-gradient(#bfdbfe33, #3b82f6)",
1302 "selection.background": "linear-gradient(#3b82f600, #3b82f6)",
1306 }
1307 }))
1308 .unwrap();
1309
1310 let mut theme = Theme::default();
1311 theme.apply_config(&std::rc::Rc::new(config));
1312
1313 let blue = try_parse_color("#3b82f6").unwrap();
1315 assert_eq!(theme.list_active, with_alpha(blue, 0.2));
1316 assert_eq!(
1317 theme.tokens.list_active.background,
1318 with_alpha(blue, 0.2).into()
1319 );
1320
1321 let faint = try_parse_color("#bfdbfe33").unwrap();
1323 assert_eq!(
1324 theme.tokens.table_active.background,
1325 linear_gradient(
1326 180.,
1327 linear_color_stop(with_alpha(faint, faint.alpha.min(0.2)), 0.),
1328 linear_color_stop(with_alpha(blue, 0.2), 1.),
1329 )
1330 );
1331
1332 let clear = try_parse_color("#3b82f600").unwrap();
1335 assert_eq!(
1336 theme.tokens.selection.background,
1337 linear_gradient(
1338 180.,
1339 linear_color_stop(with_alpha(clear, clear.alpha.min(0.3)), 0.),
1340 linear_color_stop(with_alpha(blue, 0.3), 1.),
1341 )
1342 );
1343 }
1344}