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