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}
514
515#[derive(Clone, Deserialize, Default)]
516pub struct DiagnosticPathHeader {
517 #[serde(flatten)]
518 pub container: ContainerStyle,
519 pub filename: ContainedText,
520 pub path: ContainedText,
521 pub text_scale_factor: f32,
522}
523
524#[derive(Clone, Deserialize, Default)]
525pub struct DiagnosticHeader {
526 #[serde(flatten)]
527 pub container: ContainerStyle,
528 pub message: ContainedLabel,
529 pub code: ContainedText,
530 pub text_scale_factor: f32,
531 pub icon_width_factor: f32,
532}
533
534#[derive(Clone, Deserialize, Default)]
535pub struct DiagnosticStyle {
536 pub message: LabelStyle,
537 #[serde(default)]
538 pub header: ContainerStyle,
539 pub text_scale_factor: f32,
540}
541
542#[derive(Clone, Deserialize, Default)]
543pub struct AutocompleteStyle {
544 #[serde(flatten)]
545 pub container: ContainerStyle,
546 pub item: ContainerStyle,
547 pub selected_item: ContainerStyle,
548 pub hovered_item: ContainerStyle,
549 pub match_highlight: HighlightStyle,
550}
551
552#[derive(Clone, Copy, Default, Deserialize)]
553pub struct SelectionStyle {
554 pub cursor: Color,
555 pub selection: Color,
556}
557
558#[derive(Clone, Deserialize, Default)]
559pub struct FieldEditor {
560 #[serde(flatten)]
561 pub container: ContainerStyle,
562 pub text: TextStyle,
563 #[serde(default)]
564 pub placeholder_text: Option<TextStyle>,
565 pub selection: SelectionStyle,
566}
567
568#[derive(Clone, Deserialize, Default)]
569pub struct CodeActions {
570 #[serde(default)]
571 pub indicator: Color,
572 pub vertical_scale: f32,
573}
574
575#[derive(Clone, Deserialize, Default)]
576pub struct DiffStyle {
577 pub inserted: Color,
578 pub modified: Color,
579 pub deleted: Color,
580 pub removed_width_em: f32,
581 pub width_em: f32,
582 pub corner_radius: f32,
583}
584
585#[derive(Debug, Default, Clone, Copy)]
586pub struct Interactive<T> {
587 pub default: T,
588 pub hover: Option<T>,
589 pub clicked: Option<T>,
590 pub active: Option<T>,
591 pub disabled: Option<T>,
592}
593
594impl<T> Interactive<T> {
595 pub fn style_for(&self, state: MouseState, active: bool) -> &T {
596 if active {
597 self.active.as_ref().unwrap_or(&self.default)
598 } else if state.clicked == Some(gpui::MouseButton::Left) && self.clicked.is_some() {
599 self.clicked.as_ref().unwrap()
600 } else if state.hovered {
601 self.hover.as_ref().unwrap_or(&self.default)
602 } else {
603 &self.default
604 }
605 }
606
607 pub fn disabled_style(&self) -> &T {
608 self.disabled.as_ref().unwrap_or(&self.default)
609 }
610}
611
612impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
613 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
614 where
615 D: serde::Deserializer<'de>,
616 {
617 #[derive(Deserialize)]
618 struct Helper {
619 #[serde(flatten)]
620 default: Value,
621 hover: Option<Value>,
622 clicked: Option<Value>,
623 active: Option<Value>,
624 disabled: Option<Value>,
625 }
626
627 let json = Helper::deserialize(deserializer)?;
628
629 let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
630 if let Some(mut state_json) = state_json {
631 if let Value::Object(state_json) = &mut state_json {
632 if let Value::Object(default) = &json.default {
633 for (key, value) in default {
634 if !state_json.contains_key(key) {
635 state_json.insert(key.clone(), value.clone());
636 }
637 }
638 }
639 }
640 Ok(Some(
641 serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
642 ))
643 } else {
644 Ok(None)
645 }
646 };
647
648 let hover = deserialize_state(json.hover)?;
649 let clicked = deserialize_state(json.clicked)?;
650 let active = deserialize_state(json.active)?;
651 let disabled = deserialize_state(json.disabled)?;
652 let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
653
654 Ok(Interactive {
655 default,
656 hover,
657 clicked,
658 active,
659 disabled,
660 })
661 }
662}
663
664impl Editor {
665 pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
666 let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
667 if style_ix == 0 {
668 &self.selection
669 } else {
670 &self.guest_selections[style_ix - 1]
671 }
672 }
673}
674
675#[derive(Default)]
676pub struct SyntaxTheme {
677 pub highlights: Vec<(String, HighlightStyle)>,
678}
679
680impl SyntaxTheme {
681 pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
682 Self { highlights }
683 }
684}
685
686impl<'de> Deserialize<'de> for SyntaxTheme {
687 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
688 where
689 D: serde::Deserializer<'de>,
690 {
691 let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
692
693 let mut result = Self::new(Vec::new());
694 for (key, style) in syntax_data {
695 match result
696 .highlights
697 .binary_search_by(|(needle, _)| needle.cmp(&key))
698 {
699 Ok(i) | Err(i) => {
700 result.highlights.insert(i, (key, style));
701 }
702 }
703 }
704
705 Ok(result)
706 }
707}
708
709#[derive(Clone, Deserialize, Default)]
710pub struct HoverPopover {
711 pub container: ContainerStyle,
712 pub info_container: ContainerStyle,
713 pub warning_container: ContainerStyle,
714 pub error_container: ContainerStyle,
715 pub block_style: ContainerStyle,
716 pub prose: TextStyle,
717 pub highlight: Color,
718}
719
720#[derive(Clone, Deserialize, Default)]
721pub struct TerminalStyle {
722 pub colors: TerminalColors,
723 pub modal_container: ContainerStyle,
724}
725
726#[derive(Clone, Deserialize, Default)]
727pub struct TerminalColors {
728 pub black: Color,
729 pub red: Color,
730 pub green: Color,
731 pub yellow: Color,
732 pub blue: Color,
733 pub magenta: Color,
734 pub cyan: Color,
735 pub white: Color,
736 pub bright_black: Color,
737 pub bright_red: Color,
738 pub bright_green: Color,
739 pub bright_yellow: Color,
740 pub bright_blue: Color,
741 pub bright_magenta: Color,
742 pub bright_cyan: Color,
743 pub bright_white: Color,
744 pub foreground: Color,
745 pub background: Color,
746 pub modal_background: Color,
747 pub cursor: Color,
748 pub dim_black: Color,
749 pub dim_red: Color,
750 pub dim_green: Color,
751 pub dim_yellow: Color,
752 pub dim_blue: Color,
753 pub dim_magenta: Color,
754 pub dim_cyan: Color,
755 pub dim_white: Color,
756 pub bright_foreground: Color,
757 pub dim_foreground: Color,
758}