1use std::collections::HashMap;
2use std::fmt;
3use std::sync::Arc;
4
5use gpui3::{
6 BorrowAppContext, Bounds, Element, Hsla, LayoutId, Pixels, Result, ViewContext, WindowContext,
7};
8use serde::{de::Visitor, Deserialize, Deserializer};
9
10#[derive(Deserialize, Clone, Default, Debug)]
11pub struct Theme {
12 pub name: String,
13 pub is_light: bool,
14 pub lowest: Layer,
15 pub middle: Layer,
16 pub highest: Layer,
17 pub popover_shadow: Shadow,
18 pub modal_shadow: Shadow,
19 #[serde(deserialize_with = "deserialize_player_colors")]
20 pub players: Vec<PlayerColors>,
21 #[serde(deserialize_with = "deserialize_syntax_colors")]
22 pub syntax: HashMap<String, Hsla>,
23}
24
25#[derive(Deserialize, Clone, Default, Debug)]
26pub struct Layer {
27 pub base: StyleSet,
28 pub variant: StyleSet,
29 pub on: StyleSet,
30 pub accent: StyleSet,
31 pub positive: StyleSet,
32 pub warning: StyleSet,
33 pub negative: StyleSet,
34}
35
36#[derive(Deserialize, Clone, Default, Debug)]
37pub struct StyleSet {
38 #[serde(rename = "default")]
39 pub default: ContainerColors,
40 pub hovered: ContainerColors,
41 pub pressed: ContainerColors,
42 pub active: ContainerColors,
43 pub disabled: ContainerColors,
44 pub inverted: ContainerColors,
45}
46
47#[derive(Deserialize, Clone, Default, Debug)]
48pub struct ContainerColors {
49 pub background: Hsla,
50 pub foreground: Hsla,
51 pub border: Hsla,
52}
53
54#[derive(Deserialize, Clone, Default, Debug)]
55pub struct PlayerColors {
56 pub selection: Hsla,
57 pub cursor: Hsla,
58}
59
60#[derive(Deserialize, Clone, Default, Debug)]
61pub struct Shadow {
62 pub blur: u8,
63 pub color: Hsla,
64 pub offset: Vec<u8>,
65}
66
67fn deserialize_player_colors<'de, D>(deserializer: D) -> Result<Vec<PlayerColors>, D::Error>
68where
69 D: Deserializer<'de>,
70{
71 struct PlayerArrayVisitor;
72
73 impl<'de> Visitor<'de> for PlayerArrayVisitor {
74 type Value = Vec<PlayerColors>;
75
76 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
77 formatter.write_str("an object with integer keys")
78 }
79
80 fn visit_map<A: serde::de::MapAccess<'de>>(
81 self,
82 mut map: A,
83 ) -> Result<Self::Value, A::Error> {
84 let mut players = Vec::with_capacity(8);
85 while let Some((key, value)) = map.next_entry::<usize, PlayerColors>()? {
86 if key < 8 {
87 players.push(value);
88 } else {
89 return Err(serde::de::Error::invalid_value(
90 serde::de::Unexpected::Unsigned(key as u64),
91 &"a key in range 0..7",
92 ));
93 }
94 }
95 Ok(players)
96 }
97 }
98
99 deserializer.deserialize_map(PlayerArrayVisitor)
100}
101
102fn deserialize_syntax_colors<'de, D>(deserializer: D) -> Result<HashMap<String, Hsla>, D::Error>
103where
104 D: serde::Deserializer<'de>,
105{
106 #[derive(Deserialize)]
107 struct ColorWrapper {
108 color: Hsla,
109 }
110
111 struct SyntaxVisitor;
112
113 impl<'de> Visitor<'de> for SyntaxVisitor {
114 type Value = HashMap<String, Hsla>;
115
116 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
117 formatter.write_str("a map with keys and objects with a single color field as values")
118 }
119
120 fn visit_map<M>(self, mut map: M) -> Result<HashMap<String, Hsla>, M::Error>
121 where
122 M: serde::de::MapAccess<'de>,
123 {
124 let mut result = HashMap::new();
125 while let Some(key) = map.next_key()? {
126 let wrapper: ColorWrapper = map.next_value()?; // Deserialize values as Hsla
127 result.insert(key, wrapper.color);
128 }
129 Ok(result)
130 }
131 }
132 deserializer.deserialize_map(SyntaxVisitor)
133}
134
135pub fn themed<E, F>(theme: Theme, cx: &mut ViewContext<E::State>, build_child: F) -> Themed<E>
136where
137 E: Element,
138 F: FnOnce(&mut ViewContext<E::State>) -> E,
139{
140 let child = cx.with_state(theme.clone(), |cx| build_child(cx));
141 Themed { theme, child }
142}
143
144pub struct Themed<E> {
145 pub(crate) theme: Theme,
146 pub(crate) child: E,
147}
148
149impl<E: Element> Element for Themed<E> {
150 type State = E::State;
151 type FrameState = E::FrameState;
152
153 fn layout(
154 &mut self,
155 state: &mut E::State,
156 cx: &mut ViewContext<E::State>,
157 ) -> anyhow::Result<(LayoutId, Self::FrameState)>
158 where
159 Self: Sized,
160 {
161 cx.with_state(self.theme.clone(), |cx| self.child.layout(state, cx))
162 }
163
164 fn paint(
165 &mut self,
166 bounds: Bounds<Pixels>,
167 state: &mut Self::State,
168 frame_state: &mut Self::FrameState,
169 cx: &mut ViewContext<Self::State>,
170 ) -> Result<()>
171 where
172 Self: Sized,
173 {
174 cx.with_state(self.theme.clone(), |cx| {
175 self.child.paint(bounds, state, frame_state, cx)
176 })
177 }
178}
179
180pub fn theme(cx: &WindowContext) -> Arc<Theme> {
181 Arc::new(cx.state::<Theme>().clone())
182}