1mod theme_registry;
2
3use gpui::{
4 color::Color,
5 elements::{ContainerStyle, ImageStyle, LabelStyle},
6 fonts::{HighlightStyle, TextStyle},
7 Border,
8};
9use serde::Deserialize;
10use std::{collections::HashMap, sync::Arc};
11
12pub use theme_registry::*;
13
14pub const DEFAULT_THEME_NAME: &'static str = "dark";
15
16#[derive(Deserialize, Default)]
17pub struct Theme {
18 #[serde(default)]
19 pub name: String,
20 pub workspace: Workspace,
21 pub chat_panel: ChatPanel,
22 pub contacts_panel: ContactsPanel,
23 pub project_panel: ProjectPanel,
24 pub command_palette: CommandPalette,
25 pub selector: Selector,
26 pub editor: Editor,
27 pub search: Search,
28 pub project_diagnostics: ProjectDiagnostics,
29 pub breadcrumbs: ContainedText,
30}
31
32#[derive(Deserialize, Default)]
33pub struct Workspace {
34 pub background: Color,
35 pub titlebar: Titlebar,
36 pub tab: Tab,
37 pub active_tab: Tab,
38 pub pane_divider: Border,
39 pub leader_border_opacity: f32,
40 pub leader_border_width: f32,
41 pub left_sidebar: Sidebar,
42 pub right_sidebar: Sidebar,
43 pub status_bar: StatusBar,
44 pub toolbar: Toolbar,
45 pub disconnected_overlay: ContainedText,
46}
47
48#[derive(Clone, Deserialize, Default)]
49pub struct Titlebar {
50 #[serde(flatten)]
51 pub container: ContainerStyle,
52 pub height: f32,
53 pub title: TextStyle,
54 pub avatar_width: f32,
55 pub avatar_ribbon: AvatarRibbon,
56 pub offline_icon: OfflineIcon,
57 pub share_icon_color: Color,
58 pub share_icon_active_color: Color,
59 pub avatar: ImageStyle,
60 pub sign_in_prompt: ContainedText,
61 pub hovered_sign_in_prompt: 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 Tab {
83 pub height: f32,
84 #[serde(flatten)]
85 pub container: ContainerStyle,
86 #[serde(flatten)]
87 pub label: LabelStyle,
88 pub spacing: f32,
89 pub icon_width: f32,
90 pub icon_close: Color,
91 pub icon_close_active: Color,
92 pub icon_dirty: Color,
93 pub icon_conflict: Color,
94}
95
96#[derive(Clone, Deserialize, Default)]
97pub struct Toolbar {
98 #[serde(flatten)]
99 pub container: ContainerStyle,
100 pub height: f32,
101 pub item_spacing: f32,
102}
103
104#[derive(Clone, Deserialize, Default)]
105pub struct Search {
106 #[serde(flatten)]
107 pub container: ContainerStyle,
108 pub editor: FindEditor,
109 pub invalid_editor: ContainerStyle,
110 pub option_button_group: ContainerStyle,
111 pub option_button: ContainedText,
112 pub active_option_button: ContainedText,
113 pub hovered_option_button: ContainedText,
114 pub active_hovered_option_button: ContainedText,
115 pub match_background: Color,
116 pub match_index: ContainedText,
117 pub results_status: TextStyle,
118 pub tab_icon_width: f32,
119 pub tab_icon_spacing: f32,
120}
121
122#[derive(Clone, Deserialize, Default)]
123pub struct FindEditor {
124 #[serde(flatten)]
125 pub input: FieldEditor,
126 pub min_width: f32,
127 pub max_width: f32,
128}
129
130#[derive(Deserialize, Default)]
131pub struct Sidebar {
132 #[serde(flatten)]
133 pub container: ContainerStyle,
134 pub width: f32,
135 pub item: SidebarItem,
136 pub active_item: SidebarItem,
137 pub resize_handle: ContainerStyle,
138}
139
140#[derive(Deserialize, Default)]
141pub struct SidebarItem {
142 pub icon_color: Color,
143 pub icon_size: f32,
144 pub height: f32,
145}
146
147#[derive(Deserialize, Default)]
148pub struct StatusBar {
149 #[serde(flatten)]
150 pub container: ContainerStyle,
151 pub height: f32,
152 pub item_spacing: f32,
153 pub cursor_position: TextStyle,
154 pub diagnostic_message: TextStyle,
155 pub lsp_message: TextStyle,
156 pub auto_update_progress_message: TextStyle,
157 pub auto_update_done_message: TextStyle,
158}
159
160#[derive(Deserialize, Default)]
161pub struct ChatPanel {
162 #[serde(flatten)]
163 pub container: ContainerStyle,
164 pub message: ChatMessage,
165 pub pending_message: ChatMessage,
166 pub channel_select: ChannelSelect,
167 pub input_editor: FieldEditor,
168 pub sign_in_prompt: TextStyle,
169 pub hovered_sign_in_prompt: TextStyle,
170}
171
172#[derive(Debug, Deserialize, Default)]
173pub struct ProjectPanel {
174 #[serde(flatten)]
175 pub container: ContainerStyle,
176 pub entry: ProjectPanelEntry,
177 pub hovered_entry: ProjectPanelEntry,
178 pub selected_entry: ProjectPanelEntry,
179 pub hovered_selected_entry: ProjectPanelEntry,
180}
181
182#[derive(Debug, Deserialize, Default)]
183pub struct ProjectPanelEntry {
184 pub height: f32,
185 #[serde(flatten)]
186 pub container: ContainerStyle,
187 pub text: TextStyle,
188 pub icon_color: Color,
189 pub icon_size: f32,
190 pub icon_spacing: f32,
191}
192
193#[derive(Debug, Deserialize, Default)]
194pub struct CommandPalette {
195 pub key: ContainedLabel,
196 pub keystroke_spacing: f32,
197}
198
199#[derive(Deserialize, Default)]
200pub struct ContactsPanel {
201 #[serde(flatten)]
202 pub container: ContainerStyle,
203 pub host_row_height: f32,
204 pub host_avatar: ImageStyle,
205 pub host_username: ContainedText,
206 pub tree_branch_width: f32,
207 pub tree_branch_color: Color,
208 pub shared_project: WorktreeRow,
209 pub hovered_shared_project: WorktreeRow,
210 pub unshared_project: WorktreeRow,
211 pub hovered_unshared_project: WorktreeRow,
212}
213
214#[derive(Deserialize, Default)]
215pub struct WorktreeRow {
216 #[serde(flatten)]
217 pub container: ContainerStyle,
218 pub height: f32,
219 pub name: ContainedText,
220 pub guest_avatar: ImageStyle,
221 pub guest_avatar_spacing: f32,
222}
223
224#[derive(Deserialize, Default)]
225pub struct ChatMessage {
226 #[serde(flatten)]
227 pub container: ContainerStyle,
228 pub body: TextStyle,
229 pub sender: ContainedText,
230 pub timestamp: ContainedText,
231}
232
233#[derive(Deserialize, Default)]
234pub struct ChannelSelect {
235 #[serde(flatten)]
236 pub container: ContainerStyle,
237 pub header: ChannelName,
238 pub item: ChannelName,
239 pub active_item: ChannelName,
240 pub hovered_item: ChannelName,
241 pub hovered_active_item: ChannelName,
242 pub menu: ContainerStyle,
243}
244
245#[derive(Deserialize, Default)]
246pub struct ChannelName {
247 #[serde(flatten)]
248 pub container: ContainerStyle,
249 pub hash: ContainedText,
250 pub name: TextStyle,
251}
252
253#[derive(Deserialize, Default)]
254pub struct Selector {
255 #[serde(flatten)]
256 pub container: ContainerStyle,
257 pub empty: ContainedLabel,
258 pub input_editor: FieldEditor,
259 pub item: ContainedLabel,
260 pub active_item: ContainedLabel,
261}
262
263#[derive(Clone, Debug, Deserialize, Default)]
264pub struct ContainedText {
265 #[serde(flatten)]
266 pub container: ContainerStyle,
267 #[serde(flatten)]
268 pub text: TextStyle,
269}
270
271#[derive(Clone, Debug, Deserialize, Default)]
272pub struct ContainedLabel {
273 #[serde(flatten)]
274 pub container: ContainerStyle,
275 #[serde(flatten)]
276 pub label: LabelStyle,
277}
278
279#[derive(Clone, Deserialize, Default)]
280pub struct ProjectDiagnostics {
281 #[serde(flatten)]
282 pub container: ContainerStyle,
283 pub empty_message: TextStyle,
284 pub status_bar_item: ContainedText,
285 pub tab_icon_width: f32,
286 pub tab_icon_spacing: f32,
287 pub tab_summary_spacing: f32,
288}
289
290#[derive(Clone, Deserialize, Default)]
291pub struct Editor {
292 pub text_color: Color,
293 #[serde(default)]
294 pub background: Color,
295 pub selection: SelectionStyle,
296 pub gutter_background: Color,
297 pub gutter_padding_factor: f32,
298 pub active_line_background: Color,
299 pub highlighted_line_background: Color,
300 pub rename_fade: f32,
301 pub document_highlight_read_background: Color,
302 pub document_highlight_write_background: Color,
303 pub diff_background_deleted: Color,
304 pub diff_background_inserted: Color,
305 pub line_number: Color,
306 pub line_number_active: Color,
307 pub guest_selections: Vec<SelectionStyle>,
308 pub syntax: Arc<SyntaxTheme>,
309 pub diagnostic_path_header: DiagnosticPathHeader,
310 pub diagnostic_header: DiagnosticHeader,
311 pub error_diagnostic: DiagnosticStyle,
312 pub invalid_error_diagnostic: DiagnosticStyle,
313 pub warning_diagnostic: DiagnosticStyle,
314 pub invalid_warning_diagnostic: DiagnosticStyle,
315 pub information_diagnostic: DiagnosticStyle,
316 pub invalid_information_diagnostic: DiagnosticStyle,
317 pub hint_diagnostic: DiagnosticStyle,
318 pub invalid_hint_diagnostic: DiagnosticStyle,
319 pub autocomplete: AutocompleteStyle,
320 pub code_actions_indicator: Color,
321 pub unnecessary_code_fade: f32,
322}
323
324#[derive(Clone, Deserialize, Default)]
325pub struct DiagnosticPathHeader {
326 #[serde(flatten)]
327 pub container: ContainerStyle,
328 pub filename: ContainedText,
329 pub path: ContainedText,
330 pub text_scale_factor: f32,
331}
332
333#[derive(Clone, Deserialize, Default)]
334pub struct DiagnosticHeader {
335 #[serde(flatten)]
336 pub container: ContainerStyle,
337 pub message: ContainedLabel,
338 pub code: ContainedText,
339 pub text_scale_factor: f32,
340 pub icon_width_factor: f32,
341}
342
343#[derive(Clone, Deserialize, Default)]
344pub struct DiagnosticStyle {
345 pub message: LabelStyle,
346 #[serde(default)]
347 pub header: ContainerStyle,
348 pub text_scale_factor: f32,
349}
350
351#[derive(Clone, Deserialize, Default)]
352pub struct AutocompleteStyle {
353 #[serde(flatten)]
354 pub container: ContainerStyle,
355 pub item: ContainerStyle,
356 pub selected_item: ContainerStyle,
357 pub hovered_item: ContainerStyle,
358 pub match_highlight: HighlightStyle,
359}
360
361#[derive(Clone, Copy, Default, Deserialize)]
362pub struct SelectionStyle {
363 pub cursor: Color,
364 pub selection: Color,
365}
366
367#[derive(Clone, Deserialize, Default)]
368pub struct FieldEditor {
369 #[serde(flatten)]
370 pub container: ContainerStyle,
371 pub text: TextStyle,
372 #[serde(default)]
373 pub placeholder_text: Option<TextStyle>,
374 pub selection: SelectionStyle,
375}
376
377impl Editor {
378 pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
379 let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
380 if style_ix == 0 {
381 &self.selection
382 } else {
383 &self.guest_selections[style_ix - 1]
384 }
385 }
386}
387
388#[derive(Default)]
389pub struct SyntaxTheme {
390 pub highlights: Vec<(String, HighlightStyle)>,
391}
392
393impl SyntaxTheme {
394 pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
395 Self { highlights }
396 }
397}
398
399impl<'de> Deserialize<'de> for SyntaxTheme {
400 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
401 where
402 D: serde::Deserializer<'de>,
403 {
404 let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
405
406 let mut result = Self::new(Vec::new());
407 for (key, style) in syntax_data {
408 match result
409 .highlights
410 .binary_search_by(|(needle, _)| needle.cmp(&key))
411 {
412 Ok(i) | Err(i) => {
413 result.highlights.insert(i, (key, style));
414 }
415 }
416 }
417
418 Ok(result)
419 }
420}