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