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
15pub const DEFAULT_THEME_NAME: &'static str = "cave-dark";
16
17#[derive(Deserialize, Default)]
18pub struct Theme {
19 #[serde(default)]
20 pub name: String,
21 pub workspace: Workspace,
22 pub context_menu: ContextMenu,
23 pub chat_panel: ChatPanel,
24 pub contacts_panel: ContactsPanel,
25 pub contact_finder: ContactFinder,
26 pub project_panel: ProjectPanel,
27 pub command_palette: CommandPalette,
28 pub picker: Picker,
29 pub editor: Editor,
30 pub search: Search,
31 pub project_diagnostics: ProjectDiagnostics,
32 pub breadcrumbs: ContainedText,
33 pub contact_notification: ContactNotification,
34 pub update_notification: UpdateNotification,
35 pub tooltip: TooltipStyle,
36 pub terminal: TerminalStyle,
37}
38
39#[derive(Deserialize, Default)]
40pub struct Workspace {
41 pub background: Color,
42 pub titlebar: Titlebar,
43 pub tab: Tab,
44 pub active_tab: Tab,
45 pub pane_divider: Border,
46 pub leader_border_opacity: f32,
47 pub leader_border_width: f32,
48 pub sidebar_resize_handle: ContainerStyle,
49 pub status_bar: StatusBar,
50 pub toolbar: Toolbar,
51 pub disconnected_overlay: ContainedText,
52 pub modal: ContainerStyle,
53 pub notification: ContainerStyle,
54 pub notifications: Notifications,
55 pub joining_project_avatar: ImageStyle,
56 pub joining_project_message: ContainedText,
57}
58
59#[derive(Clone, Deserialize, Default)]
60pub struct Titlebar {
61 #[serde(flatten)]
62 pub container: ContainerStyle,
63 pub height: f32,
64 pub title: TextStyle,
65 pub avatar_width: f32,
66 pub avatar_margin: f32,
67 pub avatar_ribbon: AvatarRibbon,
68 pub offline_icon: OfflineIcon,
69 pub avatar: ImageStyle,
70 pub sign_in_prompt: Interactive<ContainedText>,
71 pub outdated_warning: ContainedText,
72}
73
74#[derive(Clone, Deserialize, Default)]
75pub struct AvatarRibbon {
76 #[serde(flatten)]
77 pub container: ContainerStyle,
78 pub width: f32,
79 pub height: f32,
80}
81
82#[derive(Clone, Deserialize, Default)]
83pub struct OfflineIcon {
84 #[serde(flatten)]
85 pub container: ContainerStyle,
86 pub width: f32,
87 pub color: Color,
88}
89
90#[derive(Clone, Deserialize, Default)]
91pub struct Tab {
92 pub height: f32,
93 #[serde(flatten)]
94 pub container: ContainerStyle,
95 #[serde(flatten)]
96 pub label: LabelStyle,
97 pub spacing: f32,
98 pub icon_width: f32,
99 pub icon_close: Color,
100 pub icon_close_active: Color,
101 pub icon_dirty: Color,
102 pub icon_conflict: Color,
103}
104
105#[derive(Clone, Deserialize, Default)]
106pub struct Toolbar {
107 #[serde(flatten)]
108 pub container: ContainerStyle,
109 pub height: f32,
110 pub item_spacing: f32,
111 pub nav_button: Interactive<IconButton>,
112}
113
114#[derive(Clone, Deserialize, Default)]
115pub struct Notifications {
116 #[serde(flatten)]
117 pub container: ContainerStyle,
118 pub width: f32,
119}
120
121#[derive(Clone, Deserialize, Default)]
122pub struct Search {
123 #[serde(flatten)]
124 pub container: ContainerStyle,
125 pub editor: FindEditor,
126 pub invalid_editor: ContainerStyle,
127 pub option_button_group: ContainerStyle,
128 pub option_button: Interactive<ContainedText>,
129 pub match_background: Color,
130 pub match_index: ContainedText,
131 pub results_status: TextStyle,
132 pub tab_icon_width: f32,
133 pub tab_icon_spacing: f32,
134}
135
136#[derive(Clone, Deserialize, Default)]
137pub struct FindEditor {
138 #[serde(flatten)]
139 pub input: FieldEditor,
140 pub min_width: f32,
141 pub max_width: f32,
142}
143
144#[derive(Deserialize, Default)]
145pub struct StatusBar {
146 #[serde(flatten)]
147 pub container: ContainerStyle,
148 pub height: f32,
149 pub item_spacing: f32,
150 pub cursor_position: TextStyle,
151 pub auto_update_progress_message: TextStyle,
152 pub auto_update_done_message: TextStyle,
153 pub lsp_status: Interactive<StatusBarLspStatus>,
154 pub feedback: Interactive<TextStyle>,
155 pub sidebar_buttons: StatusBarSidebarButtons,
156 pub diagnostic_summary: Interactive<StatusBarDiagnosticSummary>,
157 pub diagnostic_message: Interactive<ContainedText>,
158}
159
160#[derive(Deserialize, Default)]
161pub struct StatusBarSidebarButtons {
162 pub group_left: ContainerStyle,
163 pub group_right: ContainerStyle,
164 pub item: Interactive<SidebarItem>,
165 pub badge: ContainerStyle,
166}
167
168#[derive(Deserialize, Default)]
169pub struct StatusBarDiagnosticSummary {
170 pub container_ok: ContainerStyle,
171 pub container_warning: ContainerStyle,
172 pub container_error: ContainerStyle,
173 pub text: TextStyle,
174 pub icon_color_ok: Color,
175 pub icon_color_warning: Color,
176 pub icon_color_error: Color,
177 pub height: f32,
178 pub icon_width: f32,
179 pub icon_spacing: f32,
180 pub summary_spacing: f32,
181}
182
183#[derive(Deserialize, Default)]
184pub struct StatusBarLspStatus {
185 #[serde(flatten)]
186 pub container: ContainerStyle,
187 pub height: f32,
188 pub icon_spacing: f32,
189 pub icon_color: Color,
190 pub icon_width: f32,
191 pub message: TextStyle,
192}
193
194#[derive(Deserialize, Default)]
195pub struct Sidebar {
196 pub resize_handle: ContainerStyle,
197}
198
199#[derive(Clone, Copy, Deserialize, Default)]
200pub struct SidebarItem {
201 #[serde(flatten)]
202 pub container: ContainerStyle,
203 pub icon_color: Color,
204 pub icon_size: f32,
205}
206
207#[derive(Deserialize, Default)]
208pub struct ChatPanel {
209 #[serde(flatten)]
210 pub container: ContainerStyle,
211 pub message: ChatMessage,
212 pub pending_message: ChatMessage,
213 pub channel_select: ChannelSelect,
214 pub input_editor: FieldEditor,
215 pub sign_in_prompt: TextStyle,
216 pub hovered_sign_in_prompt: TextStyle,
217}
218
219#[derive(Deserialize, Default)]
220pub struct ProjectPanel {
221 #[serde(flatten)]
222 pub container: ContainerStyle,
223 pub entry: Interactive<ProjectPanelEntry>,
224 pub cut_entry_fade: f32,
225 pub ignored_entry_fade: f32,
226 pub filename_editor: FieldEditor,
227 pub indent_width: f32,
228}
229
230#[derive(Clone, Debug, Deserialize, Default)]
231pub struct ProjectPanelEntry {
232 pub height: f32,
233 #[serde(flatten)]
234 pub container: ContainerStyle,
235 pub text: TextStyle,
236 pub icon_color: Color,
237 pub icon_size: f32,
238 pub icon_spacing: f32,
239}
240
241#[derive(Clone, Debug, Deserialize, Default)]
242pub struct ContextMenu {
243 #[serde(flatten)]
244 pub container: ContainerStyle,
245 pub item: Interactive<ContextMenuItem>,
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 active_hover: Option<T>,
514}
515
516impl<T> Interactive<T> {
517 pub fn style_for(&self, state: MouseState, active: bool) -> &T {
518 if active {
519 if state.hovered {
520 self.active_hover
521 .as_ref()
522 .or(self.active.as_ref())
523 .unwrap_or(&self.default)
524 } else {
525 self.active.as_ref().unwrap_or(&self.default)
526 }
527 } else {
528 if state.hovered {
529 self.hover.as_ref().unwrap_or(&self.default)
530 } else {
531 &self.default
532 }
533 }
534 }
535}
536
537impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
538 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
539 where
540 D: serde::Deserializer<'de>,
541 {
542 #[derive(Deserialize)]
543 struct Helper {
544 #[serde(flatten)]
545 default: Value,
546 hover: Option<Value>,
547 active: Option<Value>,
548 active_hover: Option<Value>,
549 }
550
551 let json = Helper::deserialize(deserializer)?;
552
553 let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
554 if let Some(mut state_json) = state_json {
555 if let Value::Object(state_json) = &mut state_json {
556 if let Value::Object(default) = &json.default {
557 for (key, value) in default {
558 if !state_json.contains_key(key) {
559 state_json.insert(key.clone(), value.clone());
560 }
561 }
562 }
563 }
564 Ok(Some(
565 serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
566 ))
567 } else {
568 Ok(None)
569 }
570 };
571
572 let hover = deserialize_state(json.hover)?;
573 let active = deserialize_state(json.active)?;
574 let active_hover = deserialize_state(json.active_hover)?;
575 let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
576
577 Ok(Interactive {
578 default,
579 hover,
580 active,
581 active_hover,
582 })
583 }
584}
585
586impl Editor {
587 pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
588 let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
589 if style_ix == 0 {
590 &self.selection
591 } else {
592 &self.guest_selections[style_ix - 1]
593 }
594 }
595}
596
597#[derive(Default)]
598pub struct SyntaxTheme {
599 pub highlights: Vec<(String, HighlightStyle)>,
600}
601
602impl SyntaxTheme {
603 pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
604 Self { highlights }
605 }
606}
607
608impl<'de> Deserialize<'de> for SyntaxTheme {
609 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
610 where
611 D: serde::Deserializer<'de>,
612 {
613 let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
614
615 let mut result = Self::new(Vec::new());
616 for (key, style) in syntax_data {
617 match result
618 .highlights
619 .binary_search_by(|(needle, _)| needle.cmp(&key))
620 {
621 Ok(i) | Err(i) => {
622 result.highlights.insert(i, (key, style));
623 }
624 }
625 }
626
627 Ok(result)
628 }
629}
630
631#[derive(Clone, Deserialize, Default)]
632pub struct HoverPopover {
633 pub container: ContainerStyle,
634 pub block_style: ContainerStyle,
635 pub prose: TextStyle,
636 pub highlight: Color,
637}
638
639#[derive(Clone, Deserialize, Default)]
640pub struct TerminalStyle {
641 pub black: Color,
642 pub red: Color,
643 pub green: Color,
644 pub yellow: Color,
645 pub blue: Color,
646 pub magenta: Color,
647 pub cyan: Color,
648 pub white: Color,
649 pub bright_black: Color,
650 pub bright_red: Color,
651 pub bright_green: Color,
652 pub bright_yellow: Color,
653 pub bright_blue: Color,
654 pub bright_magenta: Color,
655 pub bright_cyan: Color,
656 pub bright_white: Color,
657 pub foreground: Color,
658 pub background: Color,
659 pub cursor: Color,
660 pub dim_black: Color,
661 pub dim_red: Color,
662 pub dim_green: Color,
663 pub dim_yellow: Color,
664 pub dim_blue: Color,
665 pub dim_magenta: Color,
666 pub dim_cyan: Color,
667 pub dim_white: Color,
668 pub bright_foreground: Color,
669 pub dim_foreground: Color,
670}