From 02ab616249c0da1d56af397fa75b91d5ee7a223f Mon Sep 17 00:00:00 2001 From: Lcoding15 <125505497+Lcoding15@users.noreply.github.com> Date: Sun, 1 Nov 2026 21:11:52 +0100 Subject: [PATCH] Curvature Pen: carry an open path on from its selected endpoint (#10726) After another tool, the Curvature Pen could not continue an open path: a click on its endpoint only selected it and the next click started a new path. As in Photoshop, with an endpoint of an open path selected (a click or Ctrl-click on it) the next click now carries that path on from there, and the commit extends the same subpath in one History step. The resumed knots are kept in PenPath::fixed and curvature_refresh puts them back after each pass, so the existing segments keep their shape; only the endpoint's outgoing handle (and, once closed, the first knot's incoming one) follows the new anchors. Old anchors don't move while drawing on. --- crates/ui-egui/src/pen_variants_tests.rs | 48 +++++++++++++++++++ crates/ui-egui/src/tools/direct_select.rs | 12 +++++ crates/ui-egui/src/tools/vector_ui.rs | 5 ++ .../ui-egui/src/tools/vector_ui/curvature.rs | 34 ++++++++++++- crates/ui-egui/src/tools/vector_ui/pen.rs | 2 +- 5 files changed, 98 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/pen_variants_tests.rs b/crates/ui-egui/src/pen_variants_tests.rs index 4b5cf04d617..d4d4910eab9 100644 --- a/crates/ui-egui/src/pen_variants_tests.rs +++ b/crates/ui-egui/src/pen_variants_tests.rs @@ -210,6 +210,54 @@ fn the_pen_carries_on_an_open_path_from_either_end() { assert_eq!((work.subpaths.len(), work.subpaths[1].closed, work.subpaths[1].knots.len()), (2, true, 3)); } +/// #10726: after another tool, the Curvature Pen carries an open path on as in Photoshop: a click on +/// its endpoint selects it, and the next click continues the path from there. The subpath is +/// extended in one History step, its segments keep their shape, and the new anchors take smooth +/// curvature handles. +#[test] +fn the_curvature_pen_carries_on_an_open_path() { + let mut app = app(Tool::Pen); + click(&mut app, 20.0, 20.0); + // A curve: the middle anchor's handles dragged out further than the Curvature Pen would set. + tool_event(&mut app, ToolEvent::Down { x: 80.0, y: 20.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Move { x: 130.0, y: 10.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 130.0, y: 10.0 }, Modifiers::NONE); + click(&mut app, 80.0, 80.0); + crate::vector_ui::pen_commit(&mut app, false); + let before = work_path(&app).subpaths[0].knots.clone(); + assert_ne!(before[1].out_ctrl, before[1].anchor, "the middle anchor has handles"); + let steps = |app: &PhotocraftApp| app.session.active().unwrap().history.past_len(); + let at = steps(&app); + app.ui.tool = Tool::CurvaturePen; + click(&mut app, 80.0, 80.0); + assert!(app.ui.pen.is_none(), "the click selects the endpoint, it draws nothing"); + click(&mut app, 140.0, 120.0); + assert!(app.ui.pen.as_ref().is_some_and(|p| p.curvature && p.knots.len() == 4), "the next click carries the path on"); + click(&mut app, 200.0, 80.0); + crate::vector_ui::pen_commit(&mut app, false); + let work = work_path(&app); + assert_eq!(work.subpaths.len(), 1, "the same subpath, extended"); + let knots = &work.subpaths[0].knots; + let anchors: Vec<[f64; 2]> = knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect(); + assert_eq!(anchors, [[20.0, 20.0], [80.0, 20.0], [80.0, 80.0], [140.0, 120.0], [200.0, 80.0]]); + assert_eq!(steps(&app), at + 1, "one History step"); + // The Pen's straight segments keep their shape: the first knots are as they were, and the + // endpoint keeps its incoming handle. + assert_eq!((knots[0], knots[1]), (before[0], before[1])); + assert_eq!((knots[2].anchor, knots[2].in_ctrl), (before[2].anchor, before[2].in_ctrl)); + // The new anchor in the middle is smooth: its handles lie either side of it. + let mid = &knots[3]; + assert!(mid.in_ctrl != mid.anchor && mid.out_ctrl != mid.anchor, "{mid:?}"); + assert!((mid.in_ctrl.x - mid.anchor.x) * (mid.out_ctrl.x - mid.anchor.x) < 0.0, "handles on either side: {mid:?}"); + // While it draws on, the Curvature Pen moves only the new anchors: a press on an old corner + // adds an anchor there. + click(&mut app, 200.0, 80.0); + click(&mut app, 260.0, 40.0); + click(&mut app, 80.0, 20.0); + assert_eq!(app.ui.pen.as_ref().map(|p| p.knots.len()), Some(7), "a new anchor, the old one untouched"); + assert_eq!(app.ui.pen.as_ref().map(|p| p.knots[1][0]), Some([80.0, 20.0])); +} + /// #6593 review: carrying on a subpath keeps its path operation, and a draft whose subpath changed /// meanwhile refuses without overwriting newer work. The historical test name is retained /// for reviewed-head provenance; captured-revision validation supersedes its stale fallback. diff --git a/crates/ui-egui/src/tools/direct_select.rs b/crates/ui-egui/src/tools/direct_select.rs index 86df553ddcf..231307e5acf 100644 --- a/crates/ui-egui/src/tools/direct_select.rs +++ b/crates/ui-egui/src/tools/direct_select.rs @@ -241,6 +241,18 @@ fn converting(tool: Tool, mods: Modifiers, pen_idle: bool) -> bool { !mods.command && (tool == Tool::ConvertPoint || (tool == Tool::Pen && mods.alt && pen_idle)) } +/// The selected anchor when it is the only one and an endpoint of an open subpath of the active +/// path: where the Curvature Pen carries the path on from (#10726), as Photoshop does after a +/// click, or ⌘/Ctrl-click, on the last point. +pub(crate) fn selected_open_end(app: &PhotocraftApp) -> Option<[f64; 2]> { + let ds = &app.ui.direct_selection; + let ([[s, k]], Some(target)) = (ds.anchors.as_slice(), ds.target) else { return None }; + let (_, path) = active_paths(app).into_iter().find(|(r, _)| *r == target)?; + let sub = path.subpaths.get(*s).filter(|sub| !sub.closed)?; + let knot = sub.knots.get(*k).filter(|_| *k == 0 || *k + 1 == sub.knots.len())?; + Some([knot.anchor.x, knot.anchor.y]) +} + /// Is the Curvature Pen idle (no path being drawn, no ⌘/Ctrl)? Then it edits the active path in /// place, as in Photoshop: a press on one of its anchors drags it, with no modifier. fn curvature_idle(app: &PhotocraftApp, mods: Modifiers) -> bool { diff --git a/crates/ui-egui/src/tools/vector_ui.rs b/crates/ui-egui/src/tools/vector_ui.rs index 72ec518f819..5db7968763a 100644 --- a/crates/ui-egui/src/tools/vector_ui.rs +++ b/crates/ui-egui/src/tools/vector_ui.rs @@ -100,6 +100,11 @@ pub struct PenPath { /// commit replaces that subpath instead of adding one. #[serde(default)] pub resumes: Option, + /// The Curvature Pen carrying on a path: its knots as they were, which keep their shape while + /// the new anchors' handles follow them (`curvature_refresh`, #10726). The last one is the + /// endpoint drawing carries on from; only its outgoing handle bends into the new segment. + #[serde(default)] + pub fixed: Vec<[[f64; 2]; 3]>, } /// The subpath a Pen path carries on: where it is and what it was when drawing resumed. The diff --git a/crates/ui-egui/src/tools/vector_ui/curvature.rs b/crates/ui-egui/src/tools/vector_ui/curvature.rs index ebd62becdbf..d09dc6947a6 100644 --- a/crates/ui-egui/src/tools/vector_ui/curvature.rs +++ b/crates/ui-egui/src/tools/vector_ui/curvature.rs @@ -1,11 +1,27 @@ -use super::pen::{cancel_stale, pen_commit}; +use super::pen::{cancel_stale, pen_commit, resume_at}; use super::{PenPath, PhotocraftApp}; -/// Recompute a Curvature Pen path's handles from its anchors and corner points. +/// Recompute a Curvature Pen path's handles from its anchors and corner points. The knots of a +/// path it carries on (`PenPath::fixed`) keep theirs, but for the endpoint's outgoing handle, +/// which bends into the first new segment (#10726), and, once the path closes, the first knot's +/// incoming handle, which bends into the closing one. pub(super) fn curvature_refresh(pen: &mut PenPath, closed: bool) { let anchors: Vec<[f64; 2]> = pen.knots.iter().map(|k| k[0]).collect(); let corners: Vec = (0..anchors.len()).map(|i| pen.unlinked.contains(&i)).collect(); pen.knots = photocraft_vector::edit::curvature_knots(&anchors, &corners, closed); + let last = pen.fixed.len().saturating_sub(1); + let extended = pen.knots.len() > pen.fixed.len(); + for (i, was) in pen.fixed.iter().enumerate() { + let Some(k) = pen.knots.get_mut(i) else { break }; + let (keep_in, keep_out) = (!(closed && i == 0), !(extended && i == last)); + k[0] = was[0]; + if keep_in { + k[1] = was[1]; + } + if keep_out { + k[2] = was[2]; + } + } } /// The anchor of the Curvature Pen path being drawn under (x, y), for the cursor. @@ -44,6 +60,20 @@ pub fn curvature_down(app: &mut PhotocraftApp, x: f64, y: f64) { if app.ui.pen.is_none() { app.ui.pen_doc = app.session.active().map(|st| st.doc.id); } + // With an endpoint of an open path selected (a click, or ⌘/Ctrl-click, on it), the press + // carries that path on from there, as in Photoshop (#10726): its segments keep their shape, + // the press adds the next anchor and the commit extends that subpath. + if app.ui.pen.as_ref().is_none_or(|p| p.knots.is_empty()) + && let Some(end) = crate::direct_select::selected_open_end(app) + && let Some(mut resumed) = resume_at(app, end[0], end[1], 0.5) + { + resumed.curvature = true; + resumed.fixed = resumed.knots.clone(); + app.ui.pen = Some(resumed); + } + // The anchors of the path carried on stay where they are; the new ones move. + let fixed = app.ui.pen.as_ref().map_or(0, |p| p.fixed.len()); + let hit = hit.filter(|&i| i >= fixed); let Some(owner) = capture_pen_owner(app) else { return }; let pen = app.ui.pen.get_or_insert_with(|| PenPath { curvature: true, ..Default::default() }); if pen.owner.is_none() { diff --git a/crates/ui-egui/src/tools/vector_ui/pen.rs b/crates/ui-egui/src/tools/vector_ui/pen.rs index baa536f3b87..bf5a742b866 100644 --- a/crates/ui-egui/src/tools/vector_ui/pen.rs +++ b/crates/ui-egui/src/tools/vector_ui/pen.rs @@ -479,7 +479,7 @@ fn doc_path<'a>(doc: &'a Document, name: &str) -> Option<&'a Path> { /// layer, else of the path a new Pen path would join (the one selected in the Paths panel, else /// the work path). Its knots run towards that anchor: reversed, handles swapped, when it is the /// first. -fn resume_at(app: &PhotocraftApp, x: f64, y: f64, tol: f64) -> Option { +pub(super) fn resume_at(app: &PhotocraftApp, x: f64, y: f64, tol: f64) -> Option { let st = app.session.active()?; let doc = st.doc.id.0; let (name, layer, path) = if let Some((id, p)) = targeted_vector_mask(app) {