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