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_underline: Color,
231    pub warning_underline: Color,
232    #[serde(default)]
233    pub information_underline: Color,
234    #[serde(default)]
235    pub hint_underline: Color,
236}
237
238#[derive(Clone, Copy, Default, Deserialize)]
239pub struct SelectionStyle {
240    pub cursor: Color,
241    pub selection: Color,
242}
243
244#[derive(Clone, Deserialize, Default)]
245pub struct InputEditorStyle {
246    #[serde(flatten)]
247    pub container: ContainerStyle,
248    pub text: TextStyle,
249    #[serde(default)]
250    pub placeholder_text: Option<TextStyle>,
251    pub selection: SelectionStyle,
252}
253
254impl EditorStyle {
255    pub fn placeholder_text(&self) -> &TextStyle {
256        self.placeholder_text.as_ref().unwrap_or(&self.text)
257    }
258}
259
260impl InputEditorStyle {
261    pub fn as_editor(&self) -> EditorStyle {
262        EditorStyle {
263            text: self.text.clone(),
264            placeholder_text: self.placeholder_text.clone(),
265            background: self
266                .container
267                .background_color
268                .unwrap_or(Color::transparent_black()),
269            selection: self.selection,
270            gutter_background: Default::default(),
271            active_line_background: Default::default(),
272            line_number: Default::default(),
273            line_number_active: Default::default(),
274            guest_selections: Default::default(),
275            syntax: Default::default(),
276            error_underline: Default::default(),
277            warning_underline: Default::default(),
278            information_underline: Default::default(),
279            hint_underline: Default::default(),
280        }
281    }
282}
283
284#[derive(Default)]
285pub struct SyntaxTheme {
286    pub highlights: Vec<(String, HighlightStyle)>,
287}
288
289impl SyntaxTheme {
290    pub fn new(highlights: Vec<(String, HighlightStyle)>) -> Self {
291        Self { highlights }
292    }
293}
294
295impl<'de> Deserialize<'de> for SyntaxTheme {
296    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
297    where
298        D: serde::Deserializer<'de>,
299    {
300        let syntax_data: HashMap<String, HighlightStyle> = Deserialize::deserialize(deserializer)?;
301
302        let mut result = Self::new(Vec::new());
303        for (key, style) in syntax_data {
304            match result
305                .highlights
306                .binary_search_by(|(needle, _)| needle.cmp(&key))
307            {
308                Ok(i) | Err(i) => {
309                    result.highlights.insert(i, (key, style));
310                }
311            }
312        }
313
314        Ok(result)
315    }
316}