1use gpui::{App, Global, Hsla, hsla};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::{ScrollbarMode, ScrollbarMotion, ScrollbarStyles, input::CursorBlink};
7
8#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
14#[non_exhaustive]
15pub struct InputColors {
16 pub foreground: Hsla,
17 pub muted_foreground: Hsla,
18 pub background: Hsla,
19 pub border: Hsla,
20 pub selection: Hsla,
21}
22
23impl Default for InputColors {
24 fn default() -> Self {
25 Self {
26 foreground: hsla(0., 0., 0.039, 1.),
27 muted_foreground: hsla(0., 0., 0.451, 1.),
28 background: hsla(0., 0., 1., 1.),
29 border: hsla(0., 0., 0.898, 1.),
30 selection: hsla(0., 0., 0.961, 0.4),
31 }
32 }
33}
34
35#[derive(Clone, Default)]
38#[non_exhaustive]
39pub struct Theme {
40 pub input_colors: InputColors,
42 pub scrollbar: ScrollbarTheme,
43 pub cursor_blink: CursorBlink,
45}
46
47impl Global for Theme {}
48
49impl Theme {
50 pub fn global(cx: &App) -> Self {
51 cx.try_global::<Self>().cloned().unwrap_or_default()
52 }
53
54 pub fn global_mut(cx: &mut App) -> &mut Self {
55 if !cx.has_global::<Self>() {
56 cx.set_global(Self::default());
57 }
58 cx.global_mut::<Self>()
59 }
60}
61
62pub(crate) trait ActiveTheme {
64 fn theme(&self) -> Theme;
65}
66
67impl ActiveTheme for App {
68 #[inline(always)]
69 fn theme(&self) -> Theme {
70 Theme::global(self)
71 }
72}
73
74#[derive(Clone, Default)]
79pub struct ScrollbarTheme {
80 mode: ScrollbarMode,
81 motion: ScrollbarMotion,
82 styles: ScrollbarStyles,
83}
84
85impl ScrollbarTheme {
86 pub fn new() -> Self {
87 Self::default()
88 }
89
90 pub fn with_mode(mut self, mode: ScrollbarMode) -> Self {
91 self.mode = mode;
92 self
93 }
94
95 pub fn with_motion(mut self, motion: ScrollbarMotion) -> Self {
96 self.motion = motion;
97 self
98 }
99
100 pub fn with_styles(mut self, styles: ScrollbarStyles) -> Self {
101 self.styles = styles;
102 self
103 }
104
105 pub fn mode(&self) -> ScrollbarMode {
106 self.mode
107 }
108
109 pub fn motion(&self) -> ScrollbarMotion {
110 self.motion
111 }
112
113 pub fn styles(&self) -> &ScrollbarStyles {
114 &self.styles
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121
122 #[test]
123 fn scrollbar_theme_supports_builder_construction() {
124 let mode = ScrollbarMode::Hover;
125 let motion = ScrollbarMotion::default().with_enter(std::time::Duration::from_millis(120));
126 let styles = ScrollbarStyles::default();
127 let theme = ScrollbarTheme::new()
128 .with_mode(mode)
129 .with_motion(motion)
130 .with_styles(styles);
131
132 assert_eq!(theme.mode(), mode);
133 assert_eq!(theme.motion(), motion);
134 let _ = theme.styles();
135 }
136}