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