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