PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Pen: Auto Add/Delete on the path being drawn (#6140) (#6172)
Khawar Zamman Wani1b666f4790f9
diff --git a/crates/ui-egui/src/anchor_tools.rs b/crates/ui-egui/src/anchor_tools.rsindex 9dc8b6cfe..e6f9ac72d 100644--- a/crates/ui-egui/src/anchor_tools.rs+++ b/crates/ui-egui/src/anchor_tools.rs@@ -7,9 +7,10 @@//! * Delete Anchor Point: hovering an anchor highlights it; a click removes it//! (`path.deleteAnchor`), joining its neighbours.//! * ⌥ swaps the two tools and ⌘/Ctrl is Direct Selection (`direct_select`) while held.-//! * The Pen does both with Auto Add/Delete on (its options bar, on by default, #4144), on the-//! active path only and while no path is being drawn: a click on a segment adds, a click on an-//! anchor deletes, except an open subpath's end points; ⇧ turns it off while held.+//! * The Pen does both with Auto Add/Delete on (its options bar, on by default, #4144): a click on+//! a segment adds, a click on an anchor deletes, except an open subpath's end points; ⇧ turns it+//! off while held. It edits the active path, or while a path is being drawn that path (#6140),+//! and drawing carries on from its last anchor.//!//! Each click is one command and one history step; a click on nothing it can edit does nothing.@@ -48,16 +49,26 @@ fn target(app: &PhotocraftApp, tool: Tool, p: [f64; 2]) -> Option<(PathRef, Path/// The Pen's Auto Add/Delete (#4144): what a Pen click at `p` does to the active path instead of/// starting a new one, as the anchor tool that does it (Add Anchor Point on a segment, Delete-/// Anchor Point on an anchor), the path and the hit. `None` when the option is off, ⇧, ⌥ or ⌘ is-/// held, a path is being drawn, or the click hits nothing it edits. The end points of an open-/// subpath are left to the Pen, as in Photoshop.+/// Anchor Point on an anchor), the path and the hit. While a path is being drawn it edits that+/// path only (#6140): a click anywhere else places the next anchor. `None` when the option is off,+/// ⇧, ⌥ or ⌘ is held, an anchor's handles are being pulled out, or the click hits nothing it edits.+/// The end points of an open subpath are left to the Pen, as in Photoshop: on the path being+/// drawn, the first closes it and the last breaks its handle.pub fn pen_auto(app: &PhotocraftApp, tool: Tool, mods: Modifiers, p: [f64; 2]) -> Option<(Tool, PathRef, Path, Hit)> {- if tool != Tool::Pen || !app.ui.tool_options.pen_auto_add_delete || app.ui.pen.is_some() || mods.shift || mods.alt || mods.command {+ if tool != Tool::Pen || !app.ui.tool_options.pen_auto_add_delete || mods.shift || mods.alt || mods.command {return None;}let tol = 6.0 / f64::from(app.point_zoom().max(0.01));- let (r, path, hit) =- crate::direct_select::active_paths(app).into_iter().find_map(|(r, path)| edit::hit(&path, Point::new(p[0], p[1]), tol, &[]).map(|h| (r, path, h)))?;+ let at = Point::new(p[0], p[1]);+ let (r, path, hit) = match app.ui.pen.as_ref() {+ Some(pen) if pen.dragging || pen.curvature => return None,+ Some(pen) => {+ let path = crate::vector_ui::pen_path(pen);+ let hit = edit::hit(&path, at, tol, &[])?;+ (PathRef::Pen, path, hit)+ }+ None => crate::direct_select::active_paths(app).into_iter().find_map(|(r, path)| edit::hit(&path, at, tol, &[]).map(|h| (r, path, h)))?,+ };let tool = match hit {Hit::Segment(..) => Tool::AddAnchorPoint,Hit::Anchor([s, k]) => {@@ -76,10 +87,14 @@ pub fn pen_auto(app: &PhotocraftApp, tool: Tool, mods: Modifiers, p: [f64; 2]) -/// when they aren't theirs.pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool {if let ToolEvent::Down { x, y, .. } = ev- && let Some((_, r, _, hit)) = pen_auto(app, app.ui.tool, mods, [x, y])+ && let Some((_, r, path, hit)) = pen_auto(app, app.ui.tool, mods, [x, y]){- // The release finds no Pen path to finish, so the Pen draws nothing.- apply(app, r, hit);+ // The release finds no Pen drag to finish, so the Pen draws nothing.+ if r == PathRef::Pen {+ edit_pen(app, path, hit);+ } else {+ apply(app, r, hit);+ }return true;}let Some(tool) = effective(app.ui.tool, mods) else { return false };@@ -90,23 +105,28 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool}fn click(app: &mut PhotocraftApp, tool: Tool, p: [f64; 2]) {- let Some((r, mut path, hit)) = target(app, tool, p) else { return };+ let Some((r, path, hit)) = target(app, tool, p) else { return };if r == PathRef::Pen {- // The Pen path being drawn isn't in the document yet: it takes the edit directly.- let edited = match hit {- Hit::Segment(k, t) => edit::add_anchor(&mut path, k, t).is_ok(),- Hit::Anchor(k) => edit::delete_anchor(&mut path, k).is_ok(),- Hit::Handle(..) => false,- };- if edited {- app.ui.direct_selection = DirectSelection { target: Some(r), ..Default::default() };- crate::vector_ui::pen_replace(app, &path);- }+ edit_pen(app, path, hit);return;}apply(app, r, hit);}+/// Add or delete an anchor of the Pen path being drawn (`path`, from `pen_path`). It isn't in the+/// document yet, so it takes the edit directly; drawing carries on from its last anchor.+fn edit_pen(app: &mut PhotocraftApp, mut path: Path, hit: Hit) {+ let edited = match hit {+ Hit::Segment(k, t) => edit::add_anchor(&mut path, k, t).is_ok(),+ Hit::Anchor(k) => edit::delete_anchor(&mut path, k).is_ok(),+ Hit::Handle(..) => false,+ };+ if edited {+ app.ui.direct_selection = DirectSelection { target: Some(PathRef::Pen), ..Default::default() };+ crate::vector_ui::pen_replace(app, &path);+ }+}+/// Add an anchor where `hit` is on a segment of `r`, or delete the anchor it is on: one command.fn apply(app: &mut PhotocraftApp, r: PathRef, hit: Hit) {let (id, mut params) = match hit {diff --git a/crates/ui-egui/src/anchor_tools_tests.rs b/crates/ui-egui/src/anchor_tools_tests.rsindex 87c045eba..c21cc5aaf 100644--- a/crates/ui-egui/src/anchor_tools_tests.rs+++ b/crates/ui-egui/src/anchor_tools_tests.rs@@ -230,3 +230,67 @@ fn the_pen_cursor_shows_what_auto_add_delete_would_do() {app.ui.tool_options.pen_auto_add_delete = false;assert_eq!(badge(&app, Tool::Pen, Modifiers::NONE), None);}++fn pen_anchors(app: &PhotocraftApp) -> Vec<[f64; 2]> {+ app.ui.pen.as_ref().map(|p| p.knots.iter().map(|k| k[0]).collect()).unwrap_or_default()+}++/// #6140: while drawing, a Pen click on the path being drawn adds an anchor on a segment or+/// deletes a middle anchor, and drawing carries on from the last anchor. Nothing reaches History.+#[test]+fn the_pen_adds_and_deletes_anchors_on_the_path_being_drawn() {+ let mut app = app(Tool::Pen);+ let before = steps(&app);+ for at in [[40.0, 40.0], [120.0, 40.0], [120.0, 120.0]] {+ click(&mut app, at, Modifiers::NONE);+ }+ click(&mut app, [80.0, 40.0], Modifiers::NONE);+ assert_eq!(pen_anchors(&app), vec![[40.0, 40.0], [80.0, 40.0], [120.0, 40.0], [120.0, 120.0]], "added on the first segment");+ click(&mut app, [40.0, 120.0], Modifiers::NONE);+ assert_eq!(pen_anchors(&app).last(), Some(&[40.0, 120.0]), "the next click carries on from the last anchor");+ assert_eq!(pen_anchors(&app).len(), 5);++ click(&mut app, [120.0, 40.0], Modifiers::NONE);+ assert_eq!(pen_anchors(&app), vec![[40.0, 40.0], [80.0, 40.0], [120.0, 120.0], [40.0, 120.0]], "a middle anchor is deleted");+ assert_eq!(steps(&app), before, "the path being drawn is not in the document yet");+}++/// The path being drawn keeps its end points (the first closes it, the last breaks its handle), ⇧+/// or the option turned off places an anchor on its segment, and another path takes no edit.+#[test]+fn the_pen_draws_on_the_path_being_drawn_where_auto_add_delete_does_not_apply() {+ let mut app = app(Tool::Pen);+ with_path(&mut app);+ let work_before = work(&app);+ for at in [[20.0, 160.0], [180.0, 160.0], [180.0, 180.0]] {+ click(&mut app, at, Modifiers::NONE);+ }+ click(&mut app, [180.0, 180.0], Modifiers::NONE);+ assert_eq!(pen_anchors(&app).len(), 3, "a click on the last anchor breaks its handle");+ click(&mut app, [100.0, 160.0], Modifiers::SHIFT);+ assert_eq!(pen_anchors(&app).len(), 4, "⇧ places an anchor");+ app.ui.tool_options.pen_auto_add_delete = false;+ click(&mut app, [100.0, 160.0], Modifiers::NONE);+ assert_eq!(pen_anchors(&app).len(), 5, "with the option off the Pen places an anchor");+ app.ui.tool_options.pen_auto_add_delete = true;+ click(&mut app, [80.0, 120.0], Modifiers::NONE);+ assert_eq!((pen_anchors(&app).len(), work(&app)), (6, work_before), "another path takes no edit while drawing");+ click(&mut app, [20.0, 160.0], Modifiers::NONE);+ assert!(app.ui.pen.is_none(), "the first anchor closes the path");+}++#[test]+fn the_pen_cursor_shows_auto_add_delete_on_the_path_being_drawn() {+ use crate::tool_feedback::{Badge, badge};+ let mut app = app(Tool::Pen);+ for at in [[40.0, 40.0], [120.0, 40.0], [120.0, 120.0]] {+ click(&mut app, at, Modifiers::NONE);+ }+ let cases = [([80.0, 40.0], Some(Badge::Add)), ([120.0, 40.0], Some(Badge::Subtract)), ([40.0, 40.0], None), ([120.0, 120.0], None), ([60.0, 100.0], None)];+ for (at, want) in cases {+ app.hover_doc = Some(at);+ assert_eq!(badge(&app, Tool::Pen, Modifiers::NONE), want, "{at:?}");+ }+ app.hover_doc = Some([80.0, 40.0]);+ assert_eq!(badge(&app, Tool::Pen, Modifiers::SHIFT), None);+}diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rsindex 01bb3c133..8b5e914af 100644--- a/crates/ui-egui/src/vector_ui.rs+++ b/crates/ui-egui/src/vector_ui.rs@@ -1016,8 +1016,6 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXformif let Some((_, p)) = targeted_vector_mask(app) {draw_path(&p, tool == Tool::PathSelection);}- // The segment the Pen's Auto Add/Delete would add an anchor to, or the anchor it would delete.- crate::anchor_tools::draw_pen_hover(app, painter, &to_scr, accent, painter.ctx().input(|i| i.modifiers));crate::corner_radius::draw(app, painter, &to_scr, accent);}// Pen path in progress, with handles of the last knot and a rubber band to the pointer. While@@ -1038,9 +1036,12 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXformpts.extend(curve_points(w[0][0], w[0][2], w[1][1], w[1][0], &to_scr));}painter.add(egui::Shape::line(pts, Stroke::new(1.5, accent)));+ // No rubber band where a click adds or deletes an anchor of this path instead (#6140).+ let auto = app.hover_doc.is_some_and(|h| crate::anchor_tools::pen_auto(app, tool, painter.ctx().input(|i| i.modifiers), h).is_some());if let (Some(last), Some(h)) = (pen.knots.last(), app.hover_doc)&& !pen.dragging&& pen.moving.is_none()+ && !auto{if pen.curvature && pen.knots.len() >= 2 {// The Curvature Pen previews the curve the next click makes through the pointer.@@ -1079,6 +1080,11 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXformpainter.circle_stroke(c + vec2(11.0, 11.0), 3.5, Stroke::new(1.5, accent));}}+ // The segment the Pen's Auto Add/Delete would add an anchor to, or the anchor it would delete,+ // over the path being drawn. While drawing, the other paths can be hidden: it edits none of them.+ if paths || app.ui.pen.is_some() {+ crate::anchor_tools::draw_pen_hover(app, painter, &to_scr, accent, painter.ctx().input(|i| i.modifiers));+ }}// ---------------------------------------------------------------------------------------------diff --git a/docs/ui-parity.md b/docs/ui-parity.mdindex 6edff953d..793f09840 100644--- a/docs/ui-parity.md+++ b/docs/ui-parity.md@@ -1,6 +1,6 @@# UI parity: tools, handles, modifiers and feel-> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-14 · **Change:** minor (Show Transform Controls frames every selected layer; guide keys and clicks; menu bar from the keyboard, #4988; Crop tool Content-Aware Fill; on-canvas corner radius widgets, TOOL-214-12; Curvature Pen edits the active path; Clip Thumbnails to Layer Bounds; Pen ⇧ 45°; last selected tool restored at launch; Layers panel filter-type dropdown with Kind, Name and Mode, #5048; Free Transform box and marching ants follow the selection, #5297; History: the trash deletes the selected snapshot, rows drag onto the trash, #5324; smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows; stale missing-tool rows corrected: Color Replacement, Red Eye and the Add/Delete Anchor Point pair are live toolbar tools, Frame landed; no percentage changed) · **Target:** Adobe Photoshop 2026+> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-15 · **Change:** minor (Pen Auto Add/Delete on the path being drawn, #6140; Show Transform Controls frames every selected layer; guide keys and clicks; menu bar from the keyboard, #4988; Crop tool Content-Aware Fill; on-canvas corner radius widgets, TOOL-214-12; Curvature Pen edits the active path; Clip Thumbnails to Layer Bounds; Pen ⇧ 45°; last selected tool restored at launch; Layers panel filter-type dropdown with Kind, Name and Mode, #5048; Free Transform box and marching ants follow the selection, #5297; History: the trash deletes the selected snapshot, rows drag onto the trash, #5324; smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows; stale missing-tool rows corrected: Color Replacement, Red Eye and the Add/Delete Anchor Point pair are live toolbar tools, Frame landed; no percentage changed) · **Target:** Adobe Photoshop 2026How PhotoCraft's direct manipulation compares with Photoshop 2026 (27.11.0): the toolbox, thehandles and modifier keys of each gesture, nudging, snapping, numeric precision, navigation,@@ -121,7 +121,7 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme|---|---|---|| Click corner, drag smooth, ⌥ breaks a handle, ⌘ direct-selects, ⇧ 45° | done | TOOL-214-10; `vector_ui.rs` (#3751) || Handle on the last point, close-drag handle | partial | #1482 |-| Pen Auto Add/Delete: click a segment of the active path to add, an anchor to delete; ⇧ suspends it | done | #4144 |+| Pen Auto Add/Delete: click a segment of the active path, or of the path being drawn, to add, an anchor to delete; ⇧ suspends it | done | #4144, #6140 || Curvature Pen edits the active path with no modifier: drag an anchor (the curve reflows), click a segment to add an anchor, double-click smooth ↔ corner; cursor badge + on segments, four arrows on anchors | done: `direct_select.rs` (`curvature_press`, `curvature_hover`), `path.moveAnchors`/`path.convertPoint` with `curvature`, `direct_select_tests.rs` | || Path operations in the options bar (combine, subtract, intersect, exclude) | partial | `layer.combineShapes.*`; Pen options #1397 || On-canvas corner-radius handles on live shapes | done: every selected live rectangle shows a widget at each arc centre with Path Selection, the Rectangle tool and Move with Show Transform Controls; a drag rounds every corner of every selected rectangle to the same radius, ⌥ that corner on each, with the corner radius cursor and an `R:` readout, previewed and committed as one history step (`corner_radius.rs`, `corner_radius_tests.rs`) | TOOL-214-12 |@@ -243,6 +243,7 @@ Paragraph; the rest open as in-app floating windows.| 2026-10-11 | minor | Spring-loaded tool letters done (#2672): holding any tool letter (Z included) past 300 ms, or using the canvas while it is down, makes the tool temporary until release || 2026-10-11 | minor | Type formatting keys done: ⇧⌘K, U, B, I and /, ⌥↑/↓, ⇧⌥↑/↓, ⌥←/→ over a selection and ⌥⇧⌘H format the selected type while editing, and no longer reach Color Settings, Desaturate, Auto Color or Inverse || 2026-10-12 | minor | Pen Auto Add/Delete on the active path, with its options-bar checkbox, hover feedback and + / − cursor badge (#4144) |+| 2026-10-15 | minor | Pen Auto Add/Delete works on the path being drawn: a click on its segment adds an anchor, on a middle anchor deletes it, and drawing carries on (#6140) || 2026-10-12 | minor | Toolbar tool shortcuts can be reassigned, removed and reset in Keyboard Shortcuts (#4086); capture, restart, group cycling, conflict and text-focus regressions || 2026-10-12 | minor | Smart filter rows can be selected, and dragged (⌥: copied) onto another smart object or onto Delete (#4271) || 2026-10-12 | minor | Free Transform options bar: H and V skew fields; a sheared box reads as skew instead of a taller H |