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 .w_full()
75 .h(px(24.))
76 .flex_none()
77 .px(px(12.))
78 .gap(px(14.))
79 .items_center()
80 .text_size(px(11.))
81 .bg(theme::background())
82 .border_t_1()
83 .border_color(theme::border())
84 .text_color(theme::text_muted());
85 match live {
86 Some((label, tone)) if connection_tone == Tone::Good => {
87 bar = bar.child(item(label.to_owned(), tone))
88 }
89 _ => bar = bar.child(item(connection, connection_tone)),
90 }
91 bar = bar.child(div().child(sync_text(state.last_sync)));
92 if state.mode.read_only {
93 bar = bar.child(div().text_color(theme::amber()).child("Read-only"));
94 }
95 match update {
96 UpdateStatus::UpToDate => {}
97 UpdateStatus::Ready => {
98 bar = bar.child(
99 div()
100 .id("update-restart")
101 .px(px(8.))
102 .rounded(px(4.))
103 .bg(theme::accent())
104 .text_color(theme::on_accent())
105 .cursor_pointer()
106 .hover(|button| button.bg(theme::accent_text()))
107 .child("Update ready - Restart")
108 .on_click(on_restart),
109 )
110 }
111 UpdateStatus::Installing => {
112 bar = bar.child(div().text_color(theme::accent_text()).child("Restarting..."))
113 }
114 UpdateStatus::Failed(message) => {
115 bar = bar.child(
116 div()
117 .text_color(theme::red())
118 .child(format!("Update failed: {message}")),
119 )
120 }
121 }
122 if let Some(notice) = &state.notice {
123 bar = bar.child(
124 h_flex()
125 .min_w_0()
126 .gap(px(8.))
127 .items_center()
128 .text_color(theme::amber())
129 .child(div().min_w_0().truncate().child(notice.text.clone()))
130 .children(notice.action.as_ref().map(|action| {
131 div()
132 .id("notice-action")
133 .flex_none()
134 .px(px(6.))
135 .rounded(px(4.))
136 .cursor_pointer()
137 .font_weight(FontWeight::SEMIBOLD)
138 .text_color(theme::accent_text())
139 .hover(|button| button.bg(theme::row_hover()))
140 .child(action.label.clone())
141 .on_click(on_notice_action)
142 })),
143 );
144 }
145 let full_version = updater::running_version();
146 bar.child(
147 div()
148 .id("build-version")
149 .ml_auto()
150 .tooltip(move |window, cx| Tooltip::new(full_version).build(window, cx))
151 .child(format::short_version(
152 full_version,
153 env!("CARGO_PKG_VERSION"),
154 )),
155 )
156}
157
158#[cfg(test)]
159mod tests {
160 use super::{Tone, connection_text, live_text, sync_text};
161 use crate::app_state::Mode;
162 use crate::backend::{ConnectionState, LiveState};
163 use chrono::Utc;
164
165 #[test]
166 fn states_map_to_text_and_tone() {
167 let mode = Mode::default();
168 assert_eq!(
169 connection_text(&ConnectionState::Online, mode).1,
170 Tone::Good
171 );
172 assert_eq!(
173 connection_text(&ConnectionState::LoginRequired, mode).0,
174 "Sign-in required"
175 );
176 assert_eq!(
177 connection_text(&ConnectionState::Connecting, mode).1,
178 Tone::Warn
179 );
180 assert!(
181 connection_text(&ConnectionState::Failed("x".into()), mode)
182 .0
183 .contains('x')
184 );
185 }
186
187 #[test]
188 fn demo_mode_overrides_the_connection() {
189 let mode = Mode {
190 demo: true,
191 read_only: false,
192 demo_sync: None,
193 };
194 assert_eq!(
195 connection_text(&ConnectionState::NoBrowser, mode).0,
196 "Demo data"
197 );
198 }
199
200 #[test]
201 fn live_off_has_no_label() {
202 assert_eq!(live_text(LiveState::Off), None);
203 assert_eq!(live_text(LiveState::Live).unwrap().1, Tone::Good);
204 }
205
206 #[test]
207 fn sync_text_without_sync() {
208 assert_eq!(sync_text(None), "Not synced yet");
209 assert!(sync_text(Some(Utc::now())).starts_with("Synced "));
210 }
211}