diff --git a/crates/ui-egui/src/place.rs b/crates/ui-egui/src/place.rs index 1b2a678a4e..478cc4157d 100644 --- a/crates/ui-egui/src/place.rs +++ b/crates/ui-egui/src/place.rs @@ -341,16 +341,23 @@ fn pick(app: &mut VectorcraftApp, p: &Value) -> Value { // The web answers with the files' contents instead of paths. crate::file_asks::ask(app, request, Owner::Document, move |app, picked| { let files = match picked { - Picked::Paths(paths) => paths.into_iter().map(|path| with_importer(json!({ "path": path.to_string_lossy() }), &with)).collect(), - Picked::Contents(files) => files - .into_iter() - .map(|(name, bytes)| { - app.place.picked.retain(|(n, _)| *n != name); - let entry = with_importer(json!({ "name": name }), &with); - app.place.picked.push((name, bytes)); - entry - }) - .collect(), + Picked::Paths(paths) => { + app.place.picked.clear(); + paths.into_iter().map(|path| with_importer(json!({ "path": path.to_string_lossy() }), &with)).collect() + } + Picked::Contents(files) => { + // The next Place dialog replaces the previous selection, including its source bytes. + app.place.picked.clear(); + files + .into_iter() + .map(|(name, bytes)| { + app.place.picked.retain(|(n, _)| *n != name); + let entry = with_importer(json!({ "name": name }), &with); + app.place.picked.push((name, bytes)); + entry + }) + .collect() + } // Only an open's answers carry files. _ => return Ok(Value::Null), }; @@ -396,20 +403,29 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { use crate::dialogs::place::PlaceDialog; use craft::ui::dialog::DialogResult; let Some(dialogs) = app.dialogs() else { return }; - let (reading, answer) = dialogs.with(|host| { + let (reading, answer, current_open) = dialogs.with(|host| { let reading = host.get_mut::().is_some_and(|d| fill_lines(app, &d.files, &mut d.info)); - (reading, host.take::()) + let answer = host.take::(); + (reading, answer, host.is_open::()) }); if reading { ctx.request_repaint_after(std::time::Duration::from_millis(100)); } + // Place answers only after closing. Replacing an open Place leaves its Cancel while the new + // dialog is open; that retired selection must not clear the current selection's cache. + if current_open { + return; + } match answer { Some(DialogResult::Ok((files, options))) => { if let Err(e) = confirm(app, files, options) { app.status(e); } } - Some(_) => app.place.reading_lines = None, + Some(_) => { + app.place.reading_lines = None; + app.place.picked.clear(); + } None => {} } } diff --git a/crates/ui-egui/src/tests/place.rs b/crates/ui-egui/src/tests/place.rs index f23f63b763..5aad0ffa32 100644 --- a/crates/ui-egui/src/tests/place.rs +++ b/crates/ui-egui/src/tests/place.rs @@ -121,6 +121,125 @@ fn dismiss(app: &mut VectorcraftApp) { } } +#[test] +fn cancelling_browser_place_releases_the_picked_file_buffers() { + let mut app = app(); + let picker = scripted(&mut app); + let ctx = window(&app); + let bytes = png(6, 4, 72.0); + let undo = app.session.active().unwrap().history.undo.len(); + for name in ["first.png", "second.png"] { + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[(name, &bytes)]); + crate::file_asks::poll(&mut app); + assert_eq!(dialog_kind(&app).as_deref(), Some(crate::dialogs::place::KIND)); + assert_eq!(app.place.picked.len(), 1); + // Cancelling a replacement file picker leaves the existing Place selection usable. + app.run("file.place", json!({})).unwrap(); + picker.cancel(); + crate::file_asks::poll(&mut app); + assert_eq!(app.place.picked[0].0, name); + dismiss(&mut app); + crate::place::pump(&mut app, &ctx); + assert!(app.place.picked.is_empty(), "a cancelled Place selection no longer owns file bytes"); + assert!(app.session.active().unwrap().doc.layers[0].children().unwrap().is_empty()); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + } +} + +#[test] +fn a_finished_replacement_places_only_its_own_browser_file_before_the_next_pump() { + for old_ok in [false, true] { + let mut app = app(); + let picker = scripted(&mut app); + let ctx = window(&app); + let old = png(6, 4, 72.0); + let new = png(8, 5, 72.0); + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[("same.png", &old)]); + crate::file_asks::poll(&mut app); + let id = open_dialogs(&app).pop().unwrap().id; + let dialogs = app.dialogs().unwrap(); + dialogs.with(|host| { + if old_ok { + assert!(host.confirm_by_id(&ctx, id)); + } else { + assert!(host.cancel_by_id(&ctx, id)); + } + }); + // Both openings finish before the application takes a Place result. + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[("same.png", &new)]); + crate::file_asks::poll(&mut app); + let id = open_dialogs(&app).pop().unwrap().id; + dialogs.with(|host| { + host.set_json_field(&ctx, id, "link", json!(false)).unwrap(); + host.set_json_field(&ctx, id, "showImportOptions", json!(false)).unwrap(); + assert!(host.confirm_by_id(&ctx, id)); + }); + crate::place::pump(&mut app, &ctx); + assert!(app.place.picked.is_empty()); + assert_eq!(app.session.active().unwrap().doc.layers[0].children().unwrap().len(), 1); + let image = selected_image(&app); + assert!(matches!(&image.kind, NodeKind::Image(im) if (im.width, im.height) == (8, 5))); + let status = app.ui.status.clone(); + let undo = app.session.active().unwrap().history.undo.len(); + crate::place::pump(&mut app, &ctx); + assert_eq!(app.ui.status, status, "no earlier result remains to report an error"); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + } +} + +#[test] +fn an_old_place_ok_cannot_consume_a_newer_dialogs_browser_bytes() { + let mut app = app(); + let picker = scripted(&mut app); + let ctx = window(&app); + let bytes = png(6, 4, 72.0); + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[("old.png", &bytes)]); + crate::file_asks::poll(&mut app); + let id = open_dialogs(&app).pop().unwrap().id; + assert!(app.dialogs().unwrap().with(|host| host.confirm_by_id(&ctx, id)), "Place closes when its answer is filed"); + assert_ne!(dialog_kind(&app).as_deref(), Some(crate::dialogs::place::KIND)); + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[("new.png", &bytes)]); + crate::file_asks::poll(&mut app); + app.ui.status = "new selection".into(); + crate::place::pump(&mut app, &ctx); + assert_eq!(app.ui.status, "new selection", "the retired OK reports no stale error"); + assert_eq!(dialog_kind(&app).as_deref(), Some(crate::dialogs::place::KIND)); + assert_eq!(app.place.picked.len(), 1); + assert_eq!(app.place.picked[0].0, "new.png"); + assert!(app.session.active().unwrap().doc.layers[0].children().unwrap().is_empty()); + answer(&mut app, &[("link", json!(false)), ("showImportOptions", json!(false))]); + assert!(app.place.picked.is_empty()); + assert_eq!(app.session.active().unwrap().doc.layers[0].children().unwrap().len(), 1); +} + +#[test] +fn an_old_place_cancellation_keeps_a_newer_dialogs_browser_bytes() { + let mut app = app(); + let picker = scripted(&mut app); + let ctx = window(&app); + let bytes = png(6, 4, 72.0); + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[("old.png", &bytes)]); + crate::file_asks::poll(&mut app); + dismiss(&mut app); + // Open the replacement before the application pump consumes the previous cancellation. + app.run("file.place", json!({})).unwrap(); + picker.answer_contents(&[("new.png", &bytes)]); + crate::file_asks::poll(&mut app); + crate::place::pump(&mut app, &ctx); + assert_eq!(dialog_kind(&app).as_deref(), Some(crate::dialogs::place::KIND)); + assert_eq!(app.place.picked.len(), 1, "the replacement owns only its own source bytes"); + assert_eq!(app.place.picked[0].0, "new.png"); + answer(&mut app, &[("link", json!(false)), ("showImportOptions", json!(false))]); + assert!(app.place.picked.is_empty(), "confirmation transfers the bytes out of the dialog cache"); + assert_eq!(app.session.active().unwrap().doc.layers[0].children().unwrap().len(), 1, "the newer file still places"); +} + #[derive(Debug)] struct Dropped(std::path::PathBuf);