| 1 | use std::time::{Duration, Instant}; |
| 2 | |
| 3 | use gpui_kit::component::v_flex; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use super::title_bar::{TITLE_BAR_HEIGHT, app_icon_image}; |
| 8 | use crate::backend::ConnectionState; |
| 9 | use crate::browser_sign_in::BrowserSignInState; |
| 10 | use crate::i18n::tr; |
| 11 | use crate::theme; |
| 12 | |
| 13 | const MIN_VISIBLE: Duration = Duration::from_millis(400); |
| 14 | const STARTING_FOR: Duration = Duration::from_millis(300); |
| 15 | const HINT_AFTER: Duration = Duration::from_secs(8); |
| 16 | const GIVE_UP_AFTER: Duration = Duration::from_secs(30); |
| 17 | const FADE_OUT: Duration = Duration::from_millis(220); |
| 18 | pub const CONTENT_RISE: Duration = Duration::from_millis(260); |
| 19 | pub const CONTENT_RISE_OFFSET: f32 = 8.; |
| 20 | const LOGO_SIZE: f32 = 76.; |
| 21 | const LOGO_BREATH: f32 = 0.04; |
| 22 | const BREATH_PERIOD: Duration = Duration::from_millis(2400); |
| 23 | const BAR_WIDTH: f32 = 168.; |
| 24 | const BAR_HEIGHT: f32 = 3.; |
| 25 | const BAR_TOP_GAP: f32 = 32.; |
| 26 | const SWEEP_SHARE: f32 = 0.4; |
| 27 | const SWEEP_PERIOD: Duration = Duration::from_millis(1300); |
| 28 | const STATUS_TOP_GAP: f32 = 14.; |
| 29 | const STATUS_SIZE: f32 = 13.; |
| 30 | const STATUS_SWAP: Duration = Duration::from_millis(180); |
| 31 | const STATUS_SWAP_OFFSET: f32 = 4.; |
| 32 | const HINT_SIZE: f32 = 12.; |
| 33 | pub const STILL_RECHECK: Duration = Duration::from_millis(250); |
| 34 | |
| 35 | pub struct StartupFacts<'a> { |
| 36 | pub elapsed: Duration, |
| 37 | pub connection: &'a ConnectionState, |
| 38 | pub browser_sign_in: &'a BrowserSignInState, |
| 39 | pub has_chats: bool, |
| 40 | pub synced: bool, |
| 41 | pub demo: bool, |
| 42 | pub opening: Option<String>, |
| 43 | } |
| 44 | |
| 45 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 46 | pub enum Phase { |
| 47 | Loading { |
| 48 | status: String, |
| 49 | running: bool, |
| 50 | hint: Option<String>, |
| 51 | }, |
| 52 | Ready, |
| 53 | } |
| 54 | |
| 55 | pub fn phase(facts: &StartupFacts) -> Phase { |
| 56 | // Managed-browser phases must win over a warm cache, or re-auth would |
| 57 | // skip the splash and never show opening/awaiting/finishing/failed copy. |
| 58 | if let Some(phase) = browser_phase(facts) { |
| 59 | return phase; |
| 60 | } |
| 61 | let content_ready = facts.has_chats || facts.synced || facts.demo; |
| 62 | if facts.elapsed >= MIN_VISIBLE && content_ready && facts.opening.is_none() { |
| 63 | return Phase::Ready; |
| 64 | } |
| 65 | let signing_in = matches!(facts.connection, ConnectionState::LoginRequired); |
| 66 | if facts.elapsed >= GIVE_UP_AFTER && !(signing_in && !content_ready) { |
| 67 | return Phase::Ready; |
| 68 | } |
| 69 | let late_hint = |
| 70 | (facts.elapsed >= HINT_AFTER && !content_ready).then(|| tr("auth.splash.first_start_hint")); |
| 71 | let loading = |status: String| Phase::Loading { |
| 72 | status, |
| 73 | running: true, |
| 74 | hint: late_hint.clone(), |
| 75 | }; |
| 76 | if matches!(facts.connection, ConnectionState::LoginRequired) { |
| 77 | return Phase::Loading { |
| 78 | status: tr("auth.splash.awaiting_browser"), |
| 79 | running: false, |
| 80 | hint: None, |
| 81 | }; |
| 82 | } |
| 83 | if facts.elapsed < STARTING_FOR { |
| 84 | return loading(tr("auth.splash.starting")); |
| 85 | } |
| 86 | if content_ready { |
| 87 | return loading(facts.opening.as_ref().map_or_else( |
| 88 | || tr("auth.splash.syncing"), |
| 89 | |title| tr("auth.splash.opening").replace("%{title}", title), |
| 90 | )); |
| 91 | } |
| 92 | match facts.connection { |
| 93 | ConnectionState::Connecting => loading(tr("auth.splash.signing_in")), |
| 94 | ConnectionState::Online => loading(tr("auth.splash.loading_chats")), |
| 95 | _ => loading(tr("auth.splash.cannot_reach")), |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | fn browser_phase(facts: &StartupFacts) -> Option<Phase> { |
| 100 | let late_browser_hint = |
| 101 | (facts.elapsed >= HINT_AFTER).then(|| tr("auth.splash.browser_still_loading_hint")); |
| 102 | match facts.browser_sign_in { |
| 103 | BrowserSignInState::Idle => None, |
| 104 | BrowserSignInState::Opening => Some(Phase::Loading { |
| 105 | status: tr("auth.splash.opening_browser"), |
| 106 | running: true, |
| 107 | hint: late_browser_hint, |
| 108 | }), |
| 109 | BrowserSignInState::AwaitingSignIn => Some(Phase::Loading { |
| 110 | status: tr("auth.splash.awaiting_browser"), |
| 111 | running: false, |
| 112 | hint: late_browser_hint, |
| 113 | }), |
| 114 | BrowserSignInState::Finishing => Some(Phase::Loading { |
| 115 | status: tr("auth.splash.finishing"), |
| 116 | running: true, |
| 117 | hint: None, |
| 118 | }), |
| 119 | BrowserSignInState::Failed(detail) => Some(Phase::Loading { |
| 120 | status: tr("auth.splash.browser_failed"), |
| 121 | running: false, |
| 122 | hint: Some(detail.clone()), |
| 123 | }), |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub struct Splash { |
| 128 | started: Instant, |
| 129 | hidden_at: Option<Instant>, |
| 130 | pub recheck_scheduled: bool, |
| 131 | } |
| 132 | |
| 133 | impl Default for Splash { |
| 134 | fn default() -> Self { |
| 135 | Splash { |
| 136 | started: Instant::now(), |
| 137 | hidden_at: None, |
| 138 | recheck_scheduled: false, |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl Splash { |
| 144 | pub fn elapsed(&self) -> Duration { |
| 145 | self.started.elapsed() |
| 146 | } |
| 147 | |
| 148 | pub fn is_up(&self) -> bool { |
| 149 | self.hidden_at.is_none() |
| 150 | } |
| 151 | |
| 152 | pub fn hide(&mut self) { |
| 153 | self.hidden_at.get_or_insert_with(Instant::now); |
| 154 | } |
| 155 | |
| 156 | pub fn content_rising(&self) -> Option<Duration> { |
| 157 | self.hidden_at |
| 158 | .map(|hidden_at| hidden_at.elapsed()) |
| 159 | .filter(|since| *since < CONTENT_RISE) |
| 160 | } |
| 161 | |
| 162 | pub fn render(&self, phase: &Phase, cx: &App) -> Option<AnyElement> { |
| 163 | let fading = self.hidden_at.map(|hidden_at| hidden_at.elapsed()); |
| 164 | if fading.is_some_and(|since| since >= FADE_OUT) { |
| 165 | return None; |
| 166 | } |
| 167 | let still = cx.reduce_motion(); |
| 168 | let app_icon = crate::active_appearance::current(cx).app_icon; |
| 169 | let (status, running, hint) = match phase { |
| 170 | Phase::Loading { |
| 171 | status, |
| 172 | running, |
| 173 | hint, |
| 174 | } => (status.clone(), *running, hint.clone()), |
| 175 | Phase::Ready => (String::new(), true, None), |
| 176 | }; |
| 177 | let overlay = v_flex() |
| 178 | .id("startup-splash") |
| 179 | .debug_selector(|| "shell/splash".into()) |
| 180 | .occlude() |
| 181 | .absolute() |
| 182 | .top(px(TITLE_BAR_HEIGHT)) |
| 183 | .left_0() |
| 184 | .right_0() |
| 185 | .bottom_0() |
| 186 | .items_center() |
| 187 | .justify_center() |
| 188 | .bg(theme::background()) |
| 189 | .child(logo(app_icon, still)) |
| 190 | .child(bar(running, still)) |
| 191 | .child(status_line(status, still)) |
| 192 | .child( |
| 193 | div() |
| 194 | .mt(px(6.)) |
| 195 | .h(px(16.)) |
| 196 | .text_size(px(HINT_SIZE)) |
| 197 | .text_color(theme::text_faint()) |
| 198 | .children(hint), |
| 199 | ); |
| 200 | Some(match fading { |
| 201 | Some(_) if !still => overlay |
| 202 | .with_animation( |
| 203 | "startup-splash-fade", |
| 204 | Animation::new(FADE_OUT).with_easing(ease_out_quint()), |
| 205 | |overlay, delta| overlay.opacity(1. - delta), |
| 206 | ) |
| 207 | .into_any_element(), |
| 208 | _ => overlay.into_any_element(), |
| 209 | }) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fn logo(app_icon: crate::appearance::AppIcon, still: bool) -> AnyElement { |
| 214 | let image = img(app_icon_image(app_icon)).size(px(LOGO_SIZE)); |
| 215 | let frame = div() |
| 216 | .size(px(LOGO_SIZE * (1. + LOGO_BREATH))) |
| 217 | .flex() |
| 218 | .items_center() |
| 219 | .justify_center(); |
| 220 | if still { |
| 221 | return frame.child(image).into_any_element(); |
| 222 | } |
| 223 | frame |
| 224 | .child(image.with_animation( |
| 225 | "startup-splash-logo", |
| 226 | Animation::new(BREATH_PERIOD).repeat(), |
| 227 | |image, delta| { |
| 228 | let wave = (1. - (delta * std::f32::consts::TAU).cos()) / 2.; |
| 229 | image.size(px(LOGO_SIZE * (1. + LOGO_BREATH * wave))) |
| 230 | }, |
| 231 | )) |
| 232 | .into_any_element() |
| 233 | } |
| 234 | |
| 235 | fn bar(running: bool, still: bool) -> Div { |
| 236 | let sweep_width = BAR_WIDTH * SWEEP_SHARE; |
| 237 | let sweep = || { |
| 238 | div() |
| 239 | .absolute() |
| 240 | .top_0() |
| 241 | .bottom_0() |
| 242 | .w(px(sweep_width)) |
| 243 | .rounded(px(BAR_HEIGHT)) |
| 244 | .bg(theme::accent()) |
| 245 | }; |
| 246 | div() |
| 247 | .relative() |
| 248 | .mt(px(BAR_TOP_GAP)) |
| 249 | .w(px(BAR_WIDTH)) |
| 250 | .h(px(BAR_HEIGHT)) |
| 251 | .rounded(px(BAR_HEIGHT)) |
| 252 | .overflow_hidden() |
| 253 | .bg(theme::border_strong()) |
| 254 | .when(!running, |bar| bar.invisible()) |
| 255 | .when(running && still, |bar| { |
| 256 | bar.child(sweep().left(px((BAR_WIDTH - sweep_width) / 2.))) |
| 257 | }) |
| 258 | .when(running && !still, |bar| { |
| 259 | bar.child( |
| 260 | sweep().with_animation( |
| 261 | "startup-splash-sweep", |
| 262 | Animation::new(SWEEP_PERIOD) |
| 263 | .repeat() |
| 264 | .with_easing(ease_in_out), |
| 265 | move |sweep, delta| { |
| 266 | let head = (BAR_WIDTH + sweep_width) * delta; |
| 267 | let start = (head - sweep_width).max(0.); |
| 268 | sweep.left(px(start)).w(px(head.min(BAR_WIDTH) - start)) |
| 269 | }, |
| 270 | ), |
| 271 | ) |
| 272 | }) |
| 273 | } |
| 274 | |
| 275 | fn status_line(status: String, still: bool) -> AnyElement { |
| 276 | let line = div() |
| 277 | .text_size(px(STATUS_SIZE)) |
| 278 | .text_color(theme::text_muted()) |
| 279 | .child(status.clone()); |
| 280 | let slot = div() |
| 281 | .mt(px(STATUS_TOP_GAP)) |
| 282 | .h(px(STATUS_SIZE * 1.4)) |
| 283 | .relative(); |
| 284 | if still { |
| 285 | return slot.child(line).into_any_element(); |
| 286 | } |
| 287 | slot.child(line.relative().with_animation( |
| 288 | ElementId::Name(format!("startup-splash-status-{status}").into()), |
| 289 | Animation::new(STATUS_SWAP).with_easing(ease_out_quint()), |
| 290 | |line, delta| { |
| 291 | line.top(px(STATUS_SWAP_OFFSET * (1. - delta))) |
| 292 | .opacity(delta) |
| 293 | }, |
| 294 | )) |
| 295 | .into_any_element() |
| 296 | } |
| 297 | |
| 298 | #[cfg(test)] |
| 299 | mod tests { |
| 300 | use std::time::Duration; |
| 301 | |
| 302 | use super::{ConnectionState, Phase, StartupFacts, phase}; |
| 303 | use crate::browser_sign_in::BrowserSignInState; |
| 304 | |
| 305 | fn facts(elapsed_ms: u64, connection: &ConnectionState) -> StartupFacts<'_> { |
| 306 | facts_with(elapsed_ms, connection, &BrowserSignInState::Idle) |
| 307 | } |
| 308 | |
| 309 | fn facts_with<'a>( |
| 310 | elapsed_ms: u64, |
| 311 | connection: &'a ConnectionState, |
| 312 | browser_sign_in: &'a BrowserSignInState, |
| 313 | ) -> StartupFacts<'a> { |
| 314 | StartupFacts { |
| 315 | elapsed: Duration::from_millis(elapsed_ms), |
| 316 | connection, |
| 317 | browser_sign_in, |
| 318 | has_chats: false, |
| 319 | synced: false, |
| 320 | demo: false, |
| 321 | opening: None, |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | fn status(phase: Phase) -> String { |
| 326 | match phase { |
| 327 | Phase::Loading { status, .. } => status, |
| 328 | Phase::Ready => "ready".to_owned(), |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | #[test] |
| 333 | fn a_warm_cache_shows_the_app_after_the_minimum() { |
| 334 | let online = ConnectionState::Connecting; |
| 335 | let warm = StartupFacts { |
| 336 | has_chats: true, |
| 337 | ..facts(100, &online) |
| 338 | }; |
| 339 | assert_translated(&status(phase(&warm))); |
| 340 | let warm = StartupFacts { |
| 341 | has_chats: true, |
| 342 | ..facts(450, &online) |
| 343 | }; |
| 344 | assert_eq!(phase(&warm), Phase::Ready); |
| 345 | } |
| 346 | |
| 347 | #[test] |
| 348 | fn the_open_chat_holds_the_splash_until_its_messages_show() { |
| 349 | let online = ConnectionState::Online; |
| 350 | let opening = StartupFacts { |
| 351 | has_chats: true, |
| 352 | opening: Some("Release planning".to_owned()), |
| 353 | ..facts(900, &online) |
| 354 | }; |
| 355 | assert!(status(phase(&opening)).contains("Release planning")); |
| 356 | } |
| 357 | |
| 358 | fn assert_translated(status: &str) { |
| 359 | assert!(!status.is_empty()); |
| 360 | assert!( |
| 361 | !status.starts_with("auth.splash."), |
| 362 | "raw i18n key leaked: {status}" |
| 363 | ); |
| 364 | } |
| 365 | |
| 366 | #[test] |
| 367 | fn a_first_start_walks_through_sign_in_and_chats_with_a_late_hint() { |
| 368 | let connecting = ConnectionState::Connecting; |
| 369 | assert_translated(&status(phase(&facts(500, &connecting)))); |
| 370 | let online = ConnectionState::Online; |
| 371 | assert_translated(&status(phase(&facts(2000, &online)))); |
| 372 | let Phase::Loading { hint, .. } = phase(&facts(9000, &online)) else { |
| 373 | panic!("still loading") |
| 374 | }; |
| 375 | assert!(hint.is_some()); |
| 376 | let synced = StartupFacts { |
| 377 | synced: true, |
| 378 | ..facts(9000, &online) |
| 379 | }; |
| 380 | assert_eq!(phase(&synced), Phase::Ready); |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn sign_in_stops_the_bar_and_never_gives_up_without_content() { |
| 385 | let login = ConnectionState::LoginRequired; |
| 386 | let Phase::Loading { running, .. } = phase(&facts(60_000, &login)) else { |
| 387 | panic!("still loading") |
| 388 | }; |
| 389 | assert!(!running); |
| 390 | let offline = ConnectionState::Failed("down".to_owned()); |
| 391 | assert_eq!(phase(&facts(31_000, &offline)), Phase::Ready); |
| 392 | } |
| 393 | |
| 394 | #[test] |
| 395 | fn browser_awaiting_sign_in_stops_the_bar() { |
| 396 | let connecting = ConnectionState::Connecting; |
| 397 | let awaiting = BrowserSignInState::AwaitingSignIn; |
| 398 | let Phase::Loading { |
| 399 | running, status, .. |
| 400 | } = phase(&facts_with(2_000, &connecting, &awaiting)) |
| 401 | else { |
| 402 | panic!("still loading") |
| 403 | }; |
| 404 | assert!(!running); |
| 405 | assert_translated(&status); |
| 406 | } |
| 407 | |
| 408 | #[test] |
| 409 | fn browser_opening_keeps_the_bar_running() { |
| 410 | let connecting = ConnectionState::Connecting; |
| 411 | let opening = BrowserSignInState::Opening; |
| 412 | let Phase::Loading { |
| 413 | running, status, .. |
| 414 | } = phase(&facts_with(2_000, &connecting, &opening)) |
| 415 | else { |
| 416 | panic!("still loading") |
| 417 | }; |
| 418 | assert!(running); |
| 419 | assert_translated(&status); |
| 420 | } |
| 421 | |
| 422 | #[test] |
| 423 | fn warm_cache_still_shows_managed_browser_phases() { |
| 424 | let online = ConnectionState::Online; |
| 425 | let awaiting = BrowserSignInState::AwaitingSignIn; |
| 426 | let warm = StartupFacts { |
| 427 | has_chats: true, |
| 428 | ..facts_with(450, &online, &awaiting) |
| 429 | }; |
| 430 | let Phase::Loading { |
| 431 | running, status, .. |
| 432 | } = phase(&warm) |
| 433 | else { |
| 434 | panic!("managed browser must keep splash over warm cache") |
| 435 | }; |
| 436 | assert!(!running); |
| 437 | assert_translated(&status); |
| 438 | } |
| 439 | } |