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