diff --git a/crates/ui-egui/src/dialogs/script_run.rs b/crates/ui-egui/src/dialogs/script_run.rs index 488fb70e57..4804675d8d 100644 --- a/crates/ui-egui/src/dialogs/script_run.rs +++ b/crates/ui-egui/src/dialogs/script_run.rs @@ -29,6 +29,7 @@ //! are not the dialog's default. `selectDlg` opens the host's folder picker in its own folder. use std::path::Path; +use std::sync::Arc; use craft::host::{PickRequest, Picked}; use craft::theme::variant::Tone; @@ -276,6 +277,7 @@ impl StackDialog for ScriptNotice { /// The dialog of a script's file dialog call: what the run asked for, read from the run's own /// request when the dialog opens, so no field of the dialog changes what the person reads. pub(crate) struct ScriptFiles { + opening: Arc<()>, kind: PickerKind, prompt: String, can_always: bool, @@ -290,6 +292,7 @@ pub(crate) struct ScriptFiles { impl ScriptFiles { fn new(p: ScriptPicker) -> Self { Self { + opening: Arc::new(()), kind: p.kind, prompt: p.prompt, can_always: p.can_always, @@ -719,8 +722,11 @@ pub(crate) fn settle_files(app: &mut VectorcraftApp, result: DialogResult().map(|dialog| Arc::clone(&dialog.opening))).flatten() else { + return; + }; let Some(p) = app.session.script_picker() else { return }; let several = p.multiple && p.kind == PickerKind::Open; let mut request = match p.kind { @@ -750,7 +756,11 @@ fn choose(app: &mut VectorcraftApp) { file_asks::ask(app, request, Owner::Free, move |app, picked| { let chosen = picked.paths().iter().map(|path| path.to_string_lossy().into_owned()).collect(); let automated = app.file_asks.automated(); - with_host(app, |host, _| host.get_mut::().map(|dialog| dialog.add(chosen, several, automated))); + with_host(app, |host, _| { + if let Some(dialog) = host.get_mut::().filter(|dialog| Arc::ptr_eq(&dialog.opening, &opening)) { + dialog.add(chosen, several, automated); + } + }); Ok(Value::Null) }); } diff --git a/crates/ui-egui/src/tests/script_run.rs b/crates/ui-egui/src/tests/script_run.rs index 53aa6f39cb..4a65e884e1 100644 --- a/crates/ui-egui/src/tests/script_run.rs +++ b/crates/ui-egui/src/tests/script_run.rs @@ -70,6 +70,30 @@ fn allow_once(app: &mut VectorcraftApp) { script_run::settle_files(app, DialogResult::Ok(FilesAnswer { answer: "once", paths, left_out: false, automated: false })); } +#[test] +fn a_script_file_reply_does_not_populate_a_reopened_dialog() { + let mut app = app(); + let picker = scripted(&mut app); + app.session.prefs.show_external_script_warning = false; + menus::run_ui_command(&mut app, "ui.runScript", &json!({"source": "File.openDialog('Choose');"})).unwrap().unwrap(); + script_runs::poll(&mut app); + script_run::with_host(&app, |host, _| host.get_mut::().unwrap().choose = true).unwrap(); + pump(&mut app); + assert_eq!(picker.requests().len(), 1); + + script_run::forget::(&app); + script_run::open_picker(&app, app.session.script_picker().unwrap()); + let old_path = std::env::temp_dir().join("original-script-picker.txt"); + picker.answer(&[&old_path.to_string_lossy()]); + let ctx = app.ui_ctx.clone().unwrap(); + crate::tests::picks::poll(&mut app, &ctx); + + assert!(picked(&app).is_empty(), "the old file chooser must not populate the new open dialog"); + set(&app, PICKER_KIND, "answer", json!("deny")).unwrap(); + assert!(confirm(&mut app, PICKER_KIND)); + finish(&mut app); +} + #[test] fn the_warning_comes_before_the_run_and_cancel_runs_nothing() { let mut app = app();