From 2d083e83aae25a990232cc0b8afbf451ba35de52 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 08:39:00 -0800 Subject: [PATCH] Drop stale texture replies before activating a document --- crates/ui-egui/src/dialogs/effect.rs | 100 ++++++++++++++++++++++++++- crates/ui-egui/src/file_asks.rs | 24 ++++++- 2 files changed, 119 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/dialogs/effect.rs b/crates/ui-egui/src/dialogs/effect.rs index 97dc95426d..7849d98124 100644 --- a/crates/ui-egui/src/dialogs/effect.rs +++ b/crates/ui-egui/src/dialogs/effect.rs @@ -10,6 +10,7 @@ //! effect's defaults for the rest. use std::path::PathBuf; +use std::sync::Arc; use craft::host::{PickRequest, Picked}; use craft::ui::contract::Bind; @@ -48,6 +49,8 @@ pub(crate) struct EffectDialogs { pub(crate) struct Open { /// The dialog's id; while [`asking`](Open::asking), the question's. pub id: DialogId, + /// Identity of this opening, since effect dialogs reuse the same dialog ID. + opening: Arc<()>, /// The item the effect goes to, resolved. target: Value, /// The parameters that find the applied effect (`item`, `aligned`). @@ -107,6 +110,7 @@ pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result { let target = edit_target.get("item").cloned().unwrap_or(target); let mut open = Open { id: DialogId::NULL, + opening: Arc::new(()), target: target.clone(), edit_target, index: None, @@ -144,6 +148,7 @@ pub(crate) fn open_last(app: &mut VectorcraftApp, effect: &str, params: Value) - vectorcraft_effects::effect_info(effect).ok_or_else(|| format!("unknown effect `{effect}`"))?; let open = Open { id: DialogId::NULL, + opening: Arc::new(()), target: Value::Null, edit_target: json!({}), index: None, @@ -465,7 +470,18 @@ fn drive(app: &mut VectorcraftApp, mut open: Open) { let _ = open.preview.run(app, previewing, &english, cmd, ¶ms); if let Some(request) = path { let (id, key) = (open.id, request.key); - pick_texture(app, Owner::Document, move |app, path, fields| { + let opening = Arc::clone(&open.opening); + let is_current = move |app: &VectorcraftApp| { + app.effect_dialogs.effect.as_ref().is_some_and(|current| current.id == id && Arc::ptr_eq(&opening, ¤t.opening)) + }; + let same_opening = is_current.clone(); + let is_live = move |app: &VectorcraftApp| { + same_opening(app) && app.dialogs().is_some_and(|dialogs| dialogs.with(|host| host.get_by_id::(id).is_some())) + }; + pick_texture_while(app, Owner::Document, is_live, move |app, path, fields| { + if !is_current(app) { + return; + } let Some(dialogs) = app.dialogs() else { return }; dialogs.with(|h| { if let Some(d) = h.get_mut_by_id::(id) { @@ -486,11 +502,20 @@ pub(super) fn pick_texture( app: &mut VectorcraftApp, owner: Owner, then: impl FnOnce(&mut VectorcraftApp, PathBuf, [(&'static str, FieldValue); 2]) + 'static, +) { + pick_texture_while(app, owner, |_| true, then); +} + +fn pick_texture_while( + app: &mut VectorcraftApp, + owner: Owner, + current: impl FnOnce(&VectorcraftApp) -> bool + 'static, + then: impl FnOnce(&mut VectorcraftApp, PathBuf, [(&'static str, FieldValue); 2]) + 'static, ) { use vectorcraft_engine::cmd::fileio; let rasters = fileio::FORMATS.iter().filter(|f| f.read && f.raster); let request = rasters.fold(PickRequest::open("texture"), |r, f| r.filter(f.label, fileio::read_extensions(f))); - file_asks::ask(app, request, owner, move |app: &mut VectorcraftApp, picked: Picked| { + file_asks::ask_while(app, request, owner, current, move |app: &mut VectorcraftApp, picked: Picked| { let Some(path) = picked.first_path() else { return Ok(Value::Null) }; let bytes = match picked { Picked::Contents(mut files) if !files.is_empty() => files.swap_remove(0).1, @@ -668,6 +693,77 @@ mod tests { } } + #[test] + fn a_delayed_texture_reply_only_reaches_the_effect_opening_that_requested_it() { + use std::cell::RefCell; + use std::rc::Rc; + + use craft::ui::testing::{Fixture, Room}; + use vectorcraft_effects::pixel::TEXTURE_IMAGE; + + let mut png = Vec::new(); + let img: image::DynamicImage = image::RgbaImage::from_pixel(6, 4, image::Rgba([40, 60, 90, 255])).into(); + img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap(); + for (reopen, switch_document) in [(true, false), (false, true), (false, false)] { + let mut app = app(); + rect(&mut app, 10); + app.run("select.all", json!({})).unwrap(); + let app = Rc::new(RefCell::new(app)); + let shown = Rc::clone(&app); + let mut fixture = Fixture::new(move |ui| crate::tests::picks::dialogs(&mut shown.borrow_mut(), ui)); + fixture.in_room(Room::Window); + let ctx = fixture.ctx().clone(); + app.borrow_mut().ui_ctx = Some(ctx.clone()); + let picker = crate::tests::picks::scripted(&mut app.borrow_mut()); + let open = || app.borrow_mut().run("effect.dialog", json!({"effect": ROUGH_PASTELS})).unwrap(); + open(); + fixture.settle(); + let browse = fixture.by_label("Browse…").expect("the Load Texture Browse button"); + fixture.click(&browse).settle(); + assert_eq!(picker.requests().len(), 1, "Browse requested a file"); + + if reopen || switch_document { + let dialog = id(&app.borrow()); + assert!(app.borrow().dialogs().unwrap().with(|host| host.cancel_by_id(&ctx, dialog))); + } + if switch_document { + let mut app = app.borrow_mut(); + let original = app.session.active().unwrap().uid; + // The reply arrives before the host pump has cleared the closed effect's app record. + app.session.execute("file.new", &json!({"width": 300, "height": 300})).unwrap(); + let active = app.session.active().unwrap().uid; + assert_ne!(active, original, "a different document is active"); + let status = app.ui.status.clone(); + picker.answer_contents(&[("invalid.png", b"not an image")]); + crate::file_asks::poll(&mut app); + assert_eq!(app.session.active().unwrap().uid, active, "a stale reply cannot switch documents"); + assert_eq!(app.ui.status, status, "a stale reply cannot report a texture error"); + continue; + } + if reopen { + fixture.settle(); + open(); + fixture.settle(); + } + let current = || { + let app = app.borrow(); + fields(&app, id(&app)) + }; + let before = current(); + picker.answer_contents(&[("texture.png", &png)]); + crate::file_asks::poll(&mut app.borrow_mut()); + fixture.settle(); + let after = current(); + if reopen { + assert_eq!(after.text("texture"), before.text("texture"), "a reopened effect keeps its own texture choice"); + assert_eq!(after.text(TEXTURE_IMAGE), before.text(TEXTURE_IMAGE), "the previous effect's image is absent"); + } else { + assert_eq!(after.text("texture"), Some("loaded"), "the originating effect loads the picked texture"); + assert!(after.text(TEXTURE_IMAGE).and_then(vectorcraft_effects::pixel::texture).is_some()); + } + } + } + /// A Pathfinder effect opens with its own Pathfinder Options (or, saved without them, the /// values it evaluates with); OK with nothing changed records no step, a change one step. #[test] diff --git a/crates/ui-egui/src/file_asks.rs b/crates/ui-egui/src/file_asks.rs index ba6a6f57dd..b5df477b42 100644 --- a/crates/ui-egui/src/file_asks.rs +++ b/crates/ui-egui/src/file_asks.rs @@ -21,6 +21,8 @@ use crate::VectorcraftApp; use crate::dialogs::confirm::{ReplaceFiles, ReplaceFilesPrompt}; type Then = Box Result>; +/// Whether the part of the app that requested a pick is still waiting for it. +type PickGuard = Box bool>; /// What proceeding past a replace question runs. type Proceed = Box Result>; @@ -45,6 +47,7 @@ struct Waiting { scope: ScopeHandle, /// Automation asked ([`VectorcraftApp::automation_asked`]): the continuation runs as its. automated: bool, + current: Option, then: Then, } @@ -120,7 +123,18 @@ pub(crate) fn ask( owner: Owner, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { - start(app, owner, |files| files.pick(request), then) + start(app, owner, |files| files.pick(request), None, then) +} + +/// Like [`ask`], but drops a reply if its requester is gone, before choosing a document or reporting a picker error. +pub(crate) fn ask_while( + app: &mut VectorcraftApp, + request: PickRequest, + owner: Owner, + 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) } /// Save `bytes` as the file service saves them (`Files::save`: the desktop asks where with `request` and @@ -133,7 +147,7 @@ pub(crate) fn save( owner: Owner, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { - start(app, owner, |files| files.save(request, bytes), then) + start(app, owner, |files| files.save(request, bytes), None, then) } /// Asks the file service with `ask` and files `then` for its answer. @@ -141,6 +155,7 @@ fn start( app: &mut VectorcraftApp, owner: Owner, ask: impl FnOnce(&dyn Files) -> PickId, + current: Option, then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result + 'static, ) -> Value { let Some(host) = app.ui_ctx.as_ref().and_then(Host::try_of_ctx) else { return Value::Null }; @@ -150,7 +165,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, then: Box::new(then) }); + app.file_asks.waiting.push(Waiting { ticket, owner, doc, scope, automated, current, then: Box::new(then) }); json!({ "pending": "fileDialog" }) } @@ -289,6 +304,9 @@ pub(crate) fn poll(app: &mut VectorcraftApp) { } } for (w, picked) in answered { + if w.current.is_some_and(|current| !current(app)) { + continue; + } match &picked { Picked::Cancelled => continue, Picked::Unavailable(why) => {