colors.rs

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