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 feedback: Interactive<TextStyle>,
161    pub sidebar_buttons: StatusBarSidebarButtons,
162    pub diagnostic_summary: Interactive<StatusBarDiagnosticSummary>,
163    pub diagnostic_message: Interactive<ContainedText>,
164}
165
166#[derive(Deserialize, Default)]
167pub struct StatusBarSidebarButtons {
168    pub group_left: ContainerStyle,
169    pub group_right: ContainerStyle,
170    pub item: Interactive<SidebarItem>,
171    pub badge: ContainerStyle,
172}
173
174#[derive(Deserialize, Default)]
175pub struct StatusBarDiagnosticSummary {
176    pub container_ok: ContainerStyle,
177    pub container_warning: ContainerStyle,
178    pub container_error: ContainerStyle,
179    pub text: TextStyle,
180    pub icon_color_ok: Color,
181    pub icon_color_warning: Color,
182    pub icon_color_error: Color,
183    pub height: f32,
184    pub icon_width: f32,
185    pub icon_spacing: f32,
186    pub summary_spacing: f32,
187}
188
189#[derive(Deserialize, Default)]
190pub struct StatusBarLspStatus {
191    #[serde(flatten)]
192    pub container: ContainerStyle,
193    pub height: f32,
194    pub icon_spacing: f32,
195    pub icon_color: Color,
196    pub icon_width: f32,
197    pub message: TextStyle,
198}
199
200#[derive(Deserialize, Default)]
201pub struct Sidebar {
202    pub resize_handle: ContainerStyle,
203}
204
205#[derive(Clone, Copy, Deserialize, Default)]
206pub struct SidebarItem {
207    #[serde(flatten)]
208    pub container: ContainerStyle,
209    pub icon_color: Color,
210    pub icon_size: f32,
211}
212
213#[derive(Deserialize, Default)]
214pub struct ChatPanel {
215    #[serde(flatten)]
216    pub container: ContainerStyle,
217    pub message: ChatMessage,
218    pub pending_message: ChatMessage,
219    pub channel_select: ChannelSelect,
220    pub input_editor: FieldEditor,
221    pub sign_in_prompt: TextStyle,
222    pub hovered_sign_in_prompt: TextStyle,
223}
224
225#[derive(Deserialize, Default)]
226pub struct ProjectPanel {
227    #[serde(flatten)]
228    pub container: ContainerStyle,
229    pub entry: Interactive<ProjectPanelEntry>,
230    pub cut_entry_fade: f32,
231    pub ignored_entry_fade: f32,
232    pub filename_editor: FieldEditor,
233    pub indent_width: f32,
234}
235
236#[derive(Clone, Debug, Deserialize, Default)]
237pub struct ProjectPanelEntry {
238    pub height: f32,
239    #[serde(flatten)]
240    pub container: ContainerStyle,
241    pub text: TextStyle,
242    pub icon_color: Color,
243    pub icon_size: f32,
244    pub icon_spacing: f32,
245}
246
247#[derive(Clone, Debug, Deserialize, Default)]
248pub struct ContextMenu {
249    #[serde(flatten)]
250    pub container: ContainerStyle,
251    pub item: Interactive<ContextMenuItem>,
252    pub separator: ContainerStyle,
253}
254
255#[derive(Clone, Debug, Deserialize, Default)]
256pub struct ContextMenuItem {
257    #[serde(flatten)]
258    pub container: ContainerStyle,
259    pub label: TextStyle,
260    pub keystroke: ContainedText,
261}
262
263#[derive(Debug, Deserialize, Default)]
264pub struct CommandPalette {
265    pub key: Interactive<ContainedLabel>,
266    pub keystroke_spacing: f32,
267}
268
269#[derive(Deserialize, Default)]
270pub struct ContactsPanel {
271    #[serde(flatten)]
272    pub container: ContainerStyle,
273    pub user_query_editor: FieldEditor,
274    pub user_query_editor_height: f32,
275    pub add_contact_button: IconButton,
276    pub header_row: Interactive<ContainedText>,
277    pub contact_row: Interactive<ContainerStyle>,
278    pub project_row: Interactive<ProjectRow>,
279    pub row_height: f32,
280    pub contact_avatar: ImageStyle,
281    pub contact_username: ContainedText,
282    pub contact_button: Interactive<IconButton>,
283    pub contact_button_spacing: f32,
284    pub disabled_button: IconButton,
285    pub tree_branch: Interactive<TreeBranch>,
286    pub private_button: Interactive<IconButton>,
287    pub section_icon_size: f32,
288    pub invite_row: Interactive<ContainedLabel>,
289}
290
291#[derive(Deserialize, Default)]
292pub struct InviteLink {
293    #[serde(flatten)]
294    pub container: ContainerStyle,
295    #[serde(flatten)]
296    pub label: LabelStyle,
297    pub icon: Icon,
298}
299
300#[derive(Deserialize, Default, Clone, Copy)]
301pub struct TreeBranch {
302    pub width: f32,
303    pub color: Color,
304}
305
306#[derive(Deserialize, Default)]
307pub struct ContactFinder {
308    pub row_height: f32,
309    pub contact_avatar: ImageStyle,
310    pub contact_username: ContainerStyle,
311    pub contact_button: IconButton,
312    pub disabled_contact_button: IconButton,
313}
314
315#[derive(Deserialize, Default)]
316pub struct Icon {
317    #[serde(flatten)]
318    pub container: ContainerStyle,
319    pub color: Color,
320    pub width: f32,
321    pub path: String,
322}
323
324#[derive(Deserialize, Clone, Copy, Default)]
325pub struct IconButton {
326    #[serde(flatten)]
327    pub container: ContainerStyle,
328    pub color: Color,
329    pub icon_width: f32,
330    pub button_width: f32,
331}
332
333#[derive(Deserialize, Default)]
334pub struct ProjectRow {
335    #[serde(flatten)]
336    pub container: ContainerStyle,
337    pub name: ContainedText,
338    pub guests: ContainerStyle,
339    pub guest_avatar: ImageStyle,
340    pub guest_avatar_spacing: f32,
341}
342
343#[derive(Deserialize, Default)]
344pub struct ChatMessage {
345    #[serde(flatten)]
346    pub container: ContainerStyle,
347    pub body: TextStyle,
348    pub sender: ContainedText,
349    pub timestamp: ContainedText,
350}
351
352#[derive(Deserialize, Default)]
353pub struct ChannelSelect {
354    #[serde(flatten)]
355    pub container: ContainerStyle,
356    pub header: ChannelName,
357    pub item: ChannelName,
358    pub active_item: ChannelName,
359    pub hovered_item: ChannelName,
360    pub hovered_active_item: ChannelName,
361    pub menu: ContainerStyle,
362}
363
364#[derive(Deserialize, Default)]
365pub struct ChannelName {
366    #[serde(flatten)]
367    pub container: ContainerStyle,
368    pub hash: ContainedText,
369    pub name: TextStyle,
370}
371
372#[derive(Deserialize, Default)]
373pub struct Picker {
374    #[serde(flatten)]
375    pub container: ContainerStyle,
376    pub empty: ContainedLabel,
377    pub input_editor: FieldEditor,
378    pub item: Interactive<ContainedLabel>,
379}
380
381#[derive(Clone, Debug, Deserialize, Default)]
382pub struct ContainedText {
383    #[serde(flatten)]
384    pub container: ContainerStyle,
385    #[serde(flatten)]
386    pub text: TextStyle,
387}
388
389#[derive(Clone, Debug, Deserialize, Default)]
390pub struct ContainedLabel {
391    #[serde(flatten)]
392    pub container: ContainerStyle,
393    #[serde(flatten)]
394    pub label: LabelStyle,
395}
396
397#[derive(Clone, Deserialize, Default)]
398pub struct ProjectDiagnostics {
399    #[serde(flatten)]
400    pub container: ContainerStyle,
401    pub empty_message: TextStyle,
402    pub tab_icon_width: f32,
403    pub tab_icon_spacing: f32,
404    pub tab_summary_spacing: f32,
405}
406
407#[derive(Deserialize, Default)]
408pub struct ContactNotification {
409    pub header_avatar: ImageStyle,
410    pub header_message: ContainedText,
411    pub header_height: f32,
412    pub body_message: ContainedText,
413    pub button: Interactive<ContainedText>,
414    pub dismiss_button: Interactive<IconButton>,
415}
416
417#[derive(Deserialize, Default)]
418pub struct UpdateNotification {
419    pub message: ContainedText,
420    pub action_message: Interactive<ContainedText>,
421    pub dismiss_button: Interactive<IconButton>,
422}
423
424#[derive(Clone, Deserialize, Default)]
425pub struct Editor {
426    pub text_color: Color,
427    #[serde(default)]
428    pub background: Color,
429    pub selection: SelectionStyle,
430    pub gutter_background: Color,
431    pub gutter_padding_factor: f32,
432    pub active_line_background: Color,
433    pub highlighted_line_background: Color,
434    pub rename_fade: f32,
435    pub document_highlight_read_background: Color,
436    pub document_highlight_write_background: Color,
437    pub diff_background_deleted: Color,
438    pub diff_background_inserted: Color,
439    pub line_number: Color,
440    pub line_number_active: Color,
441    pub guest_selections: Vec<SelectionStyle>,
442    pub syntax: Arc<SyntaxTheme>,
443    pub diagnostic_path_header: DiagnosticPathHeader,
444    pub diagnostic_header: DiagnosticHeader,
445    pub error_diagnostic: DiagnosticStyle,
446    pub invalid_error_diagnostic: DiagnosticStyle,
447    pub warning_diagnostic: DiagnosticStyle,
448    pub invalid_warning_diagnostic: DiagnosticStyle,
449    pub information_diagnostic: DiagnosticStyle,
450    pub invalid_information_diagnostic: DiagnosticStyle,
451    pub hint_diagnostic: DiagnosticStyle,
452    pub invalid_hint_diagnostic: DiagnosticStyle,
453    pub autocomplete: AutocompleteStyle,
454    pub code_actions_indicator: Color,
455    pub unnecessary_code_fade: f32,
456    pub hover_popover: HoverPopover,
457}
458
459#[derive(Clone, Deserialize, Default)]
460pub struct DiagnosticPathHeader {
461    #[serde(flatten)]
462    pub container: ContainerStyle,
463    pub filename: ContainedText,
464    pub path: ContainedText,
465    pub text_scale_factor: f32,
466}
467
468#[derive(Clone, Deserialize, Default)]
469pub struct DiagnosticHeader {
470    #[serde(flatten)]
471    pub container: ContainerStyle,
472    pub message: ContainedLabel,
473    pub code: ContainedText,
474    pub text_scale_factor: f32,
475    pub icon_width_factor: f32,
476    pub jump_icon: Interactive<IconButton>,
477}
478
479#[derive(Clone, Deserialize, Default)]
480pub struct DiagnosticStyle {
481    pub message: LabelStyle,
482    #[serde(default)]
483    pub header: ContainerStyle,
484    pub text_scale_factor: f32,
485}
486
487#[derive(Clone, Deserialize, Default)]
488pub struct AutocompleteStyle {
489    #[serde(flatten)]
490    pub container: ContainerStyle,
491    pub item: ContainerStyle,
492    pub selected_item: ContainerStyle,
493    pub hovered_item: ContainerStyle,
494    pub match_highlight: HighlightStyle,
495}
496
497#[derive(Clone, Copy, Default, Deserialize)]
498pub struct SelectionStyle {
499    pub cursor: Color,
500    pub selection: Color,
501}
502
503#[derive(Clone, Deserialize, Default)]
504pub struct FieldEditor {
505    #[serde(flatten)]
506    pub container: ContainerStyle,
507    pub text: TextStyle,
508    #[serde(default)]
509    pub placeholder_text: Option<TextStyle>,
510    pub selection: SelectionStyle,
511}
512
513#[derive(Debug, Default, Clone, Copy)]
514pub struct Interactive<T> {
515    pub default: T,
516    pub hover: Option<T>,
517    pub active: Option<T>,
518    pub active_hover: Option<T>,
519}
520
521impl<T> Interactive<T> {
522    pub fn style_for(&self, state: MouseState, active: bool) -> &T {
523        if active {
524            if state.hovered {
525                self.active_hover
526                    .as_ref()
527                    .or(self.active.as_ref())
528                    .unwrap_or(&self.default)
529            } else {
530                self.active.as_ref().unwrap_or(&self.default)
531            }
532        } else {
533            if state.hovered {
534                self.hover.as_ref().unwrap_or(&self.default)
535            } else {
536                &self.default
537            }
538        }
539    }
540}
541
542impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
543    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
544    where
545        D: serde::Deserializer<'de>,
546    {
547        #[derive(Deserialize)]
548        struct Helper {
549            #[serde(flatten)]
550            default: Value,
551            hover: Option<Value>,
552            active: Option<Value>,
553            active_hover: Option<Value>,
554        }
555
556        let json = Helper::deserialize(deserializer)?;
557
558        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
559            if let Some(mut state_json) = state_json {
560                if let Value::Object(state_json) = &mut state_json {
561                    if let Value::Object(default) = &json.default {
562                        for (key, value) in default {
563                            if !state_json.contains_key(key) {
564                                state_json.insert(key.clone(), value.clone());
565                            }
566                        }
567                    }
568                }
569                Ok(Some(
570                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
571                ))
572            } else {
573                Ok(None)
574            }
575        };
576
577        let hover = deserialize_state(json.hover)?;
578        let active = deserialize_state(json.active)?;
579        let active_hover = deserialize_state(json.active_hover)?;
580        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
581
582        Ok(Interactive {
583            default,
584            hover,
585            active,
586            active_hover,
587        })
588    }
589}
590
591impl Editor {
592    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
593        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
594        if style_ix == 0 {
595            &self.selection
596        } else {
597            &self.guest_selections[style_ix - 1]
598        }
599    }
600}
601
602#[derive(Default)]
603pub struct SyntaxTheme {
604    pub highlights: Vec<(String, HighlightStyle)>,
605}
606
607impl SyntaxTheme {
608    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
609        Self { highlights }
610    }
611}
612
613impl<'de> Deserialize<'de> for SyntaxTheme {
614    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
615    where
616        D: serde::Deserializer<'de>,
617    {
618        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
619
620        let mut result = Self::new(Vec::new());
621        for (key, style) in syntax_data {
622            match result
623                .highlights
624                .binary_search_by(|(needle, _)| needle.cmp(&key))
625            {
626                Ok(i) | Err(i) => {
627                    result.highlights.insert(i, (key, style));
628                }
629            }
630        }
631
632        Ok(result)
633    }
634}
635
636#[derive(Clone, Deserialize, Default)]
637pub struct HoverPopover {
638    pub container: ContainerStyle,
639    pub block_style: ContainerStyle,
640    pub prose: TextStyle,
641    pub highlight: Color,
642}