BlueGit

ict rusty-teams

Files

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

splash.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
splash.rs 10.5 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::theme;
10
11const MIN_VISIBLE: Duration = Duration::from_millis(400);
12const STARTING_FOR: Duration = Duration::from_millis(300);
13const HINT_AFTER: Duration = Duration::from_secs(8);
14const GIVE_UP_AFTER: Duration = Duration::from_secs(30);
15const FADE_OUT: Duration = Duration::from_millis(220);
16pub const CONTENT_RISE: Duration = Duration::from_millis(260);
17pub const CONTENT_RISE_OFFSET: f32 = 8.;
18const LOGO_SIZE: f32 = 76.;
19const LOGO_BREATH: f32 = 0.04;
20const BREATH_PERIOD: Duration = Duration::from_millis(2400);
21const BAR_WIDTH: f32 = 168.;
22const BAR_HEIGHT: f32 = 3.;
23const BAR_TOP_GAP: f32 = 32.;
24const SWEEP_SHARE: f32 = 0.4;
25const SWEEP_PERIOD: Duration = Duration::from_millis(1300);
26const STATUS_TOP_GAP: f32 = 14.;
27const STATUS_SIZE: f32 = 13.;
28const STATUS_SWAP: Duration = Duration::from_millis(180);
29const STATUS_SWAP_OFFSET: f32 = 4.;
30const HINT_SIZE: f32 = 12.;
31pub const STILL_RECHECK: Duration = Duration::from_millis(250);
32
33pub struct StartupFacts<'a> {
34 pub elapsed: Duration,
35 pub connection: &'a ConnectionState,
36 pub has_chats: bool,
37 pub synced: bool,
38 pub demo: bool,
39 pub opening: Option<String>,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
43pub enum Phase {
44 Loading {
45 status: String,
46 running: bool,
47 hint: Option<&'static str>,
48 },
49 Ready,
50}
51
52pub fn phase(facts: &StartupFacts) -> Phase {
53 let content_ready = facts.has_chats || facts.synced || facts.demo;
54 if facts.elapsed >= MIN_VISIBLE && content_ready && facts.opening.is_none() {
55 return Phase::Ready;
56 }
57 let signing_in = matches!(facts.connection, ConnectionState::LoginRequired);
58 if facts.elapsed >= GIVE_UP_AFTER && !(signing_in && !content_ready) {
59 return Phase::Ready;
60 }
61 let loading = |status: String| Phase::Loading {
62 status,
63 running: true,
64 hint: (facts.elapsed >= HINT_AFTER && !content_ready)
65 .then_some("First start: this takes a little longer once"),
66 };
67 if signing_in {
68 return Phase::Loading {
69 status: "Sign in to Teams in the window that opened".to_owned(),
70 running: false,
71 hint: None,
72 };
73 }
74 if facts.elapsed < STARTING_FOR {
75 return loading("Starting Rusty Teams".to_owned());
76 }
77 if content_ready {
78 return loading(facts.opening.as_ref().map_or_else(
79 || "Syncing new messages".to_owned(),
80 |title| format!("Opening {title}"),
81 ));
82 }
83 match facts.connection {
84 ConnectionState::Connecting => loading("Signing in".to_owned()),
85 ConnectionState::Online => loading("Loading your chats".to_owned()),
86 _ => loading("Cannot reach Teams, trying again".to_owned()),
87 }
88}
89
90pub struct Splash {
91 started: Instant,
92 hidden_at: Option<Instant>,
93 pub recheck_scheduled: bool,
94}
95
96impl Default for Splash {
97 fn default() -> Self {
98 Splash {
99 started: Instant::now(),
100 hidden_at: None,
101 recheck_scheduled: false,
102 }
103 }
104}
105
106impl Splash {
107 pub fn elapsed(&self) -> Duration {
108 self.started.elapsed()
109 }
110
111 pub fn is_up(&self) -> bool {
112 self.hidden_at.is_none()
113 }
114
115 pub fn hide(&mut self) {
116 self.hidden_at.get_or_insert_with(Instant::now);
117 }
118
119 pub fn content_rising(&self) -> Option<Duration> {
120 self.hidden_at
121 .map(|hidden_at| hidden_at.elapsed())
122 .filter(|since| *since < CONTENT_RISE)
123 }
124
125 pub fn render(&self, phase: &Phase, cx: &App) -> Option<AnyElement> {
126 let fading = self.hidden_at.map(|hidden_at| hidden_at.elapsed());
127 if fading.is_some_and(|since| since >= FADE_OUT) {
128 return None;
129 }
130 let still = cx.reduce_motion();
131 let app_icon = crate::active_appearance::current(cx).app_icon;
132 let (status, running, hint) = match phase {
133 Phase::Loading {
134 status,
135 running,
136 hint,
137 } => (status.clone(), *running, *hint),
138 Phase::Ready => (String::new(), true, None),
139 };
140 let overlay = v_flex()
141 .id("startup-splash")
142 .debug_selector(|| "shell/splash".into())
143 .occlude()
144 .absolute()
145 .top(px(TITLE_BAR_HEIGHT))
146 .left_0()
147 .right_0()
148 .bottom_0()
149 .items_center()
150 .justify_center()
151 .bg(theme::background())
152 .child(logo(app_icon, still))
153 .child(bar(running, still))
154 .child(status_line(status, still))
155 .child(
156 div()
157 .mt(px(6.))
158 .h(px(16.))
159 .text_size(px(HINT_SIZE))
160 .text_color(theme::text_faint())
161 .children(hint),
162 );
163 Some(match fading {
164 Some(_) if !still => overlay
165 .with_animation(
166 "startup-splash-fade",
167 Animation::new(FADE_OUT).with_easing(ease_out_quint()),
168 |overlay, delta| overlay.opacity(1. - delta),
169 )
170 .into_any_element(),
171 _ => overlay.into_any_element(),
172 })
173 }
174}
175
176fn logo(app_icon: crate::appearance::AppIcon, still: bool) -> AnyElement {
177 let image = img(app_icon_image(app_icon)).size(px(LOGO_SIZE));
178 let frame = div()
179 .size(px(LOGO_SIZE * (1. + LOGO_BREATH)))
180 .flex()
181 .items_center()
182 .justify_center();
183 if still {
184 return frame.child(image).into_any_element();
185 }
186 frame
187 .child(image.with_animation(
188 "startup-splash-logo",
189 Animation::new(BREATH_PERIOD).repeat(),
190 |image, delta| {
191 let wave = (1. - (delta * std::f32::consts::TAU).cos()) / 2.;
192 image.size(px(LOGO_SIZE * (1. + LOGO_BREATH * wave)))
193 },
194 ))
195 .into_any_element()
196}
197
198fn bar(running: bool, still: bool) -> Div {
199 let sweep_width = BAR_WIDTH * SWEEP_SHARE;
200 let sweep = || {
201 div()
202 .absolute()
203 .top_0()
204 .bottom_0()
205 .w(px(sweep_width))
206 .rounded(px(BAR_HEIGHT))
207 .bg(theme::accent())
208 };
209 div()
210 .relative()
211 .mt(px(BAR_TOP_GAP))
212 .w(px(BAR_WIDTH))
213 .h(px(BAR_HEIGHT))
214 .rounded(px(BAR_HEIGHT))
215 .overflow_hidden()
216 .bg(theme::border_strong())
217 .when(!running, |bar| bar.invisible())
218 .when(running && still, |bar| {
219 bar.child(sweep().left(px((BAR_WIDTH - sweep_width) / 2.)))
220 })
221 .when(running && !still, |bar| {
222 bar.child(
223 sweep().with_animation(
224 "startup-splash-sweep",
225 Animation::new(SWEEP_PERIOD)
226 .repeat()
227 .with_easing(ease_in_out),
228 move |sweep, delta| {
229 let head = (BAR_WIDTH + sweep_width) * delta;
230 let start = (head - sweep_width).max(0.);
231 sweep.left(px(start)).w(px(head.min(BAR_WIDTH) - start))
232 },
233 ),
234 )
235 })
236}
237
238fn status_line(status: String, still: bool) -> AnyElement {
239 let line = div()
240 .text_size(px(STATUS_SIZE))
241 .text_color(theme::text_muted())
242 .child(status.clone());
243 let slot = div()
244 .mt(px(STATUS_TOP_GAP))
245 .h(px(STATUS_SIZE * 1.4))
246 .relative();
247 if still {
248 return slot.child(line).into_any_element();
249 }
250 slot.child(line.relative().with_animation(
251 ElementId::Name(format!("startup-splash-status-{status}").into()),
252 Animation::new(STATUS_SWAP).with_easing(ease_out_quint()),
253 |line, delta| {
254 line.top(px(STATUS_SWAP_OFFSET * (1. - delta)))
255 .opacity(delta)
256 },
257 ))
258 .into_any_element()
259}
260
261#[cfg(test)]
262mod tests {
263 use std::time::Duration;
264
265 use super::{ConnectionState, Phase, StartupFacts, phase};
266
267 fn facts(elapsed_ms: u64, connection: &ConnectionState) -> StartupFacts<'_> {
268 StartupFacts {
269 elapsed: Duration::from_millis(elapsed_ms),
270 connection,
271 has_chats: false,
272 synced: false,
273 demo: false,
274 opening: None,
275 }
276 }
277
278 fn status(phase: Phase) -> String {
279 match phase {
280 Phase::Loading { status, .. } => status,
281 Phase::Ready => "ready".to_owned(),
282 }
283 }
284
285 #[test]
286 fn a_warm_cache_shows_the_app_after_the_minimum() {
287 let online = ConnectionState::Connecting;
288 let warm = StartupFacts {
289 has_chats: true,
290 ..facts(100, &online)
291 };
292 assert_eq!(status(phase(&warm)), "Starting Rusty Teams");
293 let warm = StartupFacts {
294 has_chats: true,
295 ..facts(450, &online)
296 };
297 assert_eq!(phase(&warm), Phase::Ready);
298 }
299
300 #[test]
301 fn the_open_chat_holds_the_splash_until_its_messages_show() {
302 let online = ConnectionState::Online;
303 let opening = StartupFacts {
304 has_chats: true,
305 opening: Some("Release planning".to_owned()),
306 ..facts(900, &online)
307 };
308 assert_eq!(status(phase(&opening)), "Opening Release planning");
309 }
310
311 #[test]
312 fn a_first_start_walks_through_sign_in_and_chats_with_a_late_hint() {
313 let connecting = ConnectionState::Connecting;
314 assert_eq!(status(phase(&facts(500, &connecting))), "Signing in");
315 let online = ConnectionState::Online;
316 assert_eq!(status(phase(&facts(2000, &online))), "Loading your chats");
317 let Phase::Loading { hint, .. } = phase(&facts(9000, &online)) else {
318 panic!("still loading")
319 };
320 assert!(hint.is_some());
321 let synced = StartupFacts {
322 synced: true,
323 ..facts(9000, &online)
324 };
325 assert_eq!(phase(&synced), Phase::Ready);
326 }
327
328 #[test]
329 fn sign_in_stops_the_bar_and_never_gives_up_without_content() {
330 let login = ConnectionState::LoginRequired;
331 let Phase::Loading { running, .. } = phase(&facts(60_000, &login)) else {
332 panic!("still loading")
333 };
334 assert!(!running);
335 let offline = ConnectionState::Failed("down".to_owned());
336 assert_eq!(phase(&facts(31_000, &offline)), Phase::Ready);
337 }
338}