| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use gpui_kit::component::{Theme, ThemeMode}; |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | const FONT_FAMILY: &str = "Google Sans Flex"; |
| 7 | const MONO_FAMILIES: [&str; 2] = ["Cascadia Code", "Consolas"]; |
| 8 | const BASE_FONT_SIZE: f32 = 14.; |
| 9 | const FONT_FILES: [&[u8]; 4] = [ |
| 10 | include_bytes!("../assets/fonts/GoogleSansFlex-Regular.ttf"), |
| 11 | include_bytes!("../assets/fonts/GoogleSansFlex-SemiBold.ttf"), |
| 12 | include_bytes!("../assets/fonts/GoogleSansFlex-Bold.ttf"), |
| 13 | include_bytes!("../assets/fonts/GoogleSansFlex-Italic.ttf"), |
| 14 | ]; |
| 15 | |
| 16 | pub const BACKGROUND: u32 = 0x0f0f10; |
| 17 | const SURFACE: u32 = 0x161617; |
| 18 | const SURFACE_RAISED: u32 = 0x232325; |
| 19 | const ROW_HOVER: u32 = 0x1c1c1e; |
| 20 | const BORDER: u32 = 0x262628; |
| 21 | const BORDER_STRONG: u32 = 0x3f3f42; |
| 22 | const FOREGROUND: u32 = 0xf4f4f5; |
| 23 | const TEXT_STRONG: u32 = 0xe4e4e7; |
| 24 | const TEXT_SOFT: u32 = 0xd4d4d8; |
| 25 | const TEXT_MUTED: u32 = 0xa1a1a6; |
| 26 | const TEXT_FAINT: u32 = 0x8e8e93; |
| 27 | const ACCENT: u32 = 0xce6a3b; |
| 28 | const ACCENT_TEXT: u32 = 0xe08a5c; |
| 29 | const ACCENT_SOFT: u32 = 0xf5c4a5; |
| 30 | const ACCENT_STRONG: u32 = 0xa84f27; |
| 31 | const BUBBLE_OWN: u32 = 0x4f2a1b; |
| 32 | const BUBBLE_OTHER: u32 = 0x232325; |
| 33 | const REACTION_ON_OWN: u32 = 0x6b3a26; |
| 34 | const WHITE: u32 = 0xffffff; |
| 35 | const MENTION_BACKGROUND: u32 = 0x3a2a23; |
| 36 | const MENTION_BACKGROUND_OWN: u32 = 0x3b1d11; |
| 37 | const OWN_META: u32 = 0xc9a28c; |
| 38 | const OWN_READ: u32 = 0xf5c4a5; |
| 39 | const DROP_BACKGROUND: u32 = 0xce6a3b14; |
| 40 | const SELECTION: u32 = 0xce6a3b66; |
| 41 | const CODE_SURFACE: u32 = 0x0000003d; |
| 42 | const CODE_HEADER_BORDER: u32 = 0xffffff0f; |
| 43 | const INLINE_CODE_FILL: u32 = 0xffffff14; |
| 44 | const INLINE_CODE_FILL_OWN: u32 = 0x0000003d; |
| 45 | const QUOTE_FILL: u32 = 0x00000052; |
| 46 | const INLINE_CODE_BORDER: u32 = 0xffffff1a; |
| 47 | const TABLE_HEADER: u32 = 0xffffff0d; |
| 48 | const RULE_OWN: u32 = 0x6a3a25; |
| 49 | const BADGE_MUTED: u32 = 0x3f3f42; |
| 50 | const TOAST_MENTION: u32 = 0x2e1d15; |
| 51 | const GREEN: u32 = 0x22c55e; |
| 52 | const RED: u32 = 0xef4444; |
| 53 | const CLOSE_BUTTON_HOVER: u32 = 0xc42b1c; |
| 54 | const CLOSE_BUTTON_PRESSED: u32 = 0xb5271a; |
| 55 | const CLOSE_BUTTON_ICON: u32 = 0xffffff; |
| 56 | const RED_SOFT: u32 = 0xf87171; |
| 57 | const RED_TINT: u32 = 0xfca5a5; |
| 58 | const AMBER: u32 = 0xfbbf24; |
| 59 | const AVATAR_PALETTE: [u32; 6] = [0x9a4a22, 0x44606e, 0x8a3434, 0x4f6a3c, 0x6a4f7a, 0x2f6b63]; |
| 60 | |
| 61 | pub fn color(hex: u32) -> Hsla { |
| 62 | rgb(hex).into() |
| 63 | } |
| 64 | |
| 65 | fn color_with_alpha(hex: u32) -> Hsla { |
| 66 | rgba(hex).into() |
| 67 | } |
| 68 | |
| 69 | pub fn load_fonts(cx: &mut App) { |
| 70 | let fonts = FONT_FILES |
| 71 | .iter() |
| 72 | .map(|bytes| Cow::Borrowed(*bytes)) |
| 73 | .collect(); |
| 74 | let _ = cx.text_system().add_fonts(fonts); |
| 75 | } |
| 76 | |
| 77 | pub fn apply(cx: &mut App) { |
| 78 | let installed = cx.text_system().all_font_names(); |
| 79 | let mono = MONO_FAMILIES |
| 80 | .iter() |
| 81 | .find(|family| installed.iter().any(|name| name == *family)) |
| 82 | .map(|family| SharedString::from(*family)); |
| 83 | Theme::change(ThemeMode::Dark, None, cx); |
| 84 | Theme::update(cx, |theme| { |
| 85 | theme.font_family = FONT_FAMILY.into(); |
| 86 | theme.font_size = px(BASE_FONT_SIZE); |
| 87 | if let Some(mono) = mono { |
| 88 | theme.mono_font_family = mono; |
| 89 | } |
| 90 | let colors = &mut theme.colors; |
| 91 | colors.background = color(BACKGROUND); |
| 92 | colors.foreground = color(FOREGROUND); |
| 93 | colors.border = color(BORDER); |
| 94 | colors.muted = color(BORDER); |
| 95 | colors.muted_foreground = color(TEXT_MUTED); |
| 96 | colors.sidebar = color(SURFACE); |
| 97 | colors.sidebar_foreground = color(FOREGROUND); |
| 98 | colors.sidebar_border = color(BORDER); |
| 99 | colors.sidebar_accent = color(SURFACE_RAISED); |
| 100 | colors.sidebar_accent_foreground = color(FOREGROUND); |
| 101 | colors.sidebar_primary = color(ACCENT); |
| 102 | colors.popover = color(SURFACE); |
| 103 | colors.popover_foreground = color(FOREGROUND); |
| 104 | colors.accent = color(SURFACE_RAISED); |
| 105 | colors.accent_foreground = color(FOREGROUND); |
| 106 | colors.secondary = color(SURFACE_RAISED); |
| 107 | colors.secondary_hover = color(BORDER_STRONG); |
| 108 | colors.secondary_active = color(BORDER_STRONG); |
| 109 | colors.secondary_foreground = color(FOREGROUND); |
| 110 | colors.primary = color(ACCENT); |
| 111 | colors.primary_hover = color(ACCENT_TEXT); |
| 112 | colors.primary_active = color(ACCENT_STRONG); |
| 113 | colors.primary_foreground = color(BACKGROUND); |
| 114 | colors.link = color(ACCENT_TEXT); |
| 115 | colors.link_hover = color(ACCENT_SOFT); |
| 116 | colors.link_active = color(ACCENT_STRONG); |
| 117 | colors.ring = color(ACCENT); |
| 118 | colors.caret = color(ACCENT); |
| 119 | colors.input = color(BORDER_STRONG); |
| 120 | colors.selection = color_with_alpha(SELECTION); |
| 121 | colors.title_bar = color(BACKGROUND); |
| 122 | colors.title_bar_border = color(BORDER); |
| 123 | colors.status_bar = color(BACKGROUND); |
| 124 | colors.status_bar_border = color(BORDER); |
| 125 | colors.success = color(GREEN); |
| 126 | colors.warning = color(AMBER); |
| 127 | colors.danger = color(CLOSE_BUTTON_HOVER); |
| 128 | colors.danger_active = color(CLOSE_BUTTON_PRESSED); |
| 129 | colors.danger_foreground = color(CLOSE_BUTTON_ICON); |
| 130 | colors.drop_target = color_with_alpha(DROP_BACKGROUND); |
| 131 | colors.window_border = color(BORDER); |
| 132 | colors.scrollbar_thumb = color(BORDER_STRONG); |
| 133 | colors.scrollbar_thumb_hover = color(TEXT_FAINT); |
| 134 | colors.list = color(BACKGROUND); |
| 135 | colors.list_hover = color(ROW_HOVER); |
| 136 | colors.list_active = color(SURFACE_RAISED); |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | pub fn font_family() -> SharedString { |
| 141 | FONT_FAMILY.into() |
| 142 | } |
| 143 | |
| 144 | pub fn background() -> Hsla { |
| 145 | color(BACKGROUND) |
| 146 | } |
| 147 | |
| 148 | pub fn toast_mention_fill() -> Hsla { |
| 149 | color(TOAST_MENTION) |
| 150 | } |
| 151 | |
| 152 | pub fn surface() -> Hsla { |
| 153 | color(SURFACE) |
| 154 | } |
| 155 | |
| 156 | pub fn surface_raised() -> Hsla { |
| 157 | color(SURFACE_RAISED) |
| 158 | } |
| 159 | |
| 160 | pub fn row_hover() -> Hsla { |
| 161 | color(ROW_HOVER) |
| 162 | } |
| 163 | |
| 164 | pub fn border() -> Hsla { |
| 165 | color(BORDER) |
| 166 | } |
| 167 | |
| 168 | pub fn border_strong() -> Hsla { |
| 169 | color(BORDER_STRONG) |
| 170 | } |
| 171 | |
| 172 | pub fn text() -> Hsla { |
| 173 | color(FOREGROUND) |
| 174 | } |
| 175 | |
| 176 | pub fn text_strong() -> Hsla { |
| 177 | color(TEXT_STRONG) |
| 178 | } |
| 179 | |
| 180 | pub fn text_soft() -> Hsla { |
| 181 | color(TEXT_SOFT) |
| 182 | } |
| 183 | |
| 184 | pub fn text_muted() -> Hsla { |
| 185 | color(TEXT_MUTED) |
| 186 | } |
| 187 | |
| 188 | pub fn text_faint() -> Hsla { |
| 189 | color(TEXT_FAINT) |
| 190 | } |
| 191 | |
| 192 | pub fn accent() -> Hsla { |
| 193 | color(ACCENT) |
| 194 | } |
| 195 | |
| 196 | pub fn accent_text() -> Hsla { |
| 197 | color(ACCENT_TEXT) |
| 198 | } |
| 199 | |
| 200 | pub fn accent_soft() -> Hsla { |
| 201 | color(ACCENT_SOFT) |
| 202 | } |
| 203 | |
| 204 | pub fn on_accent() -> Hsla { |
| 205 | color(BACKGROUND) |
| 206 | } |
| 207 | |
| 208 | pub fn bubble_own() -> Hsla { |
| 209 | color(BUBBLE_OWN) |
| 210 | } |
| 211 | |
| 212 | pub fn bubble_other() -> Hsla { |
| 213 | color(BUBBLE_OTHER) |
| 214 | } |
| 215 | |
| 216 | pub fn reaction_on_own() -> Hsla { |
| 217 | color(REACTION_ON_OWN) |
| 218 | } |
| 219 | |
| 220 | pub fn white() -> Hsla { |
| 221 | color(WHITE) |
| 222 | } |
| 223 | |
| 224 | pub fn mention_background(own: bool) -> Hsla { |
| 225 | color(if own { |
| 226 | MENTION_BACKGROUND_OWN |
| 227 | } else { |
| 228 | MENTION_BACKGROUND |
| 229 | }) |
| 230 | } |
| 231 | |
| 232 | pub fn own_meta() -> Hsla { |
| 233 | color(OWN_META) |
| 234 | } |
| 235 | |
| 236 | pub fn own_read() -> Hsla { |
| 237 | color(OWN_READ) |
| 238 | } |
| 239 | |
| 240 | pub fn mention_text() -> Hsla { |
| 241 | color(ACCENT_SOFT) |
| 242 | } |
| 243 | |
| 244 | pub fn drop_background() -> Hsla { |
| 245 | color_with_alpha(DROP_BACKGROUND) |
| 246 | } |
| 247 | |
| 248 | pub fn code_surface() -> Hsla { |
| 249 | color_with_alpha(CODE_SURFACE) |
| 250 | } |
| 251 | |
| 252 | pub fn code_header_border() -> Hsla { |
| 253 | color_with_alpha(CODE_HEADER_BORDER) |
| 254 | } |
| 255 | |
| 256 | pub fn inline_code_fill(own: bool) -> Hsla { |
| 257 | color_with_alpha(if own { |
| 258 | INLINE_CODE_FILL_OWN |
| 259 | } else { |
| 260 | INLINE_CODE_FILL |
| 261 | }) |
| 262 | } |
| 263 | |
| 264 | pub fn quote_fill() -> Hsla { |
| 265 | color_with_alpha(QUOTE_FILL) |
| 266 | } |
| 267 | |
| 268 | pub fn inline_code_border() -> Hsla { |
| 269 | color_with_alpha(INLINE_CODE_BORDER) |
| 270 | } |
| 271 | |
| 272 | pub fn table_header() -> Hsla { |
| 273 | color_with_alpha(TABLE_HEADER) |
| 274 | } |
| 275 | |
| 276 | pub fn rule(own: bool) -> Hsla { |
| 277 | if own { |
| 278 | color(RULE_OWN) |
| 279 | } else { |
| 280 | border_strong() |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | pub fn badge_muted() -> Hsla { |
| 285 | color(BADGE_MUTED) |
| 286 | } |
| 287 | |
| 288 | pub fn green() -> Hsla { |
| 289 | color(GREEN) |
| 290 | } |
| 291 | |
| 292 | pub fn red() -> Hsla { |
| 293 | color(RED) |
| 294 | } |
| 295 | |
| 296 | pub fn red_soft() -> Hsla { |
| 297 | color(RED_SOFT) |
| 298 | } |
| 299 | |
| 300 | pub fn red_tint() -> Hsla { |
| 301 | color(RED_TINT) |
| 302 | } |
| 303 | |
| 304 | pub fn amber() -> Hsla { |
| 305 | color(AMBER) |
| 306 | } |
| 307 | |
| 308 | pub fn avatar_color(index: usize) -> Hsla { |
| 309 | color(AVATAR_PALETTE[index % AVATAR_PALETTE.len()]) |
| 310 | } |
| 311 | |
| 312 | pub fn avatar_palette_len() -> usize { |
| 313 | AVATAR_PALETTE.len() |
| 314 | } |
| 315 | |
| 316 | #[cfg(test)] |
| 317 | mod tests { |
| 318 | use super::{avatar_color, avatar_palette_len}; |
| 319 | |
| 320 | #[test] |
| 321 | fn avatar_colors_wrap_around_the_palette() { |
| 322 | assert_eq!(avatar_color(0), avatar_color(avatar_palette_len())); |
| 323 | assert_ne!(avatar_color(0), avatar_color(1)); |
| 324 | } |
| 325 | } |