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 icon_color: Color,
 52    pub avatar: ImageStyle,
 53    pub sign_in_prompt: ContainedText,
 54    pub hovered_sign_in_prompt: ContainedText,
 55    pub outdated_warning: ContainedText,
 56}
 57
 58#[derive(Clone, Deserialize, Default)]
 59pub struct AvatarRibbon {
 60    #[serde(flatten)]
 61    pub container: ContainerStyle,
 62    pub width: f32,
 63    pub height: f32,
 64}
 65
 66#[derive(Clone, Deserialize, Default)]
 67pub struct OfflineIcon {
 68    #[serde(flatten)]
 69    pub container: ContainerStyle,
 70    pub width: f32,
 71}
 72
 73#[derive(Clone, Deserialize, Default)]
 74pub struct Tab {
 75    pub height: f32,
 76    #[serde(flatten)]
 77    pub container: ContainerStyle,
 78    #[serde(flatten)]
 79    pub label: LabelStyle,
 80    pub spacing: f32,
 81    pub icon_width: f32,
 82    pub icon_close: Color,
 83    pub icon_close_active: Color,
 84    pub icon_dirty: Color,
 85    pub icon_conflict: Color,
 86}
 87
 88#[derive(Deserialize, Default)]
 89pub struct Sidebar {
 90    #[serde(flatten)]
 91    pub container: ContainerStyle,
 92    pub width: f32,
 93    pub item: SidebarItem,
 94    pub active_item: SidebarItem,
 95    pub resize_handle: ContainerStyle,
 96}
 97
 98#[derive(Deserialize, Default)]
 99pub struct SidebarItem {
100    pub icon_color: Color,
101    pub icon_size: f32,
102    pub height: f32,
103}
104
105#[derive(Deserialize, Default)]
106pub struct StatusBar {
107    #[serde(flatten)]
108    pub container: ContainerStyle,
109    pub height: f32,
110    pub cursor_position: TextStyle,
111    pub diagnostic_icon_size: f32,
112    pub diagnostic_icon_spacing: f32,
113    pub diagnostic_icon_color: Color,
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(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}
237
238#[derive(Clone, Deserialize, Default)]
239pub struct EditorStyle {
240    pub text: TextStyle,
241    #[serde(default)]
242    pub placeholder_text: Option<TextStyle>,
243    pub background: Color,
244    pub selection: SelectionStyle,
245    pub gutter_background: Color,
246    pub active_line_background: Color,
247    pub highlighted_line_background: Color,
248    pub line_number: Color,
249    pub line_number_active: Color,
250    pub guest_selections: Vec<SelectionStyle>,
251    pub syntax: Arc<SyntaxTheme>,
252    pub error_diagnostic: DiagnosticStyle,
253    pub invalid_error_diagnostic: DiagnosticStyle,
254    pub warning_diagnostic: DiagnosticStyle,
255    pub invalid_warning_diagnostic: DiagnosticStyle,
256    pub information_diagnostic: DiagnosticStyle,
257    pub invalid_information_diagnostic: DiagnosticStyle,
258    pub hint_diagnostic: DiagnosticStyle,
259    pub invalid_hint_diagnostic: DiagnosticStyle,
260}
261
262#[derive(Copy, Clone, Deserialize, Default)]
263pub struct DiagnosticStyle {
264    pub text: Color,
265    #[serde(default)]
266    pub header: ContainerStyle,
267}
268
269#[derive(Clone, Copy, Default, Deserialize)]
270pub struct SelectionStyle {
271    pub cursor: Color,
272    pub selection: Color,
273}
274
275#[derive(Clone, Deserialize, Default)]
276pub struct InputEditorStyle {
277    #[serde(flatten)]
278    pub container: ContainerStyle,
279    pub text: TextStyle,
280    #[serde(default)]
281    pub placeholder_text: Option<TextStyle>,
282    pub selection: SelectionStyle,
283}
284
285impl EditorStyle {
286    pub fn placeholder_text(&self) -> &TextStyle {
287        self.placeholder_text.as_ref().unwrap_or(&self.text)
288    }
289
290    pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
291        let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
292        if style_ix == 0 {
293            &self.selection
294        } else {
295            &self.guest_selections[style_ix - 1]
296        }
297    }
298}
299
300impl InputEditorStyle {
301    pub fn as_editor(&self) -> EditorStyle {
302        EditorStyle {
303            text: self.text.clone(),
304            placeholder_text: self.placeholder_text.clone(),
305            background: self
306                .container
307                .background_color
308                .unwrap_or(Color::transparent_black()),
309            selection: self.selection,
310            gutter_background: Default::default(),
311            active_line_background: Default::default(),
312            highlighted_line_background: Default::default(),
313            line_number: Default::default(),
314            line_number_active: Default::default(),
315            guest_selections: Default::default(),
316            syntax: Default::default(),
317            error_diagnostic: Default::default(),
318            invalid_error_diagnostic: Default::default(),
319            warning_diagnostic: Default::default(),
320            invalid_warning_diagnostic: Default::default(),
321            information_diagnostic: Default::default(),
322            invalid_information_diagnostic: Default::default(),
323            hint_diagnostic: Default::default(),
324            invalid_hint_diagnostic: Default::default(),
325        }
326    }
327}
328
329#[derive(Default)]
330pub struct SyntaxTheme {
331    pub highlights: Vec<(String, HighlightStyle)>,
332}
333
334impl SyntaxTheme {
335    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
336        Self { highlights }
337    }
338}
339
340impl<'de> Deserialize<'de> for SyntaxTheme {
341    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
342    where
343        D: serde::Deserializer<'de>,
344    {
345        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
346
347        let mut result = Self::new(Vec::new());
348        for (key, style) in syntax_data {
349            match result
350                .highlights
351                .binary_search_by(|(needle, _)| needle.cmp(&key))
352            {
353                Ok(i) | Err(i) => {
354                    result.highlights.insert(i, (key, style));
355                }
356            }
357        }
358
359        Ok(result)
360    }
361}