colors.rs

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