use crate::lang::w; use crate::update::Phase; use gpui_kit::{AnyElement, IntoElement, ParentElement, Styled, div, px}; use super::{ Yumush, parts, style::{Tokens, size, space, text}, }; fn percent(fraction: f32) -> String { format!("{}%", (fraction * 100.) as u32) } fn status(phase: Phase) -> &'static str { match phase { Phase::Downloading { .. } => w().update.downloading, Phase::Installing { .. } => w().update.installing, Phase::Ready(_) => w().update.restarting, _ => "", } } impl Yumush { pub(super) fn update_control(&self, t: Tokens) -> Option { if self.update.required() { return None; } let (label, action): (&str, fn()) = match self.update { Phase::Idle => return None, Phase::Available(_) => (w().update.update, crate::update::install), Phase::Ready(_) => (w().update.restart, crate::update::restart), Phase::Failed(_) => (w().update.retry, crate::update::install), Phase::Downloading { .. } | Phase::Installing { .. } => { let fraction = self.update.progress().unwrap_or_default(); return Some( div() .flex() .flex_col() .gap(px(space::XS)) .child( div() .flex() .justify_between() .text_size(t.text(text::CAPTION)) .text_color(t.dim()) .child(status(self.update)) .child(percent(fraction)), ) .child(parts::track(parts::Fill::Part(fraction), false, t)) .into_any_element(), ); } }; Some( parts::button( "update_action", label, parts::Press::Plain, t, move |_, _| action(), ) .into_any_element(), ) } pub(super) fn update_required(&self, t: Tokens) -> impl IntoElement { let status = status(self.update); let label = match self.update.progress() { Some(fraction) => format!("{status} {}", percent(fraction)), None => status.to_string(), }; let failed = matches!(self.update, Phase::Failed(_)); div() .size_full() .flex() .flex_col() .items_center() .justify_center() .gap(px(space::LG)) .child(parts::words( w().update.required, parts::Text::Title, parts::Ink::Paper, t, )) .children(self.update.progress().map(|fraction| { div().w(px(size::SIDEBAR)).child(parts::track( parts::Fill::Part(fraction), false, t, )) })) .child(parts::words( label, parts::Text::Caption, parts::Ink::Dim, t, )) .children(failed.then(|| { parts::button( "update_retry", w().update.retry, parts::Press::Primary, t, |_, _| crate::update::install(), ) })) } }