BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / splash.rs

splash.rs

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