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