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