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