BlueGit

ict rusty-teams

Files

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

status_bar.rs

status_bar.rs 6.8 KiB Download Raw
1use chrono::{DateTime, Local, Utc};
2use gpui_kit::component::{h_flex, tooltip::Tooltip};
3use gpui_kit::*;
4
5use crate::app_state::{AppState, Mode};
6use crate::backend::{ConnectionState, LiveState};
7use crate::format;
8use crate::theme;
9use crate::updater::{self, UpdateStatus};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum Tone {
13 Good,
14 Warn,
15 Bad,
16}
17
18pub fn connection_text(connection: &ConnectionState, mode: Mode) -> (String, Tone) {
19 if mode.demo {
20 return ("Demo data".to_owned(), Tone::Warn);
21 }
22 match connection {
23 ConnectionState::Connecting => ("Connecting".to_owned(), Tone::Warn),
24 ConnectionState::Online => ("Connected".to_owned(), Tone::Good),
25 ConnectionState::NoBrowser => ("Browser not running".to_owned(), Tone::Bad),
26 ConnectionState::NoAppTab => ("Teams not loaded".to_owned(), Tone::Bad),
27 ConnectionState::LoginRequired => ("Sign-in required".to_owned(), Tone::Bad),
28 ConnectionState::Failed(message) => (format!("Error: {message}"), Tone::Bad),
29 }
30}
31
32pub fn live_text(live: LiveState) -> Option<(&'static str, Tone)> {
33 match live {
34 LiveState::Off => None,
35 LiveState::Connecting => Some(("Live: connecting", Tone::Warn)),
36 LiveState::Live => Some(("Live", Tone::Good)),
37 LiveState::Reconnecting => Some(("Live: reconnecting", Tone::Warn)),
38 LiveState::MessageLoss => Some(("Live: missed messages, syncing", Tone::Warn)),
39 LiveState::Failed => Some(("Live: unavailable", Tone::Bad)),
40 }
41}
42
43pub fn sync_text(last_sync: Option<DateTime<Utc>>) -> String {
44 match last_sync {
45 Some(time) => format!("Synced {}", time.with_timezone(&Local).format("%H:%M")),
46 None => "Not synced yet".to_owned(),
47 }
48}
49
50fn tone_color(tone: Tone) -> Hsla {
51 match tone {
52 Tone::Good => theme::green(),
53 Tone::Warn => theme::amber(),
54 Tone::Bad => theme::red(),
55 }
56}
57
58pub fn render_status_bar(
59 state: &AppState,
60 update: &UpdateStatus,
61 on_restart: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
62 on_notice_action: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
63) -> impl IntoElement {
64 let (connection, connection_tone) = connection_text(&state.connection, state.mode);
65 let item = |label: String, tone: Tone| {
66 h_flex()
67 .gap(px(6.))
68 .items_center()
69 .child(div().size(px(7.)).rounded_full().bg(tone_color(tone)))
70 .child(label)
71 };
72 let live = live_text(state.live);
73 let mut bar = h_flex()
74 .debug_selector(|| "shell/status-bar".into())
75 .w_full()
76 .h(px(24.))
77 .flex_none()
78 .px(px(12.))
79 .gap(px(14.))
80 .items_center()
81 .text_size(px(11.))
82 .bg(theme::background())
83 .text_color(theme::text_muted());
84 match live {
85 Some((label, tone)) if connection_tone == Tone::Good => {
86 bar = bar.child(item(label.to_owned(), tone))
87 }
88 _ => bar = bar.child(item(connection, connection_tone)),
89 }
90 bar = bar.child(div().child(sync_text(state.last_sync)));
91 if state.mode.read_only {
92 bar = bar.child(div().text_color(theme::amber()).child("Read-only"));
93 }
94 match update {
95 UpdateStatus::UpToDate => {}
96 UpdateStatus::Ready => {
97 bar = bar.child(
98 div()
99 .id("update-restart")
100 .px(px(8.))
101 .rounded(px(4.))
102 .bg(theme::accent())
103 .text_color(theme::on_accent())
104 .cursor_pointer()
105 .hover(|button| button.bg(theme::accent_text()))
106 .child("Update ready - Restart")
107 .on_click(on_restart),
108 )
109 }
110 UpdateStatus::Installing => {
111 bar = bar.child(
112 div()
113 .text_color(theme::accent_text())
114 .child("Restarting..."),
115 )
116 }
117 UpdateStatus::Failed(message) => {
118 bar = bar.child(
119 div()
120 .text_color(theme::red())
121 .child(format!("Update failed: {message}")),
122 )
123 }
124 }
125 if let Some(notice) = &state.notice {
126 bar = bar.child(
127 h_flex()
128 .min_w_0()
129 .gap(px(8.))
130 .items_center()
131 .text_color(theme::amber())
132 .child(div().min_w_0().truncate().child(notice.text.clone()))
133 .children(notice.action.as_ref().map(|action| {
134 div()
135 .id("notice-action")
136 .flex_none()
137 .px(px(6.))
138 .rounded(px(4.))
139 .cursor_pointer()
140 .font_weight(FontWeight::SEMIBOLD)
141 .text_color(theme::accent_text())
142 .hover(|button| button.bg(theme::row_hover()))
143 .child(action.label.clone())
144 .on_click(on_notice_action)
145 })),
146 );
147 }
148 let full_version = updater::running_version();
149 bar.child(
150 div()
151 .id("build-version")
152 .ml_auto()
153 .tooltip(move |window, cx| Tooltip::new(full_version).build(window, cx))
154 .child(format::short_version(
155 full_version,
156 env!("CARGO_PKG_VERSION"),
157 )),
158 )
159}
160
161#[cfg(test)]
162mod tests {
163 use super::{Tone, connection_text, live_text, sync_text};
164 use crate::app_state::Mode;
165 use crate::backend::{ConnectionState, LiveState};
166 use chrono::Utc;
167
168 #[test]
169 fn states_map_to_text_and_tone() {
170 let mode = Mode::default();
171 assert_eq!(
172 connection_text(&ConnectionState::Online, mode).1,
173 Tone::Good
174 );
175 assert_eq!(
176 connection_text(&ConnectionState::LoginRequired, mode).0,
177 "Sign-in required"
178 );
179 assert_eq!(
180 connection_text(&ConnectionState::Connecting, mode).1,
181 Tone::Warn
182 );
183 assert!(
184 connection_text(&ConnectionState::Failed("x".into()), mode)
185 .0
186 .contains('x')
187 );
188 }
189
190 #[test]
191 fn demo_mode_overrides_the_connection() {
192 let mode = Mode {
193 demo: true,
194 read_only: false,
195 demo_sync: None,
196 };
197 assert_eq!(
198 connection_text(&ConnectionState::NoBrowser, mode).0,
199 "Demo data"
200 );
201 }
202
203 #[test]
204 fn live_off_has_no_label() {
205 assert_eq!(live_text(LiveState::Off), None);
206 assert_eq!(live_text(LiveState::Live).unwrap().1, Tone::Good);
207 }
208
209 #[test]
210 fn sync_text_without_sync() {
211 assert_eq!(sync_text(None), "Not synced yet");
212 assert!(sync_text(Some(Utc::now())).starts_with("Synced "));
213 }
214}