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}
 99
100#[derive(Deserialize, Default)]
101pub struct ChatPanel {
102    #[serde(flatten)]
103    pub container: ContainerStyle,
104    pub message: ChatMessage,
105    pub pending_message: ChatMessage,
106    pub channel_select: ChannelSelect,
107    pub input_editor: InputEditorStyle,
108    pub sign_in_prompt: TextStyle,
109    pub hovered_sign_in_prompt: TextStyle,
110}
111
112#[derive(Debug, Deserialize, Default)]
113pub struct ProjectPanel {
114    #[serde(flatten)]
115    pub container: ContainerStyle,
116    pub entry: ProjectPanelEntry,
117    pub hovered_entry: ProjectPanelEntry,
118    pub selected_entry: ProjectPanelEntry,
119    pub hovered_selected_entry: ProjectPanelEntry,
120}
121
122#[derive(Debug, Deserialize, Default)]
123pub struct ProjectPanelEntry {
124    pub height: f32,
125    #[serde(flatten)]
126    pub container: ContainerStyle,
127    pub text: TextStyle,
128    pub icon_color: Color,
129    pub icon_size: f32,
130    pub icon_spacing: f32,
131}
132
133#[derive(Deserialize, Default)]
134pub struct PeoplePanel {
135    #[serde(flatten)]
136    pub container: ContainerStyle,
137    pub host_row_height: f32,
138    pub host_avatar: ImageStyle,
139    pub host_username: ContainedText,
140    pub tree_branch_width: f32,
141    pub tree_branch_color: Color,
142    pub shared_worktree: WorktreeRow,
143    pub hovered_shared_worktree: WorktreeRow,
144    pub unshared_worktree: WorktreeRow,
145    pub hovered_unshared_worktree: WorktreeRow,
146}
147
148#[derive(Deserialize, Default)]
149pub struct WorktreeRow {
150    #[serde(flatten)]
151    pub container: ContainerStyle,
152    pub height: f32,
153    pub name: ContainedText,
154    pub guest_avatar: ImageStyle,
155    pub guest_avatar_spacing: f32,
156}
157
158#[derive(Deserialize, Default)]
159pub struct ChatMessage {
160    #[serde(flatten)]
161    pub container: ContainerStyle,
162    pub body: TextStyle,
163    pub sender: ContainedText,
164    pub timestamp: ContainedText,
165}
166
167#[derive(Deserialize, Default)]
168pub struct ChannelSelect {
169    #[serde(flatten)]
170    pub container: ContainerStyle,
171    pub header: ChannelName,
172    pub item: ChannelName,
173    pub active_item: ChannelName,
174    pub hovered_item: ChannelName,
175    pub hovered_active_item: ChannelName,
176    pub menu: ContainerStyle,
177}
178
179#[derive(Deserialize, Default)]
180pub struct ChannelName {
181    #[serde(flatten)]
182    pub container: ContainerStyle,
183    pub hash: ContainedText,
184    pub name: TextStyle,
185}
186
187#[derive(Deserialize, Default)]
188pub struct Selector {
189    #[serde(flatten)]
190    pub container: ContainerStyle,
191    pub empty: ContainedLabel,
192    pub input_editor: InputEditorStyle,
193    pub item: ContainedLabel,
194    pub active_item: ContainedLabel,
195}
196
197#[derive(Clone, Debug, Deserialize, Default)]
198pub struct ContainedText {
199    #[serde(flatten)]
200    pub container: ContainerStyle,
201    #[serde(flatten)]
202    pub text: TextStyle,
203}
204
205#[derive(Deserialize, Default)]
206pub struct ContainedLabel {
207    #[serde(flatten)]
208    pub container: ContainerStyle,
209    #[serde(flatten)]
210    pub label: LabelStyle,
211}
212
213#[derive(Clone, Deserialize, Default)]
214pub struct EditorStyle {
215    pub text: TextStyle,
216    #[serde(default)]
217    pub placeholder_text: Option<TextStyle>,
218    pub background: Color,
219    pub selection: SelectionStyle,
220    pub gutter_background: Color,
221    pub active_line_background: Color,
222    pub line_number: Color,
223    pub line_number_active: Color,
224    pub guest_selections: Vec<SelectionStyle>,
225    pub syntax: Arc<SyntaxTheme>,
226    pub error_underline: Color,
227    pub warning_underline: Color,
228    #[serde(default)]
229    pub information_underline: Color,
230    #[serde(default)]
231    pub hint_underline: Color,
232}
233
234#[derive(Clone, Copy, Default, Deserialize)]
235pub struct SelectionStyle {
236    pub cursor: Color,
237    pub selection: Color,
238}
239
240#[derive(Clone, Deserialize, Default)]
241pub struct InputEditorStyle {
242    #[serde(flatten)]
243    pub container: ContainerStyle,
244    pub text: TextStyle,
245    #[serde(default)]
246    pub placeholder_text: Option<TextStyle>,
247    pub selection: SelectionStyle,
248}
249
250impl EditorStyle {
251    pub fn placeholder_text(&self) -> &TextStyle {
252        self.placeholder_text.as_ref().unwrap_or(&self.text)
253    }
254}
255
256impl InputEditorStyle {
257    pub fn as_editor(&self) -> EditorStyle {
258        EditorStyle {
259            text: self.text.clone(),
260            placeholder_text: self.placeholder_text.clone(),
261            background: self
262                .container
263                .background_color
264                .unwrap_or(Color::transparent_black()),
265            selection: self.selection,
266            gutter_background: Default::default(),
267            active_line_background: Default::default(),
268            line_number: Default::default(),
269            line_number_active: Default::default(),
270            guest_selections: Default::default(),
271            syntax: Default::default(),
272            error_underline: Default::default(),
273            warning_underline: Default::default(),
274            information_underline: Default::default(),
275            hint_underline: Default::default(),
276        }
277    }
278}
279
280#[derive(Default)]
281pub struct SyntaxTheme {
282    pub highlights: Vec<(String, HighlightStyle)>,
283}
284
285impl SyntaxTheme {
286    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
287        Self { highlights }
288    }
289}
290
291impl<'de> Deserialize<'de> for SyntaxTheme {
292    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
293    where
294        D: serde::Deserializer<'de>,
295    {
296        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
297
298        let mut result = Self::new(Vec::new());
299        for (key, style) in syntax_data {
300            match result
301                .highlights
302                .binary_search_by(|(needle, _)| needle.cmp(&key))
303            {
304                Ok(i) | Err(i) => {
305                    result.highlights.insert(i, (key, style));
306                }
307            }
308        }
309
310        Ok(result)
311    }
312}