forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

update_view.rs118 lines, 3.7 KiBHistoryRawDownload
1use crate::lang::w;
2use crate::update::Phase;
3use gpui_kit::{AnyElement, IntoElement, ParentElement, Styled, div, px};
4
5use super::{
6 Palette, Yumush, parts,
7 style::{size, space, text},
8};
9
10fn percent(fraction: f32) -> String {
11 format!("{}%", (fraction * 100.) as u32)
12}
13
14fn status(phase: Phase) -> &'static str {
15 match phase {
16 Phase::Downloading { .. } => w().update.downloading,
17 Phase::Installing { .. } => w().update.installing,
18 Phase::Ready(_) => w().update.restarting,
19 _ => "",
20 }
21}
22
23impl Yumush {
24 pub(super) fn update_control(&self, palette: Palette) -> Option<AnyElement> {
25 if self.update.required() {
26 return None;
27 }
28 let t = palette.tokens;
29 let (label, restart) = match self.update {
30 Phase::Idle => return None,
31 Phase::Available(_) => (w().update.update, false),
32 Phase::Ready(_) => (w().update.restart, true),
33 Phase::Failed(_) => (w().update.retry, false),
34 Phase::Downloading { .. } | Phase::Installing { .. } => {
35 let fraction = self.update.progress().unwrap_or_default();
36 return Some(
37 div()
38 .flex()
39 .flex_col()
40 .gap(px(space::XS))
41 .child(
42 div()
43 .flex()
44 .justify_between()
45 .text_size(t.text(text::CAPTION))
46 .text_color(palette.muted)
47 .child(status(self.update))
48 .child(percent(fraction)),
49 )
50 .child(parts::track(parts::Fill::Part(fraction), false, t))
51 .into_any_element(),
52 );
53 }
54 };
55 Some(
56 parts::button(
57 "update_action",
58 label,
59 parts::Press::Plain,
60 t,
61 move |_, _| {
62 if restart {
63 crate::update::restart();
64 } else {
65 crate::update::install();
66 }
67 },
68 )
69 .into_any_element(),
70 )
71 }
72
73 pub(super) fn update_required(&self, palette: Palette) -> impl IntoElement {
74 let t = palette.tokens;
75 let status = status(self.update);
76 let label = match self.update.progress() {
77 Some(fraction) => format!("{status} {}", percent(fraction)),
78 None => status.to_string(),
79 };
80 let failed = matches!(self.update, Phase::Failed(_));
81
82 div()
83 .size_full()
84 .flex()
85 .flex_col()
86 .items_center()
87 .justify_center()
88 .gap(px(space::LG))
89 .child(parts::words(
90 w().update.required,
91 parts::Text::Title,
92 parts::Ink::Paper,
93 t,
94 ))
95 .children(self.update.progress().map(|fraction| {
96 div().w(px(size::SIDEBAR)).child(parts::track(
97 parts::Fill::Part(fraction),
98 false,
99 t,
100 ))
101 }))
102 .child(parts::words(
103 label,
104 parts::Text::Caption,
105 parts::Ink::Dim,
106 t,
107 ))
108 .children(failed.then(|| {
109 parts::button(
110 "update_retry",
111 w().update.retry,
112 parts::Press::Primary,
113 t,
114 |_, _| crate::update::install(),
115 )
116 }))
117 }
118}