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