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