From 00eed2243f9cd577b297935dfb3413691ec8c32c Mon Sep 17 00:00:00 2001 From: rmsff <17902130+rmsff@users.noreply.github.com> Date: Tue, 10 Nov 2026 20:23:41 +0300 Subject: [PATCH] Test Escape with the Delete Anchor Point tool (#12773) --- crates/ui-egui/src/tools/anchor_tools.rs | 3 + .../delete_anchor_escape_tests.rs | 144 ++++++++++++++++++ .../ui-egui/src/tools/anchor_tools/tests.rs | 4 +- 3 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/tools/anchor_tools/delete_anchor_escape_tests.rs diff --git a/crates/ui-egui/src/tools/anchor_tools.rs b/crates/ui-egui/src/tools/anchor_tools.rs index 1fa08a7c9d4..c6279ec9546 100644 --- a/crates/ui-egui/src/tools/anchor_tools.rs +++ b/crates/ui-egui/src/tools/anchor_tools.rs @@ -195,3 +195,6 @@ mod tests; #[cfg(test)] mod add_anchor_delete_key_tests; + +#[cfg(test)] +mod delete_anchor_escape_tests; diff --git a/crates/ui-egui/src/tools/anchor_tools/delete_anchor_escape_tests.rs b/crates/ui-egui/src/tools/anchor_tools/delete_anchor_escape_tests.rs new file mode 100644 index 00000000000..2fd81fdbd2a --- /dev/null +++ b/crates/ui-egui/src/tools/anchor_tools/delete_anchor_escape_tests.rs @@ -0,0 +1,144 @@ +//! #12773: with Delete Anchor Point, Esc first clears anchors selected on the saved path targeted +//! in the Paths panel, then deselects that path, so its outline and anchors go and clicks delete +//! nothing. Esc never adds a History step. + +use egui::{Key, Modifiers, vec2}; +use egui_kittest::{Harness, kittest::Queryable}; +use photocraft_doc::vector::Path; +use serde_json::json; + +use super::tests::{click, steps}; +use crate::PhotocraftApp; +use crate::direct_select::{PathRef, active_paths, candidates}; +use crate::state::Tool; + +/// The issue's document: 800 x 600 with the saved path "Path 1", a corner square with an anchor +/// at (300, 300), targeted in the Paths panel. +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap(); + let square = json!({"subpaths": [{"closed": true, "knots": [[100, 300], [300, 300], [300, 500], [100, 500]]}]}); + app.run("path.set", json!({"name": "Path 1", "path": square})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::DeleteAnchorPoint; + app.ui.selected_path = Some("Path 1".into()); + app +} + +fn escape(app: &mut PhotocraftApp) { + let ctx = egui::Context::default(); + let key = egui::Event::Key { key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }; + ctx.begin_pass(egui::RawInput { events: vec![key], ..Default::default() }); + crate::shortcuts::handle(app, &ctx); + ctx.end_pass().textures_delta.clear(); +} + +fn path_1(app: &PhotocraftApp) -> Path { + app.session.active().unwrap().doc.paths.iter().find(|p| p.name == "Path 1").unwrap().path.clone() +} + +#[test] +fn two_escapes_deselect_path_1_and_a_click_then_deletes_nothing() { + let mut app = app(); + let (before, n) = (path_1(&app), steps(&app)); + escape(&mut app); + escape(&mut app); + assert_eq!(app.ui.selected_path, None, "Path 1 is no longer selected in the Paths panel"); + assert!(candidates(&app).is_empty(), "no outline or anchors are drawn"); + assert_eq!(steps(&app), n, "Esc adds no History step"); + click(&mut app, [300.0, 300.0], Modifiers::NONE); + assert_eq!(path_1(&app), before); + assert_eq!(steps(&app), n, "the click adds no History step"); +} + +#[test] +fn the_first_escape_clears_the_selected_anchor_and_the_second_deselects_the_path() { + let mut app = app(); + // ⌘ is Direct Selection while held: it selects the anchor instead of deleting it. + click(&mut app, [300.0, 300.0], Modifiers::COMMAND); + assert_eq!((app.ui.direct_selection.target, app.ui.direct_selection.anchors.clone()), (Some(PathRef::Saved(0)), vec![[0, 1]])); + let n = steps(&app); + escape(&mut app); + assert!(app.ui.direct_selection.anchors.is_empty(), "the first Esc clears the anchor"); + assert_eq!(app.ui.selected_path.as_deref(), Some("Path 1"), "and keeps Path 1 selected"); + assert!(!active_paths(&app).is_empty(), "its outline stays"); + escape(&mut app); + assert_eq!(app.ui.selected_path, None, "the second Esc deselects Path 1"); + assert_eq!(steps(&app), n); +} + +#[test] +fn a_targeted_work_path_keeps_todays_escape() { + let mut app = app(); + let line = json!({"subpaths": [{"knots": [[100, 100], [200, 100]]}]}); + app.run("path.set", json!({"name": "work", "path": line})).unwrap(); + app.ui.selected_path = Some("work".into()); + escape(&mut app); + assert_eq!(app.ui.selected_path.as_deref(), Some("work")); +} + +#[test] +fn anchors_selected_on_a_shape_layer_keep_todays_escape() { + let mut app = app(); + app.ui.selected_path = None; + let id = app.run("shape.create", json!({"kind": "rect", "rect": [500, 100, 100, 100], "fill": "#3070c0"})).unwrap()["layer"].as_u64().unwrap(); + click(&mut app, [600.0, 200.0], Modifiers::COMMAND); + let selected = (app.ui.direct_selection.target, app.ui.direct_selection.anchors.clone()); + assert_eq!(selected, (Some(PathRef::Layer(id)), vec![[0, 2]])); + escape(&mut app); + assert_eq!((app.ui.direct_selection.target, app.ui.direct_selection.anchors.clone()), selected); +} + +#[test] +fn with_path_1_targeted_escape_still_leaves_full_screen_and_deselects_pixels_first() { + let mut app = app(); + app.ui.view.screen_mode = "fullScreen".into(); + escape(&mut app); + assert_eq!(app.ui.view.screen_mode, "standard"); + assert_eq!(app.ui.selected_path.as_deref(), Some("Path 1"), "leaving full screen keeps Path 1 selected"); + app.run("select.all", json!({})).unwrap(); + escape(&mut app); + assert!(app.session.active().unwrap().doc.selection.is_none(), "Esc deselects the pixels"); + assert_eq!(app.ui.selected_path.as_deref(), Some("Path 1"), "and keeps Path 1 selected"); +} + +#[test] +fn with_no_path_targeted_escape_still_leaves_full_screen_and_deselects_pixels() { + let mut app = app(); + app.ui.selected_path = None; + app.ui.view.screen_mode = "fullScreen".into(); + escape(&mut app); + assert_eq!(app.ui.view.screen_mode, "standard"); + app.run("select.all", json!({})).unwrap(); + escape(&mut app); + assert!(app.session.active().unwrap().doc.selection.is_none()); +} + +/// Esc while Path 1's name is typed in the Paths panel cancels the rename and keeps Path 1 +/// selected: egui drops the field's focus before the shortcuts run, so the step must wait. +#[test] +fn escape_while_renaming_path_1_cancels_the_rename() { + let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0).with_max_steps(64).build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + let mut app = app(); + // Layers | Channels | Paths: the Paths tab. + app.ui.dock_tabs.layers = 2; + app + }); + h.run_steps(8); + let name = h.get_by_label("Path 1").rect().left_center() + vec2(60.0, 0.0); + h.event(egui::Event::PointerMoved(name)); + h.run_steps(30); + for pressed in [true, false, true, false] { + h.event(egui::Event::PointerButton { pos: name, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.step(); + } + h.run_steps(3); + h.event(egui::Event::Text("Renamed".into())); + h.run_steps(1); + h.key_press(Key::Escape); + h.run_steps(3); + let names: Vec = h.state().session.active().unwrap().doc.paths.iter().map(|p| p.name.clone()).collect(); + assert_eq!(names, ["Path 1"], "Esc cancels the rename"); + assert_eq!(h.state().ui.selected_path.as_deref(), Some("Path 1")); +} diff --git a/crates/ui-egui/src/tools/anchor_tools/tests.rs b/crates/ui-egui/src/tools/anchor_tools/tests.rs index d7cd7256a4d..bd20264189d 100644 --- a/crates/ui-egui/src/tools/anchor_tools/tests.rs +++ b/crates/ui-egui/src/tools/anchor_tools/tests.rs @@ -34,11 +34,11 @@ fn work(app: &PhotocraftApp) -> Path { app.session.active().unwrap().doc.work_path.clone().unwrap() } -fn steps(app: &PhotocraftApp) -> usize { +pub(super) fn steps(app: &PhotocraftApp) -> usize { app.session.active().unwrap().history.entries().len() } -fn click(app: &mut PhotocraftApp, at: [f64; 2], mods: Modifiers) { +pub(super) fn click(app: &mut PhotocraftApp, at: [f64; 2], mods: Modifiers) { tool_event(app, ToolEvent::Down { x: at[0], y: at[1], pressure: 1.0 }, mods); tool_event(app, ToolEvent::Up { x: at[0], y: at[1] }, mods); }