Skip to main content

gpui_component/theme/
theme_color.rs

1use std::{ops::Deref, sync::Arc};
2
3use crate::{ThemeMode, theme::DEFAULT_THEME_COLORS};
4
5use gpui::{Background, Fill, Hsla};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9/// A theme token that keeps a solid representative color and its renderable background.
10#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
11#[non_exhaustive]
12pub struct ThemeToken {
13    pub color: Hsla,
14    pub background: Background,
15}
16
17impl ThemeToken {
18    pub fn new(color: Hsla, background: Background) -> Self {
19        Self { color, background }
20    }
21}
22
23impl Deref for ThemeToken {
24    type Target = Hsla;
25
26    fn deref(&self) -> &Self::Target {
27        &self.color
28    }
29}
30
31impl From<Hsla> for ThemeToken {
32    fn from(color: Hsla) -> Self {
33        Self {
34            color,
35            background: color.into(),
36        }
37    }
38}
39
40impl From<ThemeToken> for Hsla {
41    fn from(token: ThemeToken) -> Self {
42        token.color
43    }
44}
45
46impl From<ThemeToken> for Background {
47    fn from(token: ThemeToken) -> Self {
48        token.background
49    }
50}
51
52impl From<ThemeToken> for Fill {
53    fn from(token: ThemeToken) -> Self {
54        Fill::Color(token.background)
55    }
56}
57
58/// Theme colors used throughout the UI components.
59#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
60#[non_exhaustive]
61pub struct ThemeColor {
62    /// Used for accents such as hover background on MenuItem, ListItem, etc.
63    pub accent: Hsla,
64    /// Used for accent text color.
65    pub accent_foreground: Hsla,
66    /// Accordion background color.
67    pub accordion: Hsla,
68    /// Default background color.
69    pub background: Hsla,
70    /// Default border color
71    pub border: Hsla,
72    /// Default Button background color.
73    pub button: Hsla,
74    /// Default Button active background color.
75    pub button_active: Hsla,
76    /// Default Button text color.
77    pub button_foreground: Hsla,
78    /// Default Button hover background color.
79    pub button_hover: Hsla,
80    /// Button danger background color, fallback to `danger`.
81    pub button_danger: Hsla,
82    /// Button danger active background color, fallback to `danger_active`.
83    pub button_danger_active: Hsla,
84    /// Button danger text color, fallback to `danger_foreground`.
85    pub button_danger_foreground: Hsla,
86    /// Button danger hover background color, fallback to `danger_hover`.
87    pub button_danger_hover: Hsla,
88    /// Button info background color, fallback to `info`.
89    pub button_info: Hsla,
90    /// Button info active background color, fallback to `info_active`.
91    pub button_info_active: Hsla,
92    /// Button info text color, fallback to `info_foreground`.
93    pub button_info_foreground: Hsla,
94    /// Button info hover background color, fallback to `info_hover`.
95    pub button_info_hover: Hsla,
96    /// Button primary background color, fallback to `primary`.
97    pub button_primary: Hsla,
98    /// Button primary active background color, fallback to `primary_active`.
99    pub button_primary_active: Hsla,
100    /// Button primary text color, fallback to `primary_foreground`.
101    pub button_primary_foreground: Hsla,
102    /// Button primary hover background color, fallback to `primary_hover`.
103    pub button_primary_hover: Hsla,
104    /// Button secondary background color, fallback to `secondary`.
105    pub button_secondary: Hsla,
106    /// Button secondary active background color, fallback to `secondary_active`.
107    pub button_secondary_active: Hsla,
108    /// Button secondary text color, fallback to `secondary_foreground`.
109    pub button_secondary_foreground: Hsla,
110    /// Button secondary hover background color, fallback to `secondary_hover`.
111    pub button_secondary_hover: Hsla,
112    /// Button success background color, fallback to `success`.
113    pub button_success: Hsla,
114    /// Button success active background color, fallback to `success_active`.
115    pub button_success_active: Hsla,
116    /// Button success text color, fallback to `success_foreground`.
117    pub button_success_foreground: Hsla,
118    /// Button success hover background color, fallback to `success_hover`.
119    pub button_success_hover: Hsla,
120    /// Button warning background color, fallback to `warning`.
121    pub button_warning: Hsla,
122    /// Button warning active background color, fallback to `warning_active`.
123    pub button_warning_active: Hsla,
124    /// Button warning text color, fallback to `warning_foreground`.
125    pub button_warning_foreground: Hsla,
126    /// Button warning hover background color, fallback to `warning_hover`.
127    pub button_warning_hover: Hsla,
128    /// Background color for GroupBox.
129    pub group_box: Hsla,
130    /// Text color for GroupBox.
131    pub group_box_foreground: Hsla,
132    /// Input caret color (Blinking cursor).
133    pub caret: Hsla,
134    /// Chart 1 color (`chart.1` in the theme file).
135    pub chart_1: Hsla,
136    /// Chart 2 color (`chart.2` in the theme file).
137    pub chart_2: Hsla,
138    /// Chart 3 color (`chart.3` in the theme file).
139    pub chart_3: Hsla,
140    /// Chart 4 color (`chart.4` in the theme file).
141    pub chart_4: Hsla,
142    /// Chart 5 color (`chart.5` in the theme file).
143    pub chart_5: Hsla,
144    /// Bullish color for candlestick charts (upward price movement).
145    pub chart_bullish: Hsla,
146    /// Bearish color for candlestick charts (downward price movement).
147    pub chart_bearish: Hsla,
148    /// Chart grid line color.
149    pub chart_grid: Hsla,
150    /// Danger background color.
151    pub danger: Hsla,
152    /// Danger active background color.
153    pub danger_active: Hsla,
154    /// Danger text color.
155    pub danger_foreground: Hsla,
156    /// Danger hover background color.
157    pub danger_hover: Hsla,
158    /// Description List label background color.
159    pub description_list_label: Hsla,
160    /// Description List label foreground color.
161    pub description_list_label_foreground: Hsla,
162    /// Drag border color.
163    pub drag_border: Hsla,
164    /// Drop target background color.
165    pub drop_target: Hsla,
166    /// Default text color.
167    pub foreground: Hsla,
168    /// Info background color.
169    pub info: Hsla,
170    /// Info active background color.
171    pub info_active: Hsla,
172    /// Info text color.
173    pub info_foreground: Hsla,
174    /// Info hover background color.
175    pub info_hover: Hsla,
176    /// Border color for inputs such as Input, Select, etc.
177    pub input: Hsla,
178    /// Link text color.
179    pub link: Hsla,
180    /// Active link text color.
181    pub link_active: Hsla,
182    /// Hover link text color.
183    pub link_hover: Hsla,
184    /// Background color for List and ListItem.
185    pub list: Hsla,
186    /// Background color for active ListItem.
187    pub list_active: Hsla,
188    /// Border color for active ListItem.
189    pub list_active_border: Hsla,
190    /// Stripe background color for even ListItem.
191    pub list_even: Hsla,
192    /// Background color for List header.
193    pub list_head: Hsla,
194    /// Hover background color for ListItem.
195    pub list_hover: Hsla,
196    /// Text color for the part of a list row that matches the search query.
197    pub list_highlight_foreground: Hsla,
198    /// Muted backgrounds such as Skeleton and Switch.
199    pub muted: Hsla,
200    /// Muted text color, as used in disabled text.
201    pub muted_foreground: Hsla,
202    /// Background color for Popover.
203    pub popover: Hsla,
204    /// Text color for Popover.
205    pub popover_foreground: Hsla,
206    /// Primary background color.
207    pub primary: Hsla,
208    /// Active primary background color.
209    pub primary_active: Hsla,
210    /// Primary text color.
211    pub primary_foreground: Hsla,
212    /// Hover primary background color.
213    pub primary_hover: Hsla,
214    /// Progress bar background color.
215    pub progress_bar: Hsla,
216    /// Used for focus ring.
217    pub ring: Hsla,
218    /// Scrollbar background color.
219    pub scrollbar: Hsla,
220    /// Scrollbar thumb background color.
221    pub scrollbar_thumb: Hsla,
222    /// Scrollbar thumb hover background color.
223    pub scrollbar_thumb_hover: Hsla,
224    /// Secondary background color.
225    pub secondary: Hsla,
226    /// Active secondary background color.
227    pub secondary_active: Hsla,
228    /// Secondary text color, used for secondary Button text color or secondary text.
229    pub secondary_foreground: Hsla,
230    /// Hover secondary background color.
231    pub secondary_hover: Hsla,
232    /// Input selection background color.
233    pub selection: Hsla,
234    /// Sidebar background color.
235    pub sidebar: Hsla,
236    /// Sidebar accent background color.
237    pub sidebar_accent: Hsla,
238    /// Sidebar accent text color.
239    pub sidebar_accent_foreground: Hsla,
240    /// Sidebar border color.
241    pub sidebar_border: Hsla,
242    /// Sidebar text color.
243    pub sidebar_foreground: Hsla,
244    /// Sidebar primary background color.
245    pub sidebar_primary: Hsla,
246    /// Sidebar primary text color.
247    pub sidebar_primary_foreground: Hsla,
248    /// Skeleton background color.
249    pub skeleton: Hsla,
250    /// Slider bar background color.
251    pub slider_bar: Hsla,
252    /// Slider thumb background color.
253    pub slider_thumb: Hsla,
254    /// Success background color.
255    pub success: Hsla,
256    /// Success text color.
257    pub success_foreground: Hsla,
258    /// Success hover background color.
259    pub success_hover: Hsla,
260    /// Success active background color.
261    pub success_active: Hsla,
262    /// Switch background color.
263    pub switch: Hsla,
264    /// Switch thumb background color.
265    pub switch_thumb: Hsla,
266    /// Tab background color.
267    pub tab: Hsla,
268    /// Tab active background color.
269    pub tab_active: Hsla,
270    /// Tab active text color.
271    pub tab_active_foreground: Hsla,
272    /// TabBar background color.
273    pub tab_bar: Hsla,
274    /// TabBar segmented background color.
275    pub tab_bar_segmented: Hsla,
276    /// Tab text color.
277    pub tab_foreground: Hsla,
278    /// Table background color.
279    pub table: Hsla,
280    /// Table active item background color.
281    pub table_active: Hsla,
282    /// Table active item border color.
283    pub table_active_border: Hsla,
284    /// Stripe background color for even TableRow.
285    pub table_even: Hsla,
286    /// Table head background color.
287    pub table_head: Hsla,
288    /// Table head text color.
289    pub table_head_foreground: Hsla,
290    /// Table footer background color.
291    pub table_foot: Hsla,
292    /// Table footer text color.
293    pub table_foot_foreground: Hsla,
294    /// Table item hover background color.
295    pub table_hover: Hsla,
296    /// Table row border color.
297    pub table_row_border: Hsla,
298    /// TitleBar background color, use for Window title bar.
299    pub title_bar: Hsla,
300    /// TitleBar border color.
301    pub title_bar_border: Hsla,
302    /// StatusBar background color, use for the bottom status bar.
303    pub status_bar: Hsla,
304    /// StatusBar border color.
305    pub status_bar_border: Hsla,
306    /// Warning background color.
307    pub warning: Hsla,
308    /// Warning active background color.
309    pub warning_active: Hsla,
310    /// Warning hover background color.
311    pub warning_hover: Hsla,
312    /// Warning foreground color.
313    pub warning_foreground: Hsla,
314    /// Overlay background color.
315    pub overlay: Hsla,
316    /// Window border color.
317    ///
318    /// # Platform specific:
319    ///
320    /// This is only works on Linux, other platforms we can't change the window border color.
321    pub window_border: Hsla,
322
323    /// The base red color.
324    pub red: Hsla,
325    /// The base red light color.
326    pub red_light: Hsla,
327    /// The base green color.
328    pub green: Hsla,
329    /// The base green light color.
330    pub green_light: Hsla,
331    /// The base blue color.
332    pub blue: Hsla,
333    /// The base blue light color.
334    pub blue_light: Hsla,
335    /// The base yellow color.
336    pub yellow: Hsla,
337    /// The base yellow light color.
338    pub yellow_light: Hsla,
339    /// The base magenta color.
340    pub magenta: Hsla,
341    /// The base magenta light color.
342    pub magenta_light: Hsla,
343    /// The base cyan color.
344    pub cyan: Hsla,
345    /// The base cyan light color.
346    pub cyan_light: Hsla,
347}
348
349macro_rules! define_theme_tokens {
350    ($($field:ident),+ $(,)?) => {
351        /// Legacy resolved tokens retained for compatibility with existing
352        /// `gpui-component` themes and components.
353        ///
354        /// This type intentionally includes component-specific fields such as
355        /// `button_primary`, `tab_active`, and `table_hover`. New application-owned
356        /// components should use [`gpui_base::SemanticThemeTokens`] instead. The
357        /// legacy fields remain public so existing theme files and direct field
358        /// access continue to work unchanged.
359        #[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
360        pub struct ThemeTokens {
361            $(pub $field: ThemeToken,)+
362        }
363
364        impl ThemeTokens {
365            /// Brings the tokens and `colors` back to one value per field after
366            /// an edit, given the two as they were before it.
367            ///
368            /// A field edited on `colors` wins: when its token no longer names
369            /// that color, the token becomes that solid color, dropping any
370            /// gradient it carried — the caller asked for the solid color. A
371            /// field edited only on the tokens writes its solid color back to
372            /// `colors`. A field the edit set on both sides to one color, as
373            /// applying a theme config does, keeps its token, so a gradient
374            /// from a theme file survives; so does an untouched field.
375            pub(crate) fn reconcile(
376                &mut self,
377                colors: &mut ThemeColor,
378                colors_before: &ThemeColor,
379                tokens_before: &ThemeTokens,
380            ) {
381                $(
382                    if colors.$field != colors_before.$field {
383                        if self.$field.color != colors.$field {
384                            self.$field = colors.$field.into();
385                        }
386                    } else if self.$field != tokens_before.$field {
387                        colors.$field = self.$field.color;
388                    }
389                )+
390            }
391        }
392
393        impl From<ThemeColor> for ThemeTokens {
394            fn from(colors: ThemeColor) -> Self {
395                Self {
396                    $($field: colors.$field.into(),)+
397                }
398            }
399        }
400
401        impl From<&ThemeColor> for ThemeTokens {
402            fn from(colors: &ThemeColor) -> Self {
403                Self::from(*colors)
404            }
405        }
406    };
407}
408
409define_theme_tokens! {
410    accent,
411    accent_foreground,
412    accordion,
413    background,
414    border,
415    button,
416    button_active,
417    button_foreground,
418    button_hover,
419    button_danger,
420    button_danger_active,
421    button_danger_foreground,
422    button_danger_hover,
423    button_info,
424    button_info_active,
425    button_info_foreground,
426    button_info_hover,
427    button_primary,
428    button_primary_active,
429    button_primary_foreground,
430    button_primary_hover,
431    button_secondary,
432    button_secondary_active,
433    button_secondary_foreground,
434    button_secondary_hover,
435    button_success,
436    button_success_active,
437    button_success_foreground,
438    button_success_hover,
439    button_warning,
440    button_warning_active,
441    button_warning_foreground,
442    button_warning_hover,
443    group_box,
444    group_box_foreground,
445    caret,
446    chart_1,
447    chart_2,
448    chart_3,
449    chart_4,
450    chart_5,
451    chart_bullish,
452    chart_bearish,
453    chart_grid,
454    danger,
455    danger_active,
456    danger_foreground,
457    danger_hover,
458    description_list_label,
459    description_list_label_foreground,
460    drag_border,
461    drop_target,
462    foreground,
463    info,
464    info_active,
465    info_foreground,
466    info_hover,
467    input,
468    link,
469    link_active,
470    link_hover,
471    list,
472    list_active,
473    list_active_border,
474    list_even,
475    list_head,
476    list_hover,
477    list_highlight_foreground,
478    muted,
479    muted_foreground,
480    popover,
481    popover_foreground,
482    primary,
483    primary_active,
484    primary_foreground,
485    primary_hover,
486    progress_bar,
487    ring,
488    scrollbar,
489    scrollbar_thumb,
490    scrollbar_thumb_hover,
491    secondary,
492    secondary_active,
493    secondary_foreground,
494    secondary_hover,
495    selection,
496    sidebar,
497    sidebar_accent,
498    sidebar_accent_foreground,
499    sidebar_border,
500    sidebar_foreground,
501    sidebar_primary,
502    sidebar_primary_foreground,
503    skeleton,
504    slider_bar,
505    slider_thumb,
506    success,
507    success_foreground,
508    success_hover,
509    success_active,
510    switch,
511    switch_thumb,
512    tab,
513    tab_active,
514    tab_active_foreground,
515    tab_bar,
516    tab_bar_segmented,
517    tab_foreground,
518    table,
519    table_active,
520    table_active_border,
521    table_even,
522    table_head,
523    table_head_foreground,
524    table_foot,
525    table_foot_foreground,
526    table_hover,
527    table_row_border,
528    title_bar,
529    title_bar_border,
530    status_bar,
531    status_bar_border,
532    warning,
533    warning_active,
534    warning_hover,
535    warning_foreground,
536    overlay,
537    window_border,
538    red,
539    red_light,
540    green,
541    green_light,
542    blue,
543    blue_light,
544    yellow,
545    yellow_light,
546    magenta,
547    magenta_light,
548    cyan,
549    cyan_light,
550}
551
552impl ThemeColor {
553    /// Get the default light theme colors.
554    pub fn light() -> Arc<Self> {
555        DEFAULT_THEME_COLORS[&ThemeMode::Light].0.clone()
556    }
557
558    /// Get the default dark theme colors.
559    pub fn dark() -> Arc<Self> {
560        DEFAULT_THEME_COLORS[&ThemeMode::Dark].0.clone()
561    }
562}