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