theme.rs

  1mod theme_registry;
  2
  3use gpui::{
  4    color::Color,
  5    elements::{ContainerStyle, ImageStyle, LabelStyle},
  6    fonts::{HighlightStyle, TextStyle},
  7    Border,
  8};
  9use serde::Deserialize;
 10use std::{collections::HashMap, sync::Arc};
 11
 12pub use theme_registry::*;
 13
 14pub const DEFAULT_THEME_NAME: &'static str = "dark";
 15
 16#[derive(Deserialize, Default)]
 17pub struct Theme {
 18    #[serde(default)]
 19    pub name: String,
 20    pub workspace: Workspace,
 21    pub chat_panel: ChatPanel,
 22    pub contacts_panel: ContactsPanel,
 23    pub project_panel: ProjectPanel,
 24    pub command_palette: CommandPalette,
 25    pub selector: Selector,
 26    pub editor: Editor,
 27    pub search: Search,
 28    pub project_diagnostics: ProjectDiagnostics,
 29    pub breadcrumbs: ContainedText,
 30}
 31
 32#[derive(Deserialize, Default)]
 33pub struct Workspace {
 34    pub background: Color,
 35    pub titlebar: Titlebar,
 36    pub tab: Tab,
 37    pub active_tab: Tab,
 38    pub pane_divider: Border,
 39    pub leader_border_opacity: f32,
 40    pub leader_border_width: f32,
 41    pub left_sidebar: Sidebar,
 42    pub right_sidebar: Sidebar,
 43    pub status_bar: StatusBar,
 44    pub toolbar: Toolbar,
 45    pub disconnected_overlay: ContainedText,
 46    pub modal: ContainerStyle,
 47}
 48
 49#[derive(Clone, Deserialize, Default)]
 50pub struct Titlebar {
 51    #[serde(flatten)]
 52    pub container: ContainerStyle,
 53    pub height: f32,
 54    pub title: TextStyle,
 55    pub avatar_width: f32,
 56    pub avatar_ribbon: AvatarRibbon,
 57    pub offline_icon: OfflineIcon,
 58    pub share_icon: ShareIcon,
 59    pub hovered_share_icon: ShareIcon,
 60    pub active_share_icon: ShareIcon,
 61    pub hovered_active_share_icon: ShareIcon,
 62    pub avatar: ImageStyle,
 63    pub sign_in_prompt: ContainedText,
 64    pub hovered_sign_in_prompt: 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 Search {
116    #[serde(flatten)]
117    pub container: ContainerStyle,
118    pub editor: FindEditor,
119    pub invalid_editor: ContainerStyle,
120    pub option_button_group: ContainerStyle,
121    pub option_button: ContainedText,
122    pub active_option_button: ContainedText,
123    pub hovered_option_button: ContainedText,
124    pub active_hovered_option_button: ContainedText,
125    pub match_background: Color,
126    pub match_index: ContainedText,
127    pub results_status: TextStyle,
128    pub tab_icon_width: f32,
129    pub tab_icon_spacing: f32,
130}
131
132#[derive(Clone, Deserialize, Default)]
133pub struct FindEditor {
134    #[serde(flatten)]
135    pub input: FieldEditor,
136    pub min_width: f32,
137    pub max_width: f32,
138}
139
140#[derive(Deserialize, Default)]
141pub struct Sidebar {
142    #[serde(flatten)]
143    pub container: ContainerStyle,
144    pub width: f32,
145    pub item: SidebarItem,
146    pub active_item: SidebarItem,
147    pub resize_handle: ContainerStyle,
148}
149
150#[derive(Deserialize, Default)]
151pub struct SidebarItem {
152    pub icon_color: Color,
153    pub icon_size: f32,
154    pub height: f32,
155}
156
157#[derive(Deserialize, Default)]
158pub struct StatusBar {
159    #[serde(flatten)]
160    pub container: ContainerStyle,
161    pub height: f32,
162    pub item_spacing: f32,
163    pub cursor_position: TextStyle,
164    pub diagnostic_message: TextStyle,
165    pub lsp_message: TextStyle,
166    pub auto_update_progress_message: TextStyle,
167    pub auto_update_done_message: TextStyle,
168}
169
170#[derive(Deserialize, Default)]
171pub struct ChatPanel {
172    #[serde(flatten)]
173    pub container: ContainerStyle,
174    pub message: ChatMessage,
175    pub pending_message: ChatMessage,
176    pub channel_select: ChannelSelect,
177    pub input_editor: FieldEditor,
178    pub sign_in_prompt: TextStyle,
179    pub hovered_sign_in_prompt: TextStyle,
180}
181
182#[derive(Debug, Deserialize, Default)]
183pub struct ProjectPanel {
184    #[serde(flatten)]
185    pub container: ContainerStyle,
186    pub entry: ProjectPanelEntry,
187    pub hovered_entry: ProjectPanelEntry,
188    pub selected_entry: ProjectPanelEntry,
189    pub hovered_selected_entry: ProjectPanelEntry,
190}
191
192#[derive(Debug, Deserialize, Default)]
193pub struct ProjectPanelEntry {
194    pub height: f32,
195    #[serde(flatten)]
196    pub container: ContainerStyle,
197    pub text: TextStyle,
198    pub icon_color: Color,
199    pub icon_size: f32,
200    pub icon_spacing: f32,
201}
202
203#[derive(Debug, Deserialize, Default)]
204pub struct CommandPalette {
205    pub key: ContainedLabel,
206    pub keystroke_spacing: f32,
207}
208
209#[derive(Deserialize, Default)]
210pub struct ContactsPanel {
211    #[serde(flatten)]
212    pub container: ContainerStyle,
213    pub host_row_height: f32,
214    pub host_avatar: ImageStyle,
215    pub host_username: ContainedText,
216    pub tree_branch_width: f32,
217    pub tree_branch_color: Color,
218    pub shared_project: WorktreeRow,
219    pub hovered_shared_project: WorktreeRow,
220    pub unshared_project: WorktreeRow,
221    pub hovered_unshared_project: WorktreeRow,
222}
223
224#[derive(Deserialize, Default)]
225pub struct WorktreeRow {
226    #[serde(flatten)]
227    pub container: ContainerStyle,
228    pub height: f32,
229    pub name: ContainedText,
230    pub guest_avatar: ImageStyle,
231    pub guest_avatar_spacing: f32,
232}
233
234#[derive(Deserialize, Default)]
235pub struct ChatMessage {
236    #[serde(flatten)]
237    pub container: ContainerStyle,
238    pub body: TextStyle,
239    pub sender: ContainedText,
240    pub timestamp: ContainedText,
241}
242
243#[derive(Deserialize, Default)]
244pub struct ChannelSelect {
245    #[serde(flatten)]
246    pub container: ContainerStyle,
247    pub header: ChannelName,
248    pub item: ChannelName,
249    pub active_item: ChannelName,
250    pub hovered_item: ChannelName,
251    pub hovered_active_item: ChannelName,
252    pub menu: ContainerStyle,
253}
254
255#[derive(Deserialize, Default)]
256pub struct ChannelName {
257    #[serde(flatten)]
258    pub container: ContainerStyle,
259    pub hash: ContainedText,
260    pub name: TextStyle,
261}
262
263#[derive(Deserialize, Default)]
264pub struct Selector {
265    #[serde(flatten)]
266    pub container: ContainerStyle,
267    pub empty: ContainedLabel,
268    pub input_editor: FieldEditor,
269    pub item: ContainedLabel,
270    pub active_item: ContainedLabel,
271}
272
273#[derive(Clone, Debug, Deserialize, Default)]
274pub struct ContainedText {
275    #[serde(flatten)]
276    pub container: ContainerStyle,
277    #[serde(flatten)]
278    pub text: TextStyle,
279}
280
281#[derive(Clone, Debug, Deserialize, Default)]
282pub struct ContainedLabel {
283    #[serde(flatten)]
284    pub container: ContainerStyle,
285    #[serde(flatten)]
286    pub label: LabelStyle,
287}
288
289#[derive(Clone, Deserialize, Default)]
290pub struct ProjectDiagnostics {
291    #[serde(flatten)]
292    pub container: ContainerStyle,
293    pub empty_message: TextStyle,
294    pub status_bar_item: ContainedText,
295    pub tab_icon_width: f32,
296    pub tab_icon_spacing: f32,
297    pub tab_summary_spacing: f32,
298}
299
300#[derive(Clone, Deserialize, Default)]
301pub struct Editor {
302    pub text_color: Color,
303    #[serde(default)]
304    pub background: Color,
305    pub selection: SelectionStyle,
306    pub gutter_background: Color,
307    pub gutter_padding_factor: f32,
308    pub active_line_background: Color,
309    pub highlighted_line_background: Color,
310    pub rename_fade: f32,
311    pub document_highlight_read_background: Color,
312    pub document_highlight_write_background: Color,
313    pub diff_background_deleted: Color,
314    pub diff_background_inserted: Color,
315    pub line_number: Color,
316    pub line_number_active: Color,
317    pub guest_selections: Vec<SelectionStyle>,
318    pub syntax: Arc<SyntaxTheme>,
319    pub diagnostic_path_header: DiagnosticPathHeader,
320    pub diagnostic_header: DiagnosticHeader,
321    pub error_diagnostic: DiagnosticStyle,
322    pub invalid_error_diagnostic: DiagnosticStyle,
323    pub warning_diagnostic: DiagnosticStyle,
324    pub invalid_warning_diagnostic: DiagnosticStyle,
325    pub information_diagnostic: DiagnosticStyle,
326    pub invalid_information_diagnostic: DiagnosticStyle,
327    pub hint_diagnostic: DiagnosticStyle,
328    pub invalid_hint_diagnostic: DiagnosticStyle,
329    pub autocomplete: AutocompleteStyle,
330    pub code_actions_indicator: Color,
331    pub unnecessary_code_fade: f32,
332}
333
334#[derive(Clone, Deserialize, Default)]
335pub struct DiagnosticPathHeader {
336    #[serde(flatten)]
337    pub container: ContainerStyle,
338    pub filename: ContainedText,
339    pub path: ContainedText,
340    pub text_scale_factor: f32,
341}
342
343#[derive(Clone, Deserialize, Default)]
344pub struct DiagnosticHeader {
345    #[serde(flatten)]
346    pub container: ContainerStyle,
347    pub message: ContainedLabel,
348    pub code: ContainedText,
349    pub text_scale_factor: f32,
350    pub icon_width_factor: f32,
351}
352
353#[derive(Clone, Deserialize, Default)]
354pub struct DiagnosticStyle {
355    pub message: LabelStyle,
356    #[serde(default)]
357    pub header: ContainerStyle,
358    pub text_scale_factor: f32,
359}
360
361#[derive(Clone, Deserialize, Default)]
362pub struct AutocompleteStyle {
363    #[serde(flatten)]
364    pub container: ContainerStyle,
365    pub item: ContainerStyle,
366    pub selected_item: ContainerStyle,
367    pub hovered_item: ContainerStyle,
368    pub match_highlight: HighlightStyle,
369}
370
371#[derive(Clone, Copy, Default, Deserialize)]
372pub struct SelectionStyle {
373    pub cursor: Color,
374    pub selection: Color,
375}
376
377#[derive(Clone, Deserialize, Default)]
378pub struct FieldEditor {
379    #[serde(flatten)]
380    pub container: ContainerStyle,
381    pub text: TextStyle,
382    #[serde(default)]
383    pub placeholder_text: Option<TextStyle>,
384    pub selection: SelectionStyle,
385}
386
387impl Editor {
388    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
389        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
390        if style_ix == 0 {
391            &self.selection
392        } else {
393            &self.guest_selections[style_ix - 1]
394        }
395    }
396}
397
398#[derive(Default)]
399pub struct SyntaxTheme {
400    pub highlights: Vec<(String, HighlightStyle)>,
401}
402
403impl SyntaxTheme {
404    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
405        Self { highlights }
406    }
407}
408
409impl<'de> Deserialize<'de> for SyntaxTheme {
410    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
411    where
412        D: serde::Deserializer<'de>,
413    {
414        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
415
416        let mut result = Self::new(Vec::new());
417        for (key, style) in syntax_data {
418            match result
419                .highlights
420                .binary_search_by(|(needle, _)| needle.cmp(&key))
421            {
422                Ok(i) | Err(i) => {
423                    result.highlights.insert(i, (key, style));
424                }
425            }
426        }
427
428        Ok(result)
429    }
430}