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