colors.rs

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