theme.rs

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