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