1use crate::Rgba;
2use serde::{Deserialize, Serialize};
3
4macro_rules! roles {
5 ($($role:ident: $name:ident),* $(,)?) => {
6 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7 pub struct UiPalette<T = Rgba> {
8 $(pub $role: T,)*
9 }
10
11 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12 pub enum Role {
13 $($name,)*
14 }
15
16 impl<T> UiPalette<T> {
17 pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> UiPalette<U> {
18 UiPalette { $($role: f(self.$role),)* }
19 }
20
21 #[must_use]
22 pub const fn get(&self, role: Role) -> &T {
23 match role {
24 $(Role::$name => &self.$role,)*
25 }
26 }
27 }
28
29 impl UiPalette<Role> {
30 pub const ROLES: Self = UiPalette { $($role: Role::$name,)* };
31 }
32 };
33}
34
35roles!(
36 canvas: Canvas,
37 surface: Surface,
38 surface_hover: SurfaceHover,
39 surface_selected: SurfaceSelected,
40 chrome: Chrome,
41 elevated: Elevated,
42 input: Input,
43 text: Text,
44 text_secondary: TextSecondary,
45 text_muted: TextMuted,
46 border: Border,
47 border_subtle: BorderSubtle,
48 accent: Accent,
49 accent_foreground: AccentForeground,
50 info: Info,
51 positive: Positive,
52 warning: Warning,
53 destructive: Destructive,
54 destructive_foreground: DestructiveForeground,
55 scrim: Scrim,
56);
57
58#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
59pub enum ButtonVariant {
60 Primary,
61 Secondary,
62 Destructive,
63 #[default]
64 Ghost,
65}
66#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
67pub enum InteractionState {
68 #[default]
69 Rest,
70 Hovered,
71 Disabled,
72}
73#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
74pub struct ControlState {
75 pub interaction: InteractionState,
76 pub selected: bool,
77}
78
79impl ControlState {
80 #[must_use]
81 pub const fn new(interaction: InteractionState) -> Self {
82 Self {
83 interaction,
84 selected: false,
85 }
86 }
87
88 #[must_use]
89 pub const fn selected(mut self, selected: bool) -> Self {
90 self.selected = selected;
91 self
92 }
93}
94#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
95pub enum SelectionState {
96 #[default]
97 None,
98 Selected,
99 Focused,
100 Disabled,
101}
102
103#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
104pub enum NoticeTone {
105 Neutral,
106 #[default]
107 Info,
108 Positive,
109 Warning,
110 Error,
111}
112#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
113pub enum ModalSize {
114 Compact,
115 #[default]
116 Medium,
117 Wide,
118}
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub struct SemanticStyle<T = Rgba> {
121 pub foreground: T,
122 pub background: Option<T>,
123 pub emphasized: bool,
124}
125
126impl<T> SemanticStyle<T> {
127 pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> SemanticStyle<U> {
128 SemanticStyle {
129 foreground: f(self.foreground),
130 background: self.background.map(f),
131 emphasized: self.emphasized,
132 }
133 }
134}
135
136impl<T: Copy> UiPalette<T> {
137 #[must_use]
138 pub fn control_style(self, variant: ButtonVariant, state: ControlState) -> SemanticStyle<T> {
139 if matches!(state.interaction, InteractionState::Disabled) {
140 return SemanticStyle {
141 foreground: self.text_muted,
142 background: Some(self.surface_selected),
143 emphasized: false,
144 };
145 }
146 let (foreground, background) = match (variant, state.interaction, state.selected) {
147 (ButtonVariant::Primary, _, _) => (self.accent_foreground, Some(self.accent)),
148 (ButtonVariant::Destructive, _, _) => {
149 (self.destructive_foreground, Some(self.destructive))
150 }
151 (ButtonVariant::Ghost, _, true) => (self.accent, Some(self.surface_selected)),
152 (ButtonVariant::Secondary | ButtonVariant::Ghost, InteractionState::Hovered, _) => {
153 (self.text, Some(self.surface_hover))
154 }
155 (ButtonVariant::Secondary, _, _) => (self.text, Some(self.surface)),
156 (ButtonVariant::Ghost, _, false) => (self.text_muted, None),
157 };
158 SemanticStyle {
159 foreground,
160 background,
161 emphasized: state.selected,
162 }
163 }
164 #[must_use]
165 pub fn selection_style(self, state: SelectionState) -> SemanticStyle<T> {
166 match state {
167 SelectionState::None => SemanticStyle {
168 foreground: self.text,
169 background: Some(self.surface),
170 emphasized: false,
171 },
172 SelectionState::Selected => SemanticStyle {
173 foreground: self.accent,
174 background: Some(self.surface_selected),
175 emphasized: false,
176 },
177 SelectionState::Focused => SemanticStyle {
178 foreground: self.accent_foreground,
179 background: Some(self.accent),
180 emphasized: true,
181 },
182 SelectionState::Disabled => SemanticStyle {
183 foreground: self.text_muted,
184 background: Some(self.surface),
185 emphasized: false,
186 },
187 }
188 }
189 #[must_use]
190 pub fn notice_style(self, tone: NoticeTone) -> SemanticStyle<T> {
191 let foreground = match tone {
192 NoticeTone::Neutral => self.text_secondary,
193 NoticeTone::Info => self.info,
194 NoticeTone::Positive => self.positive,
195 NoticeTone::Warning => self.warning,
196 NoticeTone::Error => self.destructive,
197 };
198 SemanticStyle {
199 foreground,
200 background: None,
201 emphasized: matches!(tone, NoticeTone::Warning | NoticeTone::Error),
202 }
203 }
204}
205
206impl UiPalette {
207 #[must_use]
208 pub const fn light() -> Self {
209 Self {
210 canvas: Rgba::rgb(0xf5f5f7),
211 surface: Rgba::rgb(0xf5f5f7),
212 surface_hover: Rgba::new(0, 0, 0, 13),
213 surface_selected: Rgba::new(0, 0, 0, 26),
214 chrome: Rgba::rgb(0xebebed),
215 elevated: Rgba::rgb(0xffffff),
216 input: Rgba::rgb(0xffffff),
217 text: Rgba::rgb(0x1d1d1f),
218 text_secondary: Rgba::rgb(0x424245),
219 text_muted: Rgba::rgb(0x6e6e73),
220 border: Rgba::rgb(0xd2d2d7),
221 border_subtle: Rgba::rgb(0xe5e5ea),
222 accent: Rgba::rgb(0x0a6cff),
223 accent_foreground: Rgba::rgb(0xffffff),
224 info: Rgba::rgb(0x0a6cff),
225 positive: Rgba::rgb(0x248a3d),
226 warning: Rgba::rgb(0xb25000),
227 destructive: Rgba::rgb(0xd70015),
228 destructive_foreground: Rgba::rgb(0xffffff),
229 scrim: Rgba::new(0, 0, 0, 89),
230 }
231 }
232
233 #[must_use]
234 pub const fn dark() -> Self {
235 Self {
236 canvas: Rgba::rgb(0x1c1c1e),
237 surface: Rgba::rgb(0x1c1c1e),
238 surface_hover: Rgba::new(255, 255, 255, 18),
239 surface_selected: Rgba::new(255, 255, 255, 33),
240 chrome: Rgba::rgb(0x161618),
241 elevated: Rgba::rgb(0x2c2c2e),
242 input: Rgba::rgb(0x2c2c2e),
243 text: Rgba::rgb(0xf5f5f7),
244 text_secondary: Rgba::rgb(0xc7c7cc),
245 text_muted: Rgba::rgb(0x98989d),
246 border: Rgba::rgb(0x48484a),
247 border_subtle: Rgba::rgb(0x38383a),
248 accent: Rgba::rgb(0x0a84ff),
249 accent_foreground: Rgba::rgb(0xffffff),
250 info: Rgba::rgb(0x0a84ff),
251 positive: Rgba::rgb(0x30d158),
252 warning: Rgba::rgb(0xff9f0a),
253 destructive: Rgba::rgb(0xff453a),
254 destructive_foreground: Rgba::rgb(0xffffff),
255 scrim: Rgba::new(0, 0, 0, 128),
256 }
257 }
258}
259
260impl Default for UiPalette {
261 fn default() -> Self {
262 Self::light()
263 }
264}