From d66bab2ae176a3ccf0891d423e023dd600d2341d Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:10:49 +0100 Subject: [PATCH] fix(ui): drop a Patch drag when its document is reopened and edited (#11024) A document closed and reopened with the same ID, then edited past the pressed revision before the next frame, kept a stale Patch or Content-Aware Move drag alive. DragDocument now also records the session's active generation, which closing and reopening advance. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas/drag.rs | 19 ++++++++++++------- crates/ui-egui/src/tools/retouch_ui/tests.rs | 15 ++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs index 76df137d16..8be1a51c5d 100644 --- a/crates/ui-egui/src/canvas/drag.rs +++ b/crates/ui-egui/src/canvas/drag.rs @@ -385,12 +385,15 @@ pub(crate) fn draw_readout(ctx: &egui::Context, id: &str, cursor ); } -/// The document a drag was pressed on. A reopened document can keep its ID (and revision), so the -/// snapshot tells it apart; an ordinary edit replaces the snapshot but advances the revision, which -/// keeps the drag on it (the rule `sync_views` applies to the Paths selection). +/// The document a drag was pressed on. A reopened document can keep its ID and revision, and be +/// edited past it before the next frame, so the session's active generation, which closing and +/// reopening advance, tells it apart; the snapshot catches one replaced in place. An ordinary edit +/// replaces the snapshot but advances the revision, which keeps the drag on it (the rule +/// `sync_views` applies to the Paths selection). #[derive(Clone, Debug)] pub struct DragDocument { id: photocraft_doc::DocId, + generation: u64, revision: u64, snapshot: std::sync::Weak, } @@ -398,14 +401,16 @@ pub struct DragDocument { impl DragDocument { pub fn capture(app: &PhotocraftApp) -> Option { let st = app.session.active()?; - Some(Self { id: st.doc.id, revision: st.revision, snapshot: std::sync::Arc::downgrade(&st.doc) }) + Some(Self { id: st.doc.id, generation: app.session.active_generation(), revision: st.revision, snapshot: std::sync::Arc::downgrade(&st.doc) }) } /// The document is still the active one. pub fn is_active(&self, app: &PhotocraftApp) -> bool { - app.session - .active() - .is_some_and(|st| st.doc.id == self.id && (st.revision > self.revision || self.snapshot.ptr_eq(&std::sync::Arc::downgrade(&st.doc)))) + app.session.active_generation() == self.generation + && app + .session + .active() + .is_some_and(|st| st.doc.id == self.id && (st.revision > self.revision || self.snapshot.ptr_eq(&std::sync::Arc::downgrade(&st.doc)))) } } diff --git a/crates/ui-egui/src/tools/retouch_ui/tests.rs b/crates/ui-egui/src/tools/retouch_ui/tests.rs index 7bb1f22bf6..3a879b26d8 100644 --- a/crates/ui-egui/src/tools/retouch_ui/tests.rs +++ b/crates/ui-egui/src/tools/retouch_ui/tests.rs @@ -504,13 +504,14 @@ fn a_patch_drag_is_dropped_when_another_document_is_active() { } } -/// A document closed and reopened with the same ID and revision mid-drag is another document: the -/// drag is dropped and its release neither lassos nor patches the reopened one. An ordinary edit -/// keeps the drag (#11024 review). +/// A document closed and reopened with the same ID mid-drag is another document, whether it comes +/// back at the same revision or is edited past it: the drag is dropped and its release neither +/// lassos nor patches the reopened one. An ordinary edit keeps the drag (#11024 review). #[test] fn a_patch_drag_is_dropped_when_its_document_is_reopened_with_the_same_id() { for tool in [Tool::Patch, Tool::ContentAwareMove] { - for reopen in [false, true] { + // (reopened, revisions the reopened document is edited past the pressed one) + for (reopen, edits) in [(false, 0), (true, 0), (true, 1)] { let mut app = app(); app.run("select.rect", json!({"x": 10, "y": 20, "width": 16, "height": 20})).unwrap(); app.ui.tool = tool; @@ -519,17 +520,17 @@ fn a_patch_drag_is_dropped_when_its_document_is_reopened_with_the_same_id() { if reopen { let closed = app.session.close(0).unwrap(); app.session.add_document((*closed.doc).clone(), None); - app.session.active_mut().unwrap().revision = closed.revision; + app.session.active_mut().unwrap().revision = closed.revision + edits; assert_eq!(app.session.active().unwrap().doc.id, closed.doc.id, "a reopened document may keep its ID"); } else { app.run("layer.new.layer", json!({})).unwrap(); } app.sync_views(); - assert_eq!(app.drag.is_some(), !reopen, "{tool:?}: reopened {reopen}"); + assert_eq!(app.drag.is_some(), !reopen, "{tool:?}: reopened {reopen}, edited {edits}"); tool_event(&mut app, ToolEvent::Up { x: 60.0, y: 40.0 }, egui::Modifiers::NONE); let sel = app.session.active().unwrap().doc.selection.clone().unwrap(); let lassoed = sel.content_bounds() != photocraft_geom::Rect::new(10, 20, 26, 40); - assert_eq!(lassoed, !reopen, "{tool:?}: reopened {reopen}: {:?}", sel.content_bounds()); + assert_eq!(lassoed, !reopen, "{tool:?}: reopened {reopen}, edited {edits}: {:?}", sel.content_bounds()); } } }