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