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