From d3bc67c94b09f00caa0347fe102939e419a1e459 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Tue, 10 Nov 2026 19:10:26 -0500 Subject: [PATCH] Delete Anchor Point: Delete with an anchor selected edits the path, never the layer (#12761) The Delete Anchor Point tool with one anchor selected now takes the Add Anchor Point route (#12703): Delete and Backspace remove the anchor and its segments instead of falling through to Edit > Clear. When the anchor is the last one of its subpath, the whole subpath goes as one History step, Delete Selection (path.deleteAnchor with segments relabels its step; the tool click keeps Delete Anchor Point). Co-Authored-By: Claude Sonnet 5.5 Model-Effort: Opus 5.5 drove; Opus 5.5 briefed; Sonnet 5.5 wrote at medium --- .../src/cmd/path_delete_last_anchor_tests.rs | 18 ++++ crates/engine/src/cmd/path_edit_cmds.rs | 8 ++ crates/ui-egui/src/tools/anchor_tools.rs | 2 + .../add_anchor_delete_key_tests.rs | 14 +-- .../delete_anchor_delete_key_tests.rs | 94 +++++++++++++++++++ .../ui-egui/src/tools/vector_ui/path_ops.rs | 4 +- 6 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 crates/ui-egui/src/tools/anchor_tools/delete_anchor_delete_key_tests.rs diff --git a/crates/engine/src/cmd/path_delete_last_anchor_tests.rs b/crates/engine/src/cmd/path_delete_last_anchor_tests.rs index a9dfb310a94..d0b4cf72520 100644 --- a/crates/engine/src/cmd/path_delete_last_anchor_tests.rs +++ b/crates/engine/src/cmd/path_delete_last_anchor_tests.rs @@ -153,3 +153,21 @@ fn a_type_paths_last_anchor_is_still_refused() { assert_eq!(type_path(&s), Some(before)); assert_eq!(s.active().unwrap().history.entries().len(), steps); } + +/// ⌫ / Delete on the Delete Anchor Point tool's last anchor of a subpath among others (#12761) +/// takes the subpath as one Delete Selection step; the tool's click keeps Delete Anchor Point. +#[test] +fn deleting_a_subpaths_last_anchor_with_its_segments_is_delete_selection() { + let two = || json!({"subpaths": [{"closed": false, "knots": [[100, 300], [200, 300]]}, {"closed": false, "knots": [[400, 300]]}]}); + let s = &mut session(); + s.execute("path.set", json!({"name": "Path 1", "path": two()})).unwrap(); + let before = doc(s).paths[0].path.clone(); + let r = s.execute("path.deleteAnchor", json!({"name": "Path 1", "subpath": 1, "knot": 0, "segments": true})).unwrap(); + assert_eq!(r["subpathRemoved"], json!(true)); + assert_eq!(doc(s).paths[0].path.subpaths.len(), 1); + assert_eq!(s.active().unwrap().history.undo_label(), Some("Delete Selection")); + assert!(s.undo()); + assert_eq!(doc(s).paths[0].path, before); + s.execute("path.deleteAnchor", json!({"name": "Path 1", "subpath": 1, "knot": 0})).unwrap(); + assert_eq!(s.active().unwrap().history.undo_label(), Some("Delete Anchor Point"), "the click's label is unchanged"); +} diff --git a/crates/engine/src/cmd/path_edit_cmds.rs b/crates/engine/src/cmd/path_edit_cmds.rs index c07ee4d3c2b..4e8b89f56e4 100644 --- a/crates/engine/src/cmd/path_edit_cmds.rs +++ b/crates/engine/src/cmd/path_edit_cmds.rs @@ -192,6 +192,14 @@ fn delete_anchor(s: &mut Session, p: &Value) -> Result { }; Ok(()) })?; + // Delete / Backspace (`segments`) that takes a whole subpath is Delete Selection, as in + // Photoshop (#12761); the tool's click on a lone anchor stays Delete Anchor Point. + if removed + && segments + && let Some(st) = s.active_mut() + { + st.history.set_current_label("Delete Selection"); + } if let Some(o) = r.as_object_mut() { o.insert("subpathRemoved".into(), json!(removed)); } diff --git a/crates/ui-egui/src/tools/anchor_tools.rs b/crates/ui-egui/src/tools/anchor_tools.rs index 1fa08a7c9d4..d1c3ff2e7d2 100644 --- a/crates/ui-egui/src/tools/anchor_tools.rs +++ b/crates/ui-egui/src/tools/anchor_tools.rs @@ -195,3 +195,5 @@ mod tests; #[cfg(test)] mod add_anchor_delete_key_tests; +#[cfg(test)] +mod delete_anchor_delete_key_tests; diff --git a/crates/ui-egui/src/tools/anchor_tools/add_anchor_delete_key_tests.rs b/crates/ui-egui/src/tools/anchor_tools/add_anchor_delete_key_tests.rs index 3808174d72a..c385841e114 100644 --- a/crates/ui-egui/src/tools/anchor_tools/add_anchor_delete_key_tests.rs +++ b/crates/ui-egui/src/tools/anchor_tools/add_anchor_delete_key_tests.rs @@ -11,7 +11,7 @@ use crate::state::Tool; /// 800 x 600 on white, pixel layer Red active, saved path Path 1 (a closed corner square) /// targeted in the Paths panel, the Add Anchor Point tool on. -fn app() -> PhotocraftApp { +pub(super) fn app() -> PhotocraftApp { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); app.session.execute("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap(); app.sync_views(); @@ -23,25 +23,25 @@ fn app() -> PhotocraftApp { app } -fn path1(app: &PhotocraftApp) -> Path { +pub(super) fn path1(app: &PhotocraftApp) -> Path { app.session.active().unwrap().doc.paths.iter().find(|p| p.name == "Path 1").unwrap().path.clone() } -fn layers(app: &PhotocraftApp) -> usize { +pub(super) fn layers(app: &PhotocraftApp) -> usize { app.session.active().unwrap().doc.layers.len() } -fn steps(app: &PhotocraftApp) -> usize { +pub(super) fn steps(app: &PhotocraftApp) -> usize { app.session.active().unwrap().history.past_len() } -fn ctrl_click(app: &mut PhotocraftApp, at: [f64; 2]) { +pub(super) fn ctrl_click(app: &mut PhotocraftApp, at: [f64; 2]) { tool_event(app, ToolEvent::Down { x: at[0], y: at[1], pressure: 1.0 }, Modifiers::COMMAND); tool_event(app, ToolEvent::Up { x: at[0], y: at[1] }, Modifiers::COMMAND); } /// A key through the app's shortcut handling, as the canvas gets it. -fn press(app: &mut PhotocraftApp, key: egui::Key) { +pub(super) fn press(app: &mut PhotocraftApp, key: egui::Key) { let ctx = egui::Context::default(); let m = Modifiers::NONE; let raw = egui::RawInput { @@ -53,7 +53,7 @@ fn press(app: &mut PhotocraftApp, key: egui::Key) { ctx.end_pass().textures_delta.clear(); } -fn anchors(path: &Path) -> Vec<[f64; 2]> { +pub(super) fn anchors(path: &Path) -> Vec<[f64; 2]> { path.subpaths.iter().flat_map(|sp| sp.knots.iter().map(|k| [k.anchor.x, k.anchor.y])).collect() } diff --git a/crates/ui-egui/src/tools/anchor_tools/delete_anchor_delete_key_tests.rs b/crates/ui-egui/src/tools/anchor_tools/delete_anchor_delete_key_tests.rs new file mode 100644 index 00000000000..b77a9dd899c --- /dev/null +++ b/crates/ui-egui/src/tools/anchor_tools/delete_anchor_delete_key_tests.rs @@ -0,0 +1,94 @@ +//! #12761: with the Delete Anchor Point tool, Delete / Backspace on a selected anchor edits the +//! path, never the active layer. The single anchor left of a subpath takes the whole subpath +//! (History: Delete Selection); an anchor of a longer subpath goes as in #12703. + +use egui::Modifiers; +use serde_json::json; + +use super::add_anchor_delete_key_tests::{anchors, app as add_app, ctrl_click, layers, path1, press, steps}; +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +/// The measured document: Path 1 holds a square, the open line B (400,300) to (700,300) and an +/// S-curve; the Delete Anchor Point tool is on. +fn app() -> PhotocraftApp { + let mut app = add_app(); + let path = json!({"subpaths": [ + {"closed": true, "knots": [[100, 100], [200, 100], [200, 200], [100, 200]]}, + {"closed": false, "knots": [[400, 300], [700, 300]]}, + {"closed": false, "knots": [[100, 400], [150, 350], [200, 450], [250, 400]]}, + ]}); + app.run("path.set", json!({"name": "Path 1", "path": path})).unwrap(); + app.ui.tool = Tool::DeleteAnchorPoint; + app +} + +fn click(app: &mut PhotocraftApp, at: [f64; 2]) { + tool_event(app, ToolEvent::Down { x: at[0], y: at[1], pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: at[0], y: at[1] }, Modifiers::NONE); +} + +/// B's end deleted by a click, then B's last anchor selected. +fn with_lone_anchor_selected() -> PhotocraftApp { + let mut app = app(); + click(&mut app, [700.0, 300.0]); + ctrl_click(&mut app, [400.0, 300.0]); + assert_eq!(app.ui.direct_selection.anchors, vec![[1, 0]], "Ctrl+click selects B's last anchor"); + app +} + +#[test] +fn delete_and_backspace_on_a_subpaths_last_anchor_remove_the_subpath_not_the_layer() { + for key in [egui::Key::Delete, egui::Key::Backspace] { + let mut app = with_lone_anchor_selected(); + let (before, n, count) = (path1(&app), steps(&app), layers(&app)); + assert_eq!(before.subpaths.len(), 3, "{key:?}: B is down to one anchor"); + press(&mut app, key); + assert_eq!(layers(&app), count, "{key:?}: every layer is still there"); + assert_eq!(path1(&app).subpaths.len(), 2, "{key:?}: Path 1 keeps the square and the S-curve"); + let st = app.session.active().unwrap(); + assert_eq!((st.history.past_len(), st.history.undo_label()), (n + 1, Some("Delete Selection")), "{key:?}"); + assert!(app.session.undo()); + assert_eq!(path1(&app), before, "{key:?}: Undo brings B back"); + } +} + +#[test] +fn delete_on_an_anchor_of_a_longer_subpath_goes_as_for_add_anchor_point() { + for key in [egui::Key::Delete, egui::Key::Backspace] { + let mut app = app(); + let (before, n, count) = (path1(&app), steps(&app), layers(&app)); + ctrl_click(&mut app, [200.0, 200.0]); + assert_eq!(app.ui.direct_selection.anchors, vec![[0, 2]], "{key:?}"); + press(&mut app, key); + assert_eq!(layers(&app), count, "{key:?}: every layer is still there"); + let p = path1(&app); + assert_eq!((p.subpaths.len(), p.subpaths[0].closed), (3, false), "{key:?}: the square opens"); + assert_eq!(anchors(&p).len(), 9, "{key:?}: one anchor fewer"); + let st = app.session.active().unwrap(); + assert_eq!((st.history.past_len(), st.history.undo_label()), (n + 1, Some("Delete Anchor Point")), "{key:?}"); + assert!(app.session.undo()); + assert_eq!(path1(&app), before, "{key:?}: Undo restores the square"); + } +} + +#[test] +fn without_a_selected_anchor_delete_keeps_its_usual_route() { + let mut app = app(); + let before = path1(&app); + press(&mut app, egui::Key::Delete); + assert_eq!(path1(&app), before, "no anchor selected: the path stays"); + assert_ne!(app.session.active().unwrap().history.undo_label(), Some("Delete Selection")); +} + +#[test] +fn other_tools_keep_their_delete() { + for tool in [Tool::Pen, Tool::FreeformPen, Tool::PathSelection] { + let mut app = with_lone_anchor_selected(); + app.ui.tool = tool; + let before = path1(&app); + press(&mut app, egui::Key::Delete); + assert_eq!(path1(&app), before, "{tool:?}: no anchor is deleted"); + } +} diff --git a/crates/ui-egui/src/tools/vector_ui/path_ops.rs b/crates/ui-egui/src/tools/vector_ui/path_ops.rs index f8fc316fcd3..6c86c1d2873 100644 --- a/crates/ui-egui/src/tools/vector_ui/path_ops.rs +++ b/crates/ui-egui/src/tools/vector_ui/path_ops.rs @@ -216,14 +216,14 @@ pub(crate) fn delete_key_deletes_work_path(app: &PhotocraftApp) -> bool { && matches!(target_path(app), Some((PathTarget::Work, _))) } -/// The anchor ⌫ / Delete removes, with its two segments: with the Add Anchor Point tool, the one +/// The anchor ⌫ / Delete removes, with its two segments: with the Add or Delete Anchor Point tool, the one /// anchor selected on it (⌘/Ctrl+click) while that selection still holds and its path is shown, /// as in Photoshop (#12703). The key reached Edit › Clear and deleted the active layer. Several /// selected anchors weren't measured and keep the key's other routes. pub(crate) fn delete_key_deletes_anchor(app: &PhotocraftApp) -> Option<(crate::direct_select::PathRef, [usize; 2])> { let ds = &app.ui.direct_selection; let ([[s, k]], Some(target)) = (ds.anchors.as_slice(), ds.target) else { return None }; - let usable = app.ui.tool == Tool::AddAnchorPoint + let usable = matches!(app.ui.tool, Tool::AddAnchorPoint | Tool::DeleteAnchorPoint) && app.ui.pen.is_none() && ds.drag.is_none() && app.ui.view.shows(app.ui.view.show.target_path)