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