| 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::format; |
| 7 | use crate::backend::{ConnectionState, LiveState}; |
| 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 ("Demodaten".to_owned(), Tone::Warn); |
| 21 | } |
| 22 | match connection { |
| 23 | ConnectionState::Connecting => ("Verbinde".to_owned(), Tone::Warn), |
| 24 | ConnectionState::Online => ("Verbunden".to_owned(), Tone::Good), |
| 25 | ConnectionState::NoBrowser => ("Browser nicht gestartet".to_owned(), Tone::Bad), |
| 26 | ConnectionState::NoAppTab => ("Teams nicht geladen".to_owned(), Tone::Bad), |
| 27 | ConnectionState::LoginRequired => ("Anmeldung nötig".to_owned(), Tone::Bad), |
| 28 | ConnectionState::Failed(message) => (format!("Fehler: {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: verbinde", Tone::Warn)), |
| 36 | LiveState::Live => Some(("Live", Tone::Good)), |
| 37 | LiveState::Reconnecting => Some(("Live: verbinde neu", Tone::Warn)), |
| 38 | LiveState::MessageLoss => Some(("Live: Nachrichten verpasst, synchronisiere", Tone::Warn)), |
| 39 | LiveState::Failed => Some(("Live: nicht verfügbar", Tone::Bad)), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn sync_text(last_sync: Option<DateTime<Utc>>) -> String { |
| 44 | match last_sync { |
| 45 | Some(time) => format!( |
| 46 | "Synchronisiert {}", |
| 47 | time.with_timezone(&Local).format("%H:%M") |
| 48 | ), |
| 49 | None => "Noch nicht synchronisiert".to_owned(), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn tone_color(tone: Tone) -> Hsla { |
| 54 | match tone { |
| 55 | Tone::Good => theme::green(), |
| 56 | Tone::Warn => theme::amber(), |
| 57 | Tone::Bad => theme::red(), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | pub fn render_status_bar( |
| 62 | state: &AppState, |
| 63 | update: &UpdateStatus, |
| 64 | on_restart: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 65 | ) -> impl IntoElement { |
| 66 | let (connection, connection_tone) = connection_text(&state.connection, state.mode); |
| 67 | let item = |label: String, tone: Tone| { |
| 68 | h_flex() |
| 69 | .gap(px(6.)) |
| 70 | .items_center() |
| 71 | .child(div().size(px(7.)).rounded_full().bg(tone_color(tone))) |
| 72 | .child(label) |
| 73 | }; |
| 74 | let live = live_text(state.live); |
| 75 | let mut bar = h_flex() |
| 76 | .w_full() |
| 77 | .h(px(24.)) |
| 78 | .flex_none() |
| 79 | .px(px(12.)) |
| 80 | .gap(px(14.)) |
| 81 | .items_center() |
| 82 | .text_size(px(11.)) |
| 83 | .bg(theme::background()) |
| 84 | .border_t_1() |
| 85 | .border_color(theme::border()) |
| 86 | .text_color(theme::text_muted()); |
| 87 | match live { |
| 88 | Some((label, tone)) if connection_tone == Tone::Good => { |
| 89 | bar = bar.child(item(label.to_owned(), tone)) |
| 90 | } |
| 91 | _ => bar = bar.child(item(connection, connection_tone)), |
| 92 | } |
| 93 | bar = bar.child(div().child(sync_text(state.last_sync))); |
| 94 | if state.mode.read_only { |
| 95 | bar = bar.child(div().text_color(theme::amber()).child("Nur lesen")); |
| 96 | } |
| 97 | match update { |
| 98 | UpdateStatus::UpToDate => {} |
| 99 | UpdateStatus::Ready => { |
| 100 | bar = bar.child( |
| 101 | div() |
| 102 | .id("update-restart") |
| 103 | .px(px(8.)) |
| 104 | .rounded(px(4.)) |
| 105 | .bg(theme::accent()) |
| 106 | .text_color(theme::on_accent()) |
| 107 | .cursor_pointer() |
| 108 | .hover(|button| button.bg(theme::accent_text())) |
| 109 | .child("Update bereit - Neu starten") |
| 110 | .on_click(on_restart), |
| 111 | ) |
| 112 | } |
| 113 | UpdateStatus::Failed(message) => { |
| 114 | bar = bar.child( |
| 115 | div() |
| 116 | .text_color(theme::red()) |
| 117 | .child(format!("Update fehlgeschlagen: {message}")), |
| 118 | ) |
| 119 | } |
| 120 | } |
| 121 | let full_version = updater::running_version(); |
| 122 | bar.child( |
| 123 | div() |
| 124 | .id("build-version") |
| 125 | .ml_auto() |
| 126 | .tooltip(move |window, cx| Tooltip::new(full_version).build(window, cx)) |
| 127 | .child(format::short_version( |
| 128 | full_version, |
| 129 | env!("CARGO_PKG_VERSION"), |
| 130 | )), |
| 131 | ) |
| 132 | } |
| 133 | |
| 134 | #[cfg(test)] |
| 135 | mod tests { |
| 136 | use super::{Tone, connection_text, live_text, sync_text}; |
| 137 | use crate::app_state::Mode; |
| 138 | use crate::backend::{ConnectionState, LiveState}; |
| 139 | use chrono::Utc; |
| 140 | |
| 141 | #[test] |
| 142 | fn states_map_to_text_and_tone() { |
| 143 | let mode = Mode::default(); |
| 144 | assert_eq!( |
| 145 | connection_text(&ConnectionState::Online, mode).1, |
| 146 | Tone::Good |
| 147 | ); |
| 148 | assert_eq!( |
| 149 | connection_text(&ConnectionState::LoginRequired, mode).0, |
| 150 | "Anmeldung nötig" |
| 151 | ); |
| 152 | assert_eq!( |
| 153 | connection_text(&ConnectionState::Connecting, mode).1, |
| 154 | Tone::Warn |
| 155 | ); |
| 156 | assert!( |
| 157 | connection_text(&ConnectionState::Failed("x".into()), mode) |
| 158 | .0 |
| 159 | .contains('x') |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn demo_mode_overrides_the_connection() { |
| 165 | let mode = Mode { |
| 166 | demo: true, |
| 167 | read_only: false, |
| 168 | }; |
| 169 | assert_eq!( |
| 170 | connection_text(&ConnectionState::NoBrowser, mode).0, |
| 171 | "Demodaten" |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn live_off_has_no_label() { |
| 177 | assert_eq!(live_text(LiveState::Off), None); |
| 178 | assert_eq!(live_text(LiveState::Live).unwrap().1, Tone::Good); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn sync_text_without_sync() { |
| 183 | assert_eq!(sync_text(None), "Noch nicht synchronisiert"); |
| 184 | assert!(sync_text(Some(Utc::now())).starts_with("Synchronisiert ")); |
| 185 | } |
| 186 | } |