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