BlueGit

ict rusty-teams

Files

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

status_bar.rs

status_bar.rs 5.7 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) -> impl IntoElement {
63 let (connection, connection_tone) = connection_text(&state.connection, state.mode);
64 let item = |label: String, tone: Tone| {
65 h_flex()
66 .gap(px(6.))
67 .items_center()
68 .child(div().size(px(7.)).rounded_full().bg(tone_color(tone)))
69 .child(label)
70 };
71 let live = live_text(state.live);
72 let mut bar = h_flex()
73 .w_full()
74 .h(px(24.))
75 .flex_none()
76 .px(px(12.))
77 .gap(px(14.))
78 .items_center()
79 .text_size(px(11.))
80 .bg(theme::background())
81 .border_t_1()
82 .border_color(theme::border())
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::Failed(message) => {
111 bar = bar.child(
112 div()
113 .text_color(theme::red())
114 .child(format!("Update failed: {message}")),
115 )
116 }
117 }
118 let full_version = updater::running_version();
119 bar.child(
120 div()
121 .id("build-version")
122 .ml_auto()
123 .tooltip(move |window, cx| Tooltip::new(full_version).build(window, cx))
124 .child(format::short_version(
125 full_version,
126 env!("CARGO_PKG_VERSION"),
127 )),
128 )
129}
130
131#[cfg(test)]
132mod tests {
133 use super::{Tone, connection_text, live_text, sync_text};
134 use crate::app_state::Mode;
135 use crate::backend::{ConnectionState, LiveState};
136 use chrono::Utc;
137
138 #[test]
139 fn states_map_to_text_and_tone() {
140 let mode = Mode::default();
141 assert_eq!(
142 connection_text(&ConnectionState::Online, mode).1,
143 Tone::Good
144 );
145 assert_eq!(
146 connection_text(&ConnectionState::LoginRequired, mode).0,
147 "Sign-in required"
148 );
149 assert_eq!(
150 connection_text(&ConnectionState::Connecting, mode).1,
151 Tone::Warn
152 );
153 assert!(
154 connection_text(&ConnectionState::Failed("x".into()), mode)
155 .0
156 .contains('x')
157 );
158 }
159
160 #[test]
161 fn demo_mode_overrides_the_connection() {
162 let mode = Mode {
163 demo: true,
164 read_only: false,
165 demo_sync: None,
166 };
167 assert_eq!(
168 connection_text(&ConnectionState::NoBrowser, mode).0,
169 "Demo data"
170 );
171 }
172
173 #[test]
174 fn live_off_has_no_label() {
175 assert_eq!(live_text(LiveState::Off), None);
176 assert_eq!(live_text(LiveState::Live).unwrap().1, Tone::Good);
177 }
178
179 #[test]
180 fn sync_text_without_sync() {
181 assert_eq!(sync_text(None), "Not synced yet");
182 assert!(sync_text(Some(Utc::now())).starts_with("Synced "));
183 }
184}