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