theme.rs

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