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