lib.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 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 line_number: Color,
227    pub line_number_active: Color,
228    pub guest_selections: Vec<SelectionStyle>,
229    pub syntax: Arc<SyntaxTheme>,
230    pub error_diagnostic: DiagnosticStyle,
231    pub invalid_error_diagnostic: DiagnosticStyle,
232    pub warning_diagnostic: DiagnosticStyle,
233    pub invalid_warning_diagnostic: DiagnosticStyle,
234    pub information_diagnostic: DiagnosticStyle,
235    pub invalid_information_diagnostic: DiagnosticStyle,
236    pub hint_diagnostic: DiagnosticStyle,
237    pub invalid_hint_diagnostic: DiagnosticStyle,
238}
239
240#[derive(Copy, Clone, Deserialize, Default)]
241pub struct DiagnosticStyle {
242    pub text: Color,
243    #[serde(flatten)]
244    pub block: BlockStyle,
245}
246
247#[derive(Clone, Copy, Default, Deserialize)]
248pub struct SelectionStyle {
249    pub cursor: Color,
250    pub selection: Color,
251}
252
253#[derive(Clone, Deserialize, Default)]
254pub struct InputEditorStyle {
255    #[serde(flatten)]
256    pub container: ContainerStyle,
257    pub text: TextStyle,
258    #[serde(default)]
259    pub placeholder_text: Option<TextStyle>,
260    pub selection: SelectionStyle,
261}
262
263#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq)]
264pub struct BlockStyle {
265    pub background: Option<Color>,
266    pub border: Option<Color>,
267    pub gutter_background: Option<Color>,
268    pub gutter_border: Option<Color>,
269}
270
271impl EditorStyle {
272    pub fn placeholder_text(&self) -> &TextStyle {
273        self.placeholder_text.as_ref().unwrap_or(&self.text)
274    }
275}
276
277impl InputEditorStyle {
278    pub fn as_editor(&self) -> EditorStyle {
279        EditorStyle {
280            text: self.text.clone(),
281            placeholder_text: self.placeholder_text.clone(),
282            background: self
283                .container
284                .background_color
285                .unwrap_or(Color::transparent_black()),
286            selection: self.selection,
287            gutter_background: Default::default(),
288            active_line_background: Default::default(),
289            line_number: Default::default(),
290            line_number_active: Default::default(),
291            guest_selections: Default::default(),
292            syntax: Default::default(),
293            error_diagnostic: Default::default(),
294            invalid_error_diagnostic: Default::default(),
295            warning_diagnostic: Default::default(),
296            invalid_warning_diagnostic: Default::default(),
297            information_diagnostic: Default::default(),
298            invalid_information_diagnostic: Default::default(),
299            hint_diagnostic: Default::default(),
300            invalid_hint_diagnostic: Default::default(),
301        }
302    }
303}
304
305#[derive(Default)]
306pub struct SyntaxTheme {
307    pub highlights: Vec<(String, HighlightStyle)>,
308}
309
310impl SyntaxTheme {
311    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
312        Self { highlights }
313    }
314}
315
316impl<'de> Deserialize<'de> for SyntaxTheme {
317    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
318    where
319        D: serde::Deserializer<'de>,
320    {
321        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
322
323        let mut result = Self::new(Vec::new());
324        for (key, style) in syntax_data {
325            match result
326                .highlights
327                .binary_search_by(|(needle, _)| needle.cmp(&key))
328            {
329                Ok(i) | Err(i) => {
330                    result.highlights.insert(i, (key, style));
331                }
332            }
333        }
334
335        Ok(result)
336    }
337}