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