From dedda70ab5fafb8f484c10363dc22c7269e80346 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 19:40:33 +0000 Subject: [PATCH] #10726: carry on the selected subpath, from the path the Pen draws on, and keep a smooth endpoint smooth Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GbuSKAS2k5248LGFEapMUR --- crates/ui-egui/src/pen_variants_tests.rs | 69 ++++++++++++++ crates/ui-egui/src/tools/direct_select.rs | 8 +- .../ui-egui/src/tools/vector_ui/curvature.rs | 27 +++++- crates/ui-egui/src/tools/vector_ui/pen.rs | 94 +++++++++++++------ 4 files changed, 161 insertions(+), 37 deletions(-) diff --git a/crates/ui-egui/src/pen_variants_tests.rs b/crates/ui-egui/src/pen_variants_tests.rs index d4d4910eab9..72d4ff07369 100644 --- a/crates/ui-egui/src/pen_variants_tests.rs +++ b/crates/ui-egui/src/pen_variants_tests.rs @@ -258,6 +258,75 @@ fn the_curvature_pen_carries_on_an_open_path() { assert_eq!(app.ui.pen.as_ref().map(|p| p.knots[1][0]), Some([80.0, 20.0])); } +/// The work path set to `subpaths`, in one step. +fn set_work_path(app: &mut PhotocraftApp, subpaths: Vec) { + app.session + .edit("path", |doc, _| { + doc.work_path = Some(Path::new(subpaths)); + Ok(()) + }) + .unwrap(); +} + +/// Direct Selection's selection: anchor `[subpath, knot]` of `target`, alone. +fn select_anchor(app: &mut PhotocraftApp, target: crate::direct_select::PathRef, anchor: [usize; 2]) { + app.ui.direct_selection.target = Some(target); + app.ui.direct_selection.anchors = vec![anchor]; +} + +/// CodeRabbit on #10744: two open subpaths end on the same point, and the Curvature Pen carries on +/// the one whose endpoint is selected, not the first one ending there. +#[test] +fn the_curvature_pen_carries_on_the_selected_subpath_of_two_that_end_together() { + use photocraft_doc::vector::Subpath; + let mut app = app(Tool::CurvaturePen); + set_work_path(&mut app, vec![Subpath::polyline(&[(20.0, 20.0), (80.0, 80.0)]), Subpath::polyline(&[(200.0, 20.0), (80.0, 80.0)])]); + select_anchor(&mut app, crate::direct_select::PathRef::Work, [1, 1]); + click(&mut app, 140.0, 150.0); + assert_eq!(app.ui.pen.as_ref().and_then(|p| p.resumes.as_ref()).map(|r| r.index), Some(1), "the selected subpath"); + crate::vector_ui::pen_commit(&mut app, false); + let work = work_path(&app); + assert_eq!(work.subpaths.iter().map(|s| s.knots.len()).collect::>(), [2, 3], "the second subpath grew"); +} + +/// CodeRabbit on #10744: a smooth endpoint carried on keeps its incoming handle, and its outgoing +/// one, bent into the new segment, stays in line with it: the knot stays smooth. +#[test] +fn a_smooth_endpoint_carried_on_stays_smooth() { + use photocraft_doc::vector::{Knot, Point, Subpath}; + let mut app = app(Tool::CurvaturePen); + let end = Knot::smooth(Point::new(80.0, 80.0), Point::new(60.0, 60.0), Point::new(100.0, 100.0)); + set_work_path(&mut app, vec![Subpath { closed: false, knots: vec![Knot::corner(20.0, 20.0), end], op: Default::default() }]); + select_anchor(&mut app, crate::direct_select::PathRef::Work, [0, 1]); + click(&mut app, 140.0, 40.0); + let knots = app.ui.pen.as_ref().map(|p| p.knots.clone()).expect("the path is carried on"); + let [anchor, in_ctrl, out_ctrl] = knots[1]; + assert_eq!(in_ctrl, [60.0, 60.0], "the incoming handle is kept"); + let (a, b) = ([anchor[0] - in_ctrl[0], anchor[1] - in_ctrl[1]], [out_ctrl[0] - anchor[0], out_ctrl[1] - anchor[1]]); + assert!(b[0].hypot(b[1]) > 1.0, "the outgoing handle bends into the new segment: {out_ctrl:?}"); + assert!((a[0] * b[1] - a[1] * b[0]).abs() < 1e-6 && a[0] * b[0] + a[1] * b[1] > 0.0, "in line: {knots:?}"); +} + +/// CodeRabbit on #10744: in Path mode, an endpoint selected on a shape layer is not one a Pen path +/// carries on, even with the work path ending on the same point: the press starts a new path. +#[test] +fn an_endpoint_on_another_path_than_the_pen_draws_on_starts_a_new_one() { + use photocraft_doc::vector::Subpath; + let mut app = app(Tool::Pen); + app.ui.tool_options.vector_mode = "shape".into(); + click(&mut app, 20.0, 20.0); + click(&mut app, 80.0, 80.0); + crate::vector_ui::pen_commit(&mut app, false); + let shape = app.session.active().and_then(|st| st.active_layer).expect("a shape layer").0; + app.ui.tool_options.vector_mode = "path".into(); + set_work_path(&mut app, vec![Subpath::polyline(&[(200.0, 20.0), (80.0, 80.0)])]); + select_anchor(&mut app, crate::direct_select::PathRef::Layer(shape), [0, 1]); + app.ui.tool = Tool::CurvaturePen; + click(&mut app, 140.0, 150.0); + let pen = app.ui.pen.as_ref().expect("a new path"); + assert!(pen.resumes.is_none() && pen.knots.len() == 1, "{pen:?}"); +} + /// #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 231307e5acf..ba8ab3b8ac3 100644 --- a/crates/ui-egui/src/tools/direct_select.rs +++ b/crates/ui-egui/src/tools/direct_select.rs @@ -243,14 +243,14 @@ fn converting(tool: Tool, mods: Modifiers, pen_idle: bool) -> bool { /// 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]> { +/// click, or ⌘/Ctrl-click, on the last point. The path, the subpath's index and the knot's. +pub(crate) fn selected_open_end(app: &PhotocraftApp) -> Option<(PathRef, usize, usize)> { 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]) + sub.knots.get(*k).filter(|_| *k == 0 || *k + 1 == sub.knots.len())?; + Some((target, *s, *k)) } /// Is the Curvature Pen idle (no path being drawn, no ⌘/Ctrl)? Then it edits the active path in diff --git a/crates/ui-egui/src/tools/vector_ui/curvature.rs b/crates/ui-egui/src/tools/vector_ui/curvature.rs index d09dc6947a6..86951ab4b24 100644 --- a/crates/ui-egui/src/tools/vector_ui/curvature.rs +++ b/crates/ui-egui/src/tools/vector_ui/curvature.rs @@ -1,10 +1,11 @@ -use super::pen::{cancel_stale, pen_commit, resume_at}; +use super::pen::{cancel_stale, pen_commit, resume_selected}; use super::{PenPath, PhotocraftApp}; /// 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. +/// incoming handle, which bends into the closing one. A smooth knot's bent handle stays in line +/// with the one it keeps, so the knot stays smooth. 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(); @@ -14,6 +15,7 @@ pub(super) fn curvature_refresh(pen: &mut PenPath, closed: bool) { 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)); + let smooth = !pen.unlinked.contains(&i); k[0] = was[0]; if keep_in { k[1] = was[1]; @@ -21,9 +23,27 @@ pub(super) fn curvature_refresh(pen: &mut PenPath, closed: bool) { if keep_out { k[2] = was[2]; } + if smooth && keep_in != keep_out { + let (kept, bent) = if keep_in { (1, 2) } else { (2, 1) }; + if let Some(h) = in_line(k[0], k[kept], k[bent]) { + k[bent] = h; + } + } } } +/// A handle as long as `bent` from `anchor`, on the far side of it from `kept`: `None` when +/// `kept` lies on the anchor and gives no direction. +fn in_line(anchor: [f64; 2], kept: [f64; 2], bent: [f64; 2]) -> Option<[f64; 2]> { + let (dx, dy) = (anchor[0] - kept[0], anchor[1] - kept[1]); + let d = dx.hypot(dy); + if !(d > 1e-9 && d.is_finite()) { + return None; + } + let len = (bent[0] - anchor[0]).hypot(bent[1] - anchor[1]); + len.is_finite().then(|| [anchor[0] + dx / d * len, anchor[1] + dy / d * len]) +} + /// The anchor of the Curvature Pen path being drawn under (x, y), for the cursor. pub(crate) fn curvature_anchor_at(app: &PhotocraftApp, x: f64, y: f64) -> Option { curvature_hit(app, x, y) @@ -64,8 +84,7 @@ pub fn curvature_down(app: &mut PhotocraftApp, x: f64, y: f64) { // 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) + && let Some(mut resumed) = resume_selected(app) { resumed.curvature = true; resumed.fixed = resumed.knots.clone(); diff --git a/crates/ui-egui/src/tools/vector_ui/pen.rs b/crates/ui-egui/src/tools/vector_ui/pen.rs index bf5a742b866..42936af66e6 100644 --- a/crates/ui-egui/src/tools/vector_ui/pen.rs +++ b/crates/ui-egui/src/tools/vector_ui/pen.rs @@ -474,28 +474,47 @@ fn doc_path<'a>(doc: &'a Document, name: &str) -> Option<&'a Path> { if name == "work" { doc.work_path.as_ref() } else { doc.paths.iter().find(|p| p.name == name).map(|p| &p.path) } } -/// The open subpath that has its first or last anchor at `(x, y)`, as a Pen path to carry on -/// drawing from there (#6593): of a targeted vector mask, else in Shape mode of the selected shape -/// 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. -pub(super) fn resume_at(app: &PhotocraftApp, x: f64, y: f64, tol: f64) -> Option { +/// The path a Pen path carries on (#6593): a targeted vector mask, else in Shape mode the +/// selected shape layer's, else the path a new Pen path would join (the one selected in the Paths +/// panel, else the work path). Its Paths-panel name ("layer" for a layer's), layer and path. +fn resume_target(app: &PhotocraftApp) -> Option<(String, Option, Path)> { let st = app.session.active()?; - let doc = st.doc.id.0; - let (name, layer, path) = if let Some((id, p)) = targeted_vector_mask(app) { - ("layer".to_string(), Some(id), p) - } else if app.ui.tool_options.vector_mode == "shape" { + if let Some((id, p)) = targeted_vector_mask(app) { + return Some(("layer".to_string(), Some(id), p)); + } + if app.ui.tool_options.vector_mode == "shape" { let (id, p) = active_shape_path(app)?; - ("layer".to_string(), Some(id), p) - } else { - let name = app.ui.selected_path.clone().filter(|n| path_exists(&st.doc, n)).unwrap_or_else(|| "work".into()); - // A Work Path hidden by Make Selection isn't carried on: a new path replaces it (#6079). - if name == "work" && !work_path_shown(app) { - return None; + return Some(("layer".to_string(), Some(id), p)); + } + let name = app.ui.selected_path.clone().filter(|n| path_exists(&st.doc, n)).unwrap_or_else(|| "work".into()); + // A Work Path hidden by Make Selection isn't carried on: a new path replaces it (#6079). + if name == "work" && !work_path_shown(app) { + return None; + } + let p = doc_path(&st.doc, &name)?.clone(); + Some((name, None, p)) +} + +/// Subpath `index` of the path a Pen path carries on, as a Pen path to carry on drawing from its +/// last anchor, or from its first when `reversed`: then its knots run backwards, handles swapped. +fn resumed(doc: u64, name: String, layer: Option, index: usize, sub: &photocraft_doc::vector::Subpath, reversed: bool) -> PenPath { + let mut knots: Vec<_> = sub.knots.iter().map(|k| (knot_points(k), k.smooth)).collect(); + if reversed { + knots.reverse(); + for (k, _) in &mut knots { + k.swap(1, 2); } - let p = doc_path(&st.doc, &name)?.clone(); - (name, None, p) - }; + } + let unlinked = knots.iter().enumerate().filter(|(_, (_, smooth))| !smooth).map(|(i, _)| i).collect(); + let resumes = Resumed { doc, path: name, layer, index, was: sub.clone() }; + PenPath { knots: knots.into_iter().map(|(k, _)| k).collect(), unlinked, resumes: Some(resumes), ..Default::default() } +} + +/// The open subpath that has its first or last anchor at `(x, y)` in the path a Pen path carries +/// on ([`resume_target`]), as a Pen path to carry on drawing from there (#6593). +pub(super) fn resume_at(app: &PhotocraftApp, x: f64, y: f64, tol: f64) -> Option { + let doc = app.session.active()?.doc.id.0; + let (name, layer, path) = resume_target(app)?; let near = |k: &photocraft_doc::Knot| (k.anchor.x - x).hypot(k.anchor.y - y) < tol; path.subpaths.iter().enumerate().filter(|(_, s)| !s.closed).find_map(|(i, s)| { let reversed = if s.knots.last().is_some_and(near) { @@ -505,19 +524,36 @@ pub(super) fn resume_at(app: &PhotocraftApp, x: f64, y: f64, tol: f64) -> Option } else { return None; }; - let mut knots: Vec<_> = s.knots.iter().map(|k| (knot_points(k), k.smooth)).collect(); - if reversed { - knots.reverse(); - for (k, _) in &mut knots { - k.swap(1, 2); - } - } - let unlinked = knots.iter().enumerate().filter(|(_, (_, smooth))| !smooth).map(|(i, _)| i).collect(); - let resumes = Resumed { doc, path: name.clone(), layer, index: i, was: s.clone() }; - Some(PenPath { knots: knots.into_iter().map(|(k, _)| k).collect(), unlinked, resumes: Some(resumes), ..Default::default() }) + Some(resumed(doc, name.clone(), layer, i, s, reversed)) }) } +/// The endpoint Direct Selection has selected ([`crate::direct_select::selected_open_end`]), as +/// a Pen path carrying its very subpath on from it (#10726). `None` unless that endpoint is in +/// the path a Pen path carries on ([`resume_target`]): one in another path starts a new one. +pub(super) fn resume_selected(app: &PhotocraftApp) -> Option { + let (target, index, knot) = crate::direct_select::selected_open_end(app)?; + let st = app.session.active()?; + let (name, layer, path) = resume_target(app)?; + let carried = match layer { + Some(id) => crate::direct_select::PathRef::Layer(id), + None if name == "work" => crate::direct_select::PathRef::Work, + None => crate::direct_select::PathRef::Saved(st.doc.paths.iter().position(|p| p.name == name)?), + }; + if carried != target { + return None; + } + let sub = path.subpaths.get(index).filter(|s| !s.closed)?; + let reversed = if knot + 1 == sub.knots.len() { + false + } else if knot == 0 { + true + } else { + return None; + }; + Some(resumed(st.doc.id.0, name, layer, index, sub, reversed)) +} + /// The resumed path with its subpath carried on: the one subpath of `drawn` takes the old one's /// anchors and closing, which keeps its other settings (its path operation). `None` when the /// subpath is no longer the one drawing resumed (another document, or the path changed).