PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(path): Shift locks a Direct Selection anchor drag to 45° (#5475) (#5498)
Juan Antonio Redondo7efebfa050ac
diff --git a/crates/ui-egui/src/direct_select.rs b/crates/ui-egui/src/direct_select.rsindex 26fba5dad..b27d9ea46 100644--- a/crates/ui-egui/src/direct_select.rs+++ b/crates/ui-egui/src/direct_select.rs@@ -94,6 +94,11 @@ pub enum Edit {}impl Edit {+ /// The drag's end with ⇧ held: moving anchors locks to multiples of 45°, as in Photoshop (#5475).+ fn constrain(&self, start: [f64; 2], p: [f64; 2], shift: bool) -> [f64; 2] {+ if shift && matches!(self, Edit::Anchors(_)) { crate::stroke_constraint::snap45(start, p) } else { p }+ }+/// The edit for a drag from `start` to `end`, as a command and its params (without target).fn command(&self, start: [f64; 2], end: [f64; 2]) -> (&'static str, Value) {let d = [end[0] - start[0], end[1] - start[1]];@@ -238,7 +243,7 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> boollet ds = &mut app.ui.direct_selection;match &mut ds.drag {Some(Drag::Edit { base, edit, start, end, preview, .. }) => {- *end = [x, y];+ *end = edit.constrain(*start, [x, y], mods.shift);*preview = edit.preview(base, *start, *end);}Some(Drag::Marquee { end, .. }) => *end = [x, y],@@ -248,7 +253,8 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool}ToolEvent::Up { x, y } => match app.ui.direct_selection.drag.take() {Some(Drag::Edit { target, base, edit, start, .. }) => {- release(app, target, &base, &edit, start, [x, y]);+ let end = edit.constrain(start, [x, y], mods.shift);+ release(app, target, &base, &edit, start, end);app.ui.direct_selection.coalesce = None;true}diff --git a/crates/ui-egui/src/direct_select_tests.rs b/crates/ui-egui/src/direct_select_tests.rsindex 0f3a5ba99..31a16bfcd 100644--- a/crates/ui-egui/src/direct_select_tests.rs+++ b/crates/ui-egui/src/direct_select_tests.rs@@ -326,6 +326,28 @@ fn arrow_keys_nudge_the_selected_anchors() {assert_eq!(work(&app), before);}+/// #5475: ⇧ during an anchor drag locks it to 45°, whether it's held from the press or added later.+#[test]+fn shift_locks_an_anchor_drag_to_45_degrees() {+ let mut app = app(Tool::DirectSelection);+ with_path(&mut app);+ tool_event(&mut app, ToolEvent::Down { x: 40.0, y: 40.0, pressure: 1.0 }, Modifiers::NONE);+ tool_event(&mut app, ToolEvent::Move { x: 70.0, y: 44.0, pressure: 1.0 }, Modifiers::SHIFT);+ let Some(Drag::Edit { preview: Some(p), .. }) = &app.ui.direct_selection.drag else { panic!("dragging") };+ assert_eq!(p.subpaths[0].knots[0].anchor, Point::new(70.0, 40.0), "the preview is locked too");+ tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 47.0 }, Modifiers::SHIFT);+ assert_eq!(anchor(&app, 0), Point::new(100.0, 40.0));++ // Diagonal, and without ⇧ the drag stays free.+ // (⇧ at the press would deselect the anchor, so it comes once the drag is under way.)+ tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 40.0, pressure: 1.0 }, Modifiers::NONE);+ tool_event(&mut app, ToolEvent::Up { x: 131.0, y: 69.0 }, Modifiers::SHIFT);+ let a = anchor(&app, 0);+ assert!((a.x - 130.0).abs() < 1e-9 && (a.y - 70.0).abs() < 1e-9, "{a:?}");+ drag(&mut app, [130.0, 70.0], [140.0, 73.0], Modifiers::NONE);+ assert_eq!(anchor(&app, 0), Point::new(140.0, 73.0));+}+#[test]fn the_curvature_pen_drags_an_anchor_of_the_active_path_without_a_modifier() {// As in Photoshop: with no path being drawn, pressing an anchor and dragging moves it.