1mod theme_registry;
2
3use gpui::{
4 color::Color,
5 elements::{ContainerStyle, ImageStyle, LabelStyle, MouseState},
6 fonts::{HighlightStyle, TextStyle},
7 Border,
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 = "dark";
16
17#[derive(Deserialize, Default)]
18pub struct Theme {
19 #[serde(default)]
20 pub name: String,
21 pub workspace: Workspace,
22 pub chat_panel: ChatPanel,
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}
33
34#[derive(Deserialize, Default)]
35pub struct Workspace {
36 pub background: Color,
37 pub titlebar: Titlebar,
38 pub tab: Tab,
39 pub active_tab: Tab,
40 pub pane_divider: Border,
41 pub leader_border_opacity: f32,
42 pub leader_border_width: f32,
43 pub sidebar_resize_handle: ContainerStyle,
44 pub status_bar: StatusBar,
45 pub toolbar: Toolbar,
46 pub disconnected_overlay: ContainedText,
47 pub modal: ContainerStyle,
48}
49
50#[derive(Clone, Deserialize, Default)]
51pub struct Titlebar {
52 #[serde(flatten)]
53 pub container: ContainerStyle,
54 pub height: f32,
55 pub title: TextStyle,
56 pub avatar_width: f32,
57 pub avatar_margin: f32,
58 pub avatar_ribbon: AvatarRibbon,
59 pub offline_icon: OfflineIcon,
60 pub share_icon: Interactive<ShareIcon>,
61 pub avatar: ImageStyle,
62 pub sign_in_prompt: Interactive<ContainedText>,
63 pub outdated_warning: ContainedText,
64}
65
66#[derive(Clone, Deserialize, Default)]
67pub struct AvatarRibbon {
68 #[serde(flatten)]
69 pub container: ContainerStyle,
70 pub width: f32,
71 pub height: f32,
72}
73
74#[derive(Clone, Deserialize, Default)]
75pub struct OfflineIcon {
76 #[serde(flatten)]
77 pub container: ContainerStyle,
78 pub width: f32,
79 pub color: Color,
80}
81
82#[derive(Clone, Deserialize, Default)]
83pub struct ShareIcon {
84 #[serde(flatten)]
85 pub container: ContainerStyle,
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}
111
112#[derive(Clone, Deserialize, Default)]
113pub struct Search {
114 #[serde(flatten)]
115 pub container: ContainerStyle,
116 pub editor: FindEditor,
117 pub invalid_editor: ContainerStyle,
118 pub option_button_group: ContainerStyle,
119 pub option_button: Interactive<ContainedText>,
120 pub match_background: Color,
121 pub match_index: ContainedText,
122 pub results_status: TextStyle,
123 pub tab_icon_width: f32,
124 pub tab_icon_spacing: f32,
125}
126
127#[derive(Clone, Deserialize, Default)]
128pub struct FindEditor {
129 #[serde(flatten)]
130 pub input: FieldEditor,
131 pub min_width: f32,
132 pub max_width: f32,
133}
134
135#[derive(Deserialize, Default)]
136pub struct StatusBar {
137 #[serde(flatten)]
138 pub container: ContainerStyle,
139 pub height: f32,
140 pub item_spacing: f32,
141 pub cursor_position: TextStyle,
142 pub auto_update_progress_message: TextStyle,
143 pub auto_update_done_message: TextStyle,
144 pub lsp_status: Interactive<StatusBarLspStatus>,
145 pub sidebar_buttons: StatusBarSidebarButtons,
146 pub diagnostic_summary: Interactive<StatusBarDiagnosticSummary>,
147 pub diagnostic_message: Interactive<ContainedText>,
148}
149
150#[derive(Deserialize, Default)]
151pub struct StatusBarSidebarButtons {
152 pub group_left: ContainerStyle,
153 pub group_right: ContainerStyle,
154 pub item: Interactive<SidebarItem>,
155}
156
157#[derive(Deserialize, Default)]
158pub struct StatusBarDiagnosticSummary {
159 pub container_ok: ContainerStyle,
160 pub container_warning: ContainerStyle,
161 pub container_error: ContainerStyle,
162 pub text: TextStyle,
163 pub icon_color_ok: Color,
164 pub icon_color_warning: Color,
165 pub icon_color_error: Color,
166 pub height: f32,
167 pub icon_width: f32,
168 pub icon_spacing: f32,
169 pub summary_spacing: f32,
170}
171
172#[derive(Deserialize, Default)]
173pub struct StatusBarLspStatus {
174 #[serde(flatten)]
175 pub container: ContainerStyle,
176 pub height: f32,
177 pub icon_spacing: f32,
178 pub icon_color: Color,
179 pub icon_width: f32,
180 pub message: TextStyle,
181}
182
183#[derive(Deserialize, Default)]
184pub struct Sidebar {
185 pub resize_handle: ContainerStyle,
186}
187
188#[derive(Clone, Copy, Deserialize, Default)]
189pub struct SidebarItem {
190 #[serde(flatten)]
191 pub container: ContainerStyle,
192 pub icon_color: Color,
193 pub icon_size: f32,
194}
195
196#[derive(Deserialize, Default)]
197pub struct ChatPanel {
198 #[serde(flatten)]
199 pub container: ContainerStyle,
200 pub message: ChatMessage,
201 pub pending_message: ChatMessage,
202 pub channel_select: ChannelSelect,
203 pub input_editor: FieldEditor,
204 pub sign_in_prompt: TextStyle,
205 pub hovered_sign_in_prompt: TextStyle,
206}
207
208#[derive(Deserialize, Default)]
209pub struct ProjectPanel {
210 #[serde(flatten)]
211 pub container: ContainerStyle,
212 pub entry: Interactive<ProjectPanelEntry>,
213 pub filename_editor: FieldEditor,
214 pub indent_width: f32,
215}
216
217#[derive(Debug, Deserialize, Default)]
218pub struct ProjectPanelEntry {
219 pub height: f32,
220 #[serde(flatten)]
221 pub container: ContainerStyle,
222 pub text: TextStyle,
223 pub icon_color: Color,
224 pub icon_size: f32,
225 pub icon_spacing: f32,
226}
227
228#[derive(Debug, Deserialize, Default)]
229pub struct CommandPalette {
230 pub key: Interactive<ContainedLabel>,
231 pub keystroke_spacing: f32,
232}
233
234#[derive(Deserialize, Default)]
235pub struct ContactsPanel {
236 #[serde(flatten)]
237 pub container: ContainerStyle,
238 pub header: ContainedText,
239 pub user_query_editor: FieldEditor,
240 pub add_contact_icon: AddContactIcon,
241 pub row_height: f32,
242 pub contact_avatar: ImageStyle,
243 pub contact_username: ContainedText,
244 pub contact_button: ContainedText,
245 pub tree_branch_width: f32,
246 pub tree_branch_color: Color,
247 pub shared_project: ProjectRow,
248 pub hovered_shared_project: ProjectRow,
249 pub unshared_project: ProjectRow,
250 pub hovered_unshared_project: ProjectRow,
251}
252
253#[derive(Deserialize, Default)]
254pub struct ContactFinder {
255 pub row_height: f32,
256 pub contact_avatar: ImageStyle,
257 pub contact_username: ContainerStyle,
258 pub contact_button: ContainedText,
259}
260
261#[derive(Deserialize, Default)]
262pub struct AddContactIcon {
263 #[serde(flatten)]
264 pub container: ContainerStyle,
265 pub color: Color,
266}
267
268#[derive(Deserialize, Default)]
269pub struct ProjectRow {
270 #[serde(flatten)]
271 pub container: ContainerStyle,
272 pub height: f32,
273 pub name: ContainedText,
274 pub guest_avatar: ImageStyle,
275 pub guest_avatar_spacing: f32,
276}
277
278#[derive(Deserialize, Default)]
279pub struct ChatMessage {
280 #[serde(flatten)]
281 pub container: ContainerStyle,
282 pub body: TextStyle,
283 pub sender: ContainedText,
284 pub timestamp: ContainedText,
285}
286
287#[derive(Deserialize, Default)]
288pub struct ChannelSelect {
289 #[serde(flatten)]
290 pub container: ContainerStyle,
291 pub header: ChannelName,
292 pub item: ChannelName,
293 pub active_item: ChannelName,
294 pub hovered_item: ChannelName,
295 pub hovered_active_item: ChannelName,
296 pub menu: ContainerStyle,
297}
298
299#[derive(Deserialize, Default)]
300pub struct ChannelName {
301 #[serde(flatten)]
302 pub container: ContainerStyle,
303 pub hash: ContainedText,
304 pub name: TextStyle,
305}
306
307#[derive(Deserialize, Default)]
308pub struct Picker {
309 #[serde(flatten)]
310 pub container: ContainerStyle,
311 pub empty: ContainedLabel,
312 pub input_editor: FieldEditor,
313 pub item: Interactive<ContainedLabel>,
314}
315
316#[derive(Clone, Debug, Deserialize, Default)]
317pub struct ContainedText {
318 #[serde(flatten)]
319 pub container: ContainerStyle,
320 #[serde(flatten)]
321 pub text: TextStyle,
322}
323
324#[derive(Clone, Debug, Deserialize, Default)]
325pub struct ContainedLabel {
326 #[serde(flatten)]
327 pub container: ContainerStyle,
328 #[serde(flatten)]
329 pub label: LabelStyle,
330}
331
332#[derive(Clone, Deserialize, Default)]
333pub struct ProjectDiagnostics {
334 #[serde(flatten)]
335 pub container: ContainerStyle,
336 pub empty_message: TextStyle,
337 pub tab_icon_width: f32,
338 pub tab_icon_spacing: f32,
339 pub tab_summary_spacing: f32,
340}
341
342#[derive(Clone, Deserialize, Default)]
343pub struct Editor {
344 pub text_color: Color,
345 #[serde(default)]
346 pub background: Color,
347 pub selection: SelectionStyle,
348 pub gutter_background: Color,
349 pub gutter_padding_factor: f32,
350 pub active_line_background: Color,
351 pub highlighted_line_background: Color,
352 pub rename_fade: f32,
353 pub document_highlight_read_background: Color,
354 pub document_highlight_write_background: Color,
355 pub diff_background_deleted: Color,
356 pub diff_background_inserted: Color,
357 pub line_number: Color,
358 pub line_number_active: Color,
359 pub guest_selections: Vec<SelectionStyle>,
360 pub syntax: Arc<SyntaxTheme>,
361 pub diagnostic_path_header: DiagnosticPathHeader,
362 pub diagnostic_header: DiagnosticHeader,
363 pub error_diagnostic: DiagnosticStyle,
364 pub invalid_error_diagnostic: DiagnosticStyle,
365 pub warning_diagnostic: DiagnosticStyle,
366 pub invalid_warning_diagnostic: DiagnosticStyle,
367 pub information_diagnostic: DiagnosticStyle,
368 pub invalid_information_diagnostic: DiagnosticStyle,
369 pub hint_diagnostic: DiagnosticStyle,
370 pub invalid_hint_diagnostic: DiagnosticStyle,
371 pub autocomplete: AutocompleteStyle,
372 pub code_actions_indicator: Color,
373 pub unnecessary_code_fade: f32,
374}
375
376#[derive(Clone, Deserialize, Default)]
377pub struct DiagnosticPathHeader {
378 #[serde(flatten)]
379 pub container: ContainerStyle,
380 pub filename: ContainedText,
381 pub path: ContainedText,
382 pub text_scale_factor: f32,
383}
384
385#[derive(Clone, Deserialize, Default)]
386pub struct DiagnosticHeader {
387 #[serde(flatten)]
388 pub container: ContainerStyle,
389 pub message: ContainedLabel,
390 pub code: ContainedText,
391 pub text_scale_factor: f32,
392 pub icon_width_factor: f32,
393}
394
395#[derive(Clone, Deserialize, Default)]
396pub struct DiagnosticStyle {
397 pub message: LabelStyle,
398 #[serde(default)]
399 pub header: ContainerStyle,
400 pub text_scale_factor: f32,
401}
402
403#[derive(Clone, Deserialize, Default)]
404pub struct AutocompleteStyle {
405 #[serde(flatten)]
406 pub container: ContainerStyle,
407 pub item: ContainerStyle,
408 pub selected_item: ContainerStyle,
409 pub hovered_item: ContainerStyle,
410 pub match_highlight: HighlightStyle,
411}
412
413#[derive(Clone, Copy, Default, Deserialize)]
414pub struct SelectionStyle {
415 pub cursor: Color,
416 pub selection: Color,
417}
418
419#[derive(Clone, Deserialize, Default)]
420pub struct FieldEditor {
421 #[serde(flatten)]
422 pub container: ContainerStyle,
423 pub text: TextStyle,
424 #[serde(default)]
425 pub placeholder_text: Option<TextStyle>,
426 pub selection: SelectionStyle,
427}
428
429#[derive(Debug, Default, Clone, Copy)]
430pub struct Interactive<T> {
431 pub default: T,
432 pub hover: Option<T>,
433 pub active: Option<T>,
434 pub active_hover: Option<T>,
435}
436
437impl<T> Interactive<T> {
438 pub fn style_for(&self, state: &MouseState, active: bool) -> &T {
439 if active {
440 if state.hovered {
441 self.active_hover
442 .as_ref()
443 .or(self.active.as_ref())
444 .unwrap_or(&self.default)
445 } else {
446 self.active.as_ref().unwrap_or(&self.default)
447 }
448 } else {
449 if state.hovered {
450 self.hover.as_ref().unwrap_or(&self.default)
451 } else {
452 &self.default
453 }
454 }
455 }
456}
457
458impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
459 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
460 where
461 D: serde::Deserializer<'de>,
462 {
463 #[derive(Deserialize)]
464 struct Helper {
465 #[serde(flatten)]
466 default: Value,
467 hover: Option<Value>,
468 active: Option<Value>,
469 active_hover: Option<Value>,
470 }
471
472 let json = Helper::deserialize(deserializer)?;
473
474 let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
475 if let Some(mut state_json) = state_json {
476 if let Value::Object(state_json) = &mut state_json {
477 if let Value::Object(default) = &json.default {
478 for (key, value) in default {
479 if !state_json.contains_key(key) {
480 state_json.insert(key.clone(), value.clone());
481 }
482 }
483 }
484 }
485 Ok(Some(
486 serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
487 ))
488 } else {
489 Ok(None)
490 }
491 };
492
493 let hover = deserialize_state(json.hover)?;
494 let active = deserialize_state(json.active)?;
495 let active_hover = deserialize_state(json.active_hover)?;
496 let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
497
498 Ok(Interactive {
499 default,
500 hover,
501 active,
502 active_hover,
503 })
504 }
505}
506
507impl Editor {
508 pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
509 let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
510 if style_ix == 0 {
511 &self.selection
512 } else {
513 &self.guest_selections[style_ix - 1]
514 }
515 }
516}
517
518#[derive(Default)]
519pub struct SyntaxTheme {
520 pub highlights: Vec<(String, HighlightStyle)>,
521}
522
523impl SyntaxTheme {
524 pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
525 Self { highlights }
526 }
527}
528
529impl<'de> Deserialize<'de> for SyntaxTheme {
530 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
531 where
532 D: serde::Deserializer<'de>,
533 {
534 let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
535
536 let mut result = Self::new(Vec::new());
537 for (key, style) in syntax_data {
538 match result
539 .highlights
540 .binary_search_by(|(needle, _)| needle.cmp(&key))
541 {
542 Ok(i) | Err(i) => {
543 result.highlights.insert(i, (key, style));
544 }
545 }
546 }
547
548 Ok(result)
549 }
550}