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