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