colors.rs

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