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