theme.rs

  1mod resolution;
  2mod theme_registry;
  3
  4use gpui::{
  5    color::Color,
  6    elements::{ContainerStyle, ImageStyle, LabelStyle},
  7    fonts::{HighlightStyle, TextStyle},
  8    Border,
  9};
 10use serde::Deserialize;
 11use std::{collections::HashMap, sync::Arc};
 12
 13pub use theme_registry::*;
 14
 15pub const DEFAULT_THEME_NAME: &'static str = "black";
 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 project_panel: ProjectPanel,
 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(Deserialize, Default)]
192pub struct ContactsPanel {
193    #[serde(flatten)]
194    pub container: ContainerStyle,
195    pub host_row_height: f32,
196    pub host_avatar: ImageStyle,
197    pub host_username: ContainedText,
198    pub tree_branch_width: f32,
199    pub tree_branch_color: Color,
200    pub shared_project: WorktreeRow,
201    pub hovered_shared_project: WorktreeRow,
202    pub unshared_project: WorktreeRow,
203    pub hovered_unshared_project: WorktreeRow,
204}
205
206#[derive(Deserialize, Default)]
207pub struct WorktreeRow {
208    #[serde(flatten)]
209    pub container: ContainerStyle,
210    pub height: f32,
211    pub name: ContainedText,
212    pub guest_avatar: ImageStyle,
213    pub guest_avatar_spacing: f32,
214}
215
216#[derive(Deserialize, Default)]
217pub struct ChatMessage {
218    #[serde(flatten)]
219    pub container: ContainerStyle,
220    pub body: TextStyle,
221    pub sender: ContainedText,
222    pub timestamp: ContainedText,
223}
224
225#[derive(Deserialize, Default)]
226pub struct ChannelSelect {
227    #[serde(flatten)]
228    pub container: ContainerStyle,
229    pub header: ChannelName,
230    pub item: ChannelName,
231    pub active_item: ChannelName,
232    pub hovered_item: ChannelName,
233    pub hovered_active_item: ChannelName,
234    pub menu: ContainerStyle,
235}
236
237#[derive(Deserialize, Default)]
238pub struct ChannelName {
239    #[serde(flatten)]
240    pub container: ContainerStyle,
241    pub hash: ContainedText,
242    pub name: TextStyle,
243}
244
245#[derive(Deserialize, Default)]
246pub struct Selector {
247    #[serde(flatten)]
248    pub container: ContainerStyle,
249    pub empty: ContainedLabel,
250    pub input_editor: FieldEditor,
251    pub item: ContainedLabel,
252    pub active_item: ContainedLabel,
253}
254
255#[derive(Clone, Debug, Deserialize, Default)]
256pub struct ContainedText {
257    #[serde(flatten)]
258    pub container: ContainerStyle,
259    #[serde(flatten)]
260    pub text: TextStyle,
261}
262
263#[derive(Clone, Deserialize, Default)]
264pub struct ContainedLabel {
265    #[serde(flatten)]
266    pub container: ContainerStyle,
267    #[serde(flatten)]
268    pub label: LabelStyle,
269}
270
271#[derive(Clone, Deserialize, Default)]
272pub struct ProjectDiagnostics {
273    #[serde(flatten)]
274    pub container: ContainerStyle,
275    pub empty_message: TextStyle,
276    pub status_bar_item: ContainedText,
277    pub tab_icon_width: f32,
278    pub tab_icon_spacing: f32,
279    pub tab_summary_spacing: f32,
280}
281
282#[derive(Clone, Deserialize, Default)]
283pub struct Editor {
284    pub text_color: Color,
285    #[serde(default)]
286    pub background: Color,
287    pub selection: SelectionStyle,
288    pub gutter_background: Color,
289    pub gutter_padding_factor: f32,
290    pub active_line_background: Color,
291    pub highlighted_line_background: Color,
292    pub rename_fade: f32,
293    pub document_highlight_read_background: Color,
294    pub document_highlight_write_background: Color,
295    pub diff_background_deleted: Color,
296    pub diff_background_inserted: Color,
297    pub line_number: Color,
298    pub line_number_active: Color,
299    pub guest_selections: Vec<SelectionStyle>,
300    pub syntax: Arc<SyntaxTheme>,
301    pub diagnostic_path_header: DiagnosticPathHeader,
302    pub diagnostic_header: DiagnosticHeader,
303    pub error_diagnostic: DiagnosticStyle,
304    pub invalid_error_diagnostic: DiagnosticStyle,
305    pub warning_diagnostic: DiagnosticStyle,
306    pub invalid_warning_diagnostic: DiagnosticStyle,
307    pub information_diagnostic: DiagnosticStyle,
308    pub invalid_information_diagnostic: DiagnosticStyle,
309    pub hint_diagnostic: DiagnosticStyle,
310    pub invalid_hint_diagnostic: DiagnosticStyle,
311    pub autocomplete: AutocompleteStyle,
312    pub code_actions_indicator: Color,
313    pub unnecessary_code_fade: f32,
314}
315
316#[derive(Clone, Deserialize, Default)]
317pub struct DiagnosticPathHeader {
318    #[serde(flatten)]
319    pub container: ContainerStyle,
320    pub filename: ContainedText,
321    pub path: ContainedText,
322    pub text_scale_factor: f32,
323}
324
325#[derive(Clone, Deserialize, Default)]
326pub struct DiagnosticHeader {
327    #[serde(flatten)]
328    pub container: ContainerStyle,
329    pub message: ContainedLabel,
330    pub code: ContainedText,
331    pub text_scale_factor: f32,
332    pub icon_width_factor: f32,
333}
334
335#[derive(Clone, Deserialize, Default)]
336pub struct DiagnosticStyle {
337    pub message: LabelStyle,
338    #[serde(default)]
339    pub header: ContainerStyle,
340    pub text_scale_factor: f32,
341}
342
343#[derive(Clone, Deserialize, Default)]
344pub struct AutocompleteStyle {
345    #[serde(flatten)]
346    pub container: ContainerStyle,
347    pub item: ContainerStyle,
348    pub selected_item: ContainerStyle,
349    pub hovered_item: ContainerStyle,
350    pub match_highlight: HighlightStyle,
351}
352
353#[derive(Clone, Copy, Default, Deserialize)]
354pub struct SelectionStyle {
355    pub cursor: Color,
356    pub selection: Color,
357}
358
359#[derive(Clone, Deserialize, Default)]
360pub struct FieldEditor {
361    #[serde(flatten)]
362    pub container: ContainerStyle,
363    pub text: TextStyle,
364    #[serde(default)]
365    pub placeholder_text: Option<TextStyle>,
366    pub selection: SelectionStyle,
367}
368
369impl Editor {
370    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
371        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
372        if style_ix == 0 {
373            &self.selection
374        } else {
375            &self.guest_selections[style_ix - 1]
376        }
377    }
378}
379
380#[derive(Default)]
381pub struct SyntaxTheme {
382    pub highlights: Vec<(String, HighlightStyle)>,
383}
384
385impl SyntaxTheme {
386    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
387        Self { highlights }
388    }
389}
390
391impl<'de> Deserialize<'de> for SyntaxTheme {
392    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
393    where
394        D: serde::Deserializer<'de>,
395    {
396        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
397
398        let mut result = Self::new(Vec::new());
399        for (key, style) in syntax_data {
400            match result
401                .highlights
402                .binary_search_by(|(needle, _)| needle.cmp(&key))
403            {
404                Ok(i) | Err(i) => {
405                    result.highlights.insert(i, (key, style));
406                }
407            }
408        }
409
410        Ok(result)
411    }
412}