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 composition_mark: HighlightStyle,
468    pub jump_icon: Interactive<IconButton>,
469}
470
471#[derive(Clone, Deserialize, Default)]
472pub struct DiagnosticPathHeader {
473    #[serde(flatten)]
474    pub container: ContainerStyle,
475    pub filename: ContainedText,
476    pub path: ContainedText,
477    pub text_scale_factor: f32,
478}
479
480#[derive(Clone, Deserialize, Default)]
481pub struct DiagnosticHeader {
482    #[serde(flatten)]
483    pub container: ContainerStyle,
484    pub message: ContainedLabel,
485    pub code: ContainedText,
486    pub text_scale_factor: f32,
487    pub icon_width_factor: f32,
488}
489
490#[derive(Clone, Deserialize, Default)]
491pub struct DiagnosticStyle {
492    pub message: LabelStyle,
493    #[serde(default)]
494    pub header: ContainerStyle,
495    pub text_scale_factor: f32,
496}
497
498#[derive(Clone, Deserialize, Default)]
499pub struct AutocompleteStyle {
500    #[serde(flatten)]
501    pub container: ContainerStyle,
502    pub item: ContainerStyle,
503    pub selected_item: ContainerStyle,
504    pub hovered_item: ContainerStyle,
505    pub match_highlight: HighlightStyle,
506}
507
508#[derive(Clone, Copy, Default, Deserialize)]
509pub struct SelectionStyle {
510    pub cursor: Color,
511    pub selection: Color,
512}
513
514#[derive(Clone, Deserialize, Default)]
515pub struct FieldEditor {
516    #[serde(flatten)]
517    pub container: ContainerStyle,
518    pub text: TextStyle,
519    #[serde(default)]
520    pub placeholder_text: Option<TextStyle>,
521    pub selection: SelectionStyle,
522}
523
524#[derive(Debug, Default, Clone, Copy)]
525pub struct Interactive<T> {
526    pub default: T,
527    pub hover: Option<T>,
528    pub active: Option<T>,
529    pub disabled: Option<T>,
530}
531
532impl<T> Interactive<T> {
533    pub fn style_for(&self, state: MouseState, active: bool) -> &T {
534        if active {
535            self.active.as_ref().unwrap_or(&self.default)
536        } else if state.hovered {
537            self.hover.as_ref().unwrap_or(&self.default)
538        } else {
539            &self.default
540        }
541    }
542
543    pub fn disabled_style(&self) -> &T {
544        self.disabled.as_ref().unwrap_or(&self.default)
545    }
546}
547
548impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
549    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
550    where
551        D: serde::Deserializer<'de>,
552    {
553        #[derive(Deserialize)]
554        struct Helper {
555            #[serde(flatten)]
556            default: Value,
557            hover: Option<Value>,
558            active: Option<Value>,
559            disabled: Option<Value>,
560        }
561
562        let json = Helper::deserialize(deserializer)?;
563
564        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
565            if let Some(mut state_json) = state_json {
566                if let Value::Object(state_json) = &mut state_json {
567                    if let Value::Object(default) = &json.default {
568                        for (key, value) in default {
569                            if !state_json.contains_key(key) {
570                                state_json.insert(key.clone(), value.clone());
571                            }
572                        }
573                    }
574                }
575                Ok(Some(
576                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
577                ))
578            } else {
579                Ok(None)
580            }
581        };
582
583        let hover = deserialize_state(json.hover)?;
584        let active = deserialize_state(json.active)?;
585        let disabled = deserialize_state(json.disabled)?;
586        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
587
588        Ok(Interactive {
589            default,
590            hover,
591            active,
592            disabled,
593        })
594    }
595}
596
597impl Editor {
598    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
599        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
600        if style_ix == 0 {
601            &self.selection
602        } else {
603            &self.guest_selections[style_ix - 1]
604        }
605    }
606}
607
608#[derive(Default)]
609pub struct SyntaxTheme {
610    pub highlights: Vec<(String, HighlightStyle)>,
611}
612
613impl SyntaxTheme {
614    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
615        Self { highlights }
616    }
617}
618
619impl<'de> Deserialize<'de> for SyntaxTheme {
620    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
621    where
622        D: serde::Deserializer<'de>,
623    {
624        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
625
626        let mut result = Self::new(Vec::new());
627        for (key, style) in syntax_data {
628            match result
629                .highlights
630                .binary_search_by(|(needle, _)| needle.cmp(&key))
631            {
632                Ok(i) | Err(i) => {
633                    result.highlights.insert(i, (key, style));
634                }
635            }
636        }
637
638        Ok(result)
639    }
640}
641
642#[derive(Clone, Deserialize, Default)]
643pub struct HoverPopover {
644    pub container: ContainerStyle,
645    pub info_container: ContainerStyle,
646    pub warning_container: ContainerStyle,
647    pub error_container: ContainerStyle,
648    pub block_style: ContainerStyle,
649    pub prose: TextStyle,
650    pub highlight: Color,
651}
652
653#[derive(Clone, Deserialize, Default)]
654pub struct TerminalStyle {
655    pub colors: TerminalColors,
656    pub modal_container: ContainerStyle,
657}
658
659#[derive(Clone, Deserialize, Default)]
660pub struct TerminalColors {
661    pub black: Color,
662    pub red: Color,
663    pub green: Color,
664    pub yellow: Color,
665    pub blue: Color,
666    pub magenta: Color,
667    pub cyan: Color,
668    pub white: Color,
669    pub bright_black: Color,
670    pub bright_red: Color,
671    pub bright_green: Color,
672    pub bright_yellow: Color,
673    pub bright_blue: Color,
674    pub bright_magenta: Color,
675    pub bright_cyan: Color,
676    pub bright_white: Color,
677    pub foreground: Color,
678    pub background: Color,
679    pub modal_background: Color,
680    pub cursor: Color,
681    pub dim_black: Color,
682    pub dim_red: Color,
683    pub dim_green: Color,
684    pub dim_yellow: Color,
685    pub dim_blue: Color,
686    pub dim_magenta: Color,
687    pub dim_cyan: Color,
688    pub dim_white: Color,
689    pub bright_foreground: Color,
690    pub dim_foreground: Color,
691}