From 94fdd279e801e996de86cbcc99b8befdcc771087 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Wed, 14 Oct 2026 19:07:55 +0200 Subject: [PATCH] fix(puppet-warp): drop open Puppet Warp and other distort sessions when the active document changes (#5092) (#5123) --- crates/ui-egui/src/canvas.rs | 1 + crates/ui-egui/src/distort_ui.rs | 21 +++++++++++++++++++++ crates/ui-egui/src/puppet_ui.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 11adb2c79a..7195620cda 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -1959,6 +1959,7 @@ pub(crate) fn retain_gpu_documents(app: &mut PhotocraftApp) { /// Tabs + canvas for the active document, or the start screen; then drops layers dragged onto /// another document (`layer_transfer`). pub fn document_area(app: &mut PhotocraftApp, ui: &mut egui::Ui) { + crate::distort_ui::drop_foreign(app); documents(app, ui); crate::layer_transfer::finish(app, ui.ctx()); // Once a frame, not per document view: the right-click or options-bar Brush Preset picker. diff --git a/crates/ui-egui/src/distort_ui.rs b/crates/ui-egui/src/distort_ui.rs index 9f0966b5c3..1f12b129f1 100644 --- a/crates/ui-egui/src/distort_ui.rs +++ b/crates/ui-egui/src/distort_ui.rs @@ -27,6 +27,8 @@ pub struct Distort { pub vanishing: Option, /// Filter Gallery dialog (gallery_ui). pub gallery: Option, + /// The document the open sessions belong to, noted by [`drop_foreign`] on the first frame they are open. + owner: Option, } impl Distort { @@ -236,8 +238,27 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> bool { false } +/// Drops the open sessions once the active document is not the one they were started on (closed, or another one +/// opened or switched to). Without this, Puppet Warp's preview of the old document replaced the new document's canvas (#5092). +pub fn drop_foreign(app: &mut PhotocraftApp) { + let current = app.session.active().map(|s| s.doc.id); + if !app.distort.active() && app.distort.gallery.is_none() { + app.distort.owner = None; + } else if app.distort.owner.is_none() { + app.distort.owner = current; + } else if app.distort.owner != current { + app.distort.liquify = None; + app.distort.puppet = None; + app.distort.perspective = None; + app.distort.vanishing = None; + app.distort.gallery = None; + app.distort.owner = None; + } +} + /// Full-window dialogs (Liquify, Filter Gallery). pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) { + drop_foreign(app); if app.distort.gallery.is_some() { crate::gallery_ui::show(app, ctx); } diff --git a/crates/ui-egui/src/puppet_ui.rs b/crates/ui-egui/src/puppet_ui.rs index b08546a51d..e233396cfd 100644 --- a/crates/ui-egui/src/puppet_ui.rs +++ b/crates/ui-egui/src/puppet_ui.rs @@ -454,6 +454,38 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { mod tests { use super::*; + /// #5092: a Puppet Warp session left open does not follow the user to a new document. + #[test] + fn session_is_dropped_when_another_document_becomes_active() { + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", json!({"width": 100, "height": 80, "depth": 8})).unwrap(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session + .edit("paint", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(20, 30, 80, 50), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + app.sync_views(); + crate::distort_ui::menu(&mut app, &ctx, "edit.puppetWarp", &json!({})).unwrap().unwrap(); + crate::distort_ui::drop_foreign(&mut app); + assert!(app.distort.puppet.is_some()); + app.session.execute("file.new", json!({"width": 32, "height": 32, "depth": 8})).unwrap(); + assert!(crate::distort_ui::display_doc(&app, 1).is_some()); + crate::distort_ui::drop_foreign(&mut app); + assert!(app.distort.puppet.is_none()); + assert!(crate::distort_ui::display_doc(&app, 1).is_none()); + // Closing the document it was started on drops it too. + assert!(app.session.set_active(0)); + crate::distort_ui::menu(&mut app, &ctx, "edit.puppetWarp", &json!({})).unwrap().unwrap(); + crate::distort_ui::drop_foreign(&mut app); + assert!(app.distort.puppet.is_some()); + assert!(app.session.close(0).is_some()); + crate::distort_ui::drop_foreign(&mut app); + assert!(app.distort.puppet.is_none()); + } + /// #4440: ⌘Z / ⌘⇧Z undo and redo pin edits while Puppet Warp is open: placing a pin, /// dragging one and removing one are a step each; a click that moves nothing isn't one. #[test]