| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use gpui_kit::component::elevation::{self, Elevation, ElevationTokens}; |
| 4 | use gpui_kit::component::{Theme, ThemeMode}; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | mod schemes; |
| 8 | |
| 9 | pub use schemes::{ |
| 10 | ColorScheme, SCHEMES, active_scheme as scheme, default_scheme, scheme_by_id, set_active_scheme, |
| 11 | set_thread_scheme, |
| 12 | }; |
| 13 | |
| 14 | const FONT_FAMILY: &str = "Google Sans Flex"; |
| 15 | const MONO_FAMILIES: [&str; 2] = ["Cascadia Code", "Consolas"]; |
| 16 | const BASE_FONT_SIZE: f32 = 14.; |
| 17 | const FONT_FILES: [&[u8]; 4] = [ |
| 18 | include_bytes!("../../app/assets/fonts/GoogleSansFlex-Regular.ttf"), |
| 19 | include_bytes!("../../app/assets/fonts/GoogleSansFlex-SemiBold.ttf"), |
| 20 | include_bytes!("../../app/assets/fonts/GoogleSansFlex-Bold.ttf"), |
| 21 | include_bytes!("../../app/assets/fonts/GoogleSansFlex-Italic.ttf"), |
| 22 | ]; |
| 23 | |
| 24 | const WHITE: u32 = 0xffffff; |
| 25 | const CODE_SURFACE: u32 = 0x0000003d; |
| 26 | const CODE_HEADER_BORDER: u32 = 0xffffff0f; |
| 27 | const INLINE_CODE_FILL: u32 = 0xffffff14; |
| 28 | const INLINE_CODE_FILL_OWN: u32 = 0x0000003d; |
| 29 | const QUOTE_FILL: u32 = 0x00000052; |
| 30 | const INLINE_CODE_BORDER: u32 = 0xffffff1a; |
| 31 | const TABLE_HEADER: u32 = 0xffffff0d; |
| 32 | const CLOSE_BUTTON_ICON: u32 = 0xffffff; |
| 33 | |
| 34 | pub fn color(hex: u32) -> Hsla { |
| 35 | rgb(hex).into() |
| 36 | } |
| 37 | |
| 38 | fn color_with_alpha(hex: u32) -> Hsla { |
| 39 | rgba(hex).into() |
| 40 | } |
| 41 | |
| 42 | pub fn load_fonts(cx: &mut App) { |
| 43 | let fonts = FONT_FILES |
| 44 | .iter() |
| 45 | .map(|bytes| Cow::Borrowed(*bytes)) |
| 46 | .collect(); |
| 47 | let _ = cx.text_system().add_fonts(fonts); |
| 48 | } |
| 49 | |
| 50 | pub fn apply(cx: &mut App) { |
| 51 | let installed = cx.text_system().all_font_names(); |
| 52 | let mono = MONO_FAMILIES |
| 53 | .iter() |
| 54 | .find(|family| installed.iter().any(|name| name == *family)) |
| 55 | .map(|family| SharedString::from(*family)); |
| 56 | Theme::change(ThemeMode::Dark, None, cx); |
| 57 | Theme::update(cx, |theme| { |
| 58 | theme.font_family = FONT_FAMILY.into(); |
| 59 | theme.font_size = px(BASE_FONT_SIZE); |
| 60 | if let Some(mono) = mono { |
| 61 | theme.mono_font_family = mono; |
| 62 | } |
| 63 | theme.radius_lg = px(10.); |
| 64 | let colors = &mut theme.colors; |
| 65 | colors.background = color(scheme().background); |
| 66 | colors.foreground = color(scheme().foreground); |
| 67 | colors.border = color(scheme().border); |
| 68 | colors.muted = color(scheme().border); |
| 69 | colors.muted_foreground = color(scheme().text_muted); |
| 70 | colors.sidebar = color(scheme().surface); |
| 71 | colors.sidebar_foreground = color(scheme().foreground); |
| 72 | colors.sidebar_border = color(scheme().border); |
| 73 | colors.sidebar_accent = color(scheme().popup_hover); |
| 74 | colors.sidebar_accent_foreground = color(scheme().foreground); |
| 75 | colors.sidebar_primary = color(scheme().accent); |
| 76 | colors.popover = color(scheme().popup); |
| 77 | colors.popover_foreground = color(scheme().foreground); |
| 78 | colors.accent = color(scheme().surface_raised); |
| 79 | colors.accent_foreground = color(scheme().foreground); |
| 80 | colors.secondary = color(scheme().surface_raised); |
| 81 | colors.secondary_hover = color(scheme().border_strong); |
| 82 | colors.secondary_active = color(scheme().border_strong); |
| 83 | colors.secondary_foreground = color(scheme().foreground); |
| 84 | colors.primary = color(scheme().accent); |
| 85 | colors.primary_hover = color(scheme().accent_text); |
| 86 | colors.primary_active = color(scheme().accent_strong); |
| 87 | colors.primary_foreground = color(scheme().on_accent); |
| 88 | colors.link = color(scheme().accent_text); |
| 89 | colors.link_hover = color(scheme().accent_soft); |
| 90 | colors.link_active = color(scheme().accent_strong); |
| 91 | colors.ring = color(scheme().accent); |
| 92 | colors.caret = color(scheme().accent); |
| 93 | colors.input = color(scheme().border_strong); |
| 94 | colors.selection = color_with_alpha(scheme().selection); |
| 95 | colors.title_bar = color(scheme().background); |
| 96 | colors.title_bar_border = color(scheme().border); |
| 97 | colors.status_bar = color(scheme().background); |
| 98 | colors.status_bar_border = color(scheme().border); |
| 99 | colors.success = color(scheme().green); |
| 100 | colors.warning = color(scheme().amber); |
| 101 | colors.danger = color(scheme().close_button_hover); |
| 102 | colors.danger_active = color(scheme().close_button_pressed); |
| 103 | colors.danger_foreground = color(CLOSE_BUTTON_ICON); |
| 104 | colors.drop_target = color_with_alpha(scheme().drop_background); |
| 105 | colors.window_border = color(scheme().border); |
| 106 | colors.scrollbar_thumb = color(scheme().border_strong); |
| 107 | colors.scrollbar_thumb_hover = color(scheme().text_faint); |
| 108 | colors.list = color(scheme().background); |
| 109 | colors.list_hover = color(scheme().row_hover); |
| 110 | colors.list_active = color(scheme().surface_raised); |
| 111 | }); |
| 112 | cx.set_global(elevation_tokens()); |
| 113 | } |
| 114 | |
| 115 | fn elevation_tokens() -> ElevationTokens { |
| 116 | let flyout = Elevation { |
| 117 | fill: color(scheme().popup), |
| 118 | hover: color(scheme().popup_hover), |
| 119 | shadow: elevation::shadow16(), |
| 120 | }; |
| 121 | let nested_flyout = Elevation { |
| 122 | fill: color(scheme().popup_nested), |
| 123 | hover: color(scheme().popup_nested_hover), |
| 124 | shadow: elevation::shadow16(), |
| 125 | }; |
| 126 | ElevationTokens { |
| 127 | flyouts: vec![flyout.clone(), nested_flyout], |
| 128 | dialog: Elevation { |
| 129 | shadow: elevation::shadow64(), |
| 130 | ..flyout.clone() |
| 131 | }, |
| 132 | toast: Elevation { |
| 133 | shadow: elevation::shadow8(), |
| 134 | ..flyout |
| 135 | }, |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | pub fn flyout_shadow() -> Vec<BoxShadow> { |
| 140 | elevation::shadow16() |
| 141 | } |
| 142 | |
| 143 | pub fn dialog_shadow() -> Vec<BoxShadow> { |
| 144 | elevation::shadow64() |
| 145 | } |
| 146 | |
| 147 | pub fn toast_shadow() -> Vec<BoxShadow> { |
| 148 | elevation::shadow8() |
| 149 | } |
| 150 | |
| 151 | pub fn popup_nested() -> Hsla { |
| 152 | color(scheme().popup_nested) |
| 153 | } |
| 154 | |
| 155 | pub trait ElevatedSurface: Styled + Sized { |
| 156 | fn flyout_surface(self) -> Self { |
| 157 | self.bg(popup()).shadow(flyout_shadow()) |
| 158 | } |
| 159 | |
| 160 | fn nested_flyout_surface(self) -> Self { |
| 161 | self.bg(popup_nested()).shadow(flyout_shadow()) |
| 162 | } |
| 163 | |
| 164 | fn dialog_surface(self) -> Self { |
| 165 | self.bg(popup()).shadow(dialog_shadow()) |
| 166 | } |
| 167 | |
| 168 | fn toast_surface(self) -> Self { |
| 169 | self.bg(popup()).shadow(toast_shadow()) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | impl<T: Styled + Sized> ElevatedSurface for T {} |
| 174 | |
| 175 | pub fn font_family() -> SharedString { |
| 176 | FONT_FAMILY.into() |
| 177 | } |
| 178 | |
| 179 | pub fn background() -> Hsla { |
| 180 | color(scheme().background) |
| 181 | } |
| 182 | |
| 183 | pub fn background_hex() -> u32 { |
| 184 | scheme().background |
| 185 | } |
| 186 | |
| 187 | pub fn toast_mention_fill() -> Hsla { |
| 188 | color(scheme().toast_mention) |
| 189 | } |
| 190 | |
| 191 | pub fn surface() -> Hsla { |
| 192 | color(scheme().surface) |
| 193 | } |
| 194 | |
| 195 | pub fn surface_raised() -> Hsla { |
| 196 | color(scheme().surface_raised) |
| 197 | } |
| 198 | |
| 199 | pub fn row_hover() -> Hsla { |
| 200 | color(scheme().row_hover) |
| 201 | } |
| 202 | |
| 203 | pub fn popup() -> Hsla { |
| 204 | color(scheme().popup) |
| 205 | } |
| 206 | |
| 207 | pub fn popup_hover() -> Hsla { |
| 208 | color(scheme().popup_hover) |
| 209 | } |
| 210 | |
| 211 | pub fn reply_surface() -> Hsla { |
| 212 | color(scheme().reply_surface) |
| 213 | } |
| 214 | |
| 215 | pub fn border() -> Hsla { |
| 216 | color(scheme().border) |
| 217 | } |
| 218 | |
| 219 | pub fn border_strong() -> Hsla { |
| 220 | color(scheme().border_strong) |
| 221 | } |
| 222 | |
| 223 | pub fn text() -> Hsla { |
| 224 | color(scheme().foreground) |
| 225 | } |
| 226 | |
| 227 | pub fn text_strong() -> Hsla { |
| 228 | color(scheme().text_strong) |
| 229 | } |
| 230 | |
| 231 | pub fn text_soft() -> Hsla { |
| 232 | color(scheme().text_soft) |
| 233 | } |
| 234 | |
| 235 | pub fn text_muted() -> Hsla { |
| 236 | color(scheme().text_muted) |
| 237 | } |
| 238 | |
| 239 | pub fn text_faint() -> Hsla { |
| 240 | color(scheme().text_faint) |
| 241 | } |
| 242 | |
| 243 | pub fn accent() -> Hsla { |
| 244 | color(scheme().accent) |
| 245 | } |
| 246 | |
| 247 | pub fn accent_text() -> Hsla { |
| 248 | color(scheme().accent_text) |
| 249 | } |
| 250 | |
| 251 | pub fn accent_soft() -> Hsla { |
| 252 | color(scheme().accent_soft) |
| 253 | } |
| 254 | |
| 255 | pub fn on_accent() -> Hsla { |
| 256 | color(scheme().on_accent) |
| 257 | } |
| 258 | |
| 259 | pub fn bubble_own() -> Hsla { |
| 260 | color(scheme().bubble_own) |
| 261 | } |
| 262 | |
| 263 | pub fn bubble_other() -> Hsla { |
| 264 | color(scheme().bubble_other) |
| 265 | } |
| 266 | |
| 267 | pub fn reaction_on_own() -> Hsla { |
| 268 | color(scheme().reaction_on_own) |
| 269 | } |
| 270 | |
| 271 | pub fn white() -> Hsla { |
| 272 | color(WHITE) |
| 273 | } |
| 274 | |
| 275 | pub fn mention_background(own: bool) -> Hsla { |
| 276 | color(if own { |
| 277 | scheme().mention_background_own |
| 278 | } else { |
| 279 | scheme().mention_background |
| 280 | }) |
| 281 | } |
| 282 | |
| 283 | pub fn own_meta() -> Hsla { |
| 284 | color(scheme().own_meta) |
| 285 | } |
| 286 | |
| 287 | pub fn own_read() -> Hsla { |
| 288 | color(scheme().own_read) |
| 289 | } |
| 290 | |
| 291 | pub fn mention_text() -> Hsla { |
| 292 | color(scheme().accent_soft) |
| 293 | } |
| 294 | |
| 295 | pub fn drop_background() -> Hsla { |
| 296 | color_with_alpha(scheme().drop_background) |
| 297 | } |
| 298 | |
| 299 | pub fn code_surface() -> Hsla { |
| 300 | color_with_alpha(CODE_SURFACE) |
| 301 | } |
| 302 | |
| 303 | pub fn code_header_border() -> Hsla { |
| 304 | color_with_alpha(CODE_HEADER_BORDER) |
| 305 | } |
| 306 | |
| 307 | pub fn inline_code_fill(own: bool) -> Hsla { |
| 308 | color_with_alpha(if own { |
| 309 | INLINE_CODE_FILL_OWN |
| 310 | } else { |
| 311 | INLINE_CODE_FILL |
| 312 | }) |
| 313 | } |
| 314 | |
| 315 | pub fn quote_fill() -> Hsla { |
| 316 | color_with_alpha(QUOTE_FILL) |
| 317 | } |
| 318 | |
| 319 | pub fn inline_code_border() -> Hsla { |
| 320 | color_with_alpha(INLINE_CODE_BORDER) |
| 321 | } |
| 322 | |
| 323 | pub fn table_header() -> Hsla { |
| 324 | color_with_alpha(TABLE_HEADER) |
| 325 | } |
| 326 | |
| 327 | pub fn rule(own: bool) -> Hsla { |
| 328 | if own { |
| 329 | color(scheme().rule_own) |
| 330 | } else { |
| 331 | border_strong() |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | pub fn badge_muted() -> Hsla { |
| 336 | color(scheme().badge_muted) |
| 337 | } |
| 338 | |
| 339 | pub fn green() -> Hsla { |
| 340 | color(scheme().green) |
| 341 | } |
| 342 | |
| 343 | pub fn red() -> Hsla { |
| 344 | color(scheme().red) |
| 345 | } |
| 346 | |
| 347 | pub fn red_soft() -> Hsla { |
| 348 | color(scheme().red_soft) |
| 349 | } |
| 350 | |
| 351 | pub fn red_tint() -> Hsla { |
| 352 | color(scheme().red_tint) |
| 353 | } |
| 354 | |
| 355 | pub fn amber() -> Hsla { |
| 356 | color(scheme().amber) |
| 357 | } |
| 358 | |
| 359 | pub fn out_of_office() -> Hsla { |
| 360 | color(scheme().out_of_office) |
| 361 | } |
| 362 | |
| 363 | pub fn meeting_avatar() -> Hsla { |
| 364 | color(scheme().meeting_avatar) |
| 365 | } |
| 366 | |
| 367 | pub fn bot_avatar() -> Hsla { |
| 368 | color(scheme().bot_avatar) |
| 369 | } |
| 370 | |
| 371 | pub fn avatar_color(index: usize) -> Hsla { |
| 372 | let palette = scheme().avatar_palette; |
| 373 | color(palette[index % palette.len()]) |
| 374 | } |
| 375 | |
| 376 | pub fn avatar_palette_len() -> usize { |
| 377 | scheme().avatar_palette.len() |
| 378 | } |
| 379 | |
| 380 | #[cfg(test)] |
| 381 | mod tests { |
| 382 | use super::{ |
| 383 | SCHEMES, avatar_color, avatar_palette_len, color, elevation_tokens, set_thread_scheme, |
| 384 | }; |
| 385 | |
| 386 | #[test] |
| 387 | fn avatar_colors_wrap_around_the_palette() { |
| 388 | assert_eq!(avatar_color(0), avatar_color(avatar_palette_len())); |
| 389 | assert_ne!(avatar_color(0), avatar_color(1)); |
| 390 | } |
| 391 | |
| 392 | #[test] |
| 393 | fn flyout_over_a_dialog_takes_a_lighter_fill_than_the_dialog() { |
| 394 | for scheme in SCHEMES { |
| 395 | set_thread_scheme(Some(scheme.id)); |
| 396 | let tokens = elevation_tokens(); |
| 397 | assert_eq!(tokens.dialog.fill, color(scheme.popup)); |
| 398 | assert_eq!(tokens.flyout_at(0).fill, color(scheme.popup)); |
| 399 | let nested = tokens.flyout_at(1); |
| 400 | assert_eq!(nested.fill, color(scheme.popup_nested)); |
| 401 | assert!(nested.fill.l > tokens.dialog.fill.l); |
| 402 | assert!(nested.hover.l > nested.fill.l); |
| 403 | assert_ne!(nested.fill, color(scheme.popup_hover)); |
| 404 | } |
| 405 | set_thread_scheme(None); |
| 406 | } |
| 407 | |
| 408 | #[test] |
| 409 | fn fluent_shadow_blur_is_halved_for_gpui() { |
| 410 | let shadow = super::flyout_shadow(); |
| 411 | assert_eq!(shadow[0].blur_radius, gpui_kit::px(1.)); |
| 412 | assert_eq!(shadow[1].offset.y, gpui_kit::px(8.)); |
| 413 | assert_eq!(shadow[1].blur_radius, gpui_kit::px(8.)); |
| 414 | assert_eq!(super::dialog_shadow()[1].blur_radius, gpui_kit::px(32.)); |
| 415 | } |
| 416 | } |