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