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