theme.rs

  1mod theme_registry;
  2
  3use gpui::{
  4    color::Color,
  5    elements::{ContainerStyle, ImageStyle, LabelStyle, TooltipStyle},
  6    fonts::{HighlightStyle, TextStyle},
  7    Border, MouseState,
  8};
  9use serde::{de::DeserializeOwned, Deserialize};
 10use serde_json::Value;
 11use std::{collections::HashMap, sync::Arc};
 12
 13pub use theme_registry::*;
 14
 15pub const DEFAULT_THEME_NAME: &'static str = "cave-dark";
 16
 17#[derive(Deserialize, Default)]
 18pub struct Theme {
 19    #[serde(default)]
 20    pub name: String,
 21    pub workspace: Workspace,
 22    pub context_menu: ContextMenu,
 23    pub chat_panel: ChatPanel,
 24    pub contacts_panel: ContactsPanel,
 25    pub contact_finder: ContactFinder,
 26    pub project_panel: ProjectPanel,
 27    pub command_palette: CommandPalette,
 28    pub picker: Picker,
 29    pub editor: Editor,
 30    pub search: Search,
 31    pub project_diagnostics: ProjectDiagnostics,
 32    pub breadcrumbs: ContainedText,
 33    pub contact_notification: ContactNotification,
 34    pub update_notification: UpdateNotification,
 35    pub tooltip: TooltipStyle,
 36}
 37
 38#[derive(Deserialize, Default)]
 39pub struct Workspace {
 40    pub background: Color,
 41    pub titlebar: Titlebar,
 42    pub tab: Tab,
 43    pub active_tab: Tab,
 44    pub pane_divider: Border,
 45    pub leader_border_opacity: f32,
 46    pub leader_border_width: f32,
 47    pub sidebar_resize_handle: ContainerStyle,
 48    pub status_bar: StatusBar,
 49    pub toolbar: Toolbar,
 50    pub disconnected_overlay: ContainedText,
 51    pub modal: ContainerStyle,
 52    pub notification: ContainerStyle,
 53    pub notifications: Notifications,
 54    pub joining_project_avatar: ImageStyle,
 55    pub joining_project_message: ContainedText,
 56}
 57
 58#[derive(Clone, Deserialize, Default)]
 59pub struct Titlebar {
 60    #[serde(flatten)]
 61    pub container: ContainerStyle,
 62    pub height: f32,
 63    pub title: TextStyle,
 64    pub avatar_width: f32,
 65    pub avatar_margin: f32,
 66    pub avatar_ribbon: AvatarRibbon,
 67    pub offline_icon: OfflineIcon,
 68    pub share_icon: Interactive<ShareIcon>,
 69    pub avatar: ImageStyle,
 70    pub sign_in_prompt: Interactive<ContainedText>,
 71    pub outdated_warning: ContainedText,
 72}
 73
 74#[derive(Clone, Deserialize, Default)]
 75pub struct AvatarRibbon {
 76    #[serde(flatten)]
 77    pub container: ContainerStyle,
 78    pub width: f32,
 79    pub height: f32,
 80}
 81
 82#[derive(Clone, Deserialize, Default)]
 83pub struct OfflineIcon {
 84    #[serde(flatten)]
 85    pub container: ContainerStyle,
 86    pub width: f32,
 87    pub color: Color,
 88}
 89
 90#[derive(Clone, Deserialize, Default)]
 91pub struct ShareIcon {
 92    #[serde(flatten)]
 93    pub container: ContainerStyle,
 94    pub color: Color,
 95}
 96
 97#[derive(Clone, Deserialize, Default)]
 98pub struct Tab {
 99    pub height: f32,
100    #[serde(flatten)]
101    pub container: ContainerStyle,
102    #[serde(flatten)]
103    pub label: LabelStyle,
104    pub spacing: f32,
105    pub icon_width: f32,
106    pub icon_close: Color,
107    pub icon_close_active: Color,
108    pub icon_dirty: Color,
109    pub icon_conflict: Color,
110}
111
112#[derive(Clone, Deserialize, Default)]
113pub struct Toolbar {
114    #[serde(flatten)]
115    pub container: ContainerStyle,
116    pub height: f32,
117    pub item_spacing: f32,
118}
119
120#[derive(Clone, Deserialize, Default)]
121pub struct Notifications {
122    #[serde(flatten)]
123    pub container: ContainerStyle,
124    pub width: f32,
125}
126
127#[derive(Clone, Deserialize, Default)]
128pub struct Search {
129    #[serde(flatten)]
130    pub container: ContainerStyle,
131    pub editor: FindEditor,
132    pub invalid_editor: ContainerStyle,
133    pub option_button_group: ContainerStyle,
134    pub option_button: Interactive<ContainedText>,
135    pub match_background: Color,
136    pub match_index: ContainedText,
137    pub results_status: TextStyle,
138    pub tab_icon_width: f32,
139    pub tab_icon_spacing: f32,
140}
141
142#[derive(Clone, Deserialize, Default)]
143pub struct FindEditor {
144    #[serde(flatten)]
145    pub input: FieldEditor,
146    pub min_width: f32,
147    pub max_width: f32,
148}
149
150#[derive(Deserialize, Default)]
151pub struct StatusBar {
152    #[serde(flatten)]
153    pub container: ContainerStyle,
154    pub height: f32,
155    pub item_spacing: f32,
156    pub cursor_position: TextStyle,
157    pub auto_update_progress_message: TextStyle,
158    pub auto_update_done_message: TextStyle,
159    pub lsp_status: Interactive<StatusBarLspStatus>,
160    pub sidebar_buttons: StatusBarSidebarButtons,
161    pub diagnostic_summary: Interactive<StatusBarDiagnosticSummary>,
162    pub diagnostic_message: Interactive<ContainedText>,
163}
164
165#[derive(Deserialize, Default)]
166pub struct StatusBarSidebarButtons {
167    pub group_left: ContainerStyle,
168    pub group_right: ContainerStyle,
169    pub item: Interactive<SidebarItem>,
170    pub badge: ContainerStyle,
171}
172
173#[derive(Deserialize, Default)]
174pub struct StatusBarDiagnosticSummary {
175    pub container_ok: ContainerStyle,
176    pub container_warning: ContainerStyle,
177    pub container_error: ContainerStyle,
178    pub text: TextStyle,
179    pub icon_color_ok: Color,
180    pub icon_color_warning: Color,
181    pub icon_color_error: Color,
182    pub height: f32,
183    pub icon_width: f32,
184    pub icon_spacing: f32,
185    pub summary_spacing: f32,
186}
187
188#[derive(Deserialize, Default)]
189pub struct StatusBarLspStatus {
190    #[serde(flatten)]
191    pub container: ContainerStyle,
192    pub height: f32,
193    pub icon_spacing: f32,
194    pub icon_color: Color,
195    pub icon_width: f32,
196    pub message: TextStyle,
197}
198
199#[derive(Deserialize, Default)]
200pub struct Sidebar {
201    pub resize_handle: ContainerStyle,
202}
203
204#[derive(Clone, Copy, Deserialize, Default)]
205pub struct SidebarItem {
206    #[serde(flatten)]
207    pub container: ContainerStyle,
208    pub icon_color: Color,
209    pub icon_size: f32,
210}
211
212#[derive(Deserialize, Default)]
213pub struct ChatPanel {
214    #[serde(flatten)]
215    pub container: ContainerStyle,
216    pub message: ChatMessage,
217    pub pending_message: ChatMessage,
218    pub channel_select: ChannelSelect,
219    pub input_editor: FieldEditor,
220    pub sign_in_prompt: TextStyle,
221    pub hovered_sign_in_prompt: TextStyle,
222}
223
224#[derive(Deserialize, Default)]
225pub struct ProjectPanel {
226    #[serde(flatten)]
227    pub container: ContainerStyle,
228    pub entry: Interactive<ProjectPanelEntry>,
229    pub cut_entry_fade: f32,
230    pub ignored_entry_fade: f32,
231    pub filename_editor: FieldEditor,
232    pub indent_width: f32,
233}
234
235#[derive(Clone, Debug, Deserialize, Default)]
236pub struct ProjectPanelEntry {
237    pub height: f32,
238    #[serde(flatten)]
239    pub container: ContainerStyle,
240    pub text: TextStyle,
241    pub icon_color: Color,
242    pub icon_size: f32,
243    pub icon_spacing: f32,
244}
245
246#[derive(Clone, Debug, Deserialize, Default)]
247pub struct ContextMenu {
248    #[serde(flatten)]
249    pub container: ContainerStyle,
250    pub item: Interactive<ContextMenuItem>,
251    pub separator: ContainerStyle,
252}
253
254#[derive(Clone, Debug, Deserialize, Default)]
255pub struct ContextMenuItem {
256    #[serde(flatten)]
257    pub container: ContainerStyle,
258    pub label: TextStyle,
259    pub keystroke: ContainedText,
260}
261
262#[derive(Debug, Deserialize, Default)]
263pub struct CommandPalette {
264    pub key: Interactive<ContainedLabel>,
265    pub keystroke_spacing: f32,
266}
267
268#[derive(Deserialize, Default)]
269pub struct ContactsPanel {
270    #[serde(flatten)]
271    pub container: ContainerStyle,
272    pub user_query_editor: FieldEditor,
273    pub user_query_editor_height: f32,
274    pub add_contact_button: IconButton,
275    pub header_row: Interactive<ContainedText>,
276    pub contact_row: Interactive<ContainerStyle>,
277    pub project_row: Interactive<ProjectRow>,
278    pub row_height: f32,
279    pub contact_avatar: ImageStyle,
280    pub contact_username: ContainedText,
281    pub contact_button: Interactive<IconButton>,
282    pub contact_button_spacing: f32,
283    pub disabled_button: IconButton,
284    pub tree_branch: Interactive<TreeBranch>,
285    pub private_button: Interactive<IconButton>,
286    pub section_icon_size: f32,
287    pub invite_row: Interactive<ContainedLabel>,
288}
289
290#[derive(Deserialize, Default)]
291pub struct InviteLink {
292    #[serde(flatten)]
293    pub container: ContainerStyle,
294    #[serde(flatten)]
295    pub label: LabelStyle,
296    pub icon: Icon,
297}
298
299#[derive(Deserialize, Default, Clone, Copy)]
300pub struct TreeBranch {
301    pub width: f32,
302    pub color: Color,
303}
304
305#[derive(Deserialize, Default)]
306pub struct ContactFinder {
307    pub row_height: f32,
308    pub contact_avatar: ImageStyle,
309    pub contact_username: ContainerStyle,
310    pub contact_button: IconButton,
311    pub disabled_contact_button: IconButton,
312}
313
314#[derive(Deserialize, Default)]
315pub struct Icon {
316    #[serde(flatten)]
317    pub container: ContainerStyle,
318    pub color: Color,
319    pub width: f32,
320    pub path: String,
321}
322
323#[derive(Deserialize, Clone, Copy, Default)]
324pub struct IconButton {
325    #[serde(flatten)]
326    pub container: ContainerStyle,
327    pub color: Color,
328    pub icon_width: f32,
329    pub button_width: f32,
330}
331
332#[derive(Deserialize, Default)]
333pub struct ProjectRow {
334    #[serde(flatten)]
335    pub container: ContainerStyle,
336    pub name: ContainedText,
337    pub guests: ContainerStyle,
338    pub guest_avatar: ImageStyle,
339    pub guest_avatar_spacing: f32,
340}
341
342#[derive(Deserialize, Default)]
343pub struct ChatMessage {
344    #[serde(flatten)]
345    pub container: ContainerStyle,
346    pub body: TextStyle,
347    pub sender: ContainedText,
348    pub timestamp: ContainedText,
349}
350
351#[derive(Deserialize, Default)]
352pub struct ChannelSelect {
353    #[serde(flatten)]
354    pub container: ContainerStyle,
355    pub header: ChannelName,
356    pub item: ChannelName,
357    pub active_item: ChannelName,
358    pub hovered_item: ChannelName,
359    pub hovered_active_item: ChannelName,
360    pub menu: ContainerStyle,
361}
362
363#[derive(Deserialize, Default)]
364pub struct ChannelName {
365    #[serde(flatten)]
366    pub container: ContainerStyle,
367    pub hash: ContainedText,
368    pub name: TextStyle,
369}
370
371#[derive(Deserialize, Default)]
372pub struct Picker {
373    #[serde(flatten)]
374    pub container: ContainerStyle,
375    pub empty: ContainedLabel,
376    pub input_editor: FieldEditor,
377    pub item: Interactive<ContainedLabel>,
378}
379
380#[derive(Clone, Debug, Deserialize, Default)]
381pub struct ContainedText {
382    #[serde(flatten)]
383    pub container: ContainerStyle,
384    #[serde(flatten)]
385    pub text: TextStyle,
386}
387
388#[derive(Clone, Debug, Deserialize, Default)]
389pub struct ContainedLabel {
390    #[serde(flatten)]
391    pub container: ContainerStyle,
392    #[serde(flatten)]
393    pub label: LabelStyle,
394}
395
396#[derive(Clone, Deserialize, Default)]
397pub struct ProjectDiagnostics {
398    #[serde(flatten)]
399    pub container: ContainerStyle,
400    pub empty_message: TextStyle,
401    pub tab_icon_width: f32,
402    pub tab_icon_spacing: f32,
403    pub tab_summary_spacing: f32,
404}
405
406#[derive(Deserialize, Default)]
407pub struct ContactNotification {
408    pub header_avatar: ImageStyle,
409    pub header_message: ContainedText,
410    pub header_height: f32,
411    pub body_message: ContainedText,
412    pub button: Interactive<ContainedText>,
413    pub dismiss_button: Interactive<IconButton>,
414}
415
416#[derive(Deserialize, Default)]
417pub struct UpdateNotification {
418    pub message: ContainedText,
419    pub action_message: Interactive<ContainedText>,
420    pub dismiss_button: Interactive<IconButton>,
421}
422
423#[derive(Clone, Deserialize, Default)]
424pub struct Editor {
425    pub text_color: Color,
426    #[serde(default)]
427    pub background: Color,
428    pub selection: SelectionStyle,
429    pub gutter_background: Color,
430    pub gutter_padding_factor: f32,
431    pub active_line_background: Color,
432    pub highlighted_line_background: Color,
433    pub rename_fade: f32,
434    pub document_highlight_read_background: Color,
435    pub document_highlight_write_background: Color,
436    pub diff_background_deleted: Color,
437    pub diff_background_inserted: Color,
438    pub line_number: Color,
439    pub line_number_active: Color,
440    pub guest_selections: Vec<SelectionStyle>,
441    pub syntax: Arc<SyntaxTheme>,
442    pub diagnostic_path_header: DiagnosticPathHeader,
443    pub diagnostic_header: DiagnosticHeader,
444    pub error_diagnostic: DiagnosticStyle,
445    pub invalid_error_diagnostic: DiagnosticStyle,
446    pub warning_diagnostic: DiagnosticStyle,
447    pub invalid_warning_diagnostic: DiagnosticStyle,
448    pub information_diagnostic: DiagnosticStyle,
449    pub invalid_information_diagnostic: DiagnosticStyle,
450    pub hint_diagnostic: DiagnosticStyle,
451    pub invalid_hint_diagnostic: DiagnosticStyle,
452    pub autocomplete: AutocompleteStyle,
453    pub code_actions_indicator: Color,
454    pub unnecessary_code_fade: f32,
455}
456
457#[derive(Clone, Deserialize, Default)]
458pub struct DiagnosticPathHeader {
459    #[serde(flatten)]
460    pub container: ContainerStyle,
461    pub filename: ContainedText,
462    pub path: ContainedText,
463    pub text_scale_factor: f32,
464}
465
466#[derive(Clone, Deserialize, Default)]
467pub struct DiagnosticHeader {
468    #[serde(flatten)]
469    pub container: ContainerStyle,
470    pub message: ContainedLabel,
471    pub code: ContainedText,
472    pub text_scale_factor: f32,
473    pub icon_width_factor: f32,
474    pub jump_icon: Interactive<IconButton>,
475}
476
477#[derive(Clone, Deserialize, Default)]
478pub struct DiagnosticStyle {
479    pub message: LabelStyle,
480    #[serde(default)]
481    pub header: ContainerStyle,
482    pub text_scale_factor: f32,
483}
484
485#[derive(Clone, Deserialize, Default)]
486pub struct AutocompleteStyle {
487    #[serde(flatten)]
488    pub container: ContainerStyle,
489    pub item: ContainerStyle,
490    pub selected_item: ContainerStyle,
491    pub hovered_item: ContainerStyle,
492    pub match_highlight: HighlightStyle,
493}
494
495#[derive(Clone, Copy, Default, Deserialize)]
496pub struct SelectionStyle {
497    pub cursor: Color,
498    pub selection: Color,
499}
500
501#[derive(Clone, Deserialize, Default)]
502pub struct FieldEditor {
503    #[serde(flatten)]
504    pub container: ContainerStyle,
505    pub text: TextStyle,
506    #[serde(default)]
507    pub placeholder_text: Option<TextStyle>,
508    pub selection: SelectionStyle,
509}
510
511#[derive(Debug, Default, Clone, Copy)]
512pub struct Interactive<T> {
513    pub default: T,
514    pub hover: Option<T>,
515    pub active: Option<T>,
516    pub active_hover: Option<T>,
517}
518
519impl<T> Interactive<T> {
520    pub fn style_for(&self, state: MouseState, active: bool) -> &T {
521        if active {
522            if state.hovered {
523                self.active_hover
524                    .as_ref()
525                    .or(self.active.as_ref())
526                    .unwrap_or(&self.default)
527            } else {
528                self.active.as_ref().unwrap_or(&self.default)
529            }
530        } else {
531            if state.hovered {
532                self.hover.as_ref().unwrap_or(&self.default)
533            } else {
534                &self.default
535            }
536        }
537    }
538}
539
540impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
541    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
542    where
543        D: serde::Deserializer<'de>,
544    {
545        #[derive(Deserialize)]
546        struct Helper {
547            #[serde(flatten)]
548            default: Value,
549            hover: Option<Value>,
550            active: Option<Value>,
551            active_hover: Option<Value>,
552        }
553
554        let json = Helper::deserialize(deserializer)?;
555
556        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
557            if let Some(mut state_json) = state_json {
558                if let Value::Object(state_json) = &mut state_json {
559                    if let Value::Object(default) = &json.default {
560                        for (key, value) in default {
561                            if !state_json.contains_key(key) {
562                                state_json.insert(key.clone(), value.clone());
563                            }
564                        }
565                    }
566                }
567                Ok(Some(
568                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
569                ))
570            } else {
571                Ok(None)
572            }
573        };
574
575        let hover = deserialize_state(json.hover)?;
576        let active = deserialize_state(json.active)?;
577        let active_hover = deserialize_state(json.active_hover)?;
578        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
579
580        Ok(Interactive {
581            default,
582            hover,
583            active,
584            active_hover,
585        })
586    }
587}
588
589impl Editor {
590    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
591        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
592        if style_ix == 0 {
593            &self.selection
594        } else {
595            &self.guest_selections[style_ix - 1]
596        }
597    }
598}
599
600#[derive(Default)]
601pub struct SyntaxTheme {
602    pub highlights: Vec<(String, HighlightStyle)>,
603}
604
605impl SyntaxTheme {
606    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
607        Self { highlights }
608    }
609}
610
611impl<'de> Deserialize<'de> for SyntaxTheme {
612    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
613    where
614        D: serde::Deserializer<'de>,
615    {
616        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
617
618        let mut result = Self::new(Vec::new());
619        for (key, style) in syntax_data {
620            match result
621                .highlights
622                .binary_search_by(|(needle, _)| needle.cmp(&key))
623            {
624                Ok(i) | Err(i) => {
625                    result.highlights.insert(i, (key, style));
626                }
627            }
628        }
629
630        Ok(result)
631    }
632}