BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / theme / schema.rs

schema.rs

schema.rs 54.7 KiB Download Raw
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
14fn try_parse_theme_token(value: &str) -> anyhow::Result<ThemeToken> {
15 Ok(ThemeToken::new(
16 try_parse_theme_color(value)?,
17 try_parse_background(value)?,
18 ))
19}
20
21/// Represents a theme configuration.
22#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
23#[serde(default)]
24pub struct ThemeSet {
25 /// The name of the theme set.
26 pub name: SharedString,
27 /// The author of the theme.
28 pub author: Option<SharedString>,
29 /// The URL of the theme.
30 pub url: Option<SharedString>,
31 /// The theme list of the theme set.
32 #[serde(rename = "themes")]
33 pub themes: Vec<ThemeConfig>,
34}
35
36#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
37#[serde(default)]
38pub struct ThemeConfig {
39 /// Whether this theme is the default theme.
40 pub is_default: bool,
41 /// The name of the theme.
42 pub name: SharedString,
43 /// The mode of the theme, default is light.
44 pub mode: ThemeMode,
45
46 /// The base font size, default is 16.
47 #[serde(rename = "font.size")]
48 pub font_size: Option<f32>,
49 /// The base font family, default is system font: `.SystemUIFont`.
50 #[serde(rename = "font.family")]
51 pub font_family: Option<SharedString>,
52 /// The monospace font family, default is platform specific:
53 /// - macOS: `Menlo`
54 /// - Windows: `Consolas`
55 /// - Linux: `DejaVu Sans Mono`
56 ///
57 /// The default falls back to an installed monospace font, then to
58 /// `.SystemUIFont`, when the machine lacks it. A family named here is used
59 /// as-is, so name one that is installed or embedded with `add_fonts`.
60 #[serde(rename = "mono_font.family")]
61 pub mono_font_family: Option<SharedString>,
62 /// The monospace font size, default is 13.
63 #[serde(rename = "mono_font.size")]
64 pub mono_font_size: Option<f32>,
65
66 /// The border radius for general elements, default is 6.
67 #[serde(rename = "radius")]
68 pub radius: Option<usize>,
69 /// The border radius for large elements like Dialogs and Notifications, default is 8.
70 #[serde(rename = "radius.lg")]
71 pub radius_lg: Option<usize>,
72 /// Set shadows in the theme, for example the Input and Button, default is true.
73 #[serde(rename = "shadow")]
74 pub shadow: Option<bool>,
75
76 /// The colors of the theme.
77 pub colors: ThemeConfigColors,
78 /// The highlight theme, this part is combilbility with `style` section in Zed theme.
79 ///
80 /// https://github.com/zed-industries/zed/blob/f50041779dcfd7a76c8aec293361c60c53f02d51/assets/themes/ayu/ayu.json#L9
81 pub highlight: Option<HighlightThemeStyle>,
82}
83
84#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
85#[serde(default)]
86pub struct SemanticThemeConfig {
87 pub colors: SemanticColorConfig,
88 pub radius: SemanticRadiusConfig,
89 pub spacing: SemanticSpacingConfig,
90 pub typography: SemanticTypographyConfig,
91 pub shadow: SemanticShadowConfig,
92}
93
94/// Standalone semantic theme configuration file.
95///
96/// This wrapper is intentionally separate from [`ThemeConfig`] so adding
97/// semantic tokens does not change the legacy public struct shape.
98#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
99#[serde(default)]
100pub struct SemanticThemeConfigFile {
101 pub tokens: SemanticThemeConfig,
102}
103
104#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
105#[serde(default)]
106pub struct SemanticColorConfig {
107 pub background: Option<SharedString>,
108 pub foreground: Option<SharedString>,
109 pub surface: Option<SharedString>,
110 pub surface_foreground: Option<SharedString>,
111 pub primary: Option<SharedString>,
112 pub primary_foreground: Option<SharedString>,
113 pub secondary: Option<SharedString>,
114 pub secondary_foreground: Option<SharedString>,
115 pub muted: Option<SharedString>,
116 pub muted_foreground: Option<SharedString>,
117 pub accent: Option<SharedString>,
118 pub accent_foreground: Option<SharedString>,
119 pub destructive: Option<SharedString>,
120 pub destructive_foreground: Option<SharedString>,
121 pub border: Option<SharedString>,
122 pub input: Option<SharedString>,
123 pub ring: Option<SharedString>,
124}
125
126#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
127#[serde(default)]
128pub struct SemanticRadiusConfig {
129 pub none: Option<f32>,
130 pub sm: Option<f32>,
131 pub md: Option<f32>,
132 pub lg: Option<f32>,
133 pub xl: Option<f32>,
134 pub full: Option<f32>,
135}
136
137#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
138#[serde(default)]
139pub struct SemanticSpacingConfig {
140 pub xxs: Option<f32>,
141 pub xs: Option<f32>,
142 pub sm: Option<f32>,
143 pub md: Option<f32>,
144 pub lg: Option<f32>,
145 pub xl: Option<f32>,
146 pub xxl: Option<f32>,
147}
148
149#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
150#[serde(default)]
151pub struct SemanticTextStyleConfig {
152 pub size: Option<f32>,
153 pub line_height: Option<f32>,
154 pub weight: Option<FontWeight>,
155}
156
157#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
158#[serde(default)]
159pub struct SemanticTypographyConfig {
160 pub sans: Option<SharedString>,
161 pub mono: Option<SharedString>,
162 pub xs: SemanticTextStyleConfig,
163 pub sm: SemanticTextStyleConfig,
164 pub md: SemanticTextStyleConfig,
165 pub lg: SemanticTextStyleConfig,
166 pub xl: SemanticTextStyleConfig,
167 pub mono_md: SemanticTextStyleConfig,
168}
169
170#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
171#[serde(default)]
172pub struct SemanticShadowConfig {
173 pub sm: Option<Vec<BoxShadow>>,
174 pub md: Option<Vec<BoxShadow>>,
175 pub lg: Option<Vec<BoxShadow>>,
176}
177
178impl SemanticThemeConfig {
179 pub(crate) fn apply_to(&self, tokens: &mut SemanticThemeTokens) {
180 macro_rules! apply_color {
181 ($field:ident) => {
182 if let Some(value) = &self.colors.$field
183 && let Ok(value) = try_parse_color(value)
184 {
185 tokens.colors.$field = value;
186 }
187 };
188 }
189 apply_color!(background);
190 apply_color!(foreground);
191 apply_color!(surface);
192 apply_color!(surface_foreground);
193 apply_color!(primary);
194 apply_color!(primary_foreground);
195 apply_color!(secondary);
196 apply_color!(secondary_foreground);
197 apply_color!(muted);
198 apply_color!(muted_foreground);
199 apply_color!(accent);
200 apply_color!(accent_foreground);
201 apply_color!(destructive);
202 apply_color!(destructive_foreground);
203 apply_color!(border);
204 apply_color!(input);
205 apply_color!(ring);
206
207 macro_rules! apply_pixels {
208 ($config:expr, $tokens:expr, $($field:ident),+ $(,)?) => {
209 $(if let Some(value) = $config.$field { $tokens.$field = px(value); })+
210 };
211 }
212 apply_pixels!(self.radius, tokens.radius, none, sm, md, lg, xl, full);
213 apply_pixels!(self.spacing, tokens.spacing, xxs, xs, sm, md, lg, xl, xxl);
214
215 if let Some(value) = &self.typography.sans {
216 tokens.typography.sans = value.clone();
217 }
218 if let Some(value) = &self.typography.mono {
219 tokens.typography.mono = value.clone();
220 }
221 apply_text_style(&self.typography.xs, &mut tokens.typography.xs);
222 apply_text_style(&self.typography.sm, &mut tokens.typography.sm);
223 apply_text_style(&self.typography.md, &mut tokens.typography.md);
224 apply_text_style(&self.typography.lg, &mut tokens.typography.lg);
225 apply_text_style(&self.typography.xl, &mut tokens.typography.xl);
226 apply_text_style(&self.typography.mono_md, &mut tokens.typography.mono_md);
227
228 if let Some(value) = &self.shadow.sm {
229 tokens.shadow.sm = value.clone();
230 }
231 if let Some(value) = &self.shadow.md {
232 tokens.shadow.md = value.clone();
233 }
234 if let Some(value) = &self.shadow.lg {
235 tokens.shadow.lg = value.clone();
236 }
237 }
238}
239
240fn apply_text_style(config: &SemanticTextStyleConfig, token: &mut gpui_base::TextStyleToken) {
241 if let Some(value) = config.size {
242 token.size = px(value);
243 }
244 if let Some(value) = config.line_height {
245 token.line_height = px(value);
246 }
247 if let Some(value) = config.weight {
248 token.weight = value;
249 }
250}
251
252#[derive(Debug, Default, Clone, JsonSchema, Serialize, Deserialize)]
253pub struct ThemeConfigColors {
254 /// Used for accents such as hover background on MenuItem, ListItem, etc.
255 #[serde(rename = "accent.background")]
256 pub accent: Option<SharedString>,
257 /// Used for accent text color.
258 #[serde(rename = "accent.foreground")]
259 pub accent_foreground: Option<SharedString>,
260 /// Accordion background color.
261 #[serde(rename = "accordion.background")]
262 pub accordion: Option<SharedString>,
263 /// Default background color.
264 #[serde(rename = "background")]
265 pub background: Option<SharedString>,
266 /// Default border color
267 #[serde(rename = "border")]
268 pub border: Option<SharedString>,
269 /// Default Button background color.
270 #[serde(rename = "button.background")]
271 pub button: Option<SharedString>,
272 /// Default Button active background color.
273 #[serde(rename = "button.active.background")]
274 pub button_active: Option<SharedString>,
275 /// Default Button text color.
276 #[serde(rename = "button.foreground")]
277 pub button_foreground: Option<SharedString>,
278 /// Default Button hover background color.
279 #[serde(rename = "button.hover.background")]
280 pub button_hover: Option<SharedString>,
281 /// Button danger background color, fallback to `danger`.
282 #[serde(rename = "button.danger.background")]
283 pub button_danger: Option<SharedString>,
284 /// Button danger active background color, fallback to `danger_active`.
285 #[serde(rename = "button.danger.active.background")]
286 pub button_danger_active: Option<SharedString>,
287 /// Button danger text color, fallback to `danger_foreground`.
288 #[serde(rename = "button.danger.foreground")]
289 pub button_danger_foreground: Option<SharedString>,
290 /// Button danger hover background color, fallback to `danger_hover`.
291 #[serde(rename = "button.danger.hover.background")]
292 pub button_danger_hover: Option<SharedString>,
293 /// Button info background color, fallback to `info`.
294 #[serde(rename = "button.info.background")]
295 pub button_info: Option<SharedString>,
296 /// Button info active background color, fallback to `info_active`.
297 #[serde(rename = "button.info.active.background")]
298 pub button_info_active: Option<SharedString>,
299 /// Button info text color, fallback to `info_foreground`.
300 #[serde(rename = "button.info.foreground")]
301 pub button_info_foreground: Option<SharedString>,
302 /// Button info hover background color, fallback to `info_hover`.
303 #[serde(rename = "button.info.hover.background")]
304 pub button_info_hover: Option<SharedString>,
305 /// Button primary background color, fallback to `primary`.
306 #[serde(rename = "button.primary.background")]
307 pub button_primary: Option<SharedString>,
308 /// Button primary active background color, fallback to `primary_active`.
309 #[serde(rename = "button.primary.active.background")]
310 pub button_primary_active: Option<SharedString>,
311 /// Button primary text color, fallback to `primary_foreground`.
312 #[serde(rename = "button.primary.foreground")]
313 pub button_primary_foreground: Option<SharedString>,
314 /// Button primary hover background color, fallback to `primary_hover`.
315 #[serde(rename = "button.primary.hover.background")]
316 pub button_primary_hover: Option<SharedString>,
317 /// Button secondary background color, fallback to `secondary`.
318 #[serde(rename = "button.secondary.background")]
319 pub button_secondary: Option<SharedString>,
320 /// Button secondary active background color, fallback to `secondary_active`.
321 #[serde(rename = "button.secondary.active.background")]
322 pub button_secondary_active: Option<SharedString>,
323 /// Button secondary text color, fallback to `secondary_foreground`.
324 #[serde(rename = "button.secondary.foreground")]
325 pub button_secondary_foreground: Option<SharedString>,
326 /// Button secondary hover background color, fallback to `secondary_hover`.
327 #[serde(rename = "button.secondary.hover.background")]
328 pub button_secondary_hover: Option<SharedString>,
329 /// Button success background color, fallback to `success`.
330 #[serde(rename = "button.success.background")]
331 pub button_success: Option<SharedString>,
332 /// Button success active background color, fallback to `success_active`.
333 #[serde(rename = "button.success.active.background")]
334 pub button_success_active: Option<SharedString>,
335 /// Button success text color, fallback to `success_foreground`.
336 #[serde(rename = "button.success.foreground")]
337 pub button_success_foreground: Option<SharedString>,
338 /// Button success hover background color, fallback to `success_hover`.
339 #[serde(rename = "button.success.hover.background")]
340 pub button_success_hover: Option<SharedString>,
341 /// Button warning background color, fallback to `warning`.
342 #[serde(rename = "button.warning.background")]
343 pub button_warning: Option<SharedString>,
344 /// Button warning active background color, fallback to `warning_active`.
345 #[serde(rename = "button.warning.active.background")]
346 pub button_warning_active: Option<SharedString>,
347 /// Button warning text color, fallback to `warning_foreground`.
348 #[serde(rename = "button.warning.foreground")]
349 pub button_warning_foreground: Option<SharedString>,
350 /// Button warning hover background color, fallback to `warning_hover`.
351 #[serde(rename = "button.warning.hover.background")]
352 pub button_warning_hover: Option<SharedString>,
353 /// Background color for GroupBox.
354 #[serde(rename = "group_box.background")]
355 pub group_box: Option<SharedString>,
356 /// Text color for GroupBox.
357 #[serde(rename = "group_box.foreground")]
358 pub group_box_foreground: Option<SharedString>,
359 /// Title text color for GroupBox.
360 #[serde(rename = "group_box.title.foreground")]
361 pub group_box_title_foreground: Option<SharedString>,
362 /// Input caret color (Blinking cursor).
363 #[serde(rename = "caret")]
364 pub caret: Option<SharedString>,
365 /// Chart 1 color.
366 #[serde(rename = "chart.1")]
367 pub chart_1: Option<SharedString>,
368 /// Chart 2 color.
369 #[serde(rename = "chart.2")]
370 pub chart_2: Option<SharedString>,
371 /// Chart 3 color.
372 #[serde(rename = "chart.3")]
373 pub chart_3: Option<SharedString>,
374 /// Chart 4 color.
375 #[serde(rename = "chart.4")]
376 pub chart_4: Option<SharedString>,
377 /// Chart 5 color.
378 #[serde(rename = "chart.5")]
379 pub chart_5: Option<SharedString>,
380 /// Bullish color for candlestick charts (upward price movement).
381 #[serde(rename = "chart.bullish")]
382 pub chart_bullish: Option<SharedString>,
383 /// Bearish color for candlestick charts (downward price movement).
384 #[serde(rename = "chart.bearish")]
385 pub chart_bearish: Option<SharedString>,
386 /// Chart grid line color.
387 #[serde(rename = "chart.grid")]
388 pub chart_grid: Option<SharedString>,
389 /// Danger background color.
390 #[serde(rename = "danger.background")]
391 pub danger: Option<SharedString>,
392 /// Danger active background color.
393 #[serde(rename = "danger.active.background")]
394 pub danger_active: Option<SharedString>,
395 /// Danger text color.
396 #[serde(rename = "danger.foreground")]
397 pub danger_foreground: Option<SharedString>,
398 /// Danger hover background color.
399 #[serde(rename = "danger.hover.background")]
400 pub danger_hover: Option<SharedString>,
401 /// Description List label background color.
402 #[serde(rename = "description_list.label.background")]
403 pub description_list_label: Option<SharedString>,
404 /// Description List label foreground color.
405 #[serde(rename = "description_list.label.foreground")]
406 pub description_list_label_foreground: Option<SharedString>,
407 /// Drag border color.
408 #[serde(rename = "drag.border")]
409 pub drag_border: Option<SharedString>,
410 /// Drop target background color.
411 #[serde(rename = "drop_target.background")]
412 pub drop_target: Option<SharedString>,
413 /// Default text color.
414 #[serde(rename = "foreground")]
415 pub foreground: Option<SharedString>,
416 /// Info background color.
417 #[serde(rename = "info.background")]
418 pub info: Option<SharedString>,
419 /// Info active background color.
420 #[serde(rename = "info.active.background")]
421 pub info_active: Option<SharedString>,
422 /// Info text color.
423 #[serde(rename = "info.foreground")]
424 pub info_foreground: Option<SharedString>,
425 /// Info hover background color.
426 #[serde(rename = "info.hover.background")]
427 pub info_hover: Option<SharedString>,
428 /// Border color for inputs such as Input, Select, etc.
429 #[serde(rename = "input.border")]
430 pub input: Option<SharedString>,
431 /// Link text color.
432 #[serde(rename = "link")]
433 pub link: Option<SharedString>,
434 /// Active link text color.
435 #[serde(rename = "link.active")]
436 pub link_active: Option<SharedString>,
437 /// Hover link text color.
438 #[serde(rename = "link.hover")]
439 pub link_hover: Option<SharedString>,
440 /// Background color for List and ListItem.
441 #[serde(rename = "list.background")]
442 pub list: Option<SharedString>,
443 /// Background color for active ListItem.
444 #[serde(rename = "list.active.background")]
445 pub list_active: Option<SharedString>,
446 /// Border color for active ListItem.
447 #[serde(rename = "list.active.border")]
448 pub list_active_border: Option<SharedString>,
449 /// Stripe background color for even ListItem.
450 #[serde(rename = "list.even.background")]
451 pub list_even: Option<SharedString>,
452 /// Background color for List header.
453 #[serde(rename = "list.head.background")]
454 pub list_head: Option<SharedString>,
455 /// Hover background color for ListItem.
456 #[serde(rename = "list.hover.background")]
457 pub list_hover: Option<SharedString>,
458 /// Muted backgrounds such as Skeleton and Switch.
459 #[serde(rename = "muted.background")]
460 pub muted: Option<SharedString>,
461 /// Muted text color, as used in disabled text.
462 #[serde(rename = "muted.foreground")]
463 pub muted_foreground: Option<SharedString>,
464 /// Background color for Popover.
465 #[serde(rename = "popover.background")]
466 pub popover: Option<SharedString>,
467 /// Text color for Popover.
468 #[serde(rename = "popover.foreground")]
469 pub popover_foreground: Option<SharedString>,
470 /// Primary background color.
471 #[serde(rename = "primary.background")]
472 pub primary: Option<SharedString>,
473 /// Active primary background color.
474 #[serde(rename = "primary.active.background")]
475 pub primary_active: Option<SharedString>,
476 /// Primary text color.
477 #[serde(rename = "primary.foreground")]
478 pub primary_foreground: Option<SharedString>,
479 /// Hover primary background color.
480 #[serde(rename = "primary.hover.background")]
481 pub primary_hover: Option<SharedString>,
482 /// Progress bar background color.
483 #[serde(rename = "progress.bar.background")]
484 pub progress_bar: Option<SharedString>,
485 /// Used for focus ring.
486 #[serde(rename = "ring")]
487 pub ring: Option<SharedString>,
488 /// Scrollbar background color.
489 #[serde(rename = "scrollbar.background")]
490 pub scrollbar: Option<SharedString>,
491 /// Scrollbar thumb background color.
492 #[serde(rename = "scrollbar.thumb.background")]
493 pub scrollbar_thumb: Option<SharedString>,
494 /// Scrollbar thumb hover background color.
495 #[serde(rename = "scrollbar.thumb.hover.background")]
496 pub scrollbar_thumb_hover: Option<SharedString>,
497 /// Secondary background color.
498 #[serde(rename = "secondary.background")]
499 pub secondary: Option<SharedString>,
500 /// Active secondary background color.
501 #[serde(rename = "secondary.active.background")]
502 pub secondary_active: Option<SharedString>,
503 /// Secondary text color, used for secondary Button text color or secondary text.
504 #[serde(rename = "secondary.foreground")]
505 pub secondary_foreground: Option<SharedString>,
506 /// Hover secondary background color.
507 #[serde(rename = "secondary.hover.background")]
508 pub secondary_hover: Option<SharedString>,
509 /// Input selection background color.
510 #[serde(rename = "selection.background")]
511 pub selection: Option<SharedString>,
512 /// Sidebar background color.
513 #[serde(rename = "sidebar.background")]
514 pub sidebar: Option<SharedString>,
515 /// Sidebar accent background color.
516 #[serde(rename = "sidebar.accent.background")]
517 pub sidebar_accent: Option<SharedString>,
518 /// Sidebar accent text color.
519 #[serde(rename = "sidebar.accent.foreground")]
520 pub sidebar_accent_foreground: Option<SharedString>,
521 /// Sidebar border color.
522 #[serde(rename = "sidebar.border")]
523 pub sidebar_border: Option<SharedString>,
524 /// Sidebar text color.
525 #[serde(rename = "sidebar.foreground")]
526 pub sidebar_foreground: Option<SharedString>,
527 /// Sidebar primary background color.
528 #[serde(rename = "sidebar.primary.background")]
529 pub sidebar_primary: Option<SharedString>,
530 /// Sidebar primary text color.
531 #[serde(rename = "sidebar.primary.foreground")]
532 pub sidebar_primary_foreground: Option<SharedString>,
533 /// Skeleton background color.
534 #[serde(rename = "skeleton.background")]
535 pub skeleton: Option<SharedString>,
536 /// Slider bar background color.
537 #[serde(rename = "slider.background")]
538 pub slider_bar: Option<SharedString>,
539 /// Slider thumb background color.
540 #[serde(rename = "slider.thumb.background")]
541 pub slider_thumb: Option<SharedString>,
542 /// Success background color.
543 #[serde(rename = "success.background")]
544 pub success: Option<SharedString>,
545 /// Success text color.
546 #[serde(rename = "success.foreground")]
547 pub success_foreground: Option<SharedString>,
548 /// Success hover background color.
549 #[serde(rename = "success.hover.background")]
550 pub success_hover: Option<SharedString>,
551 /// Success active background color.
552 #[serde(rename = "success.active.background")]
553 pub success_active: Option<SharedString>,
554 /// Switch background color.
555 #[serde(rename = "switch.background")]
556 pub switch: Option<SharedString>,
557 /// Switch thumb background color.
558 #[serde(rename = "switch.thumb.background")]
559 pub switch_thumb: Option<SharedString>,
560 /// Tab background color.
561 #[serde(rename = "tab.background")]
562 pub tab: Option<SharedString>,
563 /// Tab active background color.
564 #[serde(rename = "tab.active.background")]
565 pub tab_active: Option<SharedString>,
566 /// Tab active text color.
567 #[serde(rename = "tab.active.foreground")]
568 pub tab_active_foreground: Option<SharedString>,
569 /// TabBar background color.
570 #[serde(rename = "tab_bar.background")]
571 pub tab_bar: Option<SharedString>,
572 /// TabBar segmented background color.
573 #[serde(rename = "tab_bar.segmented.background")]
574 pub tab_bar_segmented: Option<SharedString>,
575 /// Tab text color.
576 #[serde(rename = "tab.foreground")]
577 pub tab_foreground: Option<SharedString>,
578 /// Table background color.
579 #[serde(rename = "table.background")]
580 pub table: Option<SharedString>,
581 /// Table active item background color.
582 #[serde(rename = "table.active.background")]
583 pub table_active: Option<SharedString>,
584 /// Table active item border color.
585 #[serde(rename = "table.active.border")]
586 pub table_active_border: Option<SharedString>,
587 /// Stripe background color for even TableRow.
588 #[serde(rename = "table.even.background")]
589 pub table_even: Option<SharedString>,
590 /// Table header background color.
591 #[serde(rename = "table.head.background")]
592 pub table_head: Option<SharedString>,
593 /// Table header text color.
594 #[serde(rename = "table.head.foreground")]
595 pub table_head_foreground: Option<SharedString>,
596 /// Table footer background color.
597 #[serde(rename = "table.foot.background")]
598 pub table_foot: Option<SharedString>,
599 /// Table footer text color.
600 #[serde(rename = "table.foot.foreground")]
601 pub table_foot_foreground: Option<SharedString>,
602 /// Table item hover background color.
603 #[serde(rename = "table.hover.background")]
604 pub table_hover: Option<SharedString>,
605 /// Table row border color.
606 #[serde(rename = "table.row.border")]
607 pub table_row_border: Option<SharedString>,
608 /// TitleBar background color, use for Window title bar.
609 #[serde(rename = "title_bar.background")]
610 pub title_bar: Option<SharedString>,
611 /// TitleBar border color.
612 #[serde(rename = "title_bar.border")]
613 pub title_bar_border: Option<SharedString>,
614 /// StatusBar background color, use for the bottom status bar.
615 #[serde(rename = "status_bar.background")]
616 pub status_bar: Option<SharedString>,
617 /// StatusBar border color.
618 #[serde(rename = "status_bar.border")]
619 pub status_bar_border: Option<SharedString>,
620 /// Warning background color.
621 #[serde(rename = "warning.background")]
622 pub warning: Option<SharedString>,
623 /// Warning active background color.
624 #[serde(rename = "warning.active.background")]
625 pub warning_active: Option<SharedString>,
626 /// Warning hover background color.
627 #[serde(rename = "warning.hover.background")]
628 pub warning_hover: Option<SharedString>,
629 /// Warning foreground color.
630 #[serde(rename = "warning.foreground")]
631 pub warning_foreground: Option<SharedString>,
632 /// Overlay background color.
633 #[serde(rename = "overlay")]
634 pub overlay: Option<SharedString>,
635 /// Window border color.
636 ///
637 /// # Platform specific:
638 ///
639 /// This is only works on Linux, other platforms we can't change the window border color.
640 #[serde(rename = "window.border")]
641 pub window_border: Option<SharedString>,
642
643 /// Base blue color.
644 #[serde(rename = "base.blue")]
645 blue: Option<String>,
646 /// Base light blue color.
647 #[serde(rename = "base.blue.light")]
648 blue_light: Option<String>,
649 /// Base cyan color.
650 #[serde(rename = "base.cyan")]
651 cyan: Option<String>,
652 /// Base light cyan color.
653 #[serde(rename = "base.cyan.light")]
654 cyan_light: Option<String>,
655 /// Base green color.
656 #[serde(rename = "base.green")]
657 green: Option<String>,
658 /// Base light green color.
659 #[serde(rename = "base.green.light")]
660 green_light: Option<String>,
661 /// Base magenta color.
662 #[serde(rename = "base.magenta")]
663 magenta: Option<String>,
664 #[serde(rename = "base.magenta.light")]
665 magenta_light: Option<String>,
666 /// Base red color.
667 #[serde(rename = "base.red")]
668 red: Option<String>,
669 /// Base light red color.
670 #[serde(rename = "base.red.light")]
671 red_light: Option<String>,
672 /// Base yellow color.
673 #[serde(rename = "base.yellow")]
674 yellow: Option<String>,
675 /// Base light yellow color.
676 #[serde(rename = "base.yellow.light")]
677 yellow_light: Option<String>,
678}
679
680impl ThemeColor {
681 /// Create a new `ThemeColor` from a `ThemeConfig`.
682 pub(crate) fn apply_config(
683 &mut self,
684 config: &ThemeConfig,
685 default_theme: &ThemeColor,
686 ) -> ThemeTokens {
687 let colors = config.colors.clone();
688 let default_tokens = ThemeTokens::from(default_theme);
689 let mut tokens = default_tokens;
690
691 macro_rules! apply_color {
692 ($config_field:ident) => {
693 if let Some(value) = &colors.$config_field {
694 self.$config_field =
695 try_parse_color(value).unwrap_or(default_theme.$config_field);
696 } else {
697 self.$config_field = default_theme.$config_field;
698 }
699 tokens.$config_field = self.$config_field.into();
700 };
701 // With fallback
702 ($config_field:ident, fallback = $fallback:expr) => {
703 let fallback: gpui::Hsla = ($fallback).into();
704 if let Some(value) = &colors.$config_field {
705 self.$config_field = try_parse_color(value).unwrap_or(fallback);
706 } else {
707 self.$config_field = fallback;
708 }
709 tokens.$config_field = self.$config_field.into();
710 };
711 }
712
713 macro_rules! apply_background_color {
714 ($config_field:ident) => {
715 let token = if let Some(value) = &colors.$config_field {
716 if let Ok(token) = try_parse_theme_token(&value) {
717 token
718 } else {
719 default_tokens.$config_field
720 }
721 } else {
722 default_tokens.$config_field
723 };
724 self.$config_field = token.color;
725 tokens.$config_field = token;
726 };
727 ($config_field:ident, fallback = $fallback:expr) => {
728 let fallback: ThemeToken = ($fallback).into();
729 let token = if let Some(value) = &colors.$config_field {
730 if let Ok(token) = try_parse_theme_token(&value) {
731 token
732 } else {
733 fallback
734 }
735 } else {
736 fallback
737 };
738 self.$config_field = token.color;
739 tokens.$config_field = token;
740 };
741 }
742
743 apply_background_color!(background);
744
745 // Base colors for fallback
746 apply_color!(red);
747 apply_color!(
748 red_light,
749 fallback = self.background.blend(self.red.opacity(0.8))
750 );
751 apply_color!(green);
752 apply_color!(
753 green_light,
754 fallback = self.background.blend(self.green.opacity(0.8))
755 );
756 apply_color!(blue);
757 apply_color!(
758 blue_light,
759 fallback = self.background.blend(self.blue.opacity(0.8))
760 );
761 apply_color!(magenta);
762 apply_color!(
763 magenta_light,
764 fallback = self.background.blend(self.magenta.opacity(0.8))
765 );
766 apply_color!(yellow);
767 apply_color!(
768 yellow_light,
769 fallback = self.background.blend(self.yellow.opacity(0.8))
770 );
771 apply_color!(cyan);
772 apply_color!(
773 cyan_light,
774 fallback = self.background.blend(self.cyan.opacity(0.8))
775 );
776
777 apply_color!(border);
778 apply_color!(foreground);
779 apply_color!(input, fallback = self.border);
780 apply_background_color!(muted);
781 apply_color!(
782 muted_foreground,
783 fallback = self.muted.blend(self.foreground.opacity(0.7))
784 );
785
786 // Button colors
787 let active_darken = if config.mode.is_dark() { 0.2 } else { 0.1 };
788 let hover_opacity = 0.9;
789 let transparent = gpui::transparent_black();
790 let button_background = if config.mode.is_dark() {
791 self.input.mix_oklab(transparent, 0.3)
792 } else {
793 self.background
794 };
795 apply_background_color!(button, fallback = button_background);
796 apply_color!(button_foreground, fallback = self.foreground);
797 apply_background_color!(
798 button_hover,
799 fallback = self.input.mix_oklab(transparent, 0.5)
800 );
801 apply_background_color!(
802 button_active,
803 fallback = self.input.mix_oklab(transparent, 0.7)
804 );
805 apply_background_color!(primary);
806 apply_color!(primary_foreground, fallback = self.foreground);
807 apply_background_color!(
808 primary_hover,
809 fallback = self.background.blend(self.primary.opacity(hover_opacity))
810 );
811 apply_background_color!(
812 primary_active,
813 fallback = self.primary.darken(active_darken)
814 );
815 apply_background_color!(button_primary, fallback = tokens.primary);
816 apply_color!(
817 button_primary_foreground,
818 fallback = self.primary_foreground
819 );
820 apply_background_color!(button_primary_hover, fallback = tokens.primary_hover);
821 apply_background_color!(button_primary_active, fallback = tokens.primary_active);
822 apply_background_color!(secondary);
823 apply_color!(secondary_foreground, fallback = self.foreground);
824 apply_background_color!(
825 secondary_hover,
826 fallback = self.background.blend(self.secondary.opacity(hover_opacity))
827 );
828 apply_background_color!(
829 secondary_active,
830 fallback = self.secondary.darken(active_darken)
831 );
832 apply_background_color!(button_secondary, fallback = tokens.secondary);
833 apply_color!(
834 button_secondary_foreground,
835 fallback = self.secondary_foreground
836 );
837 apply_background_color!(button_secondary_hover, fallback = tokens.secondary_hover);
838 apply_background_color!(button_secondary_active, fallback = tokens.secondary_active);
839 apply_background_color!(success, fallback = self.green);
840 apply_color!(success_foreground, fallback = self.primary_foreground);
841 apply_background_color!(
842 success_hover,
843 fallback = self.background.blend(self.success.opacity(hover_opacity))
844 );
845 apply_background_color!(
846 success_active,
847 fallback = self.success.darken(active_darken)
848 );
849 apply_background_color!(
850 button_success,
851 fallback = self.success.mix_oklab(transparent, 0.2)
852 );
853 apply_color!(button_success_foreground, fallback = self.success);
854 apply_background_color!(
855 button_success_hover,
856 fallback = self.success.mix_oklab(transparent, 0.3)
857 );
858 apply_background_color!(
859 button_success_active,
860 fallback = self.success.mix_oklab(transparent, 0.4)
861 );
862 apply_background_color!(info, fallback = self.cyan);
863 apply_color!(info_foreground, fallback = self.primary_foreground);
864 apply_background_color!(
865 info_hover,
866 fallback = self.background.blend(self.info.opacity(hover_opacity))
867 );
868 apply_background_color!(info_active, fallback = self.info.darken(active_darken));
869 apply_background_color!(
870 button_info,
871 fallback = self.info.mix_oklab(transparent, 0.2)
872 );
873 apply_color!(button_info_foreground, fallback = self.info);
874 apply_background_color!(
875 button_info_hover,
876 fallback = self.info.mix_oklab(transparent, 0.3)
877 );
878 apply_background_color!(
879 button_info_active,
880 fallback = self.info.mix_oklab(transparent, 0.4)
881 );
882 apply_background_color!(warning, fallback = self.yellow);
883 apply_color!(warning_foreground, fallback = self.primary_foreground);
884 apply_background_color!(
885 warning_hover,
886 fallback = self.background.blend(self.warning.opacity(0.9))
887 );
888 apply_background_color!(
889 warning_active,
890 fallback = self.background.blend(self.warning.darken(active_darken))
891 );
892 apply_background_color!(
893 button_warning,
894 fallback = self.warning.mix_oklab(transparent, 0.2)
895 );
896 apply_color!(button_warning_foreground, fallback = self.warning);
897 apply_background_color!(
898 button_warning_hover,
899 fallback = self.warning.mix_oklab(transparent, 0.3)
900 );
901 apply_background_color!(
902 button_warning_active,
903 fallback = self.warning.mix_oklab(transparent, 0.4)
904 );
905
906 // Other colors
907 apply_background_color!(accent, fallback = tokens.secondary);
908 apply_color!(accent_foreground, fallback = self.foreground);
909 apply_background_color!(accordion, fallback = tokens.background);
910 apply_background_color!(
911 group_box,
912 fallback = self
913 .background
914 .blend(
915 self.secondary
916 .opacity(if config.mode.is_dark() { 0.3 } else { 0.4 })
917 )
918 );
919 apply_color!(group_box_foreground, fallback = self.foreground);
920 apply_color!(caret, fallback = self.primary);
921 apply_color!(chart_1, fallback = self.blue.lighten(0.4));
922 apply_color!(chart_2, fallback = self.blue.lighten(0.2));
923 apply_color!(chart_3, fallback = self.blue);
924 apply_color!(chart_4, fallback = self.blue.darken(0.2));
925 apply_color!(chart_5, fallback = self.blue.darken(0.4));
926 apply_color!(chart_bullish, fallback = self.green);
927 apply_color!(chart_bearish, fallback = self.red);
928 apply_color!(chart_grid, fallback = self.border.opacity(0.6));
929 apply_background_color!(danger, fallback = self.red);
930 apply_background_color!(danger_active, fallback = self.danger.darken(active_darken));
931 apply_color!(danger_foreground, fallback = self.primary_foreground);
932 apply_background_color!(
933 danger_hover,
934 fallback = self.background.blend(self.danger.opacity(0.9))
935 );
936 apply_background_color!(
937 button_danger,
938 fallback = self.danger.mix_oklab(transparent, 0.2)
939 );
940 apply_color!(button_danger_foreground, fallback = self.danger);
941 apply_background_color!(
942 button_danger_hover,
943 fallback = self.danger.mix_oklab(transparent, 0.3)
944 );
945 apply_background_color!(
946 button_danger_active,
947 fallback = self.danger.mix_oklab(transparent, 0.4)
948 );
949 apply_background_color!(
950 description_list_label,
951 fallback = self.background.blend(self.border.opacity(0.2))
952 );
953 apply_color!(
954 description_list_label_foreground,
955 fallback = self.muted_foreground
956 );
957 apply_color!(drag_border, fallback = self.primary.opacity(0.65));
958 apply_background_color!(drop_target, fallback = self.primary.opacity(0.2));
959 apply_color!(link, fallback = self.primary);
960 apply_color!(link_active, fallback = self.link);
961 apply_color!(link_hover, fallback = self.link);
962 apply_background_color!(list, fallback = tokens.background);
963 apply_background_color!(
964 list_active,
965 fallback = self.background.blend(self.primary.opacity(0.1))
966 );
967 apply_color!(
968 list_active_border,
969 fallback = self.background.blend(self.primary.opacity(0.6))
970 );
971 apply_background_color!(list_even, fallback = tokens.list);
972 apply_background_color!(list_head, fallback = tokens.list);
973 apply_background_color!(list_hover, fallback = self.accent.opacity(0.6));
974 apply_background_color!(popover, fallback = tokens.background);
975 apply_color!(popover_foreground, fallback = self.foreground);
976 apply_background_color!(progress_bar, fallback = tokens.primary);
977 apply_color!(ring, fallback = self.blue);
978 apply_background_color!(scrollbar, fallback = tokens.background);
979 apply_background_color!(scrollbar_thumb, fallback = tokens.accent);
980 apply_background_color!(scrollbar_thumb_hover, fallback = tokens.scrollbar_thumb);
981 apply_background_color!(selection, fallback = tokens.primary);
982 apply_background_color!(
983 sidebar,
984 fallback = self.background.blend(self.border.opacity(0.15))
985 );
986 apply_background_color!(sidebar_accent, fallback = tokens.accent);
987 apply_color!(sidebar_accent_foreground, fallback = self.accent_foreground);
988 apply_color!(sidebar_border, fallback = self.border);
989 apply_color!(sidebar_foreground, fallback = self.foreground);
990 apply_background_color!(sidebar_primary, fallback = tokens.primary);
991 apply_color!(
992 sidebar_primary_foreground,
993 fallback = self.primary_foreground
994 );
995 apply_background_color!(skeleton, fallback = tokens.secondary);
996 apply_background_color!(slider_bar, fallback = tokens.primary);
997 apply_background_color!(slider_thumb, fallback = self.primary_foreground);
998 apply_background_color!(switch, fallback = tokens.secondary_active);
999 apply_background_color!(switch_thumb, fallback = tokens.background);
1000 apply_background_color!(tab, fallback = tokens.background);
1001 apply_background_color!(tab_active, fallback = tokens.background);
1002 apply_color!(tab_active_foreground, fallback = self.foreground);
1003 apply_background_color!(tab_bar, fallback = tokens.background);
1004 apply_background_color!(tab_bar_segmented, fallback = tokens.secondary);
1005 apply_color!(tab_foreground, fallback = self.foreground);
1006 apply_background_color!(table, fallback = tokens.list);
1007 apply_background_color!(table_active, fallback = tokens.list_active);
1008 apply_color!(table_active_border, fallback = self.list_active_border);
1009 apply_background_color!(table_even, fallback = tokens.list_even);
1010 apply_background_color!(table_head, fallback = tokens.list_head);
1011 apply_color!(table_head_foreground, fallback = self.muted_foreground);
1012 apply_background_color!(table_foot, fallback = tokens.list_head);
1013 apply_color!(table_foot_foreground, fallback = self.muted_foreground);
1014 apply_background_color!(table_hover, fallback = tokens.list_hover);
1015 apply_color!(table_row_border, fallback = self.border);
1016 apply_background_color!(title_bar, fallback = tokens.background);
1017 apply_color!(title_bar_border, fallback = self.border);
1018 apply_background_color!(status_bar, fallback = tokens.title_bar);
1019 apply_color!(status_bar_border, fallback = self.title_bar_border);
1020 apply_background_color!(overlay);
1021 apply_color!(window_border, fallback = self.border);
1022
1023 // TODO: Apply default fallback colors to highlight.
1024
1025 // Ensure opacity for list_active, table_active, selection.
1026 let clamp_alpha = |raw: Option<&str>, color: Hsla, background: Background, max: f32| {
1027 let base = color.a;
1028 let target = base.min(max);
1029 let color = color.alpha(target);
1030 let background = raw
1031 .and_then(|value| try_parse_background_clamped(value, max).ok())
1032 .unwrap_or_else(|| {
1033 let factor = if base > 0. { target / base } else { 1. };
1034 background.opacity(factor)
1035 });
1036 (color, ThemeToken::new(color, background))
1037 };
1038
1039 (self.list_active, tokens.list_active) = clamp_alpha(
1040 colors.list_active.as_deref(),
1041 self.list_active,
1042 tokens.list_active.background,
1043 0.2,
1044 );
1045 (self.table_active, tokens.table_active) = clamp_alpha(
1046 colors.table_active.as_deref(),
1047 self.table_active,
1048 tokens.table_active.background,
1049 0.2,
1050 );
1051 (self.selection, tokens.selection) = clamp_alpha(
1052 colors.selection.as_deref(),
1053 self.selection,
1054 tokens.selection.background,
1055 0.3,
1056 );
1057
1058 tokens
1059 }
1060}
1061
1062impl Theme {
1063 /// Apply the given theme configuration to the current theme.
1064 pub fn apply_config(&mut self, config: &Rc<ThemeConfig>) {
1065 if config.mode.is_dark() {
1066 self.dark_theme = config.clone();
1067 } else {
1068 self.light_theme = config.clone();
1069 }
1070 if let Some(style) = &config.highlight {
1071 let highlight_theme = Arc::new(HighlightTheme {
1072 name: config.name.to_string(),
1073 appearance: config.mode,
1074 style: style.clone(),
1075 });
1076 self.highlight_theme = highlight_theme.clone();
1077 }
1078
1079 let default_colors = if config.mode.is_dark() {
1080 ThemeColor::dark()
1081 } else {
1082 ThemeColor::light()
1083 };
1084
1085 if let Some(font_size) = config.font_size {
1086 self.font_size = px(font_size);
1087 }
1088 if let Some(font_family) = &config.font_family {
1089 self.font_family = font_family.clone();
1090 }
1091 if let Some(mono_font_family) = &config.mono_font_family {
1092 self.mono_font_family = mono_font_family.clone();
1093 }
1094 if let Some(mono_font_size) = config.mono_font_size {
1095 self.mono_font_size = px(mono_font_size);
1096 }
1097 if let Some(radius) = config.radius {
1098 self.radius = px(radius as f32);
1099 }
1100 if let Some(radius_lg) = config.radius_lg {
1101 self.radius_lg = px(radius_lg as f32);
1102 }
1103 if let Some(shadow) = config.shadow {
1104 self.shadow = shadow;
1105 }
1106
1107 self.tokens = self.colors.apply_config(&config, &default_colors);
1108 self.mode = config.mode;
1109 }
1110}
1111
1112#[cfg(test)]
1113mod tests {
1114 use gpui::{linear_color_stop, linear_gradient, px};
1115
1116 use crate::{Colorize as _, Theme, ThemeConfig, ThemeMode, ThemeSet, try_parse_color};
1117
1118 #[test]
1119 fn test_semantic_theme_config_parses_and_roundtrips() {
1120 let value = serde_json::json!({
1121 "name": "Semantic",
1122 "mode": "dark",
1123 "tokens": {
1124 "colors": {
1125 "surface": "#111827",
1126 "surface_foreground": "#f9fafb",
1127 "primary": "#2563eb",
1128 "destructive": "#dc2626"
1129 },
1130 "radius": { "sm": 2.0, "md": 6.0, "lg": 10.0 },
1131 "spacing": { "xs": 4.0, "md": 12.0, "xl": 24.0 },
1132 "typography": {
1133 "sans": "Inter",
1134 "md": { "size": 15.0, "line_height": 22.0 }
1135 }
1136 }
1137 });
1138 let config: super::SemanticThemeConfigFile = serde_json::from_value(value).unwrap();
1139 let serialized = serde_json::to_string(&config).unwrap();
1140 let reparsed: super::SemanticThemeConfigFile = serde_json::from_str(&serialized).unwrap();
1141 let semantic = reparsed.tokens;
1142
1143 assert_eq!(semantic.colors.surface.as_deref(), Some("#111827"));
1144 assert_eq!(semantic.colors.destructive.as_deref(), Some("#dc2626"));
1145 assert_eq!(semantic.radius.lg, Some(10.0));
1146 assert_eq!(semantic.spacing.xl, Some(24.0));
1147 assert_eq!(semantic.typography.sans.as_deref(), Some("Inter"));
1148 assert_eq!(semantic.typography.md.line_height, Some(22.0));
1149
1150 let mut theme = Theme::default();
1151 let resolved = theme.apply_semantic_config_str(&serialized).unwrap();
1152 assert_eq!(theme.primary, try_parse_color("#2563eb").unwrap());
1153 assert_eq!(resolved.spacing.xl, px(24.));
1154 assert_eq!(resolved.typography.md.line_height, px(22.));
1155 }
1156
1157 #[test]
1158 fn test_semantic_tokens_override_legacy_generic_fields_only() {
1159 let config = serde_json::from_value::<super::SemanticThemeConfigFile>(serde_json::json!({
1160 "tokens": {
1161 "colors": { "primary": "#2563eb", "destructive": "#b91c1c" },
1162 "spacing": { "md": 14.0 },
1163 "typography": { "md": { "size": 15.0 } }
1164 }
1165 }))
1166 .unwrap();
1167 let mut theme = Theme::default();
1168 let component_color = theme.button_primary;
1169 let resolved = theme.apply_semantic_config(&config.tokens);
1170
1171 assert_eq!(theme.primary, try_parse_color("#2563eb").unwrap());
1172 assert_eq!(theme.danger, try_parse_color("#b91c1c").unwrap());
1173 assert_eq!(theme.button_primary, component_color);
1174 assert_eq!(resolved.spacing.md, px(14.));
1175 assert_eq!(resolved.typography.md.size, px(15.));
1176 }
1177
1178 #[test]
1179 fn test_legacy_config_without_semantic_tokens_is_unchanged() {
1180 let config = serde_json::from_value::<ThemeConfig>(serde_json::json!({
1181 "name": "Legacy",
1182 "mode": "light",
1183 "radius": 7,
1184 "colors": { "primary.background": "#7c3aed" }
1185 }))
1186 .unwrap();
1187 let mut theme = Theme::default();
1188 theme.apply_config(&std::rc::Rc::new(config));
1189 assert_eq!(theme.primary, try_parse_color("#7c3aed").unwrap());
1190 assert_eq!(theme.radius, px(7.));
1191 assert_eq!(theme.semantic_tokens().spacing, Default::default());
1192 }
1193
1194 #[test]
1195 fn test_apply_config_reads_the_chart_colors() {
1196 let config = serde_json::from_value::<ThemeConfig>(serde_json::json!({
1197 "name": "Palette",
1198 "mode": "light",
1199 "colors": {
1200 "chart.1": "#111111",
1201 "chart.2": "not a color",
1202 "chart.3": "#333333",
1203 "chart.bullish": "#00ff00",
1204 "chart.bearish": "#ff0000",
1205 "chart.grid": "#eeeeee"
1206 }
1207 }))
1208 .unwrap();
1209
1210 let mut theme = Theme::default();
1211 theme.apply_config(&std::rc::Rc::new(config));
1212
1213 // The keys match the fields; an unparsable or missing one falls back
1214 // to the ramp of the base blue.
1215 assert_eq!(theme.chart_1, try_parse_color("#111111").unwrap());
1216 assert_eq!(theme.chart_2, theme.blue.lighten(0.2));
1217 assert_eq!(theme.chart_3, try_parse_color("#333333").unwrap());
1218 assert_eq!(theme.chart_4, theme.blue.darken(0.2));
1219 assert_eq!(theme.chart_5, theme.blue.darken(0.4));
1220 assert_eq!(theme.tokens.chart_3.color, theme.chart_3);
1221 assert_eq!(theme.chart_bullish, try_parse_color("#00ff00").unwrap());
1222 assert_eq!(theme.chart_bearish, try_parse_color("#ff0000").unwrap());
1223 assert_eq!(theme.chart_grid, try_parse_color("#eeeeee").unwrap());
1224
1225 theme.apply_config(&std::rc::Rc::new(ThemeConfig::default()));
1226 assert_eq!(theme.chart_grid, theme.border.opacity(0.6));
1227 }
1228
1229 #[test]
1230 fn test_apply_config_preserves_gradient_background_and_solid_color_fallback() {
1231 let config = serde_json::from_value::<ThemeConfig>(serde_json::json!({
1232 "name": "Gradient",
1233 "mode": "light",
1234 "colors": {
1235 "primary.background": "linear-gradient(135deg, #4F46E5, #06B6D4)",
1236 "button.primary.hover.background": "linear-gradient(to right, red-500 25%, blue-600 75%)"
1237 }
1238 }))
1239 .unwrap();
1240
1241 let mut theme = Theme::default();
1242 theme.apply_config(&std::rc::Rc::new(config));
1243
1244 let primary_from = try_parse_color("#4F46E5").unwrap();
1245 let primary_to = try_parse_color("#06B6D4").unwrap();
1246 assert_eq!(theme.primary, primary_from);
1247 assert_eq!(theme.tokens.primary.color, primary_from);
1248 assert_eq!(
1249 theme.tokens.primary.background,
1250 linear_gradient(
1251 135.,
1252 linear_color_stop(primary_from, 0.),
1253 linear_color_stop(primary_to, 1.)
1254 )
1255 );
1256 assert_eq!(
1257 theme.tokens.button_primary.background,
1258 theme.tokens.primary.background
1259 );
1260 assert_eq!(
1261 theme.tokens.button_primary_hover.background,
1262 linear_gradient(
1263 90.,
1264 linear_color_stop(crate::red_500(), 0.25),
1265 linear_color_stop(crate::blue_600(), 0.75)
1266 )
1267 );
1268 assert_eq!(theme.mode, ThemeMode::Light);
1269 }
1270
1271 #[test]
1272 fn test_aurora_theme_parses_gradient_backgrounds() {
1273 let theme_set =
1274 serde_json::from_str::<ThemeSet>(include_str!("../../../../themes/aurora.json"))
1275 .unwrap();
1276 assert_eq!(theme_set.themes.len(), 1);
1277 assert!(theme_set.themes.iter().all(|theme| !theme.mode.is_dark()));
1278
1279 let light = theme_set
1280 .themes
1281 .iter()
1282 .find(|theme| theme.name.as_ref() == "Aurora Light")
1283 .unwrap();
1284 let mut theme = Theme::default();
1285 theme.apply_config(&std::rc::Rc::new(light.clone()));
1286
1287 assert_ne!(
1288 theme.tokens.button_primary.background,
1289 theme.button_primary.into()
1290 );
1291 assert_eq!(theme.tokens.background.background, theme.background.into());
1292 assert_eq!(theme.button_primary, try_parse_color("#1E293B").unwrap());
1293 assert_eq!(theme.background, try_parse_color("#FFFFFF").unwrap());
1294 assert_ne!(
1295 theme.tokens.progress_bar.background,
1296 theme.progress_bar.into()
1297 );
1298 assert_ne!(
1299 theme.tokens.scrollbar_thumb.background,
1300 theme.scrollbar_thumb.into()
1301 );
1302 assert_ne!(theme.tokens.switch.background, theme.switch.into());
1303 assert_ne!(
1304 theme.tokens.switch_thumb.background,
1305 theme.switch_thumb.into()
1306 );
1307 assert_ne!(theme.tokens.title_bar.background, theme.title_bar.into());
1308 assert_ne!(theme.tokens.status_bar.background, theme.status_bar.into());
1309 }
1310
1311 #[test]
1312 fn test_apply_config_clamps_highlight_alpha_per_gradient_stop() {
1313 let config = serde_json::from_value::<ThemeConfig>(serde_json::json!({
1314 "name": "Highlight",
1315 "mode": "light",
1316 "colors": {
1317 // Solid above the cap: must be capped to 0.2, not attenuated twice.
1318 "list.active.background": "#3b82f6",
1319 // Gradient with a faint `from` stop and an opaque `to` stop: the
1320 // `to` stop must be clamped independently, not left at full alpha.
1321 "table.active.background": "linear-gradient(#bfdbfe33, #3b82f6)",
1322 // Gradient with a transparent `from` stop: the opaque `to` stop
1323 // must still be clamped (the `base == 0` factor fallback used to
1324 // leave it untouched).
1325 "selection.background": "linear-gradient(#3b82f600, #3b82f6)",
1326 }
1327 }))
1328 .unwrap();
1329
1330 let mut theme = Theme::default();
1331 theme.apply_config(&std::rc::Rc::new(config));
1332
1333 // Solid: representative color and rendered background both capped at 0.2.
1334 let blue = try_parse_color("#3b82f6").unwrap();
1335 assert_eq!(theme.list_active, blue.alpha(0.2));
1336 assert_eq!(theme.tokens.list_active.background, blue.alpha(0.2).into());
1337
1338 // Gradient: the opaque `to` stop is clamped to 0.2, not left fully opaque.
1339 let faint = try_parse_color("#bfdbfe33").unwrap();
1340 assert_eq!(
1341 theme.tokens.table_active.background,
1342 linear_gradient(
1343 180.,
1344 linear_color_stop(faint.alpha(faint.a.min(0.2)), 0.),
1345 linear_color_stop(blue.alpha(0.2), 1.),
1346 )
1347 );
1348
1349 // Gradient: a transparent `from` stop stays transparent while the opaque
1350 // `to` stop is still clamped to 0.3 (selection cap).
1351 let clear = try_parse_color("#3b82f600").unwrap();
1352 assert_eq!(
1353 theme.tokens.selection.background,
1354 linear_gradient(
1355 180.,
1356 linear_color_stop(clear.alpha(clear.a.min(0.3)), 0.),
1357 linear_color_stop(blue.alpha(0.3), 1.),
1358 )
1359 );
1360 }
1361}