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