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