Merge pull request #9596 from dotCooCoo/fix/document-file-picker-revert-generation

Ignore file picker results for a drawing reloaded by Revert
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-26 21:32:11 -08:00
commit 8108d60bc6
1 file changed
+59 -14
+59 -14
View File
@@ -29,8 +29,8 @@ type Proceed = Box<dyn FnOnce(&mut VectorcraftApp) -> Result<Value, String>>;
/// Whose answer a continuation is.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Owner {
/// The active document when it was asked: dropped with [`closed`] when it has closed, made
/// active before the continuation runs.
/// The active document when asked, made active before the continuation runs. Closing or
/// reverting it retires the request.
Document,
/// Acts on no document (an app preference, a library, a shortcut set): it goes on whatever is open.
Free,
@@ -39,9 +39,8 @@ pub(crate) enum Owner {
/// A pick asked for and not yet answered.
struct Waiting {
ticket: PickId,
owner: Owner,
/// The active document's uid when asked (`DocState::uid`); `None` for [`Owner::Free`].
doc: Option<u64>,
/// The active drawing's UID and Revert counter when asked, if the request is document-owned.
doc: Option<(u64, u64)>,
/// The automation roots in force when asked: the continuation runs under them, not under the
/// desktop's access of the frame the answer comes in.
scope: ScopeHandle,
@@ -161,11 +160,11 @@ fn start(
let Some(host) = app.ui_ctx.as_ref().and_then(Host::try_of_ctx) else { return Value::Null };
let ticket = ask(host.files());
let doc = match owner {
Owner::Document => app.session.active().map(|d| d.uid),
Owner::Document => app.session.active().map(|d| (d.uid, d.reverts)),
Owner::Free => None,
};
let (scope, automated) = (ScopeHandle::capture(), app.automation_asked());
app.file_asks.waiting.push(Waiting { ticket, owner, doc, scope, automated, current, then: Box::new(then) });
app.file_asks.waiting.push(Waiting { ticket, doc, scope, automated, current, then: Box::new(then) });
json!({ "pending": "fileDialog" })
}
@@ -264,14 +263,17 @@ pub(crate) fn open_params(picked: &Picked) -> Option<Map<String, Value>> {
Some(params)
}
/// Make the document asked for (`doc`, by uid) active before its continuation runs: another may be
/// active by now, or it may be gone → false when it can't run (the status bar says why).
fn on_document(app: &mut VectorcraftApp, doc: Option<u64>) -> bool {
let Some(uid) = doc else { return true };
let Some(i) = app.session.documents().iter().position(|d| d.uid == uid) else {
/// Make the original drawing active before running a continuation. The status bar explains why
/// a closed drawing or a different loaded generation cannot receive the result.
fn on_document(app: &mut VectorcraftApp, uid: u64, generation: Option<u64>) -> bool {
let Some((i, state)) = app.session.documents().iter().enumerate().find(|(_, d)| d.uid == uid) else {
app.status(closed());
return false;
};
if generation.is_some_and(|reverts| state.reverts != reverts) {
app.status(tl!("The drawing was reverted while its file dialog was open"));
return false;
}
if app.session.active_index() != Some(i)
&& let Err(e) = app.run("document.activate", json!({ "index": i }))
{
@@ -319,7 +321,7 @@ pub(crate) fn poll(app: &mut VectorcraftApp) {
}
_ => {}
}
if w.owner == Owner::Document && !on_document(app, w.doc) {
if w.doc.is_some_and(|(uid, reverts)| !on_document(app, uid, Some(reverts))) {
continue;
}
let Waiting { scope, automated, then, .. } = w;
@@ -355,7 +357,7 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
if let Some(DialogResult::Ok(mut answer)) = files
&& answer.choice == PromptChoice::Proceed
&& let Some(r) = answer.take_tag::<ReplaceFiles>()
&& on_document(app, Some(r.doc))
&& on_document(app, r.doc, None)
{
let all: Vec<String> = r.files.iter().chain(&r.allowed).cloned().collect();
crate::io::save::allow_replacing(app, &all);
@@ -364,3 +366,46 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
report(app, result);
}
}
#[cfg(test)]
mod tests {
use super::*;
use vectorcraft_engine::Session;
#[test]
fn an_svg_picker_returns_to_its_opening_tab_only_if_that_drawing_was_not_reverted() {
const SVG: &[u8] = br#"<svg xmlns="http://www.w3.org/2000/svg"><filter id="Shadow"><feOffset dx="2"/></filter></svg>"#;
for revert in [true, false] {
let mut app = crate::testing::windowed(VectorcraftApp::new(Session::new(), Default::default()));
let picker = crate::tests::picks::scripted(&mut app);
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string())));
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string())));
let dir = vectorcraft_testkit::TempDir::new("svg-picker-original-drawing");
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
app.run("file.saveAs", json!({"path": dir.join("filters.vectorcraft")})).unwrap();
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
let original = app.session.active().unwrap();
let version = (original.uid, original.reverts);
let pending = app.run("ui.importSvgFilter", json!({})).unwrap();
assert_eq!(pending["pending"], json!("fileDialog"));
if revert {
app.run("file.revert", json!({"confirmed": true})).unwrap();
let doc = app.session.active().unwrap();
assert_eq!(doc.uid, version.0);
assert_ne!(doc.reverts, version.1);
}
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
picker.answer_contents(&[("shadow.svg", SVG)]);
poll(&mut app);
let docs = app.session.documents();
assert!(docs[1].doc.svg_filters.is_empty());
if revert {
assert!(docs[0].doc.svg_filters.is_empty());
assert_eq!(app.session.active_index(), Some(1));
} else {
assert_eq!(docs[0].doc.svg_filters[0].name, "Shadow");
assert_eq!(app.session.active_index(), Some(0));
}
}
}
}