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