Merge pull request #2285 from davidmann4/anchor/12707-drag-handles

Add Anchor: a drag from a segment pulls out the new anchor's handles (photocraft#12707)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-20 00:42:48 +01:00
commit 2004470919
4 files changed
+218 -16

No files matched your search

+85 -13
View File
@@ -71,14 +71,16 @@ pub enum BezierTool {
Convert,
/// Anchors drawn, not edited (PhotoCraft's Path Selection and Pen tools).
Anchors,
/// Add Anchor Point: a press on a segment, within 5 screen points of it, inserts an anchor at the hit's `t`
/// ([`PathEdit::InsertAnchor`]); a drag moves the new anchor, which is selected. A press on an anchor or a shown
/// handle selects and drags it as [`BezierTool::Direct`] does (⇧ adds to the selection, ⌥ on a handle moves it
/// alone), and a click on empty canvas deselects the anchors and keeps the path. With ⌥ a click on a segment selects
/// its whole subpath and a drag moves a copy of it ([`PathEdit::DuplicateSubpath`]; the subpath itself on a path that
/// takes no copies), and a press on an anchor does nothing. Hover: the segment mark with the anchor a click adds
/// ([`HitMark`]) and the pen with "+"; over an anchor the arrow; with ⌥ no mark and the arrow with "+"; the
/// crosshair under the canvas's precise cursor ([`CanvasSpace::precise`]).
/// Add Anchor Point: a click on a segment, within 5 screen points of it, inserts an anchor at the hit's `t`
/// ([`PathEdit::InsertAnchor`]); a drag inserts it and pulls out its handles as the Pen does
/// ([`PathEdit::PullAnchor`]: ⇧ turns them to the nearest 45°, ⌥ during the drag breaks the pair); the new anchor is
/// selected. A press on an anchor or a shown handle selects and drags it as [`BezierTool::Direct`] does (⇧ adds to
/// the selection, ⌥ on a handle moves it alone), and a click on empty canvas deselects the anchors and keeps the
/// path. With ⌥ held at the press a click on a segment selects its whole subpath and a drag moves a copy of it
/// ([`PathEdit::DuplicateSubpath`]; the subpath itself on a path that takes no copies), and a press on an anchor does
/// nothing. Hover: the segment mark with the anchor a click adds ([`HitMark`]) and the pen with "+"; over an anchor
/// the arrow; with ⌥ no mark and the arrow with "+"; the crosshair under the canvas's precise cursor
/// ([`CanvasSpace::precise`]).
AddAnchor,
/// Delete Anchor Point: a click on an anchor reports [`delete`](BezierOutput::delete) of that anchor; ⌥ makes it
/// Add Anchor Point, except that a ⌥ click on an anchor selects its whole subpath. Hover: the anchor mark.
@@ -241,6 +243,17 @@ enum Target {
t: f64,
from: Point,
},
/// Add Anchor's drag from a segment: an insert into `segment` at `t`, whose anchor stays at `from`, the point of
/// the curve at `t`, and whose handles follow the pointer. `out` is the out handle of the last pass, and
/// `in_ctrl` the in handle ⌥ left once it broke the pair.
Pull {
path: u64,
segment: KnotRef,
t: f64,
from: Point,
out: Point,
in_ctrl: Option<Point>,
},
/// Direct Selection's ⌥ drag on a segment: a copy of `subpath` dragged from `from`, the grabbed point.
Duplicate {
path: u64,
@@ -595,6 +608,12 @@ impl<'a> BezierOverlay<'a> {
match hit {
// Add Anchor with ⌥ takes the whole subpath as Direct's ⌥ does (PhotoCraft #12709).
BezierHit::Segment { .. } if self.tool.takes_subpath(mods) => self.target(hit, start, mods, paths),
// Add Anchor's drag pulls out the new anchor's handles (PhotoCraft #12707); Auto Add/Delete's, and Delete
// Anchor's with ⌥, moves it.
BezierHit::Segment { path, segment, t } if self.tool == BezierTool::AddAnchor && self.tool.adds(mods) => {
let from = Self::segment_point(paths, path, segment, t);
Target::Pull { path, segment, t, from, out: from, in_ctrl: None }
}
BezierHit::Segment { path, segment, t } if self.tool.adds(mods) => {
Target::Insert { path, segment, t, from: Self::segment_point(paths, path, segment, t) }
}
@@ -719,11 +738,40 @@ impl<'a> BezierOverlay<'a> {
PathEdit::InsertCurvature { path, segment, t, by: snapped(from + locked(delta)) - from }
}
&Target::Insert { path, segment, t, from } => PathEdit::InsertAnchor { path, segment, t, by: snapped(from + locked(delta)) - from },
&Target::Pull { path, segment, t, from, in_ctrl, .. } => {
let to = from + delta;
let out = if mods.shift { handle_45(from, to) } else { snapped(to) };
PathEdit::PullAnchor { path, segment, t, out, in_ctrl }
}
&Target::Duplicate { path, subpath, from } => PathEdit::DuplicateSubpath { path, subpath, by: snapped(from + locked(delta)) - from },
Target::Nothing | Target::Toggle { .. } | Target::Pick { .. } | Target::Marquee => return None,
})
}
/// [`edit`](Self::edit), keeping what a [`Target::Pull`] carries from pass to pass: ⌥ pressed during the drag
/// leaves the in handle where the last pass had it, and the out handle is remembered for the next pass.
fn edit_pulled(
view: &CanvasView,
snap: Option<&Snap>,
tool: BezierTool,
target: &mut Target,
origin: Pos2,
pointer: Pos2,
mods: Mods,
) -> Option<PathEdit> {
if let Target::Pull { from, out, in_ctrl, .. } = target
&& mods.alt
&& in_ctrl.is_none()
{
*in_ctrl = Some(*from - (*out - *from));
}
let edit = Self::edit(view, snap, tool, target, origin, pointer, mods);
if let (Target::Pull { out, .. }, Some(PathEdit::PullAnchor { out: pulled, .. })) = (target, &edit) {
*out = *pulled;
}
edit
}
/// Proposes `paths` with `edit` applied to the paths the press began on.
fn propose(&mut self, cx: &mut Cx<'_, Self>, start: &[EditPath], edit: PathEdit) {
let mut next = start.to_vec();
@@ -896,7 +944,9 @@ impl<'a> BezierOverlay<'a> {
let moves = !matches!(held.target, Target::Nothing | Target::Toggle { .. } | Target::Pick { .. });
if first && moves {
held.began = true;
if let Target::CurvatureInsert { path, segment, .. } | Target::Insert { path, segment, .. } = held.target {
if let Target::CurvatureInsert { path, segment, .. } | Target::Insert { path, segment, .. } | Target::Pull { path, segment, .. } =
held.target
{
self.report.select = Some(self.inserted(path, segment));
}
cx.begin();
@@ -910,7 +960,7 @@ impl<'a> BezierOverlay<'a> {
}
}
_ => {
if let Some(edit) = Self::edit(view, snap, self.tool, &held.target, start.origin, pos, mods) {
if let Some(edit) = Self::edit_pulled(view, snap, self.tool, &mut held.target, start.origin, pos, mods) {
self.propose(cx, &held.start, edit);
}
}
@@ -938,7 +988,8 @@ impl<'a> BezierOverlay<'a> {
return;
}
if held.began {
if let Some(edit) = Self::edit(view, snap, self.tool, &held.target, start.origin, pos, mods) {
let mut target = held.target.clone();
if let Some(edit) = Self::edit_pulled(view, snap, self.tool, &mut target, start.origin, pos, mods) {
self.propose(cx, &held.start, edit);
}
// The copy is what was dragged: it ends up selected, not the source.
@@ -955,7 +1006,7 @@ impl<'a> BezierOverlay<'a> {
self.set(cx, &held.start, PathEdit::InsertCurvature { path, segment, t, by: Vec2::ZERO });
self.report.select = Some(self.inserted(path, segment));
}
Target::Insert { path, segment, t, .. } => {
Target::Insert { path, segment, t, .. } | Target::Pull { path, segment, t, .. } => {
self.set(cx, &held.start, PathEdit::InsertAnchor { path, segment, t, by: Vec2::ZERO });
self.report.select = Some(self.inserted(path, segment));
}
@@ -1127,6 +1178,27 @@ fn nudge_vector(view: &CanvasView, direction: egui::Vec2, distance: f64) -> Vec2
if length > 0.0 { doc / length * distance } else { Vec2::ZERO }
}
/// ⇧ on a handle pulled out of the anchor at `anchor` to `to`, as the reference application turns it: the nearest of
/// the eight 45° directions, an axis keeping the drag's offset along it and a diagonal the shorter of the two offsets
/// on both axes. `to` itself for a zero or non-finite offset.
fn handle_45(anchor: Point, to: Point) -> Point {
let d = to - anchor;
if !d.x.is_finite() || !d.y.is_finite() || d == Vec2::ZERO {
return to;
}
let (ax, ay) = (d.x.abs(), d.y.abs());
// tan 22.5°: nearer the axis than the diagonal.
let tan = std::f64::consts::FRAC_PI_8.tan();
if ay <= ax * tan {
Point::new(to.x, anchor.y)
} else if ax <= ay * tan {
Point::new(anchor.x, to.y)
} else {
let shorter = ax.min(ay);
anchor + Vec2::new(shorter.copysign(d.x), shorter.copysign(d.y))
}
}
fn contains(r: DocRect, p: Point) -> bool {
(r.x0..=r.x1).contains(&p.x) && (r.y0..=r.y1).contains(&p.y)
}
@@ -1145,7 +1217,7 @@ pub struct BezierOutput {
pub select: Option<PathSelection>,
/// Add an anchor to the segment leaving a knot of a path, at `t` (PhotoCraft's Add Anchor Point): Insert on a
/// picked anchor (`t` 0.5). A click or drag of an anchor tool on a segment is the [`change`](Self::change)
/// [`PathEdit::InsertAnchor`] instead.
/// [`PathEdit::InsertAnchor`] instead ([`PathEdit::PullAnchor`] for Add Anchor's drag).
pub insert: Option<(u64, KnotRef, f64)>,
/// Delete or Backspace with anchors selected under a tool that deletes (Direct, Convert, Curvature, Add Anchor,
/// Delete Anchor): remove these anchors of a path (Delete Anchor Point), `segments` true; a Delete Anchor click
+23
View File
@@ -348,6 +348,22 @@ pub enum PathEdit {
/// The move of the new anchor, in document units.
by: Vec2,
},
/// Add Anchor's drag from a segment: add an anchor to the segment leaving `segment` at `t` and pull out its
/// handles, as the Pen does: the out handle at `out` and the in handle opposite it through the anchor, a smooth
/// knot; once Alt broke the pair, the in handle at `in_ctrl` and a corner knot.
#[non_exhaustive]
PullAnchor {
/// The path's host key.
path: u64,
/// The anchor the segment leaves.
segment: KnotRef,
/// Position along the segment, 0..=1.
t: f64,
/// The new anchor's out handle.
out: Point,
/// The new anchor's in handle once Alt broke the pair; `None` mirrors `out`.
in_ctrl: Option<Point>,
},
/// The Curvature tool's double-click or Enter on an anchor: a smooth knot becomes a corner
/// with retracted handles and a corner becomes smooth, then the subpath reflows.
#[non_exhaustive]
@@ -425,6 +441,12 @@ impl PathEdit {
PathEdit::InsertAnchor { path, segment, t, by }
}
/// Add Anchor's insert into the segment leaving `segment` at `t` with the new anchor's handles pulled out to `out`
/// and mirrored, or with the in handle at `in_ctrl`.
pub fn pull_anchor(path: u64, segment: KnotRef, t: f64, out: Point, in_ctrl: Option<Point>) -> Self {
PathEdit::PullAnchor { path, segment, t, out, in_ctrl }
}
/// The Curvature tool's toggle of `knot` between smooth and corner.
pub fn toggle_curvature(path: u64, knot: KnotRef) -> Self {
PathEdit::ToggleCurvature { path, knot }
@@ -445,6 +467,7 @@ impl PathEdit {
| PathEdit::MoveCurvature { path, .. }
| PathEdit::InsertCurvature { path, .. }
| PathEdit::InsertAnchor { path, .. }
| PathEdit::PullAnchor { path, .. }
| PathEdit::ToggleCurvature { path, .. }
| PathEdit::DuplicateSubpath { path, .. } => path,
}
+15
View File
@@ -137,6 +137,9 @@ impl PathEdit {
/// `by`, then the subpath reflowed.
/// - `InsertAnchor`: [`EditSubpath::insert_at`], the new knot with its handles moved by `by`; no
/// reflow and no smoothing, so a straight segment gets a corner knot.
/// - `PullAnchor`: [`EditSubpath::insert_at`], then the new knot's out handle at `out` and its in
/// handle at `in_ctrl` (a corner), or mirrored through the anchor (smooth). Nothing changes when
/// a handle is not finite.
/// - `ToggleCurvature`: a smooth knot becomes a corner with retracted handles, a corner
/// becomes smooth, then the subpath reflows.
/// - `DuplicateSubpath`: a copy of the subpath goes last and moves, with its handles, by `by`;
@@ -203,6 +206,18 @@ impl PathEdit {
k.in_ctrl += *by;
k.out_ctrl += *by;
}
PathEdit::PullAnchor { segment, t, out, in_ctrl, .. } => {
let finite = |p: &Point| p.x.is_finite() && p.y.is_finite();
if !finite(out) || !in_ctrl.as_ref().is_none_or(finite) {
return false;
}
let Some(sp) = path.subpaths.get_mut(segment.subpath) else { return false };
let Some(at) = sp.insert_at(segment.knot, *t) else { return false };
let k = &mut sp.knots[at];
k.out_ctrl = *out;
k.in_ctrl = in_ctrl.unwrap_or(k.anchor - (*out - k.anchor));
k.smooth = in_ctrl.is_none();
}
PathEdit::Convert { knot, out, .. } => {
if !knot_ok(path, *knot) {
return false;
+95 -3
View File
@@ -73,6 +73,30 @@ fn edit_subpath_insert_at_splits_a_curve_without_changing_it() {
// MoveAnchors, MoveHandle (smooth and alone), BendSegment (straight and curved), Convert give PhotoCraft's kernel
// results; a wrong key or index changes nothing and returns false.
#[trial]
fn path_edit_pull_anchor_inserts_and_pulls_out_the_handles() {
let straight = || ops_path(EditSubpath::new(vec![Knot::corner(p(0.0, 0.0)), Knot::corner(p(100.0, 0.0))], false));
let mut path = straight();
assert!(PathEdit::pull_anchor(7, kr(0), 0.5, p(50.0, 30.0), None).apply(&mut path));
let k = path.subpaths[0].knots[1];
assert!(near(k.anchor, p(50.0, 0.0)) && near(k.out_ctrl, p(50.0, 30.0)) && near(k.in_ctrl, p(50.0, -30.0)) && k.smooth, "{k:?}");
let mut path = straight();
assert!(PathEdit::pull_anchor(7, kr(0), 0.5, p(50.0, 30.0), Some(p(80.0, 0.0))).apply(&mut path));
let k = path.subpaths[0].knots[1];
assert!(near(k.in_ctrl, p(80.0, 0.0)) && !k.smooth, "{k:?}");
for bad in [
PathEdit::pull_anchor(7, kr(0), 0.5, p(f64::NAN, 0.0), None),
PathEdit::pull_anchor(7, kr(0), 0.5, p(1.0, 1.0), Some(p(0.0, f64::INFINITY))),
PathEdit::pull_anchor(7, kr(0), 1.5, p(1.0, 1.0), None),
PathEdit::pull_anchor(7, kr(4), 0.5, p(1.0, 1.0), None),
PathEdit::pull_anchor(7, KnotRef::new(3, 0), 0.5, p(1.0, 1.0), None),
] {
let mut path = straight();
assert!(!bad.apply(&mut path), "{bad:?}");
assert_eq!(path, straight());
}
}
#[trial]
fn path_edit_apply_matches_the_kernel_for_every_existing_edit() {
let base = || {
@@ -417,12 +441,13 @@ fn cmd_or_ctrl_with_page_down_on_a_focused_anchor_scrolls_the_canvas_and_picks_n
assert_eq!(h.state.borrow().selection, picked, "the page key picks no part");
}
// Add Anchor on a segment: a press, a drag and a release are one gesture that inserts the anchor and moves it, with the
// new anchor selected; a click inserts it where the segment was pressed and moves nothing.
// The Pen's Auto Add/Delete on a segment: a press, a drag and a release are one gesture that inserts the anchor and
// moves it, with the new anchor selected. Add Anchor's click inserts it where the segment was pressed and moves
// nothing (its drag pulls out handles: `an_add_anchor_drag_pulls_out_the_new_anchors_handles`).
#[trial]
fn an_add_anchor_drag_inserts_and_moves_the_new_anchor_in_one_gesture() {
let mut h = two_paths();
h.update(|st| st.tool = BezierTool::AddAnchor);
h.update(|st| st.tool = BezierTool::AutoAddDelete);
let on = h.screen(p(110.0, 60.0));
h.drag(on, vec2(0.0, FAR));
{
@@ -453,6 +478,73 @@ fn an_add_anchor_drag_inserts_and_moves_the_new_anchor_in_one_gesture() {
assert_eq!(st.selection, sel(1, &[(0, 1)]));
}
// Add Anchor's drag from a segment inserts the anchor where the segment was pressed and pulls out its handles, as the
// reference application does (PhotoCraft #12707): the out handle follows the pointer and the in handle mirrors it, in
// one gesture, the new anchor selected and smooth; ⇧ turns the handle to the nearest 45°, an axis keeping the
// drag's offset along it and a diagonal the shorter offset; ⌥ pressed during the drag breaks the pair, so only the
// out handle follows from then on.
#[trial]
fn an_add_anchor_drag_pulls_out_the_new_anchors_handles() {
let add_anchor = || {
let mut h = two_paths();
h.update(|st| st.tool = BezierTool::AddAnchor);
h
};
let close = |a: Vec2, b: Vec2| (a - b).hypot() < 1e-6;
let on = p(110.0, 60.0);
let mut h = add_anchor();
let at = h.screen(on);
h.drag(at, vec2(0.0, 60.0));
{
let st = h.state.borrow();
assert_eq!(st.seen.iter().filter(|s| **s == Seen::Begin).count(), 1, "{:?}", st.seen);
assert_eq!((st.seen.first(), st.seen.last()), (Some(&Seen::Begin), Some(&Seen::Commit)), "{:?}", st.seen);
assert!(
matches!(st.changes.last(), Some(PathEdit::PullAnchor { path: 1, segment, in_ctrl: None, .. }) if *segment == kr(0)),
"{:?}",
st.changes
);
let knots = &st.paths[0].subpaths[0].knots;
assert_eq!(knots.len(), 5);
let k = knots[1];
assert!(k.anchor.distance(on) < 1e-3, "the anchor stays where the segment was pressed: {k:?}");
assert!(close(k.out_ctrl - k.anchor, Vec2::new(0.0, 60.0)) && close(k.in_ctrl - k.anchor, Vec2::new(0.0, -60.0)), "{k:?}");
assert!(k.smooth);
assert_eq!(st.selection, sel(1, &[(0, 1)]));
}
// ⇧: the measured drags.
for (by, out) in [((30.0, 80.0), (0.0, 80.0)), ((70.0, 40.0), (40.0, 40.0)), ((-50.0, -60.0), (-50.0, -50.0)), ((60.0, -20.0), (60.0, 0.0))] {
let mut h = add_anchor();
let at = h.screen(on);
h.fixture.hold(egui::Modifiers::SHIFT);
h.drag(at, vec2(by.0, by.1));
h.fixture.hold(egui::Modifiers::NONE);
let st = h.state.borrow();
let k = st.paths[0].subpaths[0].knots[1];
let out = Vec2::new(out.0, out.1);
assert!(close(k.out_ctrl - k.anchor, out) && close(k.in_ctrl - k.anchor, -out), "{by:?}: {k:?}");
}
// ⌥ during the drag: the in handle stays where the pair had it, the out handle follows on.
let mut h = add_anchor();
let at = h.screen(on);
h.press_at(at);
h.move_to(at + vec2(-30.0, 0.0));
h.move_to(at + vec2(-60.0, 0.0));
h.fixture.hold(egui::Modifiers::ALT);
h.move_to(at + vec2(-60.0, 20.0));
h.move_to(at + vec2(-60.0, 40.0));
h.release_at(at + vec2(-60.0, 40.0));
h.fixture.hold(egui::Modifiers::NONE);
let st = h.state.borrow();
let k = st.paths[0].subpaths[0].knots[1];
assert!(close(k.out_ctrl - k.anchor, Vec2::new(-60.0, 40.0)) && close(k.in_ctrl - k.anchor, Vec2::new(60.0, 0.0)), "{k:?}");
assert!(!k.smooth, "a broken pair is a corner");
assert_eq!(st.seen.iter().filter(|s| **s == Seen::Begin).count(), 1, "{:?}", st.seen);
}
// Convert on a segment: a drag on a straight one bends it when nothing of the path is selected and moves the path when
// every anchor is, a partial selection and a curved segment do nothing; handles within the straightness tolerance of
// their anchors still make the segment straight.