forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.07.2 MiB4b67e80Tahinli1 hour ago400 commits

backups.rs185 lines, 5.7 KiBHistoryRawDownload
1use common::{
2 call::{CreateBackup, DeleteBackup, ListBackups},
3 model::BackupInfo,
4};
5use gpui_kit::{
6 Context, Div, Entity, IntoElement, ParentElement, Styled, Window, assets::IconName,
7 component::Icon, div, px,
8};
9
10use super::{
11 save_directory,
12 ui::{card, card_header, confirmed, delete_icon_button, icon_button, list_row},
13};
14use crate::gui::{
15 Palette, Yumush, entity_action, millis_time, notify_error, storage,
16 style::{size, text},
17 text,
18};
19use crate::lang::w;
20
21const STORAGE_WIDTH: f32 = 72.;
22
23fn backup_row(info: &BackupInfo, pending: bool, entity: &Entity<Yumush>, palette: Palette) -> Div {
24 let export_name = info.name.clone();
25 let export = icon_button(
26 format!("export_backup_{}", info.name),
27 IconName::Download,
28 w().builder_backups.export,
29 palette.muted,
30 palette,
31 entity_action(entity, move |yumush, window, cx| {
32 yumush.export_backup(&export_name, window, cx)
33 }),
34 );
35
36 let delete_name = info.name.clone();
37 let delete = delete_icon_button(
38 format!("delete_backup_{}", info.name),
39 pending,
40 palette,
41 entity_action(entity, move |yumush, window, cx| {
42 yumush.delete_backup(&delete_name, window, cx)
43 }),
44 );
45
46 list_row()
47 .child(
48 Icon::new(IconName::HardDrive)
49 .size(px(size::ICON))
50 .flex_shrink_0()
51 .text_color(palette.muted),
52 )
53 .child(
54 div()
55 .flex_1()
56 .min_w_0()
57 .truncate()
58 .text_size(text(text::LABEL))
59 .text_color(palette.text)
60 .child(info.name.clone()),
61 )
62 .child(
63 div()
64 .flex_shrink_0()
65 .text_size(text(text::CAPTION))
66 .text_color(palette.muted)
67 .child(millis_time(info.created_ms, crate::lang::w().common.date)),
68 )
69 .child(
70 div()
71 .w(px(STORAGE_WIDTH))
72 .flex_shrink_0()
73 .flex()
74 .justify_end()
75 .text_size(text(text::CAPTION))
76 .text_color(palette.muted)
77 .child(storage(info.size_bytes)),
78 )
79 .child(export)
80 .child(delete)
81}
82
83impl Yumush {
84 pub(super) fn backups_card(&self, entity: &Entity<Self>, palette: Palette) -> Div {
85 let create = crate::gui::parts::button(
86 "create_backup",
87 w().builder_backups.create,
88 crate::gui::parts::Press::Plain,
89 palette.tokens,
90 entity_action(entity, |yumush, window, cx| {
91 yumush.create_backup(window, cx)
92 }),
93 )
94 .into_any_element();
95 let rows: Vec<Div> = self
96 .backups
97 .iter()
98 .map(|info| {
99 let pending = self.pending_backup_delete.as_deref() == Some(info.name.as_str());
100 backup_row(info, pending, entity, palette)
101 })
102 .collect();
103 let body = if rows.is_empty() {
104 crate::gui::parts::empty(w().builder_backups.no_backups, palette.tokens)
105 } else {
106 div().flex().flex_col().children(rows)
107 };
108
109 card(palette)
110 .child(card_header(
111 w().builder_backups.backups,
112 vec![create],
113 palette,
114 ))
115 .child(body)
116 }
117
118 pub(super) fn load_backups(&self, window: &mut Window, cx: &mut Context<Self>) {
119 self.fetch_builder(
120 window,
121 cx,
122 |network| async move { network.call(ListBackups).await },
123 |yumush, backups, cx| {
124 yumush.backups = backups;
125 cx.notify();
126 },
127 );
128 }
129
130 fn create_backup(&self, window: &mut Window, cx: &mut Context<Self>) {
131 let network = self.network.clone();
132
133 cx.spawn_in(window, async move |this, cx| {
134 match network.call(CreateBackup).await {
135 Ok(_) => {
136 let _ =
137 this.update_in(cx, |yumush, window, cx| yumush.load_backups(window, cx));
138 }
139 Err(error) => notify_error(&this, cx, error),
140 }
141 })
142 .detach();
143 }
144
145 fn delete_backup(&mut self, name: &str, window: &mut Window, cx: &mut Context<Self>) {
146 if !confirmed(&mut self.pending_backup_delete, name.to_string()) {
147 cx.notify();
148 return;
149 }
150 let network = self.network.clone();
151 let name = name.to_string();
152
153 cx.spawn_in(window, async move |this, cx| {
154 match network.call(DeleteBackup { name }).await {
155 Ok(()) => {
156 let _ =
157 this.update_in(cx, |yumush, window, cx| yumush.load_backups(window, cx));
158 }
159 Err(error) => notify_error(&this, cx, error),
160 }
161 })
162 .detach();
163 }
164
165 fn export_backup(&self, name: &str, window: &mut Window, cx: &mut Context<Self>) {
166 let target = cx.prompt_for_new_path(&save_directory(), Some(name));
167 let network = self.network.clone();
168 let name = name.to_string();
169
170 cx.spawn_in(window, async move |this, cx| {
171 let target = match target.await {
172 Ok(Ok(Some(target))) => target,
173 Ok(Err(error)) => {
174 notify_error(&this, cx, error);
175 return;
176 }
177 _ => return,
178 };
179 if let Err(error) = network.download_backup(&name, target).await {
180 notify_error(&this, cx, error);
181 }
182 })
183 .detach();
184 }
185}