| 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 | 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_TINT: u32 = 0xf5c4a5; |
| 31 | const ACCENT_STRONG: u32 = 0xa84f27; |
| 32 | const BUBBLE_OWN: u32 = 0x4f2a1b; |
| 33 | const BUBBLE_OTHER: u32 = 0x232325; |
| 34 | const MENTION_BACKGROUND: u32 = 0x3a2a23; |
| 35 | const MENTION_BACKGROUND_OWN: u32 = 0x3b1d11; |
| 36 | const OWN_META: u32 = 0xc9a28c; |
| 37 | const OWN_READ: u32 = 0xf5c4a5; |
| 38 | const DROP_BACKGROUND: u32 = 0xce6a3b14; |
| 39 | const CODE_BACKGROUND: u32 = 0x0b0b0c; |
| 40 | const BADGE_MUTED: u32 = 0x3f3f42; |
| 41 | const GREEN: u32 = 0x22c55e; |
| 42 | const RED: u32 = 0xef4444; |
| 43 | const CLOSE_BUTTON_HOVER: u32 = 0xc42b1c; |
| 44 | const CLOSE_BUTTON_PRESSED: u32 = 0xb5271a; |
| 45 | const CLOSE_BUTTON_ICON: u32 = 0xffffff; |
| 46 | const RED_SOFT: u32 = 0xf87171; |
| 47 | const RED_TINT: u32 = 0xfca5a5; |
| 48 | const AMBER: u32 = 0xfbbf24; |
| 49 | const AVATAR_PALETTE: [u32; 6] = [0x9a4a22, 0x44606e, 0x8a3434, 0x4f6a3c, 0x6a4f7a, 0x2f6b63]; |
| 50 | |
| 51 | pub fn color(hex: u32) -> Hsla { |
| 52 | rgb(hex).into() |
| 53 | } |
| 54 | |
| 55 | fn color_with_alpha(hex: u32) -> Hsla { |
| 56 | rgba(hex).into() |
| 57 | } |
| 58 | |
| 59 | pub fn load_fonts(cx: &mut App) { |
| 60 | let fonts = FONT_FILES |
| 61 | .iter() |
| 62 | .map(|bytes| Cow::Borrowed(*bytes)) |
| 63 | .collect(); |
| 64 | let _ = cx.text_system().add_fonts(fonts); |
| 65 | } |
| 66 | |
| 67 | pub fn apply(cx: &mut App) { |
| 68 | let installed = cx.text_system().all_font_names(); |
| 69 | let mono = MONO_FAMILIES |
| 70 | .iter() |
| 71 | .find(|family| installed.iter().any(|name| name == *family)) |
| 72 | .map(|family| SharedString::from(*family)); |
| 73 | Theme::change(ThemeMode::Dark, None, cx); |
| 74 | Theme::update(cx, |theme| { |
| 75 | theme.font_family = FONT_FAMILY.into(); |
| 76 | theme.font_size = px(BASE_FONT_SIZE); |
| 77 | if let Some(mono) = mono { |
| 78 | theme.mono_font_family = mono; |
| 79 | } |
| 80 | let colors = &mut theme.colors; |
| 81 | colors.background = color(BACKGROUND); |
| 82 | colors.foreground = color(FOREGROUND); |
| 83 | colors.border = color(BORDER); |
| 84 | colors.muted = color(BORDER); |
| 85 | colors.muted_foreground = color(TEXT_MUTED); |
| 86 | colors.sidebar = color(SURFACE); |
| 87 | colors.sidebar_foreground = color(FOREGROUND); |
| 88 | colors.sidebar_border = color(BORDER); |
| 89 | colors.sidebar_accent = color(SURFACE_RAISED); |
| 90 | colors.sidebar_accent_foreground = color(FOREGROUND); |
| 91 | colors.sidebar_primary = color(ACCENT); |
| 92 | colors.popover = color(SURFACE); |
| 93 | colors.popover_foreground = color(FOREGROUND); |
| 94 | colors.accent = color(SURFACE_RAISED); |
| 95 | colors.accent_foreground = color(FOREGROUND); |
| 96 | colors.secondary = color(SURFACE_RAISED); |
| 97 | colors.secondary_hover = color(BORDER_STRONG); |
| 98 | colors.secondary_active = color(BORDER_STRONG); |
| 99 | colors.secondary_foreground = color(FOREGROUND); |
| 100 | colors.primary = color(ACCENT); |
| 101 | colors.primary_hover = color(ACCENT_TEXT); |
| 102 | colors.primary_active = color(ACCENT_STRONG); |
| 103 | colors.primary_foreground = color(BACKGROUND); |
| 104 | colors.link = color(ACCENT_TEXT); |
| 105 | colors.link_hover = color(ACCENT_SOFT); |
| 106 | colors.link_active = color(ACCENT_STRONG); |
| 107 | colors.ring = color(ACCENT); |
| 108 | colors.caret = color(ACCENT); |
| 109 | colors.input = color(BORDER_STRONG); |
| 110 | colors.selection = color_with_alpha(MENTION_BACKGROUND); |
| 111 | colors.title_bar = color(BACKGROUND); |
| 112 | colors.title_bar_border = color(BORDER); |
| 113 | colors.status_bar = color(BACKGROUND); |
| 114 | colors.status_bar_border = color(BORDER); |
| 115 | colors.success = color(GREEN); |
| 116 | colors.warning = color(AMBER); |
| 117 | colors.danger = color(CLOSE_BUTTON_HOVER); |
| 118 | colors.danger_active = color(CLOSE_BUTTON_PRESSED); |
| 119 | colors.danger_foreground = color(CLOSE_BUTTON_ICON); |
| 120 | colors.drop_target = color_with_alpha(DROP_BACKGROUND); |
| 121 | colors.window_border = color(BORDER); |
| 122 | colors.scrollbar_thumb = color(BORDER_STRONG); |
| 123 | colors.scrollbar_thumb_hover = color(TEXT_FAINT); |
| 124 | colors.list = color(BACKGROUND); |
| 125 | colors.list_hover = color(ROW_HOVER); |
| 126 | colors.list_active = color(SURFACE_RAISED); |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | pub fn background() -> Hsla { |
| 131 | color(BACKGROUND) |
| 132 | } |
| 133 | |
| 134 | pub fn surface() -> Hsla { |
| 135 | color(SURFACE) |
| 136 | } |
| 137 | |
| 138 | pub fn surface_raised() -> Hsla { |
| 139 | color(SURFACE_RAISED) |
| 140 | } |
| 141 | |
| 142 | pub fn row_hover() -> Hsla { |
| 143 | color(ROW_HOVER) |
| 144 | } |
| 145 | |
| 146 | pub fn border() -> Hsla { |
| 147 | color(BORDER) |
| 148 | } |
| 149 | |
| 150 | pub fn border_strong() -> Hsla { |
| 151 | color(BORDER_STRONG) |
| 152 | } |
| 153 | |
| 154 | pub fn text() -> Hsla { |
| 155 | color(FOREGROUND) |
| 156 | } |
| 157 | |
| 158 | pub fn text_strong() -> Hsla { |
| 159 | color(TEXT_STRONG) |
| 160 | } |
| 161 | |
| 162 | pub fn text_soft() -> Hsla { |
| 163 | color(TEXT_SOFT) |
| 164 | } |
| 165 | |
| 166 | pub fn text_muted() -> Hsla { |
| 167 | color(TEXT_MUTED) |
| 168 | } |
| 169 | |
| 170 | pub fn text_faint() -> Hsla { |
| 171 | color(TEXT_FAINT) |
| 172 | } |
| 173 | |
| 174 | pub fn accent() -> Hsla { |
| 175 | color(ACCENT) |
| 176 | } |
| 177 | |
| 178 | pub fn accent_text() -> Hsla { |
| 179 | color(ACCENT_TEXT) |
| 180 | } |
| 181 | |
| 182 | pub fn accent_soft() -> Hsla { |
| 183 | color(ACCENT_SOFT) |
| 184 | } |
| 185 | |
| 186 | pub fn accent_tint() -> Hsla { |
| 187 | color(ACCENT_TINT) |
| 188 | } |
| 189 | |
| 190 | pub fn on_accent() -> Hsla { |
| 191 | color(BACKGROUND) |
| 192 | } |
| 193 | |
| 194 | pub fn bubble_own() -> Hsla { |
| 195 | color(BUBBLE_OWN) |
| 196 | } |
| 197 | |
| 198 | pub fn bubble_other() -> Hsla { |
| 199 | color(BUBBLE_OTHER) |
| 200 | } |
| 201 | |
| 202 | pub fn mention_background(own: bool) -> Hsla { |
| 203 | color(if own { |
| 204 | MENTION_BACKGROUND_OWN |
| 205 | } else { |
| 206 | MENTION_BACKGROUND |
| 207 | }) |
| 208 | } |
| 209 | |
| 210 | pub fn own_meta() -> Hsla { |
| 211 | color(OWN_META) |
| 212 | } |
| 213 | |
| 214 | pub fn own_read() -> Hsla { |
| 215 | color(OWN_READ) |
| 216 | } |
| 217 | |
| 218 | pub fn mention_text() -> Hsla { |
| 219 | color(ACCENT_SOFT) |
| 220 | } |
| 221 | |
| 222 | pub fn drop_background() -> Hsla { |
| 223 | color_with_alpha(DROP_BACKGROUND) |
| 224 | } |
| 225 | |
| 226 | pub fn code_background() -> Hsla { |
| 227 | color(CODE_BACKGROUND) |
| 228 | } |
| 229 | |
| 230 | pub fn badge_muted() -> Hsla { |
| 231 | color(BADGE_MUTED) |
| 232 | } |
| 233 | |
| 234 | pub fn green() -> Hsla { |
| 235 | color(GREEN) |
| 236 | } |
| 237 | |
| 238 | pub fn red() -> Hsla { |
| 239 | color(RED) |
| 240 | } |
| 241 | |
| 242 | pub fn red_soft() -> Hsla { |
| 243 | color(RED_SOFT) |
| 244 | } |
| 245 | |
| 246 | pub fn red_tint() -> Hsla { |
| 247 | color(RED_TINT) |
| 248 | } |
| 249 | |
| 250 | pub fn amber() -> Hsla { |
| 251 | color(AMBER) |
| 252 | } |
| 253 | |
| 254 | pub fn avatar_color(index: usize) -> Hsla { |
| 255 | color(AVATAR_PALETTE[index % AVATAR_PALETTE.len()]) |
| 256 | } |
| 257 | |
| 258 | pub fn avatar_palette_len() -> usize { |
| 259 | AVATAR_PALETTE.len() |
| 260 | } |
| 261 | |
| 262 | #[cfg(test)] |
| 263 | mod tests { |
| 264 | use super::{avatar_color, avatar_palette_len}; |
| 265 | |
| 266 | #[test] |
| 267 | fn avatar_colors_wrap_around_the_palette() { |
| 268 | assert_eq!(avatar_color(0), avatar_color(avatar_palette_len())); |
| 269 | assert_ne!(avatar_color(0), avatar_color(1)); |
| 270 | } |
| 271 | } |