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