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}
 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}
257
258#[derive(Clone, Copy, Default, Deserialize)]
259pub struct SelectionStyle {
260    pub cursor: Color,
261    pub selection: Color,
262}
263
264#[derive(Clone, Deserialize, Default)]
265pub struct InputEditorStyle {
266    #[serde(flatten)]
267    pub container: ContainerStyle,
268    pub text: TextStyle,
269    #[serde(default)]
270    pub placeholder_text: Option<TextStyle>,
271    pub selection: SelectionStyle,
272}
273
274impl EditorStyle {
275    pub fn placeholder_text(&self) -> &TextStyle {
276        self.placeholder_text.as_ref().unwrap_or(&self.text)
277    }
278
279    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
280        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
281        if style_ix == 0 {
282            &self.selection
283        } else {
284            &self.guest_selections[style_ix - 1]
285        }
286    }
287}
288
289impl InputEditorStyle {
290    pub fn as_editor(&self) -> EditorStyle {
291        EditorStyle {
292            text: self.text.clone(),
293            placeholder_text: self.placeholder_text.clone(),
294            background: self
295                .container
296                .background_color
297                .unwrap_or(Color::transparent_black()),
298            selection: self.selection,
299            gutter_background: Default::default(),
300            active_line_background: Default::default(),
301            highlighted_line_background: Default::default(),
302            line_number: Default::default(),
303            line_number_active: Default::default(),
304            guest_selections: Default::default(),
305            syntax: Default::default(),
306            error_diagnostic: Default::default(),
307            invalid_error_diagnostic: Default::default(),
308            warning_diagnostic: Default::default(),
309            invalid_warning_diagnostic: Default::default(),
310            information_diagnostic: Default::default(),
311            invalid_information_diagnostic: Default::default(),
312            hint_diagnostic: Default::default(),
313            invalid_hint_diagnostic: Default::default(),
314        }
315    }
316}
317
318#[derive(Default)]
319pub struct SyntaxTheme {
320    pub highlights: Vec<(String, HighlightStyle)>,
321}
322
323impl SyntaxTheme {
324    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
325        Self { highlights }
326    }
327}
328
329impl<'de> Deserialize<'de> for SyntaxTheme {
330    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
331    where
332        D: serde::Deserializer<'de>,
333    {
334        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
335
336        let mut result = Self::new(Vec::new());
337        for (key, style) in syntax_data {
338            match result
339                .highlights
340                .binary_search_by(|(needle, _)| needle.cmp(&key))
341            {
342                Ok(i) | Err(i) => {
343                    result.highlights.insert(i, (key, style));
344                }
345            }
346        }
347
348        Ok(result)
349    }
350}