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 shared_project_row: Interactive<ProjectRow>,
255    pub unshared_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}