Keep Export Selection destination approval with the original selection

This commit is contained in:
dotCooCoo committed 2026-11-26 20:32:16 -08:00
1 parent 1868da06a6
commit 1d84d216f6
2 files changed
+68 -12

No files matched your search

+34 -12
View File
@@ -282,7 +282,35 @@ pub(crate) fn selection_path(
app: &mut VectorcraftApp,
then: impl FnOnce(&mut VectorcraftApp, String) -> Result<Value, String> + 'static,
) -> Result<Value, String> {
target_path_keeping(app, None, "png", |_| true, then)
guarded_selection_path(app, None, "png", then)
}
/// An unanswered destination belongs to the drawing and selected objects it was opened for.
fn guarded_selection_path(
app: &mut VectorcraftApp,
path: Option<String>,
ext: &str,
then: impl FnOnce(&mut VectorcraftApp, String) -> Result<Value, String> + 'static,
) -> Result<Value, String> {
let source = |app: &VectorcraftApp| {
let state = app.session.active()?;
let mut objects = state.selection.objects.clone();
objects.sort_unstable();
Some((state.uid, state.reverts, objects))
};
let opening = source(app).ok_or("no document")?;
target_path_keeping(
app,
path,
ext,
|_| true,
move |app, path| {
if source(app).as_ref() != Some(&opening) {
return Err(tl!("The selection changed. Run Export Selection again.").into());
}
then(app, path)
},
)
}
/// Run an engine command that returns `{dataBase64}` without its `path` and write the bytes to
@@ -304,17 +332,11 @@ pub(crate) fn run_to_file(
let ext = given.map_or(ext, |f| f.extensions.first().copied().unwrap_or_default());
if selection && params.as_object().is_some_and(|o| o.get("format").is_none()) {
let (id, own_ext) = (id.to_string(), ext.to_string());
return target_path_keeping(
app,
path,
ext,
|_| true,
move |app, picked| {
let e = Some(fileio::extension(&picked)).filter(|e| !e.is_empty()).unwrap_or_else(|| own_ext.clone());
params["format"] = serde_json::json!(e);
write_output(app, &id, &own_ext, params, Some(picked), then)
},
);
return guarded_selection_path(app, path, ext, move |app, picked| {
let e = Some(fileio::extension(&picked)).filter(|e| !e.is_empty()).unwrap_or_else(|| own_ext.clone());
params["format"] = serde_json::json!(e);
write_output(app, &id, &own_ext, params, Some(picked), then)
});
}
write_output(app, id, ext, params, path, then)
}
+34
View File
@@ -67,6 +67,40 @@ fn poll(app: &mut VectorcraftApp) {
picks::poll(app, &crate::testing::ui_context());
}
#[test]
fn export_selection_picker_refuses_changed_artwork_before_format_options() {
assert_selection_picker_keeps_its_opening_artwork(json!({}), true);
}
#[test]
fn export_selection_picked_format_refuses_changed_artwork_before_writing() {
assert_selection_picker_keeps_its_opening_artwork(json!({"scale": 1}), false);
}
fn assert_selection_picker_keeps_its_opening_artwork(params: Value, opens_options: bool) {
let (mut app, picker, written) = saving_app();
let first = app.session.active().unwrap().selection.objects[0];
app.run("shape.rectangle", json!({"x": 45, "y": 40, "width": 10, "height": 15})).unwrap();
let second = app.session.active().unwrap().selection.objects[0];
assert_ne!(first, second);
app.run("select.set", json!({"ids": [first]})).unwrap();
app.run("document.exportSelection", params.clone()).unwrap();
app.run("select.set", json!({"ids": [second]})).unwrap();
picker.answer_saved("/tmp/stale.png", None);
poll(&mut app);
assert!(open_dialogs(&app).is_empty(), "the old picker must not open options for the new selection");
assert!(written.borrow().is_empty(), "the old picker must not write the new selection");
assert!(app.ui.status.contains("The selection changed"), "{}", app.ui.status);
app.run("document.exportSelection", params).unwrap();
picker.answer_saved("/tmp/fresh.png", None);
poll(&mut app);
if opens_options {
answer_top(&mut app, &[], true);
}
assert_eq!(written.borrow().as_slice(), ["/tmp/fresh.png"]);
}
/// The index of the file type with extension `ext` in the last save panel's list.
fn type_index(picker: &Picker, ext: &str) -> Option<usize> {
picker.requests().last()?.filters.iter().position(|f| f.extensions.iter().any(|e| e == ext))