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