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 item_spacing: f32,
113    pub cursor_position: TextStyle,
114    pub diagnostic_message: TextStyle,
115}
116
117#[derive(Deserialize, Default)]
118pub struct ChatPanel {
119    #[serde(flatten)]
120    pub container: ContainerStyle,
121    pub message: ChatMessage,
122    pub pending_message: ChatMessage,
123    pub channel_select: ChannelSelect,
124    pub input_editor: InputEditorStyle,
125    pub sign_in_prompt: TextStyle,
126    pub hovered_sign_in_prompt: TextStyle,
127}
128
129#[derive(Debug, Deserialize, Default)]
130pub struct ProjectPanel {
131    #[serde(flatten)]
132    pub container: ContainerStyle,
133    pub entry: ProjectPanelEntry,
134    pub hovered_entry: ProjectPanelEntry,
135    pub selected_entry: ProjectPanelEntry,
136    pub hovered_selected_entry: ProjectPanelEntry,
137}
138
139#[derive(Debug, Deserialize, Default)]
140pub struct ProjectPanelEntry {
141    pub height: f32,
142    #[serde(flatten)]
143    pub container: ContainerStyle,
144    pub text: TextStyle,
145    pub icon_color: Color,
146    pub icon_size: f32,
147    pub icon_spacing: f32,
148}
149
150#[derive(Deserialize, Default)]
151pub struct ContactsPanel {
152    #[serde(flatten)]
153    pub container: ContainerStyle,
154    pub host_row_height: f32,
155    pub host_avatar: ImageStyle,
156    pub host_username: ContainedText,
157    pub tree_branch_width: f32,
158    pub tree_branch_color: Color,
159    pub shared_project: WorktreeRow,
160    pub hovered_shared_project: WorktreeRow,
161    pub unshared_project: WorktreeRow,
162    pub hovered_unshared_project: WorktreeRow,
163}
164
165#[derive(Deserialize, Default)]
166pub struct WorktreeRow {
167    #[serde(flatten)]
168    pub container: ContainerStyle,
169    pub height: f32,
170    pub name: ContainedText,
171    pub guest_avatar: ImageStyle,
172    pub guest_avatar_spacing: f32,
173}
174
175#[derive(Deserialize, Default)]
176pub struct ChatMessage {
177    #[serde(flatten)]
178    pub container: ContainerStyle,
179    pub body: TextStyle,
180    pub sender: ContainedText,
181    pub timestamp: ContainedText,
182}
183
184#[derive(Deserialize, Default)]
185pub struct ChannelSelect {
186    #[serde(flatten)]
187    pub container: ContainerStyle,
188    pub header: ChannelName,
189    pub item: ChannelName,
190    pub active_item: ChannelName,
191    pub hovered_item: ChannelName,
192    pub hovered_active_item: ChannelName,
193    pub menu: ContainerStyle,
194}
195
196#[derive(Deserialize, Default)]
197pub struct ChannelName {
198    #[serde(flatten)]
199    pub container: ContainerStyle,
200    pub hash: ContainedText,
201    pub name: TextStyle,
202}
203
204#[derive(Deserialize, Default)]
205pub struct Selector {
206    #[serde(flatten)]
207    pub container: ContainerStyle,
208    pub empty: ContainedLabel,
209    pub input_editor: InputEditorStyle,
210    pub item: ContainedLabel,
211    pub active_item: ContainedLabel,
212}
213
214#[derive(Clone, Debug, Deserialize, Default)]
215pub struct ContainedText {
216    #[serde(flatten)]
217    pub container: ContainerStyle,
218    #[serde(flatten)]
219    pub text: TextStyle,
220}
221
222#[derive(Clone, Deserialize, Default)]
223pub struct ContainedLabel {
224    #[serde(flatten)]
225    pub container: ContainerStyle,
226    #[serde(flatten)]
227    pub label: LabelStyle,
228}
229
230#[derive(Clone, Deserialize, Default)]
231pub struct ProjectDiagnostics {
232    #[serde(flatten)]
233    pub container: ContainerStyle,
234    pub empty_message: TextStyle,
235    pub status_bar_item: ContainedText,
236    pub tab_icon_width: f32,
237    pub tab_icon_spacing: f32,
238    pub tab_summary_spacing: f32,
239}
240
241#[derive(Clone, Deserialize, Default)]
242pub struct EditorStyle {
243    pub text: TextStyle,
244    #[serde(default)]
245    pub placeholder_text: Option<TextStyle>,
246    pub background: Color,
247    pub selection: SelectionStyle,
248    pub gutter_background: Color,
249    pub gutter_padding_factor: f32,
250    pub active_line_background: Color,
251    pub highlighted_line_background: Color,
252    pub line_number: Color,
253    pub line_number_active: Color,
254    pub guest_selections: Vec<SelectionStyle>,
255    pub syntax: Arc<SyntaxTheme>,
256    pub diagnostic_path_header: DiagnosticPathHeader,
257    pub diagnostic_header: DiagnosticHeader,
258    pub error_diagnostic: DiagnosticStyle,
259    pub invalid_error_diagnostic: DiagnosticStyle,
260    pub warning_diagnostic: DiagnosticStyle,
261    pub invalid_warning_diagnostic: DiagnosticStyle,
262    pub information_diagnostic: DiagnosticStyle,
263    pub invalid_information_diagnostic: DiagnosticStyle,
264    pub hint_diagnostic: DiagnosticStyle,
265    pub invalid_hint_diagnostic: DiagnosticStyle,
266}
267
268#[derive(Clone, Deserialize, Default)]
269pub struct DiagnosticPathHeader {
270    #[serde(flatten)]
271    pub container: ContainerStyle,
272    pub filename: ContainedText,
273    pub path: ContainedText,
274    pub text_scale_factor: f32,
275}
276
277#[derive(Clone, Deserialize, Default)]
278pub struct DiagnosticHeader {
279    #[serde(flatten)]
280    pub container: ContainerStyle,
281    pub message: ContainedLabel,
282    pub code: ContainedText,
283    pub text_scale_factor: f32,
284    pub icon_width_factor: f32,
285}
286
287#[derive(Clone, Deserialize, Default)]
288pub struct DiagnosticStyle {
289    pub message: LabelStyle,
290    #[serde(default)]
291    pub header: ContainerStyle,
292    pub text_scale_factor: f32,
293}
294
295#[derive(Clone, Copy, Default, Deserialize)]
296pub struct SelectionStyle {
297    pub cursor: Color,
298    pub selection: Color,
299}
300
301#[derive(Clone, Deserialize, Default)]
302pub struct InputEditorStyle {
303    #[serde(flatten)]
304    pub container: ContainerStyle,
305    pub text: TextStyle,
306    #[serde(default)]
307    pub placeholder_text: Option<TextStyle>,
308    pub selection: SelectionStyle,
309}
310
311impl EditorStyle {
312    pub fn placeholder_text(&self) -> &TextStyle {
313        self.placeholder_text.as_ref().unwrap_or(&self.text)
314    }
315
316    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
317        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
318        if style_ix == 0 {
319            &self.selection
320        } else {
321            &self.guest_selections[style_ix - 1]
322        }
323    }
324}
325
326impl InputEditorStyle {
327    pub fn as_editor(&self) -> EditorStyle {
328        let default_diagnostic_style = DiagnosticStyle {
329            message: self.text.clone().into(),
330            header: Default::default(),
331            text_scale_factor: 1.,
332        };
333        EditorStyle {
334            text: self.text.clone(),
335            placeholder_text: self.placeholder_text.clone(),
336            background: self
337                .container
338                .background_color
339                .unwrap_or(Color::transparent_black()),
340            selection: self.selection,
341            gutter_background: Default::default(),
342            gutter_padding_factor: Default::default(),
343            active_line_background: Default::default(),
344            highlighted_line_background: Default::default(),
345            line_number: Default::default(),
346            line_number_active: Default::default(),
347            guest_selections: Default::default(),
348            syntax: Default::default(),
349            diagnostic_path_header: DiagnosticPathHeader {
350                container: Default::default(),
351                filename: ContainedText {
352                    container: Default::default(),
353                    text: self.text.clone(),
354                },
355                path: ContainedText {
356                    container: Default::default(),
357                    text: self.text.clone(),
358                },
359                text_scale_factor: 1.,
360            },
361            diagnostic_header: DiagnosticHeader {
362                container: Default::default(),
363                message: ContainedLabel {
364                    container: Default::default(),
365                    label: self.text.clone().into(),
366                },
367                code: ContainedText {
368                    container: Default::default(),
369                    text: self.text.clone(),
370                },
371                icon_width_factor: Default::default(),
372                text_scale_factor: 1.,
373            },
374            error_diagnostic: default_diagnostic_style.clone(),
375            invalid_error_diagnostic: default_diagnostic_style.clone(),
376            warning_diagnostic: default_diagnostic_style.clone(),
377            invalid_warning_diagnostic: default_diagnostic_style.clone(),
378            information_diagnostic: default_diagnostic_style.clone(),
379            invalid_information_diagnostic: default_diagnostic_style.clone(),
380            hint_diagnostic: default_diagnostic_style.clone(),
381            invalid_hint_diagnostic: default_diagnostic_style.clone(),
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}