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    pub context_menu: ContextMenu,
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    pub width: f32,
246    #[serde(flatten)]
247    pub container: ContainerStyle,
248    pub label: TextStyle,
249}
250
251#[derive(Debug, Deserialize, Default)]
252pub struct CommandPalette {
253    pub key: Interactive<ContainedLabel>,
254    pub keystroke_spacing: f32,
255}
256
257#[derive(Deserialize, Default)]
258pub struct ContactsPanel {
259    #[serde(flatten)]
260    pub container: ContainerStyle,
261    pub user_query_editor: FieldEditor,
262    pub user_query_editor_height: f32,
263    pub add_contact_button: IconButton,
264    pub header_row: Interactive<ContainedText>,
265    pub contact_row: Interactive<ContainerStyle>,
266    pub project_row: Interactive<ProjectRow>,
267    pub row_height: f32,
268    pub contact_avatar: ImageStyle,
269    pub contact_username: ContainedText,
270    pub contact_button: Interactive<IconButton>,
271    pub contact_button_spacing: f32,
272    pub disabled_contact_button: IconButton,
273    pub tree_branch: Interactive<TreeBranch>,
274    pub section_icon_size: f32,
275    pub invite_row: Interactive<ContainedLabel>,
276}
277
278#[derive(Deserialize, Default)]
279pub struct InviteLink {
280    #[serde(flatten)]
281    pub container: ContainerStyle,
282    #[serde(flatten)]
283    pub label: LabelStyle,
284    pub icon: Icon,
285}
286
287#[derive(Deserialize, Default, Clone, Copy)]
288pub struct TreeBranch {
289    pub width: f32,
290    pub color: Color,
291}
292
293#[derive(Deserialize, Default)]
294pub struct ContactFinder {
295    pub row_height: f32,
296    pub contact_avatar: ImageStyle,
297    pub contact_username: ContainerStyle,
298    pub contact_button: IconButton,
299    pub disabled_contact_button: IconButton,
300}
301
302#[derive(Deserialize, Default)]
303pub struct Icon {
304    #[serde(flatten)]
305    pub container: ContainerStyle,
306    pub color: Color,
307    pub width: f32,
308    pub path: String,
309}
310
311#[derive(Deserialize, Default)]
312pub struct IconButton {
313    #[serde(flatten)]
314    pub container: ContainerStyle,
315    pub color: Color,
316    pub icon_width: f32,
317    pub button_width: f32,
318}
319
320#[derive(Deserialize, Default)]
321pub struct ProjectRow {
322    #[serde(flatten)]
323    pub container: ContainerStyle,
324    pub name: ContainedText,
325    pub guests: ContainerStyle,
326    pub guest_avatar: ImageStyle,
327    pub guest_avatar_spacing: f32,
328}
329
330#[derive(Deserialize, Default)]
331pub struct ChatMessage {
332    #[serde(flatten)]
333    pub container: ContainerStyle,
334    pub body: TextStyle,
335    pub sender: ContainedText,
336    pub timestamp: ContainedText,
337}
338
339#[derive(Deserialize, Default)]
340pub struct ChannelSelect {
341    #[serde(flatten)]
342    pub container: ContainerStyle,
343    pub header: ChannelName,
344    pub item: ChannelName,
345    pub active_item: ChannelName,
346    pub hovered_item: ChannelName,
347    pub hovered_active_item: ChannelName,
348    pub menu: ContainerStyle,
349}
350
351#[derive(Deserialize, Default)]
352pub struct ChannelName {
353    #[serde(flatten)]
354    pub container: ContainerStyle,
355    pub hash: ContainedText,
356    pub name: TextStyle,
357}
358
359#[derive(Deserialize, Default)]
360pub struct Picker {
361    #[serde(flatten)]
362    pub container: ContainerStyle,
363    pub empty: ContainedLabel,
364    pub input_editor: FieldEditor,
365    pub item: Interactive<ContainedLabel>,
366}
367
368#[derive(Clone, Debug, Deserialize, Default)]
369pub struct ContainedText {
370    #[serde(flatten)]
371    pub container: ContainerStyle,
372    #[serde(flatten)]
373    pub text: TextStyle,
374}
375
376#[derive(Clone, Debug, Deserialize, Default)]
377pub struct ContainedLabel {
378    #[serde(flatten)]
379    pub container: ContainerStyle,
380    #[serde(flatten)]
381    pub label: LabelStyle,
382}
383
384#[derive(Clone, Deserialize, Default)]
385pub struct ProjectDiagnostics {
386    #[serde(flatten)]
387    pub container: ContainerStyle,
388    pub empty_message: TextStyle,
389    pub tab_icon_width: f32,
390    pub tab_icon_spacing: f32,
391    pub tab_summary_spacing: f32,
392}
393
394#[derive(Deserialize, Default)]
395pub struct ContactNotification {
396    pub header_avatar: ImageStyle,
397    pub header_message: ContainedText,
398    pub header_height: f32,
399    pub body_message: ContainedText,
400    pub button: Interactive<ContainedText>,
401    pub dismiss_button: Interactive<IconButton>,
402}
403
404#[derive(Clone, Deserialize, Default)]
405pub struct Editor {
406    pub text_color: Color,
407    #[serde(default)]
408    pub background: Color,
409    pub selection: SelectionStyle,
410    pub gutter_background: Color,
411    pub gutter_padding_factor: f32,
412    pub active_line_background: Color,
413    pub highlighted_line_background: Color,
414    pub rename_fade: f32,
415    pub document_highlight_read_background: Color,
416    pub document_highlight_write_background: Color,
417    pub diff_background_deleted: Color,
418    pub diff_background_inserted: Color,
419    pub line_number: Color,
420    pub line_number_active: Color,
421    pub guest_selections: Vec<SelectionStyle>,
422    pub syntax: Arc<SyntaxTheme>,
423    pub diagnostic_path_header: DiagnosticPathHeader,
424    pub diagnostic_header: DiagnosticHeader,
425    pub error_diagnostic: DiagnosticStyle,
426    pub invalid_error_diagnostic: DiagnosticStyle,
427    pub warning_diagnostic: DiagnosticStyle,
428    pub invalid_warning_diagnostic: DiagnosticStyle,
429    pub information_diagnostic: DiagnosticStyle,
430    pub invalid_information_diagnostic: DiagnosticStyle,
431    pub hint_diagnostic: DiagnosticStyle,
432    pub invalid_hint_diagnostic: DiagnosticStyle,
433    pub autocomplete: AutocompleteStyle,
434    pub code_actions_indicator: Color,
435    pub unnecessary_code_fade: f32,
436}
437
438#[derive(Clone, Deserialize, Default)]
439pub struct DiagnosticPathHeader {
440    #[serde(flatten)]
441    pub container: ContainerStyle,
442    pub filename: ContainedText,
443    pub path: ContainedText,
444    pub text_scale_factor: f32,
445}
446
447#[derive(Clone, Deserialize, Default)]
448pub struct DiagnosticHeader {
449    #[serde(flatten)]
450    pub container: ContainerStyle,
451    pub message: ContainedLabel,
452    pub code: ContainedText,
453    pub text_scale_factor: f32,
454    pub icon_width_factor: f32,
455}
456
457#[derive(Clone, Deserialize, Default)]
458pub struct DiagnosticStyle {
459    pub message: LabelStyle,
460    #[serde(default)]
461    pub header: ContainerStyle,
462    pub text_scale_factor: f32,
463}
464
465#[derive(Clone, Deserialize, Default)]
466pub struct AutocompleteStyle {
467    #[serde(flatten)]
468    pub container: ContainerStyle,
469    pub item: ContainerStyle,
470    pub selected_item: ContainerStyle,
471    pub hovered_item: ContainerStyle,
472    pub match_highlight: HighlightStyle,
473}
474
475#[derive(Clone, Copy, Default, Deserialize)]
476pub struct SelectionStyle {
477    pub cursor: Color,
478    pub selection: Color,
479}
480
481#[derive(Clone, Deserialize, Default)]
482pub struct FieldEditor {
483    #[serde(flatten)]
484    pub container: ContainerStyle,
485    pub text: TextStyle,
486    #[serde(default)]
487    pub placeholder_text: Option<TextStyle>,
488    pub selection: SelectionStyle,
489}
490
491#[derive(Debug, Default, Clone, Copy)]
492pub struct Interactive<T> {
493    pub default: T,
494    pub hover: Option<T>,
495    pub active: Option<T>,
496    pub active_hover: Option<T>,
497}
498
499impl<T> Interactive<T> {
500    pub fn style_for(&self, state: &MouseState, active: bool) -> &T {
501        if active {
502            if state.hovered {
503                self.active_hover
504                    .as_ref()
505                    .or(self.active.as_ref())
506                    .unwrap_or(&self.default)
507            } else {
508                self.active.as_ref().unwrap_or(&self.default)
509            }
510        } else {
511            if state.hovered {
512                self.hover.as_ref().unwrap_or(&self.default)
513            } else {
514                &self.default
515            }
516        }
517    }
518}
519
520impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
521    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
522    where
523        D: serde::Deserializer<'de>,
524    {
525        #[derive(Deserialize)]
526        struct Helper {
527            #[serde(flatten)]
528            default: Value,
529            hover: Option<Value>,
530            active: Option<Value>,
531            active_hover: Option<Value>,
532        }
533
534        let json = Helper::deserialize(deserializer)?;
535
536        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
537            if let Some(mut state_json) = state_json {
538                if let Value::Object(state_json) = &mut state_json {
539                    if let Value::Object(default) = &json.default {
540                        for (key, value) in default {
541                            if !state_json.contains_key(key) {
542                                state_json.insert(key.clone(), value.clone());
543                            }
544                        }
545                    }
546                }
547                Ok(Some(
548                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
549                ))
550            } else {
551                Ok(None)
552            }
553        };
554
555        let hover = deserialize_state(json.hover)?;
556        let active = deserialize_state(json.active)?;
557        let active_hover = deserialize_state(json.active_hover)?;
558        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
559
560        Ok(Interactive {
561            default,
562            hover,
563            active,
564            active_hover,
565        })
566    }
567}
568
569impl Editor {
570    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
571        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
572        if style_ix == 0 {
573            &self.selection
574        } else {
575            &self.guest_selections[style_ix - 1]
576        }
577    }
578}
579
580#[derive(Default)]
581pub struct SyntaxTheme {
582    pub highlights: Vec<(String, HighlightStyle)>,
583}
584
585impl SyntaxTheme {
586    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
587        Self { highlights }
588    }
589}
590
591impl<'de> Deserialize<'de> for SyntaxTheme {
592    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
593    where
594        D: serde::Deserializer<'de>,
595    {
596        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
597
598        let mut result = Self::new(Vec::new());
599        for (key, style) in syntax_data {
600            match result
601                .highlights
602                .binary_search_by(|(needle, _)| needle.cmp(&key))
603            {
604                Ok(i) | Err(i) => {
605                    result.highlights.insert(i, (key, style));
606                }
607            }
608        }
609
610        Ok(result)
611    }
612}