BlueGit

ict rusty-teams

Files

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

status_bar.rs

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