Keep Batch folder picks with the dialog that asked

This commit is contained in:
dotCooCoo
2026-11-26 08:18:44 -08:00
parent 2a1b573a78
commit 9093809d5d
2 changed files with 55 additions and 14 deletions
+50 -9
View File
@@ -8,6 +8,7 @@
//! empty, or Data Sets without a document, is an error and the dialog stays open.
use std::hash::Hash;
use std::sync::Arc;
use craft::automation::actions::Action;
use craft::host::PickRequest;
@@ -131,6 +132,8 @@ pub(crate) enum Folder {
/// The Batch dialog on the dialog host.
pub struct BatchDialog {
id: DialogId,
/// Identifies this opening even when another Batch dialog reuses `id`.
folder_owner: Arc<()>,
/// The index of the action's set.
pub set: usize,
/// The index of the action in its set.
@@ -150,13 +153,13 @@ impl BatchDialog {
&self.form.values
}
/// The Browse presses since the last call.
pub(crate) fn take_asked(&mut self) -> Vec<Folder> {
std::mem::take(&mut self.asked)
/// The Browse presses since the last call and the opening that made them.
pub(crate) fn take_asked(&mut self) -> (Arc<()>, Vec<Folder>) {
(self.folder_owner.clone(), std::mem::take(&mut self.asked))
}
/// Writes the folder the picker answered into its row.
pub(crate) fn set_folder(&mut self, which: Folder, path: String) {
fn set_folder(&mut self, which: Folder, path: String) {
match which {
Folder::Source => self.form.values.folder = path,
Folder::Destination => self.form.values.destination_folder = path,
@@ -176,6 +179,7 @@ pub fn open(app: &mut VectorcraftApp, set: usize, action: usize) -> Result<Value
refuse_steps(&included_commands(a))?;
let dialog = BatchDialog {
id: dialog_id(KEY),
folder_owner: Arc::new(()),
set,
action,
action_name: a.name.clone(),
@@ -207,9 +211,9 @@ pub(crate) fn overwrites_one_file(app: &VectorcraftApp, set: usize, action: usiz
vectorcraft_engine::cmd::fileio::batch_overwrites_one_file(&app.session, &params(fields, "", commands))
}
/// Files the request for the folder behind `which`; the answer goes into the open dialog's row.
/// Files the request for `which`; the answer goes to the row of the Batch dialog that asked.
// PLAN: PathRow shows Browse whether or not the host offers pickers; `file_asks::ask` says so when it cannot ask.
pub(crate) fn ask_folder(app: &mut VectorcraftApp, which: Folder) {
pub(crate) fn ask_folder(app: &mut VectorcraftApp, owner: Arc<()>, which: Folder) {
let title = match which {
Folder::Source => crate::i18n::t("Source Folder"),
Folder::Destination => crate::i18n::t("Destination Folder"),
@@ -218,10 +222,9 @@ pub(crate) fn ask_folder(app: &mut VectorcraftApp, which: Folder) {
if let Some(path) = picked.paths().first()
&& let Some(dialogs) = app.dialogs()
{
let path = path.to_string_lossy().into_owned();
dialogs.with(|host| {
if let Some(dialog) = host.get_mut::<BatchDialog>() {
dialog.set_folder(which, path);
if let Some(dialog) = host.get_mut::<BatchDialog>().filter(|d| Arc::ptr_eq(&d.folder_owner, &owner)) {
dialog.set_folder(which, path.to_string_lossy().into_owned());
}
});
}
@@ -437,6 +440,44 @@ mod tests {
});
}
#[test]
fn batch_folder_answers_belong_to_the_dialog_that_asked() {
let step = ActionStep::new("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20}));
let mut app = app_with(vec![step]);
let picker = crate::tests::picks::scripted(&mut app);
let ctx = app.ui_ctx.clone().unwrap();
let dialogs = app.dialogs().unwrap();
let request_folders = |app: &mut VectorcraftApp| {
dialogs.with(|host| host.get_mut::<BatchDialog>().unwrap().asked.extend([Folder::Source, Folder::Destination]));
pump(app, &ctx);
};
let folders = || {
dialogs.with(|host| {
let fields = host.get::<BatchDialog>().unwrap().fields();
(fields.folder.clone(), fields.destination_folder.clone())
})
};
let answer = |app: &mut VectorcraftApp, source: &str, destination: &str| {
picker.answer(&[destination]);
picker.answer(&[source]);
file_asks::poll(app);
};
open(&mut app, 0, 0).unwrap();
request_folders(&mut app);
assert_eq!(picker.requests().len(), 2);
dialogs.with(|host| host.cancel::<BatchDialog>(&ctx));
open(&mut app, 0, 0).unwrap();
set_fields(&app, &[("folder", json!("new-source")), ("destinationFolder", json!("new-destination"))]);
answer(&mut app, "old-source", "old-destination");
assert_eq!(folders(), ("new-source".into(), "new-destination".into()));
request_folders(&mut app);
assert_eq!(picker.requests().len(), 4);
answer(&mut app, "picked-source", "picked-destination");
assert_eq!(folders(), ("picked-source".into(), "picked-destination".into()));
}
/// Batch plays an action recorded with a key its command no longer reads, as Play does.
#[test]
fn an_action_with_a_key_no_longer_read_still_plays_in_a_batch() {
+5 -5
View File
@@ -363,13 +363,13 @@ fn push_app_data(app: &VectorcraftApp) {
}
}
/// The folder picks the open Batch dialog asked for: each goes to the app's folder request, whose
/// answer is written into the dialog.
/// Files the folder picks the Batch dialog asked for with the identity of that opening.
fn ask_batch_folders(app: &mut VectorcraftApp) {
let Some(dialogs) = app.dialogs() else { return };
let asked = dialogs.with(|host| host.get_mut::<BatchDialog>().map(BatchDialog::take_asked).unwrap_or_default());
for which in asked {
super::action_batch::ask_folder(app, which);
let asked = dialogs.with(|host| host.get_mut::<BatchDialog>().map(BatchDialog::take_asked));
let Some((owner, folders)) = asked else { return };
for which in folders {
super::action_batch::ask_folder(app, owner.clone(), which);
}
}