diff --git a/crates/ui-egui/src/file_asks.rs b/crates/ui-egui/src/file_asks.rs index b5df477b42..f7e0531665 100644 --- a/crates/ui-egui/src/file_asks.rs +++ b/crates/ui-egui/src/file_asks.rs @@ -40,6 +40,8 @@ pub(crate) enum Owner { struct Waiting { ticket: PickId, owner: Owner, + /// The file service is writing bytes that the caller already prepared. + saving: bool, /// The active document's uid when asked (`DocState::uid`); `None` for [`Owner::Free`]. doc: Option, /// The automation roots in force when asked: the continuation runs under them, not under the @@ -123,7 +125,7 @@ pub(crate) fn ask( owner: Owner, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { - start(app, owner, |files| files.pick(request), None, then) + start(app, owner, |files| files.pick(request), false, None, then) } /// Like [`ask`], but drops a reply if its requester is gone, before choosing a document or reporting a picker error. @@ -134,7 +136,7 @@ pub(crate) fn ask_while( current: impl FnOnce(&VectorcraftApp) -> bool + 'static, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { - start(app, owner, |files| files.pick(request), Some(Box::new(current)), then) + start(app, owner, |files| files.pick(request), false, Some(Box::new(current)), then) } /// Save `bytes` as the file service saves them (`Files::save`: the desktop asks where with `request` and @@ -147,7 +149,7 @@ pub(crate) fn save( owner: Owner, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { - start(app, owner, |files| files.save(request, bytes), None, then) + start(app, owner, |files| files.save(request, bytes), true, None, then) } /// Asks the file service with `ask` and files `then` for its answer. @@ -155,6 +157,7 @@ fn start( app: &mut VectorcraftApp, owner: Owner, ask: impl FnOnce(&dyn Files) -> PickId, + saving: bool, current: Option, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { @@ -165,7 +168,7 @@ fn start( Owner::Free => None, }; let (scope, automated) = (ScopeHandle::capture(), app.automation_asked()); - app.file_asks.waiting.push(Waiting { ticket, owner, doc, scope, automated, current, then: Box::new(then) }); + app.file_asks.waiting.push(Waiting { ticket, owner, saving, doc, scope, automated, current, then: Box::new(then) }); json!({ "pending": "fileDialog" }) } @@ -309,6 +312,11 @@ pub(crate) fn poll(app: &mut VectorcraftApp) { } match &picked { Picked::Cancelled => continue, + Picked::Failed(error) if w.saving => { + // The file service already had the bytes. It can report a failure without a drawing. + report(app, Err(error.clone())); + continue; + } Picked::Unavailable(why) => { // The desktop says what gives it a file dialog; the browser has none for this kind of pick. #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/src/panels/variables.rs b/crates/ui-egui/src/panels/variables.rs index 68c1adfcb1..6db53cf30c 100644 --- a/crates/ui-egui/src/panels/variables.rs +++ b/crates/ui-egui/src/panels/variables.rs @@ -722,10 +722,7 @@ fn ask_save(app: &mut VectorcraftApp, name: &str, filters: &[(&str, &[&str])], c return Value::Null; }; let bytes = text.as_bytes().to_vec(); - file_asks::save(app, request, bytes, Owner::Document, move |_app, picked| match picked { - craft::host::Picked::Failed(error) => Err(error), - _ => Ok(Value::Null), - }) + file_asks::save(app, request, bytes, Owner::Document, |_, _| Ok(Value::Null)) } /// Import…: one file dialog for a CSV, TSV, TXT or XML file, and `variable.import` on the chosen @@ -804,6 +801,33 @@ mod tests { } } + #[test] + fn a_failed_variables_download_reports_its_error_after_the_source_closes() { + use craft::host::{Picked, ScriptedFiles}; + + for command in [EXPORT_DATA_SETS, SAVE_LIBRARY] { + for answer in [Picked::Failed("downloads are blocked".into()), Picked::Cancelled, Picked::Downloaded("export".into())] { + let mut app = app_with_data(); + let files = Arc::new(ScriptedFiles::held().in_browser()); + crate::tests::picks::install_files(&mut app, files.clone()); + run(&mut app, command, &json!({})).unwrap().unwrap(); + app.session.execute("file.close", &json!({"index": 0})).unwrap(); + app.run("file.new", json!({"width": 80, "height": 80})).unwrap(); + app.status("another drawing"); + let before = app.session.active().unwrap().doc.clone(); + let expected = match &answer { + Picked::Failed(error) => error.as_str(), + Picked::Cancelled => "another drawing", + _ => file_asks::closed(), + }; + files.answer(answer.clone()); + file_asks::poll(&mut app); + assert_eq!(app.ui.status, expected); + assert_eq!(app.session.active().unwrap().doc, before); + } + } + } + fn app_with_data() -> VectorcraftApp { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); app.session.execute("file.new", &json!({"width": 300, "height": 200})).unwrap();