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