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