theme.rs

  1mod theme_registry;
  2
  3use gpui::{
  4    color::Color,
  5    elements::{ContainerStyle, ImageStyle, LabelStyle, TooltipStyle},
  6    fonts::{HighlightStyle, TextStyle},
  7    Border, MouseState,
  8};
  9use serde::{de::DeserializeOwned, Deserialize};
 10use serde_json::Value;
 11use std::{collections::HashMap, sync::Arc};
 12
 13pub use theme_registry::*;
 14
 15#[derive(Deserialize, Default)]
 16pub struct Theme {
 17    #[serde(default)]
 18    pub name: String,
 19    pub workspace: Workspace,
 20    pub context_menu: ContextMenu,
 21    pub chat_panel: ChatPanel,
 22    pub contacts_panel: ContactsPanel,
 23    pub contact_finder: ContactFinder,
 24    pub project_panel: ProjectPanel,
 25    pub command_palette: CommandPalette,
 26    pub picker: Picker,
 27    pub editor: Editor,
 28    pub search: Search,
 29    pub project_diagnostics: ProjectDiagnostics,
 30    pub breadcrumbs: ContainedText,
 31    pub contact_notification: ContactNotification,
 32    pub update_notification: UpdateNotification,
 33    pub tooltip: TooltipStyle,
 34    pub terminal: TerminalStyle,
 35}
 36
 37#[derive(Deserialize, Default)]
 38pub struct Workspace {
 39    pub background: Color,
 40    pub titlebar: Titlebar,
 41    pub tab: Tab,
 42    pub active_tab: Tab,
 43    pub pane_divider: Border,
 44    pub leader_border_opacity: f32,
 45    pub leader_border_width: f32,
 46    pub sidebar_resize_handle: ContainerStyle,
 47    pub status_bar: StatusBar,
 48    pub toolbar: Toolbar,
 49    pub disconnected_overlay: ContainedText,
 50    pub modal: ContainerStyle,
 51    pub notification: ContainerStyle,
 52    pub notifications: Notifications,
 53    pub joining_project_avatar: ImageStyle,
 54    pub joining_project_message: ContainedText,
 55}
 56
 57#[derive(Clone, Deserialize, Default)]
 58pub struct Titlebar {
 59    #[serde(flatten)]
 60    pub container: ContainerStyle,
 61    pub height: f32,
 62    pub title: TextStyle,
 63    pub avatar_width: f32,
 64    pub avatar_margin: f32,
 65    pub avatar_ribbon: AvatarRibbon,
 66    pub offline_icon: OfflineIcon,
 67    pub avatar: ImageStyle,
 68    pub sign_in_prompt: Interactive<ContainedText>,
 69    pub outdated_warning: ContainedText,
 70}
 71
 72#[derive(Clone, Deserialize, Default)]
 73pub struct AvatarRibbon {
 74    #[serde(flatten)]
 75    pub container: ContainerStyle,
 76    pub width: f32,
 77    pub height: f32,
 78}
 79
 80#[derive(Clone, Deserialize, Default)]
 81pub struct OfflineIcon {
 82    #[serde(flatten)]
 83    pub container: ContainerStyle,
 84    pub width: f32,
 85    pub color: Color,
 86}
 87
 88#[derive(Clone, Deserialize, Default)]
 89pub struct Tab {
 90    pub height: f32,
 91    #[serde(flatten)]
 92    pub container: ContainerStyle,
 93    #[serde(flatten)]
 94    pub label: LabelStyle,
 95    pub spacing: f32,
 96    pub icon_width: f32,
 97    pub icon_close: Color,
 98    pub icon_close_active: Color,
 99    pub icon_dirty: Color,
100    pub icon_conflict: Color,
101}
102
103#[derive(Clone, Deserialize, Default)]
104pub struct Toolbar {
105    #[serde(flatten)]
106    pub container: ContainerStyle,
107    pub height: f32,
108    pub item_spacing: f32,
109    pub nav_button: Interactive<IconButton>,
110}
111
112#[derive(Clone, Deserialize, Default)]
113pub struct Notifications {
114    #[serde(flatten)]
115    pub container: ContainerStyle,
116    pub width: f32,
117}
118
119#[derive(Clone, Deserialize, Default)]
120pub struct Search {
121    #[serde(flatten)]
122    pub container: ContainerStyle,
123    pub editor: FindEditor,
124    pub invalid_editor: ContainerStyle,
125    pub option_button_group: ContainerStyle,
126    pub option_button: Interactive<ContainedText>,
127    pub match_background: Color,
128    pub match_index: ContainedText,
129    pub results_status: TextStyle,
130    pub tab_icon_width: f32,
131    pub tab_icon_spacing: f32,
132}
133
134#[derive(Clone, Deserialize, Default)]
135pub struct FindEditor {
136    #[serde(flatten)]
137    pub input: FieldEditor,
138    pub min_width: f32,
139    pub max_width: f32,
140}
141
142#[derive(Deserialize, Default)]
143pub struct StatusBar {
144    #[serde(flatten)]
145    pub container: ContainerStyle,
146    pub height: f32,
147    pub item_spacing: f32,
148    pub cursor_position: TextStyle,
149    pub auto_update_progress_message: TextStyle,
150    pub auto_update_done_message: TextStyle,
151    pub lsp_status: Interactive<StatusBarLspStatus>,
152    pub feedback: Interactive<TextStyle>,
153    pub sidebar_buttons: StatusBarSidebarButtons,
154    pub diagnostic_summary: Interactive<StatusBarDiagnosticSummary>,
155    pub diagnostic_message: Interactive<ContainedText>,
156}
157
158#[derive(Deserialize, Default)]
159pub struct StatusBarSidebarButtons {
160    pub group_left: ContainerStyle,
161    pub group_right: ContainerStyle,
162    pub item: Interactive<SidebarItem>,
163    pub badge: ContainerStyle,
164}
165
166#[derive(Deserialize, Default)]
167pub struct StatusBarDiagnosticSummary {
168    pub container_ok: ContainerStyle,
169    pub container_warning: ContainerStyle,
170    pub container_error: ContainerStyle,
171    pub text: TextStyle,
172    pub icon_color_ok: Color,
173    pub icon_color_warning: Color,
174    pub icon_color_error: Color,
175    pub height: f32,
176    pub icon_width: f32,
177    pub icon_spacing: f32,
178    pub summary_spacing: f32,
179}
180
181#[derive(Deserialize, Default)]
182pub struct StatusBarLspStatus {
183    #[serde(flatten)]
184    pub container: ContainerStyle,
185    pub height: f32,
186    pub icon_spacing: f32,
187    pub icon_color: Color,
188    pub icon_width: f32,
189    pub message: TextStyle,
190}
191
192#[derive(Deserialize, Default)]
193pub struct Sidebar {
194    pub resize_handle: ContainerStyle,
195}
196
197#[derive(Clone, Copy, Deserialize, Default)]
198pub struct SidebarItem {
199    #[serde(flatten)]
200    pub container: ContainerStyle,
201    pub icon_color: Color,
202    pub icon_size: f32,
203}
204
205#[derive(Deserialize, Default)]
206pub struct ChatPanel {
207    #[serde(flatten)]
208    pub container: ContainerStyle,
209    pub message: ChatMessage,
210    pub pending_message: ChatMessage,
211    pub channel_select: ChannelSelect,
212    pub input_editor: FieldEditor,
213    pub sign_in_prompt: TextStyle,
214    pub hovered_sign_in_prompt: TextStyle,
215}
216
217#[derive(Deserialize, Default)]
218pub struct ProjectPanel {
219    #[serde(flatten)]
220    pub container: ContainerStyle,
221    pub entry: Interactive<ProjectPanelEntry>,
222    pub cut_entry_fade: f32,
223    pub ignored_entry_fade: f32,
224    pub filename_editor: FieldEditor,
225    pub indent_width: f32,
226}
227
228#[derive(Clone, 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(Clone, Debug, Deserialize, Default)]
240pub struct ContextMenu {
241    #[serde(flatten)]
242    pub container: ContainerStyle,
243    pub item: Interactive<ContextMenuItem>,
244    pub separator: ContainerStyle,
245}
246
247#[derive(Clone, Debug, Deserialize, Default)]
248pub struct ContextMenuItem {
249    #[serde(flatten)]
250    pub container: ContainerStyle,
251    pub label: TextStyle,
252    pub keystroke: ContainedText,
253}
254
255#[derive(Debug, Deserialize, Default)]
256pub struct CommandPalette {
257    pub key: Interactive<ContainedLabel>,
258    pub keystroke_spacing: f32,
259}
260
261#[derive(Deserialize, Default)]
262pub struct ContactsPanel {
263    #[serde(flatten)]
264    pub container: ContainerStyle,
265    pub user_query_editor: FieldEditor,
266    pub user_query_editor_height: f32,
267    pub add_contact_button: IconButton,
268    pub header_row: Interactive<ContainedText>,
269    pub contact_row: Interactive<ContainerStyle>,
270    pub project_row: Interactive<ProjectRow>,
271    pub row_height: f32,
272    pub contact_avatar: ImageStyle,
273    pub contact_username: ContainedText,
274    pub contact_button: Interactive<IconButton>,
275    pub contact_button_spacing: f32,
276    pub disabled_button: IconButton,
277    pub tree_branch: Interactive<TreeBranch>,
278    pub private_button: Interactive<IconButton>,
279    pub section_icon_size: f32,
280    pub invite_row: Interactive<ContainedLabel>,
281}
282
283#[derive(Deserialize, Default)]
284pub struct InviteLink {
285    #[serde(flatten)]
286    pub container: ContainerStyle,
287    #[serde(flatten)]
288    pub label: LabelStyle,
289    pub icon: Icon,
290}
291
292#[derive(Deserialize, Default, Clone, Copy)]
293pub struct TreeBranch {
294    pub width: f32,
295    pub color: Color,
296}
297
298#[derive(Deserialize, Default)]
299pub struct ContactFinder {
300    pub row_height: f32,
301    pub contact_avatar: ImageStyle,
302    pub contact_username: ContainerStyle,
303    pub contact_button: IconButton,
304    pub disabled_contact_button: IconButton,
305}
306
307#[derive(Deserialize, Default)]
308pub struct Icon {
309    #[serde(flatten)]
310    pub container: ContainerStyle,
311    pub color: Color,
312    pub width: f32,
313    pub path: String,
314}
315
316#[derive(Deserialize, Clone, Copy, Default)]
317pub struct IconButton {
318    #[serde(flatten)]
319    pub container: ContainerStyle,
320    pub color: Color,
321    pub icon_width: f32,
322    pub button_width: f32,
323}
324
325#[derive(Deserialize, Default)]
326pub struct ProjectRow {
327    #[serde(flatten)]
328    pub container: ContainerStyle,
329    pub name: ContainedText,
330    pub guests: ContainerStyle,
331    pub guest_avatar: ImageStyle,
332    pub guest_avatar_spacing: f32,
333}
334
335#[derive(Deserialize, Default)]
336pub struct ChatMessage {
337    #[serde(flatten)]
338    pub container: ContainerStyle,
339    pub body: TextStyle,
340    pub sender: ContainedText,
341    pub timestamp: ContainedText,
342}
343
344#[derive(Deserialize, Default)]
345pub struct ChannelSelect {
346    #[serde(flatten)]
347    pub container: ContainerStyle,
348    pub header: ChannelName,
349    pub item: ChannelName,
350    pub active_item: ChannelName,
351    pub hovered_item: ChannelName,
352    pub hovered_active_item: ChannelName,
353    pub menu: ContainerStyle,
354}
355
356#[derive(Deserialize, Default)]
357pub struct ChannelName {
358    #[serde(flatten)]
359    pub container: ContainerStyle,
360    pub hash: ContainedText,
361    pub name: TextStyle,
362}
363
364#[derive(Deserialize, Default)]
365pub struct Picker {
366    #[serde(flatten)]
367    pub container: ContainerStyle,
368    pub empty: ContainedLabel,
369    pub input_editor: FieldEditor,
370    pub item: Interactive<ContainedLabel>,
371}
372
373#[derive(Clone, Debug, Deserialize, Default)]
374pub struct ContainedText {
375    #[serde(flatten)]
376    pub container: ContainerStyle,
377    #[serde(flatten)]
378    pub text: TextStyle,
379}
380
381#[derive(Clone, Debug, Deserialize, Default)]
382pub struct ContainedLabel {
383    #[serde(flatten)]
384    pub container: ContainerStyle,
385    #[serde(flatten)]
386    pub label: LabelStyle,
387}
388
389#[derive(Clone, Deserialize, Default)]
390pub struct ProjectDiagnostics {
391    #[serde(flatten)]
392    pub container: ContainerStyle,
393    pub empty_message: TextStyle,
394    pub tab_icon_width: f32,
395    pub tab_icon_spacing: f32,
396    pub tab_summary_spacing: f32,
397}
398
399#[derive(Deserialize, Default)]
400pub struct ContactNotification {
401    pub header_avatar: ImageStyle,
402    pub header_message: ContainedText,
403    pub header_height: f32,
404    pub body_message: ContainedText,
405    pub button: Interactive<ContainedText>,
406    pub dismiss_button: Interactive<IconButton>,
407}
408
409#[derive(Deserialize, Default)]
410pub struct UpdateNotification {
411    pub message: ContainedText,
412    pub action_message: Interactive<ContainedText>,
413    pub dismiss_button: Interactive<IconButton>,
414}
415
416#[derive(Clone, Deserialize, Default)]
417pub struct Editor {
418    pub text_color: Color,
419    #[serde(default)]
420    pub background: Color,
421    pub selection: SelectionStyle,
422    pub gutter_background: Color,
423    pub gutter_padding_factor: f32,
424    pub active_line_background: Color,
425    pub highlighted_line_background: Color,
426    pub rename_fade: f32,
427    pub document_highlight_read_background: Color,
428    pub document_highlight_write_background: Color,
429    pub diff_background_deleted: Color,
430    pub diff_background_inserted: Color,
431    pub line_number: Color,
432    pub line_number_active: Color,
433    pub guest_selections: Vec<SelectionStyle>,
434    pub syntax: Arc<SyntaxTheme>,
435    pub diagnostic_path_header: DiagnosticPathHeader,
436    pub diagnostic_header: DiagnosticHeader,
437    pub error_diagnostic: DiagnosticStyle,
438    pub invalid_error_diagnostic: DiagnosticStyle,
439    pub warning_diagnostic: DiagnosticStyle,
440    pub invalid_warning_diagnostic: DiagnosticStyle,
441    pub information_diagnostic: DiagnosticStyle,
442    pub invalid_information_diagnostic: DiagnosticStyle,
443    pub hint_diagnostic: DiagnosticStyle,
444    pub invalid_hint_diagnostic: DiagnosticStyle,
445    pub autocomplete: AutocompleteStyle,
446    pub code_actions_indicator: Color,
447    pub unnecessary_code_fade: f32,
448    pub hover_popover: HoverPopover,
449    pub link_definition: HighlightStyle,
450    pub jump_icon: Interactive<IconButton>,
451}
452
453#[derive(Clone, Deserialize, Default)]
454pub struct DiagnosticPathHeader {
455    #[serde(flatten)]
456    pub container: ContainerStyle,
457    pub filename: ContainedText,
458    pub path: ContainedText,
459    pub text_scale_factor: f32,
460}
461
462#[derive(Clone, Deserialize, Default)]
463pub struct DiagnosticHeader {
464    #[serde(flatten)]
465    pub container: ContainerStyle,
466    pub message: ContainedLabel,
467    pub code: ContainedText,
468    pub text_scale_factor: f32,
469    pub icon_width_factor: f32,
470}
471
472#[derive(Clone, Deserialize, Default)]
473pub struct DiagnosticStyle {
474    pub message: LabelStyle,
475    #[serde(default)]
476    pub header: ContainerStyle,
477    pub text_scale_factor: f32,
478}
479
480#[derive(Clone, Deserialize, Default)]
481pub struct AutocompleteStyle {
482    #[serde(flatten)]
483    pub container: ContainerStyle,
484    pub item: ContainerStyle,
485    pub selected_item: ContainerStyle,
486    pub hovered_item: ContainerStyle,
487    pub match_highlight: HighlightStyle,
488}
489
490#[derive(Clone, Copy, Default, Deserialize)]
491pub struct SelectionStyle {
492    pub cursor: Color,
493    pub selection: Color,
494}
495
496#[derive(Clone, Deserialize, Default)]
497pub struct FieldEditor {
498    #[serde(flatten)]
499    pub container: ContainerStyle,
500    pub text: TextStyle,
501    #[serde(default)]
502    pub placeholder_text: Option<TextStyle>,
503    pub selection: SelectionStyle,
504}
505
506#[derive(Debug, Default, Clone, Copy)]
507pub struct Interactive<T> {
508    pub default: T,
509    pub hover: Option<T>,
510    pub active: Option<T>,
511    pub disabled: Option<T>,
512}
513
514impl<T> Interactive<T> {
515    pub fn style_for(&self, state: MouseState, active: bool) -> &T {
516        if active {
517            self.active.as_ref().unwrap_or(&self.default)
518        } else if state.hovered {
519            self.hover.as_ref().unwrap_or(&self.default)
520        } else {
521            &self.default
522        }
523    }
524
525    pub fn disabled_style(&self) -> &T {
526        self.disabled.as_ref().unwrap_or(&self.default)
527    }
528}
529
530impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
531    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
532    where
533        D: serde::Deserializer<'de>,
534    {
535        #[derive(Deserialize)]
536        struct Helper {
537            #[serde(flatten)]
538            default: Value,
539            hover: Option<Value>,
540            active: Option<Value>,
541            disabled: Option<Value>,
542        }
543
544        let json = Helper::deserialize(deserializer)?;
545
546        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
547            if let Some(mut state_json) = state_json {
548                if let Value::Object(state_json) = &mut state_json {
549                    if let Value::Object(default) = &json.default {
550                        for (key, value) in default {
551                            if !state_json.contains_key(key) {
552                                state_json.insert(key.clone(), value.clone());
553                            }
554                        }
555                    }
556                }
557                Ok(Some(
558                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
559                ))
560            } else {
561                Ok(None)
562            }
563        };
564
565        let hover = deserialize_state(json.hover)?;
566        let active = deserialize_state(json.active)?;
567        let disabled = deserialize_state(json.disabled)?;
568        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
569
570        Ok(Interactive {
571            default,
572            hover,
573            active,
574            disabled,
575        })
576    }
577}
578
579impl Editor {
580    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
581        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
582        if style_ix == 0 {
583            &self.selection
584        } else {
585            &self.guest_selections[style_ix - 1]
586        }
587    }
588}
589
590#[derive(Default)]
591pub struct SyntaxTheme {
592    pub highlights: Vec<(String, HighlightStyle)>,
593}
594
595impl SyntaxTheme {
596    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
597        Self { highlights }
598    }
599}
600
601impl<'de> Deserialize<'de> for SyntaxTheme {
602    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
603    where
604        D: serde::Deserializer<'de>,
605    {
606        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
607
608        let mut result = Self::new(Vec::new());
609        for (key, style) in syntax_data {
610            match result
611                .highlights
612                .binary_search_by(|(needle, _)| needle.cmp(&key))
613            {
614                Ok(i) | Err(i) => {
615                    result.highlights.insert(i, (key, style));
616                }
617            }
618        }
619
620        Ok(result)
621    }
622}
623
624#[derive(Clone, Deserialize, Default)]
625pub struct HoverPopover {
626    pub container: ContainerStyle,
627    pub block_style: ContainerStyle,
628    pub prose: TextStyle,
629    pub highlight: Color,
630}
631
632#[derive(Clone, Deserialize, Default)]
633pub struct TerminalStyle {
634    pub colors: TerminalColors,
635    pub modal_container: ContainerStyle,
636}
637
638#[derive(Clone, Deserialize, Default)]
639pub struct TerminalColors {
640    pub black: Color,
641    pub red: Color,
642    pub green: Color,
643    pub yellow: Color,
644    pub blue: Color,
645    pub magenta: Color,
646    pub cyan: Color,
647    pub white: Color,
648    pub bright_black: Color,
649    pub bright_red: Color,
650    pub bright_green: Color,
651    pub bright_yellow: Color,
652    pub bright_blue: Color,
653    pub bright_magenta: Color,
654    pub bright_cyan: Color,
655    pub bright_white: Color,
656    pub foreground: Color,
657    pub background: Color,
658    pub modal_background: Color,
659    pub cursor: Color,
660    pub dim_black: Color,
661    pub dim_red: Color,
662    pub dim_green: Color,
663    pub dim_yellow: Color,
664    pub dim_blue: Color,
665    pub dim_magenta: Color,
666    pub dim_cyan: Color,
667    pub dim_white: Color,
668    pub bright_foreground: Color,
669    pub dim_foreground: Color,
670}