Skip to content

Commit 311dca8

Browse files
committed
Save as Picture: surface a failed browser download instead of only logging it
1 parent 15cdcd0 commit 311dca8

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎apps/deckcraft-web/src/web.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,7 @@ fn services(inbox: Inbox, ctx: egui::Context) -> Services {
135135
});
136136
})),
137137
write: Some(Box::new(|name: &str, bytes: &[u8]| download(name, bytes))),
138-
download: Some(Box::new(|name: &str, bytes: &[u8]| {
139-
if let Err(e) = download(name, bytes) {
140-
log::error!("download of {name} failed: {e}");
141-
}
142-
})),
138+
download: Some(Box::new(|name: &str, bytes: &[u8]| download(name, bytes))),
143139
inbox: Some(inbox),
144140
..Default::default()
145141
}

‎crates/ui-egui/src/lib.rs‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub type PickFn = Box<dyn FnMut(&str) -> Option<String>>;
5454
/// carrying that same id. Completing a cancelled or unknown id does nothing: the file is
5555
/// not applied to whichever document happens to be active.
5656
pub type OpenAsyncFn = Box<dyn FnMut(&str, u64)>;
57-
pub type DownloadFn = Box<dyn FnMut(&str, &[u8])>;
57+
pub type DownloadFn = Box<dyn FnMut(&str, &[u8]) -> Result<(), String>>;
5858

5959
/// A file the host finished reading for the request `id` from [`SlideApp::begin_external_file`].
6060
#[derive(Clone, Debug)]
@@ -790,10 +790,13 @@ impl SlideApp {
790790
/// bar) when there are no bytes or no way to download.
791791
fn download(&mut self, name: &str, bytes: deckcraft_engine::Result<Vec<u8>>) -> bool {
792792
match (bytes, self.services.download.as_mut()) {
793-
(Ok(b), Some(dl)) => {
794-
dl(name, &b);
795-
true
796-
}
793+
(Ok(b), Some(dl)) => match dl(name, &b) {
794+
Ok(()) => true,
795+
Err(e) => {
796+
self.set_status(e);
797+
false
798+
}
799+
},
797800
(Err(e), _) => {
798801
self.set_status(e.to_string());
799802
false
@@ -1333,7 +1336,11 @@ impl SlideApp {
13331336
&& let Some(d) = self.session.active()
13341337
{
13351338
match deckcraft_engine::cmd::file::save_bytes(&d.doc, ext) {
1336-
Ok(bytes) => dl(&suggested, &bytes),
1339+
Ok(bytes) => {
1340+
if let Err(e) = dl(&suggested, &bytes) {
1341+
self.set_status(e);
1342+
}
1343+
}
13371344
Err(e) => self.set_status(e.to_string()),
13381345
}
13391346
}
@@ -2184,7 +2191,10 @@ mod tests {
21842191
let got: Rc<RefCell<Vec<(String, Vec<u8>)>>> = Rc::default();
21852192
let log = got.clone();
21862193
let services = Services {
2187-
download: Some(Box::new(move |name: &str, b: &[u8]| log.borrow_mut().push((name.to_string(), b.to_vec())))),
2194+
download: Some(Box::new(move |name: &str, b: &[u8]| {
2195+
log.borrow_mut().push((name.to_string(), b.to_vec()));
2196+
Ok(())
2197+
})),
21882198
..Default::default()
21892199
};
21902200
let mut app = SlideApp::new(Session::with_new(), services);
@@ -2213,15 +2223,26 @@ mod tests {
22132223
};
22142224
let got: Rc<RefCell<Vec<String>>> = Rc::default();
22152225
let log = got.clone();
2216-
let services =
2217-
Services { download: Some(Box::new(move |name: &str, _: &[u8]| log.borrow_mut().push(name.to_string()))), ..Default::default() };
2226+
let services = Services {
2227+
download: Some(Box::new(move |name: &str, _: &[u8]| {
2228+
log.borrow_mut().push(name.to_string());
2229+
Ok(())
2230+
})),
2231+
..Default::default()
2232+
};
22182233
let mut app = SlideApp::new(selected(), services);
22192234
assert!(app.run("app.saveAsPictureDialog", json!({})).is_ok());
22202235
assert_eq!(*got.borrow(), ["Picture.png"]);
22212236

22222237
let mut app = SlideApp::new(selected(), Services::default());
22232238
let err = app.run("app.saveAsPictureDialog", json!({})).unwrap_err();
22242239
assert!(err.contains("file picker"), "{err}");
2240+
2241+
// The browser refused the download (Blob or DOM setup failed): the error comes through.
2242+
let services = Services { download: Some(Box::new(|_: &str, _: &[u8]| Err("blob failed".to_string()))), ..Default::default() };
2243+
let mut app = SlideApp::new(selected(), services);
2244+
let err = app.run("app.saveAsPictureDialog", json!({})).unwrap_err();
2245+
assert!(err.contains("blob failed"), "{err}");
22252246
}
22262247

22272248
fn title_text(app: &SlideApp) -> String {

0 commit comments

Comments
 (0)