colors.rs

  1use gpui::{Hsla, WindowBackgroundAppearance};
  2use refineable::Refineable;
  3use std::sync::Arc;
  4
  5use crate::{PlayerColors, StatusColors, StatusColorsRefinement, SyntaxTheme, SystemColors};
  6
  7#[derive(Refineable, Clone, Debug)]
  8#[refineable(Debug, serde::Deserialize)]
  9pub struct ThemeColors {
 10    /// Border color. Used for most borders, is usually a high contrast color.
 11    pub border: Hsla,
 12    /// Border color. Used for deemphasized borders, like a visual divider between two sections
 13    pub border_variant: Hsla,
 14    /// Border color. Used for focused elements, like keyboard focused list item.
 15    pub border_focused: Hsla,
 16    /// Border color. Used for selected elements, like an active search filter or selected checkbox.
 17    pub border_selected: Hsla,
 18    /// Border color. Used for transparent borders. Used for placeholder borders when an element gains a border on state change.
 19    pub border_transparent: Hsla,
 20    /// Border color. Used for disabled elements, like a disabled input or button.
 21    pub border_disabled: Hsla,
 22    /// Border color. Used for elevated surfaces, like a context menu, popup, or dialog.
 23    pub elevated_surface_background: Hsla,
 24    /// Background Color. Used for grounded surfaces like a panel or tab.
 25    pub surface_background: Hsla,
 26    /// Background Color. Used for the app background and blank panels or windows.
 27    pub background: Hsla,
 28    /// Background Color. Used for the background of an element that should have a different background than the surface it's on.
 29    ///
 30    /// Elements might include: Buttons, Inputs, Checkboxes, Radio Buttons...
 31    ///
 32    /// For an element that should have the same background as the surface it's on, use `ghost_element_background`.
 33    pub element_background: Hsla,
 34    /// Background Color. Used for the hover state of an element that should have a different background than the surface it's on.
 35    ///
 36    /// Hover states are triggered by the mouse entering an element, or a finger touching an element on a touch screen.
 37    pub element_hover: Hsla,
 38    /// Background Color. Used for the active state of an element that should have a different background than the surface it's on.
 39    ///
 40    /// Active states are triggered by the mouse button being pressed down on an element, or the Return button or other activator being pressd.
 41    pub element_active: Hsla,
 42    /// Background Color. Used for the selected state of an element that should have a different background than the surface it's on.
 43    ///
 44    /// Selected states are triggered by the element being selected (or "activated") by the user.
 45    ///
 46    /// This could include a selected checkbox, a toggleable button that is toggled on, etc.
 47    pub element_selected: Hsla,
 48    /// Background Color. Used for the disabled state of an element that should have a different background than the surface it's on.
 49    ///
 50    /// Disabled states are shown when a user cannot interact with an element, like a disabled button or input.
 51    pub element_disabled: Hsla,
 52    /// Background Color. Used for the area that shows where a dragged element will be dropped.
 53    pub drop_target_background: Hsla,
 54    /// Border Color. Used to show the area that shows where a dragged element will be dropped.
 55    // pub drop_target_border: Hsla,
 56    /// Used for the background of a ghost element that should have the same background as the surface it's on.
 57    ///
 58    /// Elements might include: Buttons, Inputs, Checkboxes, Radio Buttons...
 59    ///
 60    /// For an element that should have a different background than the surface it's on, use `element_background`.
 61    pub ghost_element_background: Hsla,
 62    /// Background Color. Used for the hover state of a ghost element that should have the same background as the surface it's on.
 63    ///
 64    /// Hover states are triggered by the mouse entering an element, or a finger touching an element on a touch screen.
 65    pub ghost_element_hover: Hsla,
 66    /// Background Color. Used for the active state of a ghost element that should have the same background as the surface it's on.
 67    ///
 68    /// Active states are triggered by the mouse button being pressed down on an element, or the Return button or other activator being pressd.
 69    pub ghost_element_active: Hsla,
 70    /// Background Color. Used for the selected state of a ghost element that should have the same background as the surface it's on.
 71    ///
 72    /// Selected states are triggered by the element being selected (or "activated") by the user.
 73    ///
 74    /// This could include a selected checkbox, a toggleable button that is toggled on, etc.
 75    pub ghost_element_selected: Hsla,
 76    /// Background Color. Used for the disabled state of a ghost element that should have the same background as the surface it's on.
 77    ///
 78    /// Disabled states are shown when a user cannot interact with an element, like a disabled button or input.
 79    pub ghost_element_disabled: Hsla,
 80    /// Text Color. Default text color used for most text.
 81    pub text: Hsla,
 82    /// Text Color. Color of muted or deemphasized text. It is a subdued version of the standard text color.
 83    pub text_muted: Hsla,
 84    /// Text Color. Color of the placeholder text typically shown in input fields to guide the user to enter valid data.
 85    pub text_placeholder: Hsla,
 86    /// Text Color. Color used for text denoting disabled elements. Typically, the color is faded or grayed out to emphasize the disabled state.
 87    pub text_disabled: Hsla,
 88    /// Text Color. Color used for emphasis or highlighting certain text, like an active filter or a matched character in a search.
 89    pub text_accent: Hsla,
 90    /// Fill Color. Used for the default fill color of an icon.
 91    pub icon: Hsla,
 92    /// Fill Color. Used for the muted or deemphasized fill color of an icon.
 93    ///
 94    /// This might be used to show an icon in an inactive pane, or to demphasize a series of icons to give them less visual weight.
 95    pub icon_muted: Hsla,
 96    /// Fill Color. Used for the disabled fill color of an icon.
 97    ///
 98    /// Disabled states are shown when a user cannot interact with an element, like a icon button.
 99    pub icon_disabled: Hsla,
100    /// Fill Color. Used for the placeholder fill color of an icon.
101    ///
102    /// This might be used to show an icon in an input that disappears when the user enters text.
103    pub icon_placeholder: Hsla,
104    /// Fill Color. Used for the accent fill color of an icon.
105    ///
106    /// This might be used to show when a toggleable icon button is selected.
107    pub icon_accent: Hsla,
108
109    // ===
110    // UI Elements
111    // ===
112    pub status_bar_background: Hsla,
113    pub title_bar_background: Hsla,
114    pub toolbar_background: Hsla,
115    pub tab_bar_background: Hsla,
116    pub tab_inactive_background: Hsla,
117    pub tab_active_background: Hsla,
118    pub search_match_background: Hsla,
119    pub panel_background: Hsla,
120    pub panel_focused_border: Hsla,
121    pub pane_focused_border: Hsla,
122    pub pane_group_border: Hsla,
123    /// The color of the scrollbar thumb.
124    pub scrollbar_thumb_background: Hsla,
125    /// The color of the scrollbar thumb when hovered over.
126    pub scrollbar_thumb_hover_background: Hsla,
127    /// The border color of the scrollbar thumb.
128    pub scrollbar_thumb_border: Hsla,
129    /// The background color of the scrollbar track.
130    pub scrollbar_track_background: Hsla,
131    /// The border color of the scrollbar track.
132    pub scrollbar_track_border: Hsla,
133    // /// The opacity of the scrollbar status marks, like diagnostic states and git status.
134    // todo()
135    // pub scrollbar_status_opacity: Hsla,
136
137    // ===
138    // Editor
139    // ===
140    pub editor_foreground: Hsla,
141    pub editor_background: Hsla,
142    // pub editor_inactive_background: Hsla,
143    pub editor_gutter_background: Hsla,
144    pub editor_subheader_background: Hsla,
145    pub editor_active_line_background: Hsla,
146    pub editor_highlighted_line_background: Hsla,
147    /// Text Color. Used for the text of the line number in the editor gutter.
148    pub editor_line_number: Hsla,
149    /// Text Color. Used for the text of the line number in the editor gutter when the line is highlighted.
150    pub editor_active_line_number: Hsla,
151    /// Text Color. Used to mark invisible characters in the editor.
152    ///
153    /// Example: spaces, tabs, carriage returns, etc.
154    pub editor_invisible: Hsla,
155    pub editor_wrap_guide: Hsla,
156    pub editor_active_wrap_guide: Hsla,
157    /// Read-access of a symbol, like reading a variable.
158    ///
159    /// A document highlight is a range inside a text document which deserves
160    /// special attention. Usually a document highlight is visualized by changing
161    /// the background color of its range.
162    pub editor_document_highlight_read_background: Hsla,
163    /// Read-access of a symbol, like reading a variable.
164    ///
165    /// A document highlight is a range inside a text document which deserves
166    /// special attention. Usually a document highlight is visualized by changing
167    /// the background color of its range.
168    pub editor_document_highlight_write_background: Hsla,
169
170    // ===
171    // Terminal
172    // ===
173    /// Terminal background color.
174    pub terminal_background: Hsla,
175    /// Terminal foreground color.
176    pub terminal_foreground: Hsla,
177    /// Bright terminal foreground color.
178    pub terminal_bright_foreground: Hsla,
179    /// Dim terminal foreground color.
180    pub terminal_dim_foreground: Hsla,
181
182    /// Black ANSI terminal color.
183    pub terminal_ansi_black: Hsla,
184    /// Bright black ANSI terminal color.
185    pub terminal_ansi_bright_black: Hsla,
186    /// Dim black ANSI terminal color.
187    pub terminal_ansi_dim_black: Hsla,
188    /// Red ANSI terminal color.
189    pub terminal_ansi_red: Hsla,
190    /// Bright red ANSI terminal color.
191    pub terminal_ansi_bright_red: Hsla,
192    /// Dim red ANSI terminal color.
193    pub terminal_ansi_dim_red: Hsla,
194    /// Green ANSI terminal color.
195    pub terminal_ansi_green: Hsla,
196    /// Bright green ANSI terminal color.
197    pub terminal_ansi_bright_green: Hsla,
198    /// Dim green ANSI terminal color.
199    pub terminal_ansi_dim_green: Hsla,
200    /// Yellow ANSI terminal color.
201    pub terminal_ansi_yellow: Hsla,
202    /// Bright yellow ANSI terminal color.
203    pub terminal_ansi_bright_yellow: Hsla,
204    /// Dim yellow ANSI terminal color.
205    pub terminal_ansi_dim_yellow: Hsla,
206    /// Blue ANSI terminal color.
207    pub terminal_ansi_blue: Hsla,
208    /// Bright blue ANSI terminal color.
209    pub terminal_ansi_bright_blue: Hsla,
210    /// Dim blue ANSI terminal color.
211    pub terminal_ansi_dim_blue: Hsla,
212    /// Magenta ANSI terminal color.
213    pub terminal_ansi_magenta: Hsla,
214    /// Bright magenta ANSI terminal color.
215    pub terminal_ansi_bright_magenta: Hsla,
216    /// Dim magenta ANSI terminal color.
217    pub terminal_ansi_dim_magenta: Hsla,
218    /// Cyan ANSI terminal color.
219    pub terminal_ansi_cyan: Hsla,
220    /// Bright cyan ANSI terminal color.
221    pub terminal_ansi_bright_cyan: Hsla,
222    /// Dim cyan ANSI terminal color.
223    pub terminal_ansi_dim_cyan: Hsla,
224    /// White ANSI terminal color.
225    pub terminal_ansi_white: Hsla,
226    /// Bright white ANSI terminal color.
227    pub terminal_ansi_bright_white: Hsla,
228    /// Dim white ANSI terminal color.
229    pub terminal_ansi_dim_white: Hsla,
230
231    // ===
232    // UI/Rich Text
233    // ===
234    pub link_text_hover: Hsla,
235}
236
237#[derive(Refineable, Clone)]
238pub struct ThemeStyles {
239    /// The background appearance of the window.
240    pub window_background_appearance: WindowBackgroundAppearance,
241    pub system: SystemColors,
242    /// An array of colors used for theme elements that iterate through a series of colors.
243    ///
244    /// Example: Player colors, rainbow brackets and indent guides, etc.
245    pub accents: Vec<Hsla>,
246
247    #[refineable]
248    pub colors: ThemeColors,
249
250    #[refineable]
251    pub status: StatusColors,
252
253    pub player: PlayerColors,
254    pub syntax: Arc<SyntaxTheme>,
255}
256
257#[cfg(test)]
258mod tests {
259    use serde_json::json;
260
261    use super::*;
262
263    #[test]
264    fn override_a_single_theme_color() {
265        let mut colors = ThemeColors::light();
266
267        let magenta: Hsla = gpui::rgb(0xff00ff).into();
268
269        assert_ne!(colors.text, magenta);
270
271        let overrides = ThemeColorsRefinement {
272            text: Some(magenta),
273            ..Default::default()
274        };
275
276        colors.refine(&overrides);
277
278        assert_eq!(colors.text, magenta);
279    }
280
281    #[test]
282    fn override_multiple_theme_colors() {
283        let mut colors = ThemeColors::light();
284
285        let magenta: Hsla = gpui::rgb(0xff00ff).into();
286        let green: Hsla = gpui::rgb(0x00ff00).into();
287
288        assert_ne!(colors.text, magenta);
289        assert_ne!(colors.background, green);
290
291        let overrides = ThemeColorsRefinement {
292            text: Some(magenta),
293            background: Some(green),
294            ..Default::default()
295        };
296
297        colors.refine(&overrides);
298
299        assert_eq!(colors.text, magenta);
300        assert_eq!(colors.background, green);
301    }
302
303    #[test]
304    fn deserialize_theme_colors_refinement_from_json() {
305        let colors: ThemeColorsRefinement = serde_json::from_value(json!({
306            "background": "#ff00ff",
307            "text": "#ff0000"
308        }))
309        .unwrap();
310
311        assert_eq!(colors.background, Some(gpui::rgb(0xff00ff).into()));
312        assert_eq!(colors.text, Some(gpui::rgb(0xff0000).into()));
313    }
314}