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 link_definition: HighlightStyle,
450    pub jump_icon: Interactive<IconButton>,
451}
452
453#[derive(Clone, Deserialize, Default)]
454pub struct DiagnosticPathHeader {
455    #[serde(flatten)]
456    pub container: ContainerStyle,
457    pub filename: ContainedText,
458    pub path: ContainedText,
459    pub text_scale_factor: f32,
460}
461
462#[derive(Clone, Deserialize, Default)]
463pub struct DiagnosticHeader {
464    #[serde(flatten)]
465    pub container: ContainerStyle,
466    pub message: ContainedLabel,
467    pub code: ContainedText,
468    pub text_scale_factor: f32,
469    pub icon_width_factor: f32,
470}
471
472#[derive(Clone, Deserialize, Default)]
473pub struct DiagnosticStyle {
474    pub message: LabelStyle,
475    #[serde(default)]
476    pub header: ContainerStyle,
477    pub text_scale_factor: f32,
478}
479
480#[derive(Clone, Deserialize, Default)]
481pub struct AutocompleteStyle {
482    #[serde(flatten)]
483    pub container: ContainerStyle,
484    pub item: ContainerStyle,
485    pub selected_item: ContainerStyle,
486    pub hovered_item: ContainerStyle,
487    pub match_highlight: HighlightStyle,
488}
489
490#[derive(Clone, Copy, Default, Deserialize)]
491pub struct SelectionStyle {
492    pub cursor: Color,
493    pub selection: Color,
494}
495
496#[derive(Clone, Deserialize, Default)]
497pub struct FieldEditor {
498    #[serde(flatten)]
499    pub container: ContainerStyle,
500    pub text: TextStyle,
501    #[serde(default)]
502    pub placeholder_text: Option<TextStyle>,
503    pub selection: SelectionStyle,
504}
505
506#[derive(Debug, Default, Clone, Copy)]
507pub struct Interactive<T> {
508    pub default: T,
509    pub hover: Option<T>,
510    pub active: Option<T>,
511    pub active_hover: Option<T>,
512}
513
514impl<T> Interactive<T> {
515    pub fn style_for(&self, state: MouseState, active: bool) -> &T {
516        if active {
517            if state.hovered {
518                self.active_hover
519                    .as_ref()
520                    .or(self.active.as_ref())
521                    .unwrap_or(&self.default)
522            } else {
523                self.active.as_ref().unwrap_or(&self.default)
524            }
525        } else {
526            if state.hovered {
527                self.hover.as_ref().unwrap_or(&self.default)
528            } else {
529                &self.default
530            }
531        }
532    }
533}
534
535impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
536    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
537    where
538        D: serde::Deserializer<'de>,
539    {
540        #[derive(Deserialize)]
541        struct Helper {
542            #[serde(flatten)]
543            default: Value,
544            hover: Option<Value>,
545            active: Option<Value>,
546            active_hover: Option<Value>,
547        }
548
549        let json = Helper::deserialize(deserializer)?;
550
551        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
552            if let Some(mut state_json) = state_json {
553                if let Value::Object(state_json) = &mut state_json {
554                    if let Value::Object(default) = &json.default {
555                        for (key, value) in default {
556                            if !state_json.contains_key(key) {
557                                state_json.insert(key.clone(), value.clone());
558                            }
559                        }
560                    }
561                }
562                Ok(Some(
563                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
564                ))
565            } else {
566                Ok(None)
567            }
568        };
569
570        let hover = deserialize_state(json.hover)?;
571        let active = deserialize_state(json.active)?;
572        let active_hover = deserialize_state(json.active_hover)?;
573        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
574
575        Ok(Interactive {
576            default,
577            hover,
578            active,
579            active_hover,
580        })
581    }
582}
583
584impl Editor {
585    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
586        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
587        if style_ix == 0 {
588            &self.selection
589        } else {
590            &self.guest_selections[style_ix - 1]
591        }
592    }
593}
594
595#[derive(Default)]
596pub struct SyntaxTheme {
597    pub highlights: Vec<(String, HighlightStyle)>,
598}
599
600impl SyntaxTheme {
601    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
602        Self { highlights }
603    }
604}
605
606impl<'de> Deserialize<'de> for SyntaxTheme {
607    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
608    where
609        D: serde::Deserializer<'de>,
610    {
611        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
612
613        let mut result = Self::new(Vec::new());
614        for (key, style) in syntax_data {
615            match result
616                .highlights
617                .binary_search_by(|(needle, _)| needle.cmp(&key))
618            {
619                Ok(i) | Err(i) => {
620                    result.highlights.insert(i, (key, style));
621                }
622            }
623        }
624
625        Ok(result)
626    }
627}
628
629#[derive(Clone, Deserialize, Default)]
630pub struct HoverPopover {
631    pub container: ContainerStyle,
632    pub block_style: ContainerStyle,
633    pub prose: TextStyle,
634    pub highlight: Color,
635}