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    pub notification: ContainerStyle,
 49    pub notifications: Notifications,
 50}
 51
 52#[derive(Clone, Deserialize, Default)]
 53pub struct Titlebar {
 54    #[serde(flatten)]
 55    pub container: ContainerStyle,
 56    pub height: f32,
 57    pub title: TextStyle,
 58    pub avatar_width: f32,
 59    pub avatar_margin: f32,
 60    pub avatar_ribbon: AvatarRibbon,
 61    pub offline_icon: OfflineIcon,
 62    pub share_icon: Interactive<ShareIcon>,
 63    pub avatar: ImageStyle,
 64    pub sign_in_prompt: Interactive<ContainedText>,
 65    pub outdated_warning: ContainedText,
 66}
 67
 68#[derive(Clone, Deserialize, Default)]
 69pub struct AvatarRibbon {
 70    #[serde(flatten)]
 71    pub container: ContainerStyle,
 72    pub width: f32,
 73    pub height: f32,
 74}
 75
 76#[derive(Clone, Deserialize, Default)]
 77pub struct OfflineIcon {
 78    #[serde(flatten)]
 79    pub container: ContainerStyle,
 80    pub width: f32,
 81    pub color: Color,
 82}
 83
 84#[derive(Clone, Deserialize, Default)]
 85pub struct ShareIcon {
 86    #[serde(flatten)]
 87    pub container: ContainerStyle,
 88    pub color: Color,
 89}
 90
 91#[derive(Clone, Deserialize, Default)]
 92pub struct Tab {
 93    pub height: f32,
 94    #[serde(flatten)]
 95    pub container: ContainerStyle,
 96    #[serde(flatten)]
 97    pub label: LabelStyle,
 98    pub spacing: f32,
 99    pub icon_width: f32,
100    pub icon_close: Color,
101    pub icon_close_active: Color,
102    pub icon_dirty: Color,
103    pub icon_conflict: Color,
104}
105
106#[derive(Clone, Deserialize, Default)]
107pub struct Toolbar {
108    #[serde(flatten)]
109    pub container: ContainerStyle,
110    pub height: f32,
111    pub item_spacing: f32,
112}
113
114#[derive(Clone, Deserialize, Default)]
115pub struct Notifications {
116    #[serde(flatten)]
117    pub container: ContainerStyle,
118    pub width: f32,
119}
120
121#[derive(Clone, Deserialize, Default)]
122pub struct Search {
123    #[serde(flatten)]
124    pub container: ContainerStyle,
125    pub editor: FindEditor,
126    pub invalid_editor: ContainerStyle,
127    pub option_button_group: ContainerStyle,
128    pub option_button: Interactive<ContainedText>,
129    pub match_background: Color,
130    pub match_index: ContainedText,
131    pub results_status: TextStyle,
132    pub tab_icon_width: f32,
133    pub tab_icon_spacing: f32,
134}
135
136#[derive(Clone, Deserialize, Default)]
137pub struct FindEditor {
138    #[serde(flatten)]
139    pub input: FieldEditor,
140    pub min_width: f32,
141    pub max_width: f32,
142}
143
144#[derive(Deserialize, Default)]
145pub struct StatusBar {
146    #[serde(flatten)]
147    pub container: ContainerStyle,
148    pub height: f32,
149    pub item_spacing: f32,
150    pub cursor_position: TextStyle,
151    pub auto_update_progress_message: TextStyle,
152    pub auto_update_done_message: TextStyle,
153    pub lsp_status: Interactive<StatusBarLspStatus>,
154    pub sidebar_buttons: StatusBarSidebarButtons,
155    pub diagnostic_summary: Interactive<StatusBarDiagnosticSummary>,
156    pub diagnostic_message: Interactive<ContainedText>,
157}
158
159#[derive(Deserialize, Default)]
160pub struct StatusBarSidebarButtons {
161    pub group_left: ContainerStyle,
162    pub group_right: ContainerStyle,
163    pub item: Interactive<SidebarItem>,
164}
165
166#[derive(Deserialize, Default)]
167pub struct StatusBarDiagnosticSummary {
168    pub container_ok: ContainerStyle,
169    pub container_warning: ContainerStyle,
170    pub container_error: ContainerStyle,
171    pub text: TextStyle,
172    pub icon_color_ok: Color,
173    pub icon_color_warning: Color,
174    pub icon_color_error: Color,
175    pub height: f32,
176    pub icon_width: f32,
177    pub icon_spacing: f32,
178    pub summary_spacing: f32,
179}
180
181#[derive(Deserialize, Default)]
182pub struct StatusBarLspStatus {
183    #[serde(flatten)]
184    pub container: ContainerStyle,
185    pub height: f32,
186    pub icon_spacing: f32,
187    pub icon_color: Color,
188    pub icon_width: f32,
189    pub message: TextStyle,
190}
191
192#[derive(Deserialize, Default)]
193pub struct Sidebar {
194    pub resize_handle: ContainerStyle,
195}
196
197#[derive(Clone, Copy, Deserialize, Default)]
198pub struct SidebarItem {
199    #[serde(flatten)]
200    pub container: ContainerStyle,
201    pub icon_color: Color,
202    pub icon_size: f32,
203}
204
205#[derive(Deserialize, Default)]
206pub struct ChatPanel {
207    #[serde(flatten)]
208    pub container: ContainerStyle,
209    pub message: ChatMessage,
210    pub pending_message: ChatMessage,
211    pub channel_select: ChannelSelect,
212    pub input_editor: FieldEditor,
213    pub sign_in_prompt: TextStyle,
214    pub hovered_sign_in_prompt: TextStyle,
215}
216
217#[derive(Deserialize, Default)]
218pub struct ProjectPanel {
219    #[serde(flatten)]
220    pub container: ContainerStyle,
221    pub entry: Interactive<ProjectPanelEntry>,
222    pub filename_editor: FieldEditor,
223    pub indent_width: f32,
224}
225
226#[derive(Debug, Deserialize, Default)]
227pub struct ProjectPanelEntry {
228    pub height: f32,
229    #[serde(flatten)]
230    pub container: ContainerStyle,
231    pub text: TextStyle,
232    pub icon_color: Color,
233    pub icon_size: f32,
234    pub icon_spacing: f32,
235}
236
237#[derive(Debug, Deserialize, Default)]
238pub struct CommandPalette {
239    pub key: Interactive<ContainedLabel>,
240    pub keystroke_spacing: f32,
241}
242
243#[derive(Deserialize, Default)]
244pub struct ContactsPanel {
245    #[serde(flatten)]
246    pub container: ContainerStyle,
247    pub header: ContainedText,
248    pub user_query_editor: FieldEditor,
249    pub user_query_editor_height: f32,
250    pub add_contact_button: IconButton,
251    pub row: ContainerStyle,
252    pub row_height: f32,
253    pub contact_avatar: ImageStyle,
254    pub contact_username: ContainedText,
255    pub contact_button: Interactive<IconButton>,
256    pub disabled_contact_button: IconButton,
257    pub tree_branch_width: f32,
258    pub tree_branch_color: Color,
259    pub shared_project: ProjectRow,
260    pub hovered_shared_project: ProjectRow,
261    pub unshared_project: ProjectRow,
262    pub hovered_unshared_project: ProjectRow,
263}
264
265#[derive(Deserialize, Default)]
266pub struct ContactFinder {
267    pub row_height: f32,
268    pub contact_avatar: ImageStyle,
269    pub contact_username: ContainerStyle,
270    pub contact_button: IconButton,
271    pub disabled_contact_button: IconButton,
272}
273
274#[derive(Deserialize, Default)]
275pub struct IconButton {
276    #[serde(flatten)]
277    pub container: ContainerStyle,
278    pub color: Color,
279    pub icon_width: f32,
280    pub button_width: f32,
281}
282
283#[derive(Deserialize, Default)]
284pub struct ProjectRow {
285    #[serde(flatten)]
286    pub container: ContainerStyle,
287    pub height: f32,
288    pub name: ContainedText,
289    pub guest_avatar: ImageStyle,
290    pub guest_avatar_spacing: f32,
291}
292
293#[derive(Deserialize, Default)]
294pub struct ChatMessage {
295    #[serde(flatten)]
296    pub container: ContainerStyle,
297    pub body: TextStyle,
298    pub sender: ContainedText,
299    pub timestamp: ContainedText,
300}
301
302#[derive(Deserialize, Default)]
303pub struct ChannelSelect {
304    #[serde(flatten)]
305    pub container: ContainerStyle,
306    pub header: ChannelName,
307    pub item: ChannelName,
308    pub active_item: ChannelName,
309    pub hovered_item: ChannelName,
310    pub hovered_active_item: ChannelName,
311    pub menu: ContainerStyle,
312}
313
314#[derive(Deserialize, Default)]
315pub struct ChannelName {
316    #[serde(flatten)]
317    pub container: ContainerStyle,
318    pub hash: ContainedText,
319    pub name: TextStyle,
320}
321
322#[derive(Deserialize, Default)]
323pub struct Picker {
324    #[serde(flatten)]
325    pub container: ContainerStyle,
326    pub empty: ContainedLabel,
327    pub input_editor: FieldEditor,
328    pub item: Interactive<ContainedLabel>,
329}
330
331#[derive(Clone, Debug, Deserialize, Default)]
332pub struct ContainedText {
333    #[serde(flatten)]
334    pub container: ContainerStyle,
335    #[serde(flatten)]
336    pub text: TextStyle,
337}
338
339#[derive(Clone, Debug, Deserialize, Default)]
340pub struct ContainedLabel {
341    #[serde(flatten)]
342    pub container: ContainerStyle,
343    #[serde(flatten)]
344    pub label: LabelStyle,
345}
346
347#[derive(Clone, Deserialize, Default)]
348pub struct ProjectDiagnostics {
349    #[serde(flatten)]
350    pub container: ContainerStyle,
351    pub empty_message: TextStyle,
352    pub tab_icon_width: f32,
353    pub tab_icon_spacing: f32,
354    pub tab_summary_spacing: f32,
355}
356
357#[derive(Clone, Deserialize, Default)]
358pub struct Editor {
359    pub text_color: Color,
360    #[serde(default)]
361    pub background: Color,
362    pub selection: SelectionStyle,
363    pub gutter_background: Color,
364    pub gutter_padding_factor: f32,
365    pub active_line_background: Color,
366    pub highlighted_line_background: Color,
367    pub rename_fade: f32,
368    pub document_highlight_read_background: Color,
369    pub document_highlight_write_background: Color,
370    pub diff_background_deleted: Color,
371    pub diff_background_inserted: Color,
372    pub line_number: Color,
373    pub line_number_active: Color,
374    pub guest_selections: Vec<SelectionStyle>,
375    pub syntax: Arc<SyntaxTheme>,
376    pub diagnostic_path_header: DiagnosticPathHeader,
377    pub diagnostic_header: DiagnosticHeader,
378    pub error_diagnostic: DiagnosticStyle,
379    pub invalid_error_diagnostic: DiagnosticStyle,
380    pub warning_diagnostic: DiagnosticStyle,
381    pub invalid_warning_diagnostic: DiagnosticStyle,
382    pub information_diagnostic: DiagnosticStyle,
383    pub invalid_information_diagnostic: DiagnosticStyle,
384    pub hint_diagnostic: DiagnosticStyle,
385    pub invalid_hint_diagnostic: DiagnosticStyle,
386    pub autocomplete: AutocompleteStyle,
387    pub code_actions_indicator: Color,
388    pub unnecessary_code_fade: f32,
389}
390
391#[derive(Clone, Deserialize, Default)]
392pub struct DiagnosticPathHeader {
393    #[serde(flatten)]
394    pub container: ContainerStyle,
395    pub filename: ContainedText,
396    pub path: ContainedText,
397    pub text_scale_factor: f32,
398}
399
400#[derive(Clone, Deserialize, Default)]
401pub struct DiagnosticHeader {
402    #[serde(flatten)]
403    pub container: ContainerStyle,
404    pub message: ContainedLabel,
405    pub code: ContainedText,
406    pub text_scale_factor: f32,
407    pub icon_width_factor: f32,
408}
409
410#[derive(Clone, Deserialize, Default)]
411pub struct DiagnosticStyle {
412    pub message: LabelStyle,
413    #[serde(default)]
414    pub header: ContainerStyle,
415    pub text_scale_factor: f32,
416}
417
418#[derive(Clone, Deserialize, Default)]
419pub struct AutocompleteStyle {
420    #[serde(flatten)]
421    pub container: ContainerStyle,
422    pub item: ContainerStyle,
423    pub selected_item: ContainerStyle,
424    pub hovered_item: ContainerStyle,
425    pub match_highlight: HighlightStyle,
426}
427
428#[derive(Clone, Copy, Default, Deserialize)]
429pub struct SelectionStyle {
430    pub cursor: Color,
431    pub selection: Color,
432}
433
434#[derive(Clone, Deserialize, Default)]
435pub struct FieldEditor {
436    #[serde(flatten)]
437    pub container: ContainerStyle,
438    pub text: TextStyle,
439    #[serde(default)]
440    pub placeholder_text: Option<TextStyle>,
441    pub selection: SelectionStyle,
442}
443
444#[derive(Debug, Default, Clone, Copy)]
445pub struct Interactive<T> {
446    pub default: T,
447    pub hover: Option<T>,
448    pub active: Option<T>,
449    pub active_hover: Option<T>,
450}
451
452impl<T> Interactive<T> {
453    pub fn style_for(&self, state: &MouseState, active: bool) -> &T {
454        if active {
455            if state.hovered {
456                self.active_hover
457                    .as_ref()
458                    .or(self.active.as_ref())
459                    .unwrap_or(&self.default)
460            } else {
461                self.active.as_ref().unwrap_or(&self.default)
462            }
463        } else {
464            if state.hovered {
465                self.hover.as_ref().unwrap_or(&self.default)
466            } else {
467                &self.default
468            }
469        }
470    }
471}
472
473impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
474    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
475    where
476        D: serde::Deserializer<'de>,
477    {
478        #[derive(Deserialize)]
479        struct Helper {
480            #[serde(flatten)]
481            default: Value,
482            hover: Option<Value>,
483            active: Option<Value>,
484            active_hover: Option<Value>,
485        }
486
487        let json = Helper::deserialize(deserializer)?;
488
489        let deserialize_state = |state_json: Option<Value>| -> Result<Option<T>, D::Error> {
490            if let Some(mut state_json) = state_json {
491                if let Value::Object(state_json) = &mut state_json {
492                    if let Value::Object(default) = &json.default {
493                        for (key, value) in default {
494                            if !state_json.contains_key(key) {
495                                state_json.insert(key.clone(), value.clone());
496                            }
497                        }
498                    }
499                }
500                Ok(Some(
501                    serde_json::from_value::<T>(state_json).map_err(serde::de::Error::custom)?,
502                ))
503            } else {
504                Ok(None)
505            }
506        };
507
508        let hover = deserialize_state(json.hover)?;
509        let active = deserialize_state(json.active)?;
510        let active_hover = deserialize_state(json.active_hover)?;
511        let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
512
513        Ok(Interactive {
514            default,
515            hover,
516            active,
517            active_hover,
518        })
519    }
520}
521
522impl Editor {
523    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
524        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
525        if style_ix == 0 {
526            &self.selection
527        } else {
528            &self.guest_selections[style_ix - 1]
529        }
530    }
531}
532
533#[derive(Default)]
534pub struct SyntaxTheme {
535    pub highlights: Vec<(String, HighlightStyle)>,
536}
537
538impl SyntaxTheme {
539    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
540        Self { highlights }
541    }
542}
543
544impl<'de> Deserialize<'de> for SyntaxTheme {
545    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
546    where
547        D: serde::Deserializer<'de>,
548    {
549        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
550
551        let mut result = Self::new(Vec::new());
552        for (key, style) in syntax_data {
553            match result
554                .highlights
555                .binary_search_by(|(needle, _)| needle.cmp(&key))
556            {
557                Ok(i) | Err(i) => {
558                    result.highlights.insert(i, (key, style));
559                }
560            }
561        }
562
563        Ok(result)
564    }
565}