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}
271
272#[derive(Debug, Deserialize, Default)]
273pub struct CommandPalette {
274    pub key: Interactive<ContainedLabel>,
275    pub keystroke_spacing: f32,
276}
277
278#[derive(Deserialize, Default)]
279pub struct ContactsPanel {
280    #[serde(flatten)]
281    pub container: ContainerStyle,
282    pub user_query_editor: FieldEditor,
283    pub user_query_editor_height: f32,
284    pub add_contact_button: IconButton,
285    pub header_row: Interactive<ContainedText>,
286    pub contact_row: Interactive<ContainerStyle>,
287    pub project_row: Interactive<ProjectRow>,
288    pub row_height: f32,
289    pub contact_avatar: ImageStyle,
290    pub contact_username: ContainedText,
291    pub contact_button: Interactive<IconButton>,
292    pub contact_button_spacing: f32,
293    pub disabled_button: IconButton,
294    pub tree_branch: Interactive<TreeBranch>,
295    pub private_button: Interactive<IconButton>,
296    pub section_icon_size: f32,
297    pub invite_row: Interactive<ContainedLabel>,
298}
299
300#[derive(Deserialize, Default)]
301pub struct InviteLink {
302    #[serde(flatten)]
303    pub container: ContainerStyle,
304    #[serde(flatten)]
305    pub label: LabelStyle,
306    pub icon: Icon,
307}
308
309#[derive(Deserialize, Default, Clone, Copy)]
310pub struct TreeBranch {
311    pub width: f32,
312    pub color: Color,
313}
314
315#[derive(Deserialize, Default)]
316pub struct ContactFinder {
317    pub row_height: f32,
318    pub contact_avatar: ImageStyle,
319    pub contact_username: ContainerStyle,
320    pub contact_button: IconButton,
321    pub disabled_contact_button: IconButton,
322}
323
324#[derive(Deserialize, Default)]
325pub struct Icon {
326    #[serde(flatten)]
327    pub container: ContainerStyle,
328    pub color: Color,
329    pub width: f32,
330    pub path: String,
331}
332
333#[derive(Deserialize, Clone, Copy, Default)]
334pub struct IconButton {
335    #[serde(flatten)]
336    pub container: ContainerStyle,
337    pub color: Color,
338    pub icon_width: f32,
339    pub button_width: f32,
340}
341
342#[derive(Deserialize, Default)]
343pub struct ProjectRow {
344    #[serde(flatten)]
345    pub container: ContainerStyle,
346    pub name: ContainedText,
347    pub guests: ContainerStyle,
348    pub guest_avatar: ImageStyle,
349    pub guest_avatar_spacing: f32,
350}
351
352#[derive(Deserialize, Default)]
353pub struct ChatMessage {
354    #[serde(flatten)]
355    pub container: ContainerStyle,
356    pub body: TextStyle,
357    pub sender: ContainedText,
358    pub timestamp: ContainedText,
359}
360
361#[derive(Deserialize, Default)]
362pub struct ChannelSelect {
363    #[serde(flatten)]
364    pub container: ContainerStyle,
365    pub header: ChannelName,
366    pub item: ChannelName,
367    pub active_item: ChannelName,
368    pub hovered_item: ChannelName,
369    pub hovered_active_item: ChannelName,
370    pub menu: ContainerStyle,
371}
372
373#[derive(Deserialize, Default)]
374pub struct ChannelName {
375    #[serde(flatten)]
376    pub container: ContainerStyle,
377    pub hash: ContainedText,
378    pub name: TextStyle,
379}
380
381#[derive(Deserialize, Default)]
382pub struct Picker {
383    #[serde(flatten)]
384    pub container: ContainerStyle,
385    pub empty: ContainedLabel,
386    pub input_editor: FieldEditor,
387    pub item: Interactive<ContainedLabel>,
388}
389
390#[derive(Clone, Debug, Deserialize, Default)]
391pub struct ContainedText {
392    #[serde(flatten)]
393    pub container: ContainerStyle,
394    #[serde(flatten)]
395    pub text: TextStyle,
396}
397
398#[derive(Clone, Debug, Deserialize, Default)]
399pub struct ContainedLabel {
400    #[serde(flatten)]
401    pub container: ContainerStyle,
402    #[serde(flatten)]
403    pub label: LabelStyle,
404}
405
406#[derive(Clone, Deserialize, Default)]
407pub struct ProjectDiagnostics {
408    #[serde(flatten)]
409    pub container: ContainerStyle,
410    pub empty_message: TextStyle,
411    pub tab_icon_width: f32,
412    pub tab_icon_spacing: f32,
413    pub tab_summary_spacing: f32,
414}
415
416#[derive(Deserialize, Default)]
417pub struct ContactNotification {
418    pub header_avatar: ImageStyle,
419    pub header_message: ContainedText,
420    pub header_height: f32,
421    pub body_message: ContainedText,
422    pub button: Interactive<ContainedText>,
423    pub dismiss_button: Interactive<IconButton>,
424}
425
426#[derive(Deserialize, Default)]
427pub struct UpdateNotification {
428    pub message: ContainedText,
429    pub action_message: Interactive<ContainedText>,
430    pub dismiss_button: Interactive<IconButton>,
431}
432
433#[derive(Clone, Deserialize, Default)]
434pub struct Editor {
435    pub text_color: Color,
436    #[serde(default)]
437    pub background: Color,
438    pub selection: SelectionStyle,
439    pub gutter_background: Color,
440    pub gutter_padding_factor: f32,
441    pub active_line_background: Color,
442    pub highlighted_line_background: Color,
443    pub rename_fade: f32,
444    pub document_highlight_read_background: Color,
445    pub document_highlight_write_background: Color,
446    pub diff_background_deleted: Color,
447    pub diff_background_inserted: Color,
448    pub line_number: Color,
449    pub line_number_active: Color,
450    pub guest_selections: Vec<SelectionStyle>,
451    pub syntax: Arc<SyntaxTheme>,
452    pub diagnostic_path_header: DiagnosticPathHeader,
453    pub diagnostic_header: DiagnosticHeader,
454    pub error_diagnostic: DiagnosticStyle,
455    pub invalid_error_diagnostic: DiagnosticStyle,
456    pub warning_diagnostic: DiagnosticStyle,
457    pub invalid_warning_diagnostic: DiagnosticStyle,
458    pub information_diagnostic: DiagnosticStyle,
459    pub invalid_information_diagnostic: DiagnosticStyle,
460    pub hint_diagnostic: DiagnosticStyle,
461    pub invalid_hint_diagnostic: DiagnosticStyle,
462    pub autocomplete: AutocompleteStyle,
463    pub code_actions_indicator: Color,
464    pub unnecessary_code_fade: f32,
465    pub hover_popover: HoverPopover,
466    pub link_definition: HighlightStyle,
467    pub jump_icon: Interactive<IconButton>,
468}
469
470#[derive(Clone, Deserialize, Default)]
471pub struct DiagnosticPathHeader {
472    #[serde(flatten)]
473    pub container: ContainerStyle,
474    pub filename: ContainedText,
475    pub path: ContainedText,
476    pub text_scale_factor: f32,
477}
478
479#[derive(Clone, Deserialize, Default)]
480pub struct DiagnosticHeader {
481    #[serde(flatten)]
482    pub container: ContainerStyle,
483    pub message: ContainedLabel,
484    pub code: ContainedText,
485    pub text_scale_factor: f32,
486    pub icon_width_factor: f32,
487}
488
489#[derive(Clone, Deserialize, Default)]
490pub struct DiagnosticStyle {
491    pub message: LabelStyle,
492    #[serde(default)]
493    pub header: ContainerStyle,
494    pub text_scale_factor: f32,
495}
496
497#[derive(Clone, Deserialize, Default)]
498pub struct AutocompleteStyle {
499    #[serde(flatten)]
500    pub container: ContainerStyle,
501    pub item: ContainerStyle,
502    pub selected_item: ContainerStyle,
503    pub hovered_item: ContainerStyle,
504    pub match_highlight: HighlightStyle,
505}
506
507#[derive(Clone, Copy, Default, Deserialize)]
508pub struct SelectionStyle {
509    pub cursor: Color,
510    pub selection: Color,
511}
512
513#[derive(Clone, Deserialize, Default)]
514pub struct FieldEditor {
515    #[serde(flatten)]
516    pub container: ContainerStyle,
517    pub text: TextStyle,
518    #[serde(default)]
519    pub placeholder_text: Option<TextStyle>,
520    pub selection: SelectionStyle,
521}
522
523#[derive(Debug, Default, Clone, Copy)]
524pub struct Interactive<T> {
525    pub default: T,
526    pub hover: Option<T>,
527    pub active: Option<T>,
528    pub disabled: Option<T>,
529}
530
531impl<T> Interactive<T> {
532    pub fn style_for(&self, state: MouseState, active: bool) -> &T {
533        if active {
534            self.active.as_ref().unwrap_or(&self.default)
535        } else if state.hovered {
536            self.hover.as_ref().unwrap_or(&self.default)
537        } else {
538            &self.default
539        }
540    }
541
542    pub fn disabled_style(&self) -> &T {
543        self.disabled.as_ref().unwrap_or(&self.default)
544    }
545}
546
547impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
548    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
549    where
550        D: serde::Deserializer<'de>,
551    {
552        #[derive(Deserialize)]
553        struct Helper {
554            #[serde(flatten)]
555            default: Value,
556            hover: Option<Value>,
557            active: Option<Value>,
558            disabled: Option<Value>,
559        }
560
561        let json = Helper::deserialize(deserializer)?;
562
563        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
564            if let Some(mut state_json) = state_json {
565                if let Value::Object(state_json) = &mut state_json {
566                    if let Value::Object(default) = &json.default {
567                        for (key, value) in default {
568                            if !state_json.contains_key(key) {
569                                state_json.insert(key.clone(), value.clone());
570                            }
571                        }
572                    }
573                }
574                Ok(Some(
575                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
576                ))
577            } else {
578                Ok(None)
579            }
580        };
581
582        let hover = deserialize_state(json.hover)?;
583        let active = deserialize_state(json.active)?;
584        let disabled = deserialize_state(json.disabled)?;
585        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
586
587        Ok(Interactive {
588            default,
589            hover,
590            active,
591            disabled,
592        })
593    }
594}
595
596impl Editor {
597    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
598        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
599        if style_ix == 0 {
600            &self.selection
601        } else {
602            &self.guest_selections[style_ix - 1]
603        }
604    }
605}
606
607#[derive(Default)]
608pub struct SyntaxTheme {
609    pub highlights: Vec<(String, HighlightStyle)>,
610}
611
612impl SyntaxTheme {
613    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
614        Self { highlights }
615    }
616}
617
618impl<'de> Deserialize<'de> for SyntaxTheme {
619    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
620    where
621        D: serde::Deserializer<'de>,
622    {
623        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
624
625        let mut result = Self::new(Vec::new());
626        for (key, style) in syntax_data {
627            match result
628                .highlights
629                .binary_search_by(|(needle, _)| needle.cmp(&key))
630            {
631                Ok(i) | Err(i) => {
632                    result.highlights.insert(i, (key, style));
633                }
634            }
635        }
636
637        Ok(result)
638    }
639}
640
641#[derive(Clone, Deserialize, Default)]
642pub struct HoverPopover {
643    pub container: ContainerStyle,
644    pub block_style: ContainerStyle,
645    pub prose: TextStyle,
646    pub highlight: Color,
647}
648
649#[derive(Clone, Deserialize, Default)]
650pub struct TerminalStyle {
651    pub colors: TerminalColors,
652    pub modal_container: ContainerStyle,
653}
654
655#[derive(Clone, Deserialize, Default)]
656pub struct TerminalColors {
657    pub black: Color,
658    pub red: Color,
659    pub green: Color,
660    pub yellow: Color,
661    pub blue: Color,
662    pub magenta: Color,
663    pub cyan: Color,
664    pub white: Color,
665    pub bright_black: Color,
666    pub bright_red: Color,
667    pub bright_green: Color,
668    pub bright_yellow: Color,
669    pub bright_blue: Color,
670    pub bright_magenta: Color,
671    pub bright_cyan: Color,
672    pub bright_white: Color,
673    pub foreground: Color,
674    pub background: Color,
675    pub modal_background: Color,
676    pub cursor: Color,
677    pub dim_black: Color,
678    pub dim_red: Color,
679    pub dim_green: Color,
680    pub dim_yellow: Color,
681    pub dim_blue: Color,
682    pub dim_magenta: Color,
683    pub dim_cyan: Color,
684    pub dim_white: Color,
685    pub bright_foreground: Color,
686    pub dim_foreground: Color,
687}