theme.rs

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