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 #[serde(flatten)]
256 pub container: ContainerStyle,
257 pub max_width: f32,
258 pub max_height: f32,
259 pub query_editor: FieldEditor,
260 pub row_height: f32,
261 pub contact_avatar: ImageStyle,
262 pub contact_username: ContainedText,
263 pub contact_button: ContainedText,
264}
265
266#[derive(Deserialize, Default)]
267pub struct AddContactIcon {
268 #[serde(flatten)]
269 pub container: ContainerStyle,
270 pub color: Color,
271}
272
273#[derive(Deserialize, Default)]
274pub struct ProjectRow {
275 #[serde(flatten)]
276 pub container: ContainerStyle,
277 pub height: f32,
278 pub name: ContainedText,
279 pub guest_avatar: ImageStyle,
280 pub guest_avatar_spacing: f32,
281}
282
283#[derive(Deserialize, Default)]
284pub struct ChatMessage {
285 #[serde(flatten)]
286 pub container: ContainerStyle,
287 pub body: TextStyle,
288 pub sender: ContainedText,
289 pub timestamp: ContainedText,
290}
291
292#[derive(Deserialize, Default)]
293pub struct ChannelSelect {
294 #[serde(flatten)]
295 pub container: ContainerStyle,
296 pub header: ChannelName,
297 pub item: ChannelName,
298 pub active_item: ChannelName,
299 pub hovered_item: ChannelName,
300 pub hovered_active_item: ChannelName,
301 pub menu: ContainerStyle,
302}
303
304#[derive(Deserialize, Default)]
305pub struct ChannelName {
306 #[serde(flatten)]
307 pub container: ContainerStyle,
308 pub hash: ContainedText,
309 pub name: TextStyle,
310}
311
312#[derive(Deserialize, Default)]
313pub struct Picker {
314 #[serde(flatten)]
315 pub container: ContainerStyle,
316 pub empty: ContainedLabel,
317 pub input_editor: FieldEditor,
318 pub item: Interactive<ContainedLabel>,
319}
320
321#[derive(Clone, Debug, Deserialize, Default)]
322pub struct ContainedText {
323 #[serde(flatten)]
324 pub container: ContainerStyle,
325 #[serde(flatten)]
326 pub text: TextStyle,
327}
328
329#[derive(Clone, Debug, Deserialize, Default)]
330pub struct ContainedLabel {
331 #[serde(flatten)]
332 pub container: ContainerStyle,
333 #[serde(flatten)]
334 pub label: LabelStyle,
335}
336
337#[derive(Clone, Deserialize, Default)]
338pub struct ProjectDiagnostics {
339 #[serde(flatten)]
340 pub container: ContainerStyle,
341 pub empty_message: TextStyle,
342 pub tab_icon_width: f32,
343 pub tab_icon_spacing: f32,
344 pub tab_summary_spacing: f32,
345}
346
347#[derive(Clone, Deserialize, Default)]
348pub struct Editor {
349 pub text_color: Color,
350 #[serde(default)]
351 pub background: Color,
352 pub selection: SelectionStyle,
353 pub gutter_background: Color,
354 pub gutter_padding_factor: f32,
355 pub active_line_background: Color,
356 pub highlighted_line_background: Color,
357 pub rename_fade: f32,
358 pub document_highlight_read_background: Color,
359 pub document_highlight_write_background: Color,
360 pub diff_background_deleted: Color,
361 pub diff_background_inserted: Color,
362 pub line_number: Color,
363 pub line_number_active: Color,
364 pub guest_selections: Vec<SelectionStyle>,
365 pub syntax: Arc<SyntaxTheme>,
366 pub diagnostic_path_header: DiagnosticPathHeader,
367 pub diagnostic_header: DiagnosticHeader,
368 pub error_diagnostic: DiagnosticStyle,
369 pub invalid_error_diagnostic: DiagnosticStyle,
370 pub warning_diagnostic: DiagnosticStyle,
371 pub invalid_warning_diagnostic: DiagnosticStyle,
372 pub information_diagnostic: DiagnosticStyle,
373 pub invalid_information_diagnostic: DiagnosticStyle,
374 pub hint_diagnostic: DiagnosticStyle,
375 pub invalid_hint_diagnostic: DiagnosticStyle,
376 pub autocomplete: AutocompleteStyle,
377 pub code_actions_indicator: Color,
378 pub unnecessary_code_fade: f32,
379}
380
381#[derive(Clone, Deserialize, Default)]
382pub struct DiagnosticPathHeader {
383 #[serde(flatten)]
384 pub container: ContainerStyle,
385 pub filename: ContainedText,
386 pub path: ContainedText,
387 pub text_scale_factor: f32,
388}
389
390#[derive(Clone, Deserialize, Default)]
391pub struct DiagnosticHeader {
392 #[serde(flatten)]
393 pub container: ContainerStyle,
394 pub message: ContainedLabel,
395 pub code: ContainedText,
396 pub text_scale_factor: f32,
397 pub icon_width_factor: f32,
398}
399
400#[derive(Clone, Deserialize, Default)]
401pub struct DiagnosticStyle {
402 pub message: LabelStyle,
403 #[serde(default)]
404 pub header: ContainerStyle,
405 pub text_scale_factor: f32,
406}
407
408#[derive(Clone, Deserialize, Default)]
409pub struct AutocompleteStyle {
410 #[serde(flatten)]
411 pub container: ContainerStyle,
412 pub item: ContainerStyle,
413 pub selected_item: ContainerStyle,
414 pub hovered_item: ContainerStyle,
415 pub match_highlight: HighlightStyle,
416}
417
418#[derive(Clone, Copy, Default, Deserialize)]
419pub struct SelectionStyle {
420 pub cursor: Color,
421 pub selection: Color,
422}
423
424#[derive(Clone, Deserialize, Default)]
425pub struct FieldEditor {
426 #[serde(flatten)]
427 pub container: ContainerStyle,
428 pub text: TextStyle,
429 #[serde(default)]
430 pub placeholder_text: Option<TextStyle>,
431 pub selection: SelectionStyle,
432}
433
434#[derive(Debug, Default, Clone, Copy)]
435pub struct Interactive<T> {
436 pub default: T,
437 pub hover: Option<T>,
438 pub active: Option<T>,
439 pub active_hover: Option<T>,
440}
441
442impl<T> Interactive<T> {
443 pub fn style_for(&self, state: &MouseState, active: bool) -> &T {
444 if active {
445 if state.hovered {
446 self.active_hover
447 .as_ref()
448 .or(self.active.as_ref())
449 .unwrap_or(&self.default)
450 } else {
451 self.active.as_ref().unwrap_or(&self.default)
452 }
453 } else {
454 if state.hovered {
455 self.hover.as_ref().unwrap_or(&self.default)
456 } else {
457 &self.default
458 }
459 }
460 }
461}
462
463impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
464 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
465 where
466 D: serde::Deserializer<'de>,
467 {
468 #[derive(Deserialize)]
469 struct Helper {
470 #[serde(flatten)]
471 default: Value,
472 hover: Option<Value>,
473 active: Option<Value>,
474 active_hover: Option<Value>,
475 }
476
477 let json = Helper::deserialize(deserializer)?;
478
479 let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
480 if let Some(mut state_json) = state_json {
481 if let Value::Object(state_json) = &mut state_json {
482 if let Value::Object(default) = &json.default {
483 for (key, value) in default {
484 if !state_json.contains_key(key) {
485 state_json.insert(key.clone(), value.clone());
486 }
487 }
488 }
489 }
490 Ok(Some(
491 serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
492 ))
493 } else {
494 Ok(None)
495 }
496 };
497
498 let hover = deserialize_state(json.hover)?;
499 let active = deserialize_state(json.active)?;
500 let active_hover = deserialize_state(json.active_hover)?;
501 let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
502
503 Ok(Interactive {
504 default,
505 hover,
506 active,
507 active_hover,
508 })
509 }
510}
511
512impl Editor {
513 pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
514 let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
515 if style_ix == 0 {
516 &self.selection
517 } else {
518 &self.guest_selections[style_ix - 1]
519 }
520 }
521}
522
523#[derive(Default)]
524pub struct SyntaxTheme {
525 pub highlights: Vec<(String, HighlightStyle)>,
526}
527
528impl SyntaxTheme {
529 pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
530 Self { highlights }
531 }
532}
533
534impl<'de> Deserialize<'de> for SyntaxTheme {
535 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
536 where
537 D: serde::Deserializer<'de>,
538 {
539 let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
540
541 let mut result = Self::new(Vec::new());
542 for (key, style) in syntax_data {
543 match result
544 .highlights
545 .binary_search_by(|(needle, _)| needle.cmp(&key))
546 {
547 Ok(i) | Err(i) => {
548 result.highlights.insert(i, (key, style));
549 }
550 }
551 }
552
553 Ok(result)
554 }
555}