Skip to content

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

Freeform Pen: constrain freehand drags with Shift (#7171)

nrq6993-a11yab668a1dffed
diff --git a/crates/ui-egui/src/lasso_ui.rs b/crates/ui-egui/src/lasso_ui.rs
index 1f3f7adba..2774368bd 100644
--- a/crates/ui-egui/src/lasso_ui.rs
+++ b/crates/ui-egui/src/lasso_ui.rs
@@ -197,12 +197,29 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
}
ToolEvent::Move { .. } | ToolEvent::Up { .. } => {
let up = matches!(ev, ToolEvent::Up { .. });
+ let zoom = app.point_zoom();
let Some(d) = app.drag.as_mut().filter(|d| d.tool == tool) else { return true };
+ d.track(mods);
let Some(lasso) = &d.lasso else { return true };
let (down, polygonal, previous) = (lasso.down, lasso.polygonal, lasso.cursor);
+ let p = if down && tool == Tool::FreeformPen {
+ let last = d.points.last().map_or(d.start, |pt| [pt[0], pt[1]]);
+ let [x, y] = crate::stroke_constraint::constrain(
+ d.tool,
+ &mut d.constrain,
+ d.start,
+ last,
+ p,
+ mods.shift,
+ zoom,
+ );
+ [x, y]
+ } else {
+ p
+ };
if d.reposition {
// Move relative to the rubber-band endpoint, not the last fixed vertex.
- let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]);
+ let last = d.points.last().map_or(d.start, |pt| [pt[0], pt[1]]);
d.shift_to([last[0] + p[0] - previous[0], last[1] + p[1] - previous[1]]);
} else if down {
// While the button is down the outline follows the pointer (freehand), Alt or
diff --git a/crates/ui-egui/src/lasso_ui/tests.rs b/crates/ui-egui/src/lasso_ui/tests.rs
index 6484ed03c..71a5b6877 100644
--- a/crates/ui-egui/src/lasso_ui/tests.rs
+++ b/crates/ui-egui/src/lasso_ui/tests.rs
@@ -306,6 +306,21 @@ fn clicking_the_last_point_again_closes_the_outline() {
assert!(app.session.active().unwrap().doc.selection.as_ref().unwrap().sample_channel(150, 100, 0) > 0.9);
}
+#[test]
+fn shift_drag_constrains_freeform_pen_to_a_straight_line() {
+ let mut app = app();
+ app.ui.tool = Tool::FreeformPen;
+ let shift = Modifiers::SHIFT;
+ event(&mut app, "down", 10.0, 60.0, shift);
+ for (x, y) in [(30.0, 63.0), (50.0, 55.0), (80.0, 68.0), (100.0, 52.0)] {
+ event(&mut app, "move", x, y, shift);
+ }
+ let pts = &app.drag.as_ref().unwrap().points;
+ assert!(pts.len() >= 2, "{pts:?}");
+ let y0 = pts[0][1];
+ assert!(pts.iter().all(|p| (p[1] - y0).abs() < 1e-6), "⇧ keeps the freehand segment horizontal: {pts:?}");
+}
+
/// ⌘ (Ctrl) with the Lasso is the Move tool for the drag: through the real canvas, a ⌘-drag outside
/// the selection (here: without one) moves the layer and draws no outline (`canvas::command_moves_layer`).
#[test]
diff --git a/crates/ui-egui/src/stroke_constraint.rs b/crates/ui-egui/src/stroke_constraint.rs
index a7cd51cb9..51760cc6c 100644
--- a/crates/ui-egui/src/stroke_constraint.rs
+++ b/crates/ui-egui/src/stroke_constraint.rs
@@ -23,9 +23,10 @@ pub struct Axis {
pub dir: Option<[f64; 2]>,
}
-/// Tools whose drag ⇧ constrains: every painting tool and the Gradient tool.
+/// Tools whose drag ⇧ constrains: every painting tool, the Gradient tool, and the Freeform Pen
+/// (Photoshop's freehand path segment with ⇧, #7171).
pub fn applies(tool: Tool) -> bool {
- tool.is_brushlike() || tool == Tool::Gradient
+ tool.is_brushlike() || tool == Tool::Gradient || tool == Tool::FreeformPen
}
/// Tools that ⇧-click connects to the previous stroke with a straight line.