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