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