| 1 | use chrono::{DateTime, Local, Utc}; |
| 2 | use gpui_kit::component::{h_flex, tooltip::Tooltip}; |
| 3 | use gpui_kit::*; |
| 4 | |
| 5 | use crate::app_state::{AppState, Mode}; |
| 6 | use crate::backend::{ConnectionState, LiveState}; |
| 7 | use crate::format; |
| 8 | use crate::theme; |
| 9 | use crate::updater::{self, UpdateStatus}; |
| 10 | |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 12 | pub enum Tone { |
| 13 | Good, |
| 14 | Warn, |
| 15 | Bad, |
| 16 | } |
| 17 | |
| 18 | pub 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 | |
| 32 | pub 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 | |
| 43 | pub 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 | |
| 50 | fn 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 | |
| 58 | pub 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::Failed(message) => { |
| 112 | bar = bar.child( |
| 113 | div() |
| 114 | .text_color(theme::red()) |
| 115 | .child(format!("Update failed: {message}")), |
| 116 | ) |
| 117 | } |
| 118 | } |
| 119 | if let Some(notice) = &state.notice { |
| 120 | bar = bar.child( |
| 121 | h_flex() |
| 122 | .min_w_0() |
| 123 | .gap(px(8.)) |
| 124 | .items_center() |
| 125 | .text_color(theme::amber()) |
| 126 | .child(div().min_w_0().truncate().child(notice.text.clone())) |
| 127 | .children(notice.action.as_ref().map(|action| { |
| 128 | div() |
| 129 | .id("notice-action") |
| 130 | .flex_none() |
| 131 | .px(px(6.)) |
| 132 | .rounded(px(4.)) |
| 133 | .cursor_pointer() |
| 134 | .font_weight(FontWeight::SEMIBOLD) |
| 135 | .text_color(theme::accent_text()) |
| 136 | .hover(|button| button.bg(theme::row_hover())) |
| 137 | .child(action.label.clone()) |
| 138 | .on_click(on_notice_action) |
| 139 | })), |
| 140 | ); |
| 141 | } |
| 142 | let full_version = updater::running_version(); |
| 143 | bar.child( |
| 144 | div() |
| 145 | .id("build-version") |
| 146 | .ml_auto() |
| 147 | .tooltip(move |window, cx| Tooltip::new(full_version).build(window, cx)) |
| 148 | .child(format::short_version( |
| 149 | full_version, |
| 150 | env!("CARGO_PKG_VERSION"), |
| 151 | )), |
| 152 | ) |
| 153 | } |
| 154 | |
| 155 | #[cfg(test)] |
| 156 | mod tests { |
| 157 | use super::{Tone, connection_text, live_text, sync_text}; |
| 158 | use crate::app_state::Mode; |
| 159 | use crate::backend::{ConnectionState, LiveState}; |
| 160 | use chrono::Utc; |
| 161 | |
| 162 | #[test] |
| 163 | fn states_map_to_text_and_tone() { |
| 164 | let mode = Mode::default(); |
| 165 | assert_eq!( |
| 166 | connection_text(&ConnectionState::Online, mode).1, |
| 167 | Tone::Good |
| 168 | ); |
| 169 | assert_eq!( |
| 170 | connection_text(&ConnectionState::LoginRequired, mode).0, |
| 171 | "Sign-in required" |
| 172 | ); |
| 173 | assert_eq!( |
| 174 | connection_text(&ConnectionState::Connecting, mode).1, |
| 175 | Tone::Warn |
| 176 | ); |
| 177 | assert!( |
| 178 | connection_text(&ConnectionState::Failed("x".into()), mode) |
| 179 | .0 |
| 180 | .contains('x') |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn demo_mode_overrides_the_connection() { |
| 186 | let mode = Mode { |
| 187 | demo: true, |
| 188 | read_only: false, |
| 189 | demo_sync: None, |
| 190 | }; |
| 191 | assert_eq!( |
| 192 | connection_text(&ConnectionState::NoBrowser, mode).0, |
| 193 | "Demo data" |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn live_off_has_no_label() { |
| 199 | assert_eq!(live_text(LiveState::Off), None); |
| 200 | assert_eq!(live_text(LiveState::Live).unwrap().1, Tone::Good); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn sync_text_without_sync() { |
| 205 | assert_eq!(sync_text(None), "Not synced yet"); |
| 206 | assert!(sync_text(Some(Utc::now())).starts_with("Synced ")); |
| 207 | } |
| 208 | } |