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}
 78
 79#[derive(Clone, Deserialize, Default)]
 80pub struct TabBar {
 81    #[serde(flatten)]
 82    pub container: ContainerStyle,
 83    pub pane_button: Interactive<IconButton>,
 84    pub pane_button_container: ContainerStyle,
 85    pub active_pane: TabStyles,
 86    pub inactive_pane: TabStyles,
 87    pub dragged_tab: Tab,
 88    pub height: f32,
 89    pub drop_target_overlay_color: Color,
 90}
 91
 92impl TabBar {
 93    pub fn tab_style(&self, pane_active: bool, tab_active: bool) -> &Tab {
 94        let tabs = if pane_active {
 95            &self.active_pane
 96        } else {
 97            &self.inactive_pane
 98        };
 99
100        if tab_active {
101            &tabs.active_tab
102        } else {
103            &tabs.inactive_tab
104        }
105    }
106}
107
108#[derive(Clone, Deserialize, Default)]
109pub struct TabStyles {
110    pub active_tab: Tab,
111    pub inactive_tab: Tab,
112}
113
114#[derive(Clone, Deserialize, Default)]
115pub struct AvatarRibbon {
116    #[serde(flatten)]
117    pub container: ContainerStyle,
118    pub width: f32,
119    pub height: f32,
120}
121
122#[derive(Clone, Deserialize, Default)]
123pub struct OfflineIcon {
124    #[serde(flatten)]
125    pub container: ContainerStyle,
126    pub width: f32,
127    pub color: Color,
128}
129
130#[derive(Clone, Deserialize, Default)]
131pub struct Tab {
132    pub height: f32,
133    #[serde(flatten)]
134    pub container: ContainerStyle,
135    #[serde(flatten)]
136    pub label: LabelStyle,
137    pub description: ContainedText,
138    pub spacing: f32,
139    pub icon_width: f32,
140    pub icon_close: Color,
141    pub icon_close_active: Color,
142    pub icon_dirty: Color,
143    pub icon_conflict: Color,
144}
145
146#[derive(Clone, Deserialize, Default)]
147pub struct Toolbar {
148    #[serde(flatten)]
149    pub container: ContainerStyle,
150    pub height: f32,
151    pub item_spacing: f32,
152    pub nav_button: Interactive<IconButton>,
153}
154
155#[derive(Clone, Deserialize, Default)]
156pub struct Dock {
157    pub initial_size_right: f32,
158    pub initial_size_bottom: f32,
159    pub wash_color: Color,
160    pub panel: ContainerStyle,
161    pub maximized: ContainerStyle,
162}
163
164#[derive(Clone, Deserialize, Default)]
165pub struct Notifications {
166    #[serde(flatten)]
167    pub container: ContainerStyle,
168    pub width: f32,
169}
170
171#[derive(Clone, Deserialize, Default)]
172pub struct Search {
173    #[serde(flatten)]
174    pub container: ContainerStyle,
175    pub editor: FindEditor,
176    pub invalid_editor: ContainerStyle,
177    pub option_button_group: ContainerStyle,
178    pub option_button: Interactive<ContainedText>,
179    pub match_background: Color,
180    pub match_index: ContainedText,
181    pub results_status: TextStyle,
182    pub tab_icon_width: f32,
183    pub tab_icon_spacing: f32,
184}
185
186#[derive(Clone, Deserialize, Default)]
187pub struct FindEditor {
188    #[serde(flatten)]
189    pub input: FieldEditor,
190    pub min_width: f32,
191    pub max_width: f32,
192}
193
194#[derive(Deserialize, Default)]
195pub struct StatusBar {
196    #[serde(flatten)]
197    pub container: ContainerStyle,
198    pub height: f32,
199    pub item_spacing: f32,
200    pub cursor_position: TextStyle,
201    pub auto_update_progress_message: TextStyle,
202    pub auto_update_done_message: TextStyle,
203    pub lsp_status: Interactive<StatusBarLspStatus>,
204    pub feedback: Interactive<TextStyle>,
205    pub sidebar_buttons: StatusBarSidebarButtons,
206    pub diagnostic_summary: Interactive<StatusBarDiagnosticSummary>,
207    pub diagnostic_message: Interactive<ContainedText>,
208}
209
210#[derive(Deserialize, Default)]
211pub struct StatusBarSidebarButtons {
212    pub group_left: ContainerStyle,
213    pub group_right: ContainerStyle,
214    pub item: Interactive<SidebarItem>,
215    pub badge: ContainerStyle,
216}
217
218#[derive(Deserialize, Default)]
219pub struct StatusBarDiagnosticSummary {
220    pub container_ok: ContainerStyle,
221    pub container_warning: ContainerStyle,
222    pub container_error: ContainerStyle,
223    pub text: TextStyle,
224    pub icon_color_ok: Color,
225    pub icon_color_warning: Color,
226    pub icon_color_error: Color,
227    pub height: f32,
228    pub icon_width: f32,
229    pub icon_spacing: f32,
230    pub summary_spacing: f32,
231}
232
233#[derive(Deserialize, Default)]
234pub struct StatusBarLspStatus {
235    #[serde(flatten)]
236    pub container: ContainerStyle,
237    pub height: f32,
238    pub icon_spacing: f32,
239    pub icon_color: Color,
240    pub icon_width: f32,
241    pub message: TextStyle,
242}
243
244#[derive(Deserialize, Default)]
245pub struct Sidebar {
246    pub initial_size: f32,
247    #[serde(flatten)]
248    pub container: ContainerStyle,
249}
250
251#[derive(Clone, Copy, Deserialize, Default)]
252pub struct SidebarItem {
253    #[serde(flatten)]
254    pub container: ContainerStyle,
255    pub icon_color: Color,
256    pub icon_size: f32,
257}
258
259#[derive(Deserialize, Default)]
260pub struct ChatPanel {
261    #[serde(flatten)]
262    pub container: ContainerStyle,
263    pub message: ChatMessage,
264    pub pending_message: ChatMessage,
265    pub channel_select: ChannelSelect,
266    pub input_editor: FieldEditor,
267    pub sign_in_prompt: TextStyle,
268    pub hovered_sign_in_prompt: TextStyle,
269}
270
271#[derive(Deserialize, Default)]
272pub struct ProjectPanel {
273    #[serde(flatten)]
274    pub container: ContainerStyle,
275    pub entry: Interactive<ProjectPanelEntry>,
276    pub cut_entry_fade: f32,
277    pub ignored_entry_fade: f32,
278    pub filename_editor: FieldEditor,
279    pub indent_width: f32,
280}
281
282#[derive(Clone, Debug, Deserialize, Default)]
283pub struct ProjectPanelEntry {
284    pub height: f32,
285    #[serde(flatten)]
286    pub container: ContainerStyle,
287    pub text: TextStyle,
288    pub icon_color: Color,
289    pub icon_size: f32,
290    pub icon_spacing: f32,
291}
292
293#[derive(Clone, Debug, Deserialize, Default)]
294pub struct ContextMenu {
295    #[serde(flatten)]
296    pub container: ContainerStyle,
297    pub item: Interactive<ContextMenuItem>,
298    pub keystroke_margin: f32,
299    pub separator: ContainerStyle,
300}
301
302#[derive(Clone, Debug, Deserialize, Default)]
303pub struct ContextMenuItem {
304    #[serde(flatten)]
305    pub container: ContainerStyle,
306    pub label: TextStyle,
307    pub keystroke: ContainedText,
308    pub icon_width: f32,
309    pub icon_spacing: f32,
310}
311
312#[derive(Debug, Deserialize, Default)]
313pub struct CommandPalette {
314    pub key: Interactive<ContainedLabel>,
315    pub keystroke_spacing: f32,
316}
317
318#[derive(Deserialize, Default)]
319pub struct ContactsPopover {
320    pub background: Color,
321}
322
323#[derive(Deserialize, Default)]
324pub struct ContactsPanel {
325    #[serde(flatten)]
326    pub container: ContainerStyle,
327    pub user_query_editor: FieldEditor,
328    pub user_query_editor_height: f32,
329    pub add_contact_button: IconButton,
330    pub header_row: Interactive<ContainedText>,
331    pub contact_row: Interactive<ContainerStyle>,
332    pub project_row: Interactive<ProjectRow>,
333    pub row_height: f32,
334    pub contact_avatar: ImageStyle,
335    pub contact_username: ContainedText,
336    pub contact_button: Interactive<IconButton>,
337    pub contact_button_spacing: f32,
338    pub disabled_button: IconButton,
339    pub tree_branch: Interactive<TreeBranch>,
340    pub private_button: Interactive<IconButton>,
341    pub section_icon_size: f32,
342    pub invite_row: Interactive<ContainedLabel>,
343}
344
345#[derive(Deserialize, Default)]
346pub struct InviteLink {
347    #[serde(flatten)]
348    pub container: ContainerStyle,
349    #[serde(flatten)]
350    pub label: LabelStyle,
351    pub icon: Icon,
352}
353
354#[derive(Deserialize, Default, Clone, Copy)]
355pub struct TreeBranch {
356    pub width: f32,
357    pub color: Color,
358}
359
360#[derive(Deserialize, Default)]
361pub struct ContactFinder {
362    pub row_height: f32,
363    pub contact_avatar: ImageStyle,
364    pub contact_username: ContainerStyle,
365    pub contact_button: IconButton,
366    pub disabled_contact_button: IconButton,
367}
368
369#[derive(Deserialize, Default)]
370pub struct Icon {
371    #[serde(flatten)]
372    pub container: ContainerStyle,
373    pub color: Color,
374    pub width: f32,
375    pub path: String,
376}
377
378#[derive(Deserialize, Clone, Copy, Default)]
379pub struct IconButton {
380    #[serde(flatten)]
381    pub container: ContainerStyle,
382    pub color: Color,
383    pub icon_width: f32,
384    pub button_width: f32,
385}
386
387#[derive(Deserialize, Default)]
388pub struct ProjectRow {
389    #[serde(flatten)]
390    pub container: ContainerStyle,
391    pub name: ContainedText,
392    pub guests: ContainerStyle,
393    pub guest_avatar: ImageStyle,
394    pub guest_avatar_spacing: f32,
395}
396
397#[derive(Deserialize, Default)]
398pub struct ChatMessage {
399    #[serde(flatten)]
400    pub container: ContainerStyle,
401    pub body: TextStyle,
402    pub sender: ContainedText,
403    pub timestamp: ContainedText,
404}
405
406#[derive(Deserialize, Default)]
407pub struct ChannelSelect {
408    #[serde(flatten)]
409    pub container: ContainerStyle,
410    pub header: ChannelName,
411    pub item: ChannelName,
412    pub active_item: ChannelName,
413    pub hovered_item: ChannelName,
414    pub hovered_active_item: ChannelName,
415    pub menu: ContainerStyle,
416}
417
418#[derive(Deserialize, Default)]
419pub struct ChannelName {
420    #[serde(flatten)]
421    pub container: ContainerStyle,
422    pub hash: ContainedText,
423    pub name: TextStyle,
424}
425
426#[derive(Deserialize, Default)]
427pub struct Picker {
428    #[serde(flatten)]
429    pub container: ContainerStyle,
430    pub empty: ContainedLabel,
431    pub input_editor: FieldEditor,
432    pub item: Interactive<ContainedLabel>,
433}
434
435#[derive(Clone, Debug, Deserialize, Default)]
436pub struct ContainedText {
437    #[serde(flatten)]
438    pub container: ContainerStyle,
439    #[serde(flatten)]
440    pub text: TextStyle,
441}
442
443#[derive(Clone, Debug, Deserialize, Default)]
444pub struct ContainedLabel {
445    #[serde(flatten)]
446    pub container: ContainerStyle,
447    #[serde(flatten)]
448    pub label: LabelStyle,
449}
450
451#[derive(Clone, Deserialize, Default)]
452pub struct ProjectDiagnostics {
453    #[serde(flatten)]
454    pub container: ContainerStyle,
455    pub empty_message: TextStyle,
456    pub tab_icon_width: f32,
457    pub tab_icon_spacing: f32,
458    pub tab_summary_spacing: f32,
459}
460
461#[derive(Deserialize, Default)]
462pub struct ContactNotification {
463    pub header_avatar: ImageStyle,
464    pub header_message: ContainedText,
465    pub header_height: f32,
466    pub body_message: ContainedText,
467    pub button: Interactive<ContainedText>,
468    pub dismiss_button: Interactive<IconButton>,
469}
470
471#[derive(Deserialize, Default)]
472pub struct UpdateNotification {
473    pub message: ContainedText,
474    pub action_message: Interactive<ContainedText>,
475    pub dismiss_button: Interactive<IconButton>,
476}
477
478#[derive(Clone, Deserialize, Default)]
479pub struct Editor {
480    pub text_color: Color,
481    #[serde(default)]
482    pub background: Color,
483    pub selection: SelectionStyle,
484    pub gutter_background: Color,
485    pub gutter_padding_factor: f32,
486    pub active_line_background: Color,
487    pub highlighted_line_background: Color,
488    pub rename_fade: f32,
489    pub document_highlight_read_background: Color,
490    pub document_highlight_write_background: Color,
491    pub diff_background_deleted: Color,
492    pub diff_background_inserted: Color,
493    pub diff_background_modified: 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}