From 13146f174aa0b9bb0390c0830cbb9e6951ca1215 Mon Sep 17 00:00:00 2001 From: hnoerenberg Date: Wed, 25 Nov 2026 22:02:02 +0100 Subject: [PATCH] fix(vector): a Ctrl-drag of a curvature draft anchor keeps its handles While a Curvature Pen path is still being drawn, a Ctrl/Cmd-drag of one of its anchors runs the path overlay's Direct Selection on the open draft. The finished drag went back to the Pen through `replace_draft`, which solves a Curvature draft's handles again with `reflow_kept`; a draft drawn from scratch has no kept anchors, so every smooth knot's handles were recomputed and the dragged anchor lost the handles the gesture had moved with it - the curve looked exactly as if no key had been held. The hand-back now knows which route it came from: the move a drag made goes through `replace_draft_as_moved`, which leaves the draft as the gesture left it, while every other edit of the draft (a keyboard nudge, an inserted or removed anchor) and a rebuilt live draft keep today's solve. The solve belongs to the Pen's own click in Curvature mode, which is not a Direct Selection edit. Tests: crates/ui-egui/src/tools/vector_ui/curvature_ctrl_drag_tests.rs. Every expected value is read from the app's own draft before the drag - the drag's own delta and the knots the gesture did not touch - so no expected handle position is written into the file. On main the dragged knot's in handle moved by (-26.5, 100) instead of the drag's (0, 100), and the other four knots moved too; with the change all five tests pass. --- .../ui-egui/src/tools/direct_select/edits.rs | 23 ++- crates/ui-egui/src/tools/vector_ui.rs | 5 +- .../vector_ui/curvature_ctrl_drag_tests.rs | 166 ++++++++++++++++++ crates/ui-egui/src/tools/vector_ui/host.rs | 14 +- 4 files changed, 200 insertions(+), 8 deletions(-) create mode 100644 crates/ui-egui/src/tools/vector_ui/curvature_ctrl_drag_tests.rs diff --git a/crates/ui-egui/src/tools/direct_select/edits.rs b/crates/ui-egui/src/tools/direct_select/edits.rs index be9eb8ba3c9..4e3d27703ea 100644 --- a/crates/ui-egui/src/tools/direct_select/edits.rs +++ b/crates/ui-egui/src/tools/direct_select/edits.rs @@ -90,7 +90,7 @@ pub(crate) fn delete_anchors(app: &mut PhotocraftApp, path: PathRef, anchors: &[ return; } if path == PathRef::Pen { - edit_draft(app, |draft| { + edit_draft(app, false, |draft| { let Some(sub) = draft.subpaths.get_mut(0) else { return false }; order.iter().filter(|[s, _]| *s == 0).fold(false, |changed, [_, k]| sub.remove_knot(*k) || changed) }); @@ -205,7 +205,12 @@ fn moved(x: f64, y: f64) -> bool { fn change_path(app: &mut PhotocraftApp, change: &PathEdit, refs: &[PathRef], tool: Tool, mods: Mods, dragged: bool) { let Some(r) = known(change.path(), refs) else { return }; if r == PathRef::Pen { - edit_draft(app, |path| change.apply(path)); + // A drag of an anchor of the path being drawn (⌘/Ctrl with a pen tool, or the Direct Selection tool): the + // anchor moves with its handles and the Curvature Pen takes the draft as the gesture left it, instead of + // solving the curve again under the move (#12757). An arrow-key nudge of the draft, which is no pointer + // gesture, and every other edit of it keep today's solve. + let dragged_move = dragged && matches!(change, PathEdit::MoveAnchors { .. }); + edit_draft(app, dragged_move, |path| change.apply(path)); return; } // The Convert Point tool names its steps as the reference does; the Pen's ⌥ and Direct @@ -323,7 +328,7 @@ fn add_and_move(app: &mut PhotocraftApp, r: PathRef, prefix: &str, segment: Knot /// The Insert key: an anchor in the middle of the segment leaving `knot`. fn insert(app: &mut PhotocraftApp, r: PathRef, knot: KnotRef, t: f64) { if r == PathRef::Pen { - edit_draft(app, |path| path.subpaths.get_mut(knot.subpath).and_then(|sub| sub.insert_at(knot.knot, t)).is_some()); + edit_draft(app, false, |path| path.subpaths.get_mut(knot.subpath).and_then(|sub| sub.insert_at(knot.knot, t)).is_some()); return; } command(app, r, "path.addAnchor", json!({"subpath": knot.subpath, "knot": knot.knot, "t": t})); @@ -407,15 +412,21 @@ pub(crate) fn command(app: &mut PhotocraftApp, r: PathRef, id: &str, params: Val } } -/// Edits the pen draft as a path of its own key and hands the result back to the pen. -fn edit_draft(app: &mut PhotocraftApp, edit: impl FnOnce(&mut EditPath) -> bool) { +/// Edits the pen draft as a path of its own key and hands the result back to the pen. `moved` is the ⌘/Ctrl-drag +/// of an anchor: the Pen takes the draft as the gesture left it, and a Curvature Pen draft is not solved again +/// under it (#12757). Every other edit of the draft keeps the solve. +fn edit_draft(app: &mut PhotocraftApp, moved: bool, edit: impl FnOnce(&mut EditPath) -> bool) { let Some(draft) = app.ui.pen.as_ref().map(|pen| pen.draft.clone()) else { return }; let mut path = EditPath::new(PathRef::Pen.key(), vec![draft]).copies(false); if !edit(&mut path) { return; } if let Some(draft) = path.subpaths.into_iter().next() { - crate::vector_ui::replace_draft(app, &draft); + if moved { + crate::vector_ui::replace_draft_as_moved(app, &draft); + } else { + crate::vector_ui::replace_draft(app, &draft); + } } } diff --git a/crates/ui-egui/src/tools/vector_ui.rs b/crates/ui-egui/src/tools/vector_ui.rs index 254fcaa0085..3fe7485435d 100644 --- a/crates/ui-egui/src/tools/vector_ui.rs +++ b/crates/ui-egui/src/tools/vector_ui.rs @@ -54,7 +54,7 @@ pub(crate) use shapes::{create_geometry, kind_of, tool_history_name}; use crate::shape_paint_ui::PaintChange; #[cfg(test)] pub(crate) use host::remove_last; -pub(crate) use host::{VectorTools, replace_draft}; +pub(crate) use host::{VectorTools, replace_draft, replace_draft_as_moved}; #[cfg(test)] pub(crate) use path_ops::delete_key_deletes_work_path; #[cfg(test)] @@ -221,6 +221,9 @@ mod ellipse_path_menus_tests; #[cfg(test)] mod curvature_delete_key_tests; +#[cfg(test)] +mod curvature_ctrl_drag_tests; + #[cfg(test)] mod shape_properties_tests; diff --git a/crates/ui-egui/src/tools/vector_ui/curvature_ctrl_drag_tests.rs b/crates/ui-egui/src/tools/vector_ui/curvature_ctrl_drag_tests.rs new file mode 100644 index 00000000000..c991e20101d --- /dev/null +++ b/crates/ui-egui/src/tools/vector_ui/curvature_ctrl_drag_tests.rs @@ -0,0 +1,166 @@ +//! #12757: with the Curvature Pen and a path still being drawn, ⌘/Ctrl-dragging one of its anchors moves that +//! anchor with its handles and leaves every other knot exactly as it was: the move does not solve the curve again. +//! Every value expected here is read from the app's own draft before the gesture and compared with what the +//! gesture made of it (the drag's own delta, and the knots it did not touch), so this file states no handle +//! position of its own. + +use egui::Modifiers; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::testing; +use crate::state::Tool; + +/// The five anchors of the path under test, at 100 % zoom on an 800 x 600 document. +const ANCHORS: [[f64; 2]; 5] = [[100.0, 300.0], [250.0, 150.0], [400.0, 300.0], [550.0, 150.0], [700.0, 300.0]]; +/// The middle anchor: the one the gesture drags. +const DRAGGED: usize = 2; +/// Where the drag lets go, so the drag's own delta is (0, 100). +const RELEASE: [f64; 2] = [400.0, 400.0]; +/// The anchor the path carries on from after the drag, well clear of every anchor drawn and inside the document. +const NEXT: [f64; 2] = [780.0, 420.0]; + +/// The Curvature Pen in Path mode, the five anchors clicked, and the path still being drawn. +fn drawing() -> Box { + 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(); + app.run("layer.new.layer", json!({"name": "Red"})).unwrap(); + app.ui.tool = Tool::CurvaturePen; + app.ui.tool_options.vector_mode = craft::draw::ui::VectorMode::Path; + for at in ANCHORS { + testing::click(&mut app, at, Modifiers::NONE); + } + assert_eq!(draft(&app).len(), ANCHORS.len(), "the five anchors are the draft being drawn"); + app +} + +/// The same path finished with Esc: no draft, the Work Path holds it. +fn finished() -> Box { + let mut app = drawing(); + testing::key(&mut app, egui::Key::Escape, Modifiers::NONE); + assert!(app.ui.pen.is_none(), "Esc ended the path"); + app +} + +/// The draft's knots as anchor, in handle and out handle. +fn draft(app: &PhotocraftApp) -> Vec<[[f64; 2]; 3]> { + app.ui.pen.as_ref().expect("the path is still being drawn").triples() +} + +/// The Work Path's knots the same way. +fn work(app: &PhotocraftApp) -> Vec<[[f64; 2]; 3]> { + let path = app.session.active().unwrap().doc.work_path.clone().expect("the finished path is the Work Path"); + path.subpaths[0].knots.iter().map(|k| [[k.anchor.x, k.anchor.y], [k.in_ctrl.x, k.in_ctrl.y], [k.out_ctrl.x, k.out_ctrl.y]]).collect() +} + +/// How far `after` is from `before`. +fn delta(before: [f64; 2], after: [f64; 2]) -> [f64; 2] { + [after[0] - before[0], after[1] - before[1]] +} + +/// Are two points the same to within a thousandth of a pixel? The pointer's travel is measured through the view, so +/// the drag's delta is only that exact. +fn same(a: [f64; 2], b: [f64; 2]) -> bool { + (a[0] - b[0]).abs() < 1e-3 && (a[1] - b[1]).abs() < 1e-3 +} + +fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], mods: Modifiers) { + testing::press(app, from, mods); + let mid = [(from[0] + to[0]) / 2.0, (from[1] + to[1]) / 2.0]; + testing::move_to(app, mid, mods); + testing::move_to(app, to, mods); + testing::release(app, to, mods); + // Let go of the keys: a frame keeps the modifiers of the last event, and the Pen's pass reads them to decide + // whether ⌘ still makes the pointer Direct Selection. + testing::modifiers(app, Modifiers::NONE); +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.entries().len() +} + +/// The dragged anchor went to the point the pointer let go, and every other knot of `after` is `before`'s. +fn assert_only_the_dragged_anchor_moved(before: &[[[f64; 2]; 3]], after: &[[[f64; 2]; 3]]) -> [f64; 2] { + let by = delta(before[DRAGGED][0], after[DRAGGED][0]); + assert!(same(by, [RELEASE[0] - ANCHORS[DRAGGED][0], RELEASE[1] - ANCHORS[DRAGGED][1]]), "the anchor followed the pointer to {RELEASE:?}, moved {by:?}"); + for side in 1..3 { + let handle = delta(before[DRAGGED][side], after[DRAGGED][side]); + assert!(same(handle, by), "handle {side} of the dragged knot moved with it by {by:?}, not {handle:?}"); + } + for k in 0..ANCHORS.len() { + if k == DRAGGED { + continue; + } + assert_eq!(after[k], before[k], "knot {k} is exactly what it was: the move solved the curve again"); + } + by +} + +#[test] +fn a_ctrl_drag_of_a_draft_anchor_moves_it_with_its_handles_and_leaves_the_curve_solved_as_it_was() { + let mut app = drawing(); + let before = draft(&app); + drag(&mut app, ANCHORS[DRAGGED], RELEASE, Modifiers::COMMAND); + assert_only_the_dragged_anchor_moved(&before, &draft(&app)); +} + +#[test] +fn a_ctrl_drag_of_a_draft_anchor_adds_no_step_while_the_path_is_a_draft() { + // The Curvature Pen writes its path only at the finish, and its step names are #12752's: the drag is an edit of + // the draft, so it neither adds a step of its own nor writes the document. + let mut app = drawing(); + let (before, path) = (steps(&app), app.session.active().unwrap().doc.work_path.clone()); + drag(&mut app, ANCHORS[DRAGGED], RELEASE, Modifiers::COMMAND); + assert_eq!(steps(&app), before, "the drag of a draft is no step yet"); + assert_eq!(app.session.active().unwrap().doc.work_path, path, "and wrote nothing to the document"); + testing::key(&mut app, egui::Key::Enter, Modifiers::NONE); + assert_eq!(steps(&app), before + 1, "the finish is still the one step it is today"); +} + +#[test] +fn a_plain_drag_of_a_draft_anchor_still_solves_the_whole_curve() { + let mut app = drawing(); + let before = draft(&app); + drag(&mut app, ANCHORS[DRAGGED], RELEASE, Modifiers::NONE); + let after = draft(&app); + assert!(same(after[DRAGGED][0], RELEASE), "the Pen's own drag put the anchor at the release point"); + for k in [DRAGGED - 1, DRAGGED + 1] { + assert_ne!(after[k], before[k], "the neighbouring knot {k} was solved again under the drag"); + } + // The draft is exactly the solve of its own anchors: solving it again changes nothing. + let mut again = app.ui.pen.as_ref().expect("still drawing").draft.clone(); + again.reflow(); + for (k, knot) in again.knots.iter().enumerate() { + let solved = [[knot.anchor.x, knot.anchor.y], [knot.in_ctrl.x, knot.in_ctrl.y], [knot.out_ctrl.x, knot.out_ctrl.y]]; + for side in 0..3 { + assert!(same(after[k][side], solved[side]), "a plain drag left knot {k} part {side} off its own solve"); + } + } +} + +#[test] +fn a_ctrl_drag_of_a_finished_path_moves_the_anchor_with_its_handles_in_one_step() { + let mut app = finished(); + let (before, n) = (work(&app), steps(&app)); + drag(&mut app, ANCHORS[DRAGGED], RELEASE, Modifiers::COMMAND); + assert_only_the_dragged_anchor_moved(&before, &work(&app)); + assert_eq!(steps(&app), n + 1, "the whole drag is one step"); + assert_eq!(app.session.active().unwrap().history.undo_label(), Some("Drag Anchor Point"), "and that step is named as the finished-path drag is today"); +} + +#[test] +fn drawing_on_from_the_last_anchor_keeps_the_moved_anchor_where_the_drag_put_it() { + // The next click of the Curvature Pen solves the handles of the knots it has again, and whether it keeps the + // handles a ⌘/Ctrl-drag gave the moved anchor is one of the cases the issue marks not measured: it stays as it + // is today, so only the anchor's place is pinned here. + let mut app = drawing(); + let before = draft(&app); + drag(&mut app, ANCHORS[DRAGGED], RELEASE, Modifiers::COMMAND); + let moved = draft(&app); + testing::click(&mut app, NEXT, Modifiers::NONE); + let after = draft(&app); + assert_eq!(after.len(), ANCHORS.len() + 1, "the click added the next anchor"); + assert_eq!(after[DRAGGED][0], moved[DRAGGED][0], "the moved anchor stayed where the drag put it"); + assert_eq!(after[DRAGGED + 1][0], before[DRAGGED + 1][0], "and the last anchor did not move"); +} diff --git a/crates/ui-egui/src/tools/vector_ui/host.rs b/crates/ui-egui/src/tools/vector_ui/host.rs index 23c7ad6c5d9..c70f8896a05 100644 --- a/crates/ui-egui/src/tools/vector_ui/host.rs +++ b/crates/ui-egui/src/tools/vector_ui/host.rs @@ -250,9 +250,21 @@ pub(crate) fn remove_last(app: &mut PhotocraftApp) -> bool { /// Pen draft keeps following its anchors. `false` with no draft or while a press is down. Called between passes: a call /// inside a run would reach the stand-in tools the run left in the app. pub(crate) fn replace_draft(app: &mut PhotocraftApp, draft: &EditSubpath) -> bool { + replace_draft_handback(app, draft, true) +} + +/// `replace_draft` for the anchor a ⌘/Ctrl-drag moved: the draft goes back as the gesture left it, so moving one +/// anchor of a Curvature Pen draft does not solve the rest of the curve again (#12757). +pub(crate) fn replace_draft_as_moved(app: &mut PhotocraftApp, draft: &EditSubpath) -> bool { + replace_draft_handback(app, draft, false) +} + +/// The draft hand-back of both routes: `solve` re-solves a Curvature Pen draft's handles, as an edit that changes +/// which anchors there are (or a path the Pen carried on) needs. +fn replace_draft_handback(app: &mut PhotocraftApp, draft: &EditSubpath, solve: bool) -> bool { let Some(pen) = app.ui.pen.as_mut() else { return false }; let mut next = draft.clone(); - if pen.curvature { + if pen.curvature && solve { next.reflow_kept(pen.kept); } pen.draft = next.clone();