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