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