BlueGit

ict rusty-teams

Files

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

splash.rs

splash.rs 9.7 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 { status: String, running: bool, hint: Option<&'static str> },
45 Ready,
46}
47
48pub fn phase(facts: &StartupFacts) -> Phase {
49 let content_ready = facts.has_chats || facts.synced || facts.demo;
50 if facts.elapsed >= MIN_VISIBLE && content_ready && facts.opening.is_none() {
51 return Phase::Ready;
52 }
53 let signing_in = matches!(facts.connection, ConnectionState::LoginRequired);
54 if facts.elapsed >= GIVE_UP_AFTER && !(signing_in && !content_ready) {
55 return Phase::Ready;
56 }
57 let loading = |status: String| Phase::Loading {
58 status,
59 running: true,
60 hint: (facts.elapsed >= HINT_AFTER && !content_ready).then_some("First start: this takes a little longer once"),
61 };
62 if signing_in {
63 return Phase::Loading {
64 status: "Sign in to Teams in the window that opened".to_owned(),
65 running: false,
66 hint: None,
67 };
68 }
69 if facts.elapsed < STARTING_FOR {
70 return loading("Starting Rusty Teams".to_owned());
71 }
72 if content_ready {
73 return loading(facts.opening.as_ref().map_or_else(|| "Syncing new messages".to_owned(), |title| format!("Opening {title}")));
74 }
75 match facts.connection {
76 ConnectionState::Connecting => loading("Signing in".to_owned()),
77 ConnectionState::Online => loading("Loading your chats".to_owned()),
78 _ => loading("Cannot reach Teams, trying again".to_owned()),
79 }
80}
81
82pub struct Splash {
83 started: Instant,
84 hidden_at: Option<Instant>,
85 pub recheck_scheduled: bool,
86}
87
88impl Default for Splash {
89 fn default() -> Self {
90 Splash { started: Instant::now(), hidden_at: None, recheck_scheduled: false }
91 }
92}
93
94impl Splash {
95 pub fn elapsed(&self) -> Duration {
96 self.started.elapsed()
97 }
98
99 pub fn is_up(&self) -> bool {
100 self.hidden_at.is_none()
101 }
102
103 pub fn hide(&mut self) {
104 self.hidden_at.get_or_insert_with(Instant::now);
105 }
106
107 pub fn content_rising(&self) -> Option<Duration> {
108 self.hidden_at.map(|hidden_at| hidden_at.elapsed()).filter(|since| *since < CONTENT_RISE)
109 }
110
111 pub fn render(&self, phase: &Phase, cx: &App) -> Option<AnyElement> {
112 let fading = self.hidden_at.map(|hidden_at| hidden_at.elapsed());
113 if fading.is_some_and(|since| since >= FADE_OUT) {
114 return None;
115 }
116 let still = cx.reduce_motion();
117 let app_icon = crate::active_appearance::current(cx).app_icon;
118 let (status, running, hint) = match phase {
119 Phase::Loading { status, running, hint } => (status.clone(), *running, *hint),
120 Phase::Ready => (String::new(), true, None),
121 };
122 let overlay = v_flex()
123 .id("startup-splash")
124 .debug_selector(|| "shell/splash".into())
125 .occlude()
126 .absolute()
127 .top(px(TITLE_BAR_HEIGHT))
128 .left_0()
129 .right_0()
130 .bottom_0()
131 .items_center()
132 .justify_center()
133 .bg(theme::background())
134 .child(logo(app_icon, still))
135 .child(bar(running, still))
136 .child(status_line(status, still))
137 .child(
138 div()
139 .mt(px(6.))
140 .h(px(16.))
141 .text_size(px(HINT_SIZE))
142 .text_color(theme::text_faint())
143 .children(hint),
144 );
145 Some(match fading {
146 Some(_) if !still => overlay
147 .with_animation("startup-splash-fade", Animation::new(FADE_OUT).with_easing(ease_out_quint()), |overlay, delta| {
148 overlay.opacity(1. - delta)
149 })
150 .into_any_element(),
151 _ => overlay.into_any_element(),
152 })
153 }
154}
155
156fn logo(app_icon: crate::appearance::AppIcon, still: bool) -> AnyElement {
157 let image = img(app_icon_image(app_icon)).size(px(LOGO_SIZE));
158 let frame = div().size(px(LOGO_SIZE * (1. + LOGO_BREATH))).flex().items_center().justify_center();
159 if still {
160 return frame.child(image).into_any_element();
161 }
162 frame
163 .child(image.with_animation("startup-splash-logo", Animation::new(BREATH_PERIOD).repeat(), |image, delta| {
164 let wave = (1. - (delta * std::f32::consts::TAU).cos()) / 2.;
165 image.size(px(LOGO_SIZE * (1. + LOGO_BREATH * wave)))
166 }))
167 .into_any_element()
168}
169
170fn bar(running: bool, still: bool) -> Div {
171 let sweep_width = BAR_WIDTH * SWEEP_SHARE;
172 let sweep = || div().absolute().top_0().bottom_0().w(px(sweep_width)).rounded(px(BAR_HEIGHT)).bg(theme::accent());
173 div()
174 .relative()
175 .mt(px(BAR_TOP_GAP))
176 .w(px(BAR_WIDTH))
177 .h(px(BAR_HEIGHT))
178 .rounded(px(BAR_HEIGHT))
179 .overflow_hidden()
180 .bg(theme::border_strong())
181 .when(!running, |bar| bar.invisible())
182 .when(running && still, |bar| bar.child(sweep().left(px((BAR_WIDTH - sweep_width) / 2.))))
183 .when(running && !still, |bar| {
184 bar.child(sweep().with_animation(
185 "startup-splash-sweep",
186 Animation::new(SWEEP_PERIOD).repeat().with_easing(ease_in_out),
187 move |sweep, delta| {
188 let head = (BAR_WIDTH + sweep_width) * delta;
189 let start = (head - sweep_width).max(0.);
190 sweep.left(px(start)).w(px(head.min(BAR_WIDTH) - start))
191 },
192 ))
193 })
194}
195
196fn status_line(status: String, still: bool) -> AnyElement {
197 let line = div().text_size(px(STATUS_SIZE)).text_color(theme::text_muted()).child(status.clone());
198 let slot = div().mt(px(STATUS_TOP_GAP)).h(px(STATUS_SIZE * 1.4)).relative();
199 if still {
200 return slot.child(line).into_any_element();
201 }
202 slot.child(line.relative().with_animation(
203 ElementId::Name(format!("startup-splash-status-{status}").into()),
204 Animation::new(STATUS_SWAP).with_easing(ease_out_quint()),
205 |line, delta| line.top(px(STATUS_SWAP_OFFSET * (1. - delta))).opacity(delta),
206 ))
207 .into_any_element()
208}
209
210#[cfg(test)]
211mod tests {
212 use std::time::Duration;
213
214 use super::{ConnectionState, Phase, StartupFacts, phase};
215
216 fn facts(elapsed_ms: u64, connection: &ConnectionState) -> StartupFacts<'_> {
217 StartupFacts {
218 elapsed: Duration::from_millis(elapsed_ms),
219 connection,
220 has_chats: false,
221 synced: false,
222 demo: false,
223 opening: None,
224 }
225 }
226
227 fn status(phase: Phase) -> String {
228 match phase {
229 Phase::Loading { status, .. } => status,
230 Phase::Ready => "ready".to_owned(),
231 }
232 }
233
234 #[test]
235 fn a_warm_cache_shows_the_app_after_the_minimum() {
236 let online = ConnectionState::Connecting;
237 let warm = StartupFacts { has_chats: true, ..facts(100, &online) };
238 assert_eq!(status(phase(&warm)), "Starting Rusty Teams");
239 let warm = StartupFacts { has_chats: true, ..facts(450, &online) };
240 assert_eq!(phase(&warm), Phase::Ready);
241 }
242
243 #[test]
244 fn the_open_chat_holds_the_splash_until_its_messages_show() {
245 let online = ConnectionState::Online;
246 let opening = StartupFacts { has_chats: true, opening: Some("Release planning".to_owned()), ..facts(900, &online) };
247 assert_eq!(status(phase(&opening)), "Opening Release planning");
248 }
249
250 #[test]
251 fn a_first_start_walks_through_sign_in_and_chats_with_a_late_hint() {
252 let connecting = ConnectionState::Connecting;
253 assert_eq!(status(phase(&facts(500, &connecting))), "Signing in");
254 let online = ConnectionState::Online;
255 assert_eq!(status(phase(&facts(2000, &online))), "Loading your chats");
256 let Phase::Loading { hint, .. } = phase(&facts(9000, &online)) else { panic!("still loading") };
257 assert!(hint.is_some());
258 let synced = StartupFacts { synced: true, ..facts(9000, &online) };
259 assert_eq!(phase(&synced), Phase::Ready);
260 }
261
262 #[test]
263 fn sign_in_stops_the_bar_and_never_gives_up_without_content() {
264 let login = ConnectionState::LoginRequired;
265 let Phase::Loading { running, .. } = phase(&facts(60_000, &login)) else { panic!("still loading") };
266 assert!(!running);
267 let offline = ConnectionState::Failed("down".to_owned());
268 assert_eq!(phase(&facts(31_000, &offline)), Phase::Ready);
269 }
270}