From 76a967663ff4f5a205eb1e19afe9624f213145e9 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 20:43:42 -0800 Subject: [PATCH 1/2] Keep a pending Perspective Crop open when its tab is closed --- crates/ui-egui/src/canvas/document_area.rs | 2 +- crates/ui-egui/src/canvas/tabs.rs | 19 ++- .../tabs_perspective_crop_close_tests.rs | 121 ++++++++++++++++++ 3 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs diff --git a/crates/ui-egui/src/canvas/document_area.rs b/crates/ui-egui/src/canvas/document_area.rs index 64d7572b281..256a1f3fc5a 100644 --- a/crates/ui-egui/src/canvas/document_area.rs +++ b/crates/ui-egui/src/canvas/document_area.rs @@ -320,7 +320,7 @@ fn arranged(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, active: usiz } // A clean document can leave the session immediately, so close only after the pane indices were used. for id in closing { - tabs::close_document(app, ui.ctx(), id); + tabs::close_tab(app, ui.ctx(), id); } true } diff --git a/crates/ui-egui/src/canvas/tabs.rs b/crates/ui-egui/src/canvas/tabs.rs index 0466086965d..8c5bf98edd9 100644 --- a/crates/ui-egui/src/canvas/tabs.rs +++ b/crates/ui-egui/src/canvas/tabs.rs @@ -88,7 +88,7 @@ pub(super) fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, out: DocumentT for event in out.events { match event { DocumentTabsEvent::Selected(tab) | DocumentTabsEvent::Picked(tab) | DocumentTabsEvent::Activated(tab) => show_tab(app, tab), - DocumentTabsEvent::CloseRequested(DocTab::Doc(id)) => close_document(app, ctx, id), + DocumentTabsEvent::CloseRequested(DocTab::Doc(id)) => close_tab(app, ctx, id), DocumentTabsEvent::CloseRequested(DocTab::Opening(job)) => { let _ = crate::jobs_ui::cancel(app, job); } @@ -120,7 +120,18 @@ pub(super) fn apply(app: &mut PhotocraftApp, ctx: &egui::Context, out: DocumentT menu } -/// The guarded close shared by the tab strip and a tiled document's title. +/// The close affordance shared by the tab strip and a tiled document's title. +pub(super) fn close_tab(app: &mut PhotocraftApp, ctx: &egui::Context, id: DocId) { + if app.ui.tool == crate::Tool::PerspectiveCrop + && app.session.active().is_some_and(|document| document.doc.id == id) + && crate::perspective_crop_ui::quad(app).is_some() + { + return; + } + close_document(app, ctx, id); +} + +/// Closes an open document, showing its unsaved-changes prompt when needed. pub(super) fn close_document(app: &mut PhotocraftApp, ctx: &egui::Context, id: DocId) { if let Some(index) = document_index(app, id) { let _ = crate::menus::invoke(app, ctx, "file.close", json!({"document": index})); @@ -307,6 +318,10 @@ mod file_info_copyright_title_tests; #[path = "tab_indexed_bitmap_label_tests.rs"] mod tab_indexed_bitmap_label_tests; +#[cfg(test)] +#[path = "tabs_perspective_crop_close_tests.rs"] +mod tabs_perspective_crop_close_tests; + #[cfg(test)] #[path = "../tab_title_channel_target_tests.rs"] mod tab_title_channel_target_tests; diff --git a/crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs b/crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs new file mode 100644 index 00000000000..1acf9a6e57a --- /dev/null +++ b/crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs @@ -0,0 +1,121 @@ +use egui::accesskit::Role; +use egui::{Event, Key, Modifiers, PointerButton, Pos2, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::canvas::testing::to_screen; +use crate::perspective_crop_ui; +use crate::theme::AppTheme; +use crate::{PhotocraftApp, Tool}; + +type App = Harness<'static, Box>; + +fn two_documents(tiled: bool) -> Result { + let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, AppTheme::pro()); + PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), crate::Services::default()) + }); + let app = h.state_mut(); + for name in ["Reference.psd", "Edited.psd"] { + app.run("file.new", json!({"width": 400, "height": 300, "name": name}))?; + } + app.run("layer.new.layer", json!({"name": "Retouch"}))?; + h.run_steps(4); + let ctx = h.ctx.clone(); + if tiled { + crate::menus::invoke(h.state_mut(), &ctx, "window.arrange.tile", json!({}))?; + } + h.state_mut().ui.tool = Tool::PerspectiveCrop; + h.state_mut().ui.extras.snap = false; + h.run_steps(6); + assert!(h.state().session.active().is_some_and(|doc| doc.doc.name == "Edited.psd" && doc.is_dirty())); + Ok(h) +} + +fn point(h: &mut App, at: Pos2) { + h.event(Event::PointerMoved(at)); + h.run_steps(1); +} + +fn press(h: &mut App, pos: Pos2, pressed: bool) { + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.run_steps(1); +} + +fn click(h: &mut App, at: Pos2) { + point(h, at); + press(h, at, true); + press(h, at, false); + h.run_steps(3); +} + +fn draw_quad(h: &mut App) { + let from = to_screen(h.state(), [40.0, 30.0]); + let to = to_screen(h.state(), [360.0, 270.0]); + point(h, from); + press(h, from, true); + point(h, to); + press(h, to, false); + h.run_steps(2); + assert!(perspective_crop_ui::quad(h.state()).is_some()); +} + +fn click_close(h: &mut App, name: &str, tiled: bool) -> Result<(), String> { + let label = format!("Close {name}"); + let canvas = h.state().last_canvas_rect; + let at = h + .query_all_by_role(Role::Button) + .filter(|node| node.accesskit_node().label().is_some_and(|text| text.starts_with(label.as_str()))) + .map(|node| node.rect().center()) + .find(|at| canvas.contains(*at) == tiled) + .ok_or_else(|| format!("Missing {label} button for tiled={tiled}"))?; + click(h, at); + Ok(()) +} + +/// The active tab and active tiled title must leave the same quad editing session in place. +#[test] +fn a_complete_quad_protects_its_dirty_document_from_tab_and_tile_close() -> Result<(), String> { + for tiled in [false, true] { + let mut h = two_documents(tiled)?; + draw_quad(&mut h); + let quad = perspective_crop_ui::quad(h.state()); + let active = h.state().session.active().map(|doc| doc.doc.id); + click_close(&mut h, "Edited.psd", tiled)?; + assert!(h.state().discard.is_none(), "close opened a save prompt for tiled={tiled}"); + assert_eq!(h.state().session.documents().len(), 2); + assert_eq!(h.state().session.active().map(|doc| doc.doc.id), active); + assert_eq!(perspective_crop_ui::quad(h.state()), quad); + + h.key_press(Key::Escape); + h.run_steps(3); + assert!(perspective_crop_ui::quad(h.state()).is_none()); + let corner = to_screen(h.state(), [40.0, 30.0]); + click(&mut h, corner); + assert!(perspective_crop_ui::pending(h.state()), "one corner starts a partial session"); + assert!(perspective_crop_ui::quad(h.state()).is_none()); + click_close(&mut h, "Edited.psd", tiled)?; + assert!(h.state().discard.is_some(), "without a quad the existing dirty-document prompt is available for tiled={tiled}"); + assert_eq!(h.state().session.documents().len(), 2); + } + Ok(()) +} + +#[test] +fn a_complete_quad_leaves_other_tabs_and_the_file_close_command_available() -> Result<(), String> { + let mut h = two_documents(false)?; + draw_quad(&mut h); + let quad = perspective_crop_ui::quad(h.state()); + let active = h.state().session.active().map(|doc| doc.doc.id); + click_close(&mut h, "Reference.psd", false)?; + assert!(h.state().discard.is_none()); + assert_eq!(h.state().session.documents().len(), 1); + assert_eq!(h.state().session.active().map(|doc| doc.doc.id), active); + assert_eq!(perspective_crop_ui::quad(h.state()), quad); + + let ctx = h.ctx.clone(); + crate::menus::invoke(h.state_mut(), &ctx, "file.close", json!({}))?; + assert!(h.state().discard.is_some(), "File Close still asks before closing the dirty document"); + Ok(()) +} From d513539af98a10fab52d23b836863d1a021c1eb7 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Tue, 1 Dec 2026 21:15:02 -0800 Subject: [PATCH 2/2] Use the Perspective Crop cancel handler in the close fixture --- .../ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs b/crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs index 1acf9a6e57a..1c881ed5ae0 100644 --- a/crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs +++ b/crates/ui-egui/src/canvas/tabs_perspective_crop_close_tests.rs @@ -1,5 +1,5 @@ use egui::accesskit::Role; -use egui::{Event, Key, Modifiers, PointerButton, Pos2, vec2}; +use egui::{Event, Modifiers, PointerButton, Pos2, vec2}; use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::json; @@ -88,7 +88,8 @@ fn a_complete_quad_protects_its_dirty_document_from_tab_and_tile_close() -> Resu assert_eq!(h.state().session.active().map(|doc| doc.doc.id), active); assert_eq!(perspective_crop_ui::quad(h.state()), quad); - h.key_press(Key::Escape); + // Reset through the tool before testing a partial session, independently of the tab's focus. + perspective_crop_ui::cancel(h.state_mut()); h.run_steps(3); assert!(perspective_crop_ui::quad(h.state()).is_none()); let corner = to_screen(h.state(), [40.0, 30.0]);