Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(pen): Alt-drag grabs the last anchor's incoming handle too (#6077)

Aditya Vishwakarma4e89f04d050f
diff --git a/crates/ui-egui/src/pen_draw_tests.rs b/crates/ui-egui/src/pen_draw_tests.rs
index 57be940bd..e11bd4abf 100644
--- a/crates/ui-egui/src/pen_draw_tests.rs
+++ b/crates/ui-egui/src/pen_draw_tests.rs
@@ -1,5 +1,5 @@
//! The Pen's modifiers while drawing, through the real canvas (#3306): ⌥/Alt breaks the handle
-//! being pulled out and grabs the last anchor's outgoing handle alone, and dragging on the first
+//! being pulled out and grabs either of the last anchor's handles alone, and dragging on the first
//! anchor closes the path with that anchor's handles pulled out.
use egui::{Key, Modifiers, PointerButton, Pos2, vec2};
@@ -122,6 +122,33 @@ fn alt_breaks_the_handle_being_dragged_and_grabs_the_last_outgoing_handle() {
assert!(near([k.out_ctrl.x, k.out_ctrl.y], [140.0, 20.0]));
}
+/// #6077: both handles of the last anchor are drawn, so ⌥/Alt grabs either one, as in Photoshop.
+/// Dragging the incoming handle turns it alone; the outgoing handle and the anchors stay put.
+#[test]
+fn alt_drag_turns_the_last_anchors_incoming_handle_alone() {
+ let mut h = harness();
+ click(&mut h, [20.0, 20.0]);
+ drag(&mut h, &[[100.0, 20.0], [120.0, 40.0]], None);
+ let k = h.state().ui.pen.clone().unwrap().knots[1];
+ assert!(near(k[1], [80.0, 0.0]) && near(k[2], [120.0, 40.0]), "smooth anchor pulled out: {k:?}");
+
+ drag(&mut h, &[[80.0, 0.0], [60.0, 30.0]], Some(0));
+ let pen = h.state().ui.pen.clone().unwrap();
+ assert_eq!(pen.knots.len(), 2, "no new anchor on the handle");
+ let k = pen.knots[1];
+ assert!(near(k[0], [100.0, 20.0]), "the anchor stayed put: {k:?}");
+ assert!(near(k[1], [60.0, 30.0]), "the incoming handle followed the pointer: {k:?}");
+ assert!(near(k[2], [120.0, 40.0]), "the outgoing handle stayed put: {k:?}");
+
+ click(&mut h, [150.0, 120.0]);
+ h.key_press(Key::Enter);
+ h.run_steps(2);
+ let doc = &h.state().session.active().unwrap().doc;
+ let k = &doc.work_path.as_ref().unwrap().subpaths[0].knots[1];
+ assert!(!k.smooth, "the broken handle is a cusp in the work path");
+ assert!(near([k.in_ctrl.x, k.in_ctrl.y], [60.0, 30.0]) && near([k.out_ctrl.x, k.out_ctrl.y], [120.0, 40.0]));
+}
+
#[test]
fn opening_a_new_document_finishes_the_previous_pen_path() {
let mut h = harness();
diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs
index 205e47aaf..ae91497d1 100644
--- a/crates/ui-egui/src/vector_ui.rs
+++ b/crates/ui-egui/src/vector_ui.rs
@@ -26,6 +26,9 @@ pub struct PenPath {
/// Re-dragging the final anchor changes its outgoing control only; the incoming curve stays put.
#[serde(skip)]
pub adjusting_last: bool,
+ /// ⌥/Alt-dragging the final anchor's incoming handle: it turns alone (#6077).
+ #[serde(skip)]
+ pub adjusting_in: bool,
/// Drawn with the Curvature Pen: handles follow the anchors (`curvature_refresh`) and
/// `unlinked` lists its corner points.
#[serde(default)]
@@ -224,6 +227,7 @@ pub fn closes_path_at(app: &PhotocraftApp, tool: Tool, x: f64, y: f64) -> bool {
pub fn pen_down(app: &mut PhotocraftApp, x: f64, y: f64, shift: bool) {
let tol = 6.0 / app.point_zoom().max(0.01) as f64;
let pen = app.ui.pen.get_or_insert_with(PenPath::default);
+ pen.adjusting_in = false;
if let Some(first) = pen.knots.first().map(|k| k[0])
&& on_closing_anchor(pen, x, y, tol)
{
@@ -263,10 +267,14 @@ pub fn pen_move(app: &mut PhotocraftApp, x: f64, y: f64, shift: bool) {
if let Some(pen) = app.ui.pen.as_mut()
&& pen.dragging
{
- let adjusting_last = pen.adjusting_last;
+ let (adjusting_last, adjusting_in) = (pen.adjusting_last, pen.adjusting_in);
if let Some(k) = pen.knots.last_mut() {
let a = k[0];
let [x, y] = shift_45(a, [x, y], shift);
+ if adjusting_in {
+ k[1] = [x, y];
+ return;
+ }
k[2] = [x, y];
if !adjusting_last {
k[1] = [2.0 * a[0] - x, 2.0 * a[1] - y];
@@ -313,6 +321,7 @@ pub fn pen_up(app: &mut PhotocraftApp) {
let Some(pen) = app.ui.pen.as_mut() else { return };
pen.dragging = false;
pen.adjusting_last = false;
+ pen.adjusting_in = false;
if closing(pen) {
// A click on the first anchor closes the path and leaves that anchor's handles alone.
if let Some(k) = pen.knots.last_mut()
@@ -336,21 +345,31 @@ pub fn pen_break_handle(app: &mut PhotocraftApp) {
pen.adjusting_last = true;
}
-/// ⌥/Alt-press on the last anchor's outgoing handle while drawing grabs that handle alone: the
-/// drag turns it and the incoming curve stays put (#3306). False when the press misses it.
+/// ⌥/Alt-press on one of the last anchor's handles while drawing grabs that handle alone: the
+/// drag turns it and the other handle stays put (#3306, #6077). False when the press misses both.
pub fn pen_grab_handle(app: &mut PhotocraftApp, x: f64, y: f64) -> bool {
let tol = 6.0 / f64::from(app.point_zoom().max(0.01));
let Some(pen) = app.ui.pen.as_mut() else { return false };
let Some((i, k)) = pen.knots.len().checked_sub(1).zip(pen.knots.last()) else { return false };
let dist = |p: [f64; 2]| (p[0] - x).hypot(p[1] - y);
- if k[2] == k[0] || dist(k[2]) >= tol || dist(k[2]) >= dist(k[0]) {
+ // Both handles show only while the outgoing one is pulled out (`draw_overlay`).
+ if k[2] == k[0] {
return false;
}
+ let grabbed = |h: [f64; 2]| h != k[0] && dist(h) < tol && dist(h) < dist(k[0]);
+ let incoming = if grabbed(k[2]) && dist(k[2]) <= dist(k[1]) {
+ false
+ } else if grabbed(k[1]) {
+ true
+ } else {
+ return false;
+ };
if !pen.unlinked.contains(&i) {
pen.unlinked.push(i);
}
pen.dragging = true;
pen.adjusting_last = true;
+ pen.adjusting_in = incoming;
true
}
@@ -414,6 +433,7 @@ pub(crate) fn pen_replace(app: &mut PhotocraftApp, path: &Path) {
pen.unlinked = knots.iter().enumerate().filter(|(_, k)| !k.smooth).map(|(i, _)| i).collect();
pen.dragging = false;
pen.adjusting_last = false;
+ pen.adjusting_in = false;
pen.moving = None;
if pen.curvature {
curvature_refresh(pen, false);