Merge pull request #13179 from TrevorBrown-dev/feat/anchor-tools-path-menu-12771

Delete Anchor Point and Convert Point: right-click opens the path menu with the point's entry on top (#12771, #12744)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-12 13:27:56 +01:00
commit 1f0bd2f807
5 files changed
+357 -4

No files matched your search

+82 -3
View File
@@ -8,7 +8,9 @@ use photocraft_engine::prefs::{self, CanvasColor};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use crate::direct_select::PathRef;
use crate::{PhotocraftApp, state::Tool};
use photocraft_vector::edit::Hit;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CanvasToolMenu {
@@ -70,6 +72,12 @@ pub struct CanvasToolMenu {
/// Opened with the Content-Aware Move tool inside the active selection (#12194).
#[serde(default)]
pub inside_selection: bool,
/// Opened with the Delete Anchor Point or Convert Point tool over an anchor or a segment of the
/// targeted path (#12771, #12744): the engine command that entry runs and its params. It heads
/// the path menu, above a separator. The third part is the document id and revision it was read
/// from: the knot index means something only there, so the entry expires once either moves on.
#[serde(default)]
pub point: Option<(String, Value, (u64, u64))>,
}
/// A misspelled word in the type layer being edited (`edit.checkSpelling`'s `list` entry).
@@ -382,6 +390,7 @@ pub fn open_crop(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32;
sampler_at: None,
color: None,
inside_selection: false,
point: None,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -759,7 +768,54 @@ pub fn magnetic_drawing(app: &PhotocraftApp) -> bool {
/// Selection and Direct Selection tools, whose Photoshop menu offers the same whole-path actions
/// for the selected path (#1056).
pub fn path_menu(tool: Tool) -> bool {
matches!(tool, Tool::Pen | Tool::PathSelection | Tool::DirectSelection)
matches!(tool, Tool::Pen | Tool::PathSelection | Tool::DirectSelection) || point_menu(tool)
}
/// The anchor tools whose path menu also heads with the entry for the point under the right-click
/// (#12771, #12744). Add Anchor Point (#12710) joins here.
fn point_menu(tool: Tool) -> bool {
matches!(tool, Tool::DeleteAnchorPoint | Tool::ConvertPoint)
}
/// The anchor or segment of the targeted path under document point `doc`, as the anchor tools hit
/// it (`direct_select::hit_active`): Delete Anchor Point on an anchor, Add Anchor Point on a
/// segment, at the clicked point. `None` on empty canvas, a handle, or the Pen draft, which the
/// engine doesn't edit.
fn point_at(app: &PhotocraftApp, doc: [f64; 2]) -> Option<(String, Value, (u64, u64))> {
let (target, _, hit) = crate::direct_select::hit_active(app, doc)?;
let (command, mut params) = match hit {
Hit::Anchor([subpath, knot]) => (POINT_DELETE, json!({"subpath": subpath, "knot": knot})),
Hit::Segment([subpath, knot], t) => (POINT_ADD, json!({"subpath": subpath, "knot": knot, "t": t})),
Hit::Handle(..) => return None,
};
if target == PathRef::Pen || crate::anchor_tools::ignores(app, &target) {
return None;
}
if let (Some(o), Some(t)) = (params.as_object_mut(), target.params(app).as_object()) {
o.extend(t.clone());
}
let stamp = app.session.active().map(|st| (st.doc.id.0, st.revision))?;
Some((command.to_string(), params, stamp))
}
const POINT_DELETE: &str = "path.deleteAnchor";
const POINT_ADD: &str = "path.addAnchor";
/// The label of a menu's top point entry, spelled out so `cargo xtask i18n-coverage` lists it.
fn point_label(command: &str) -> String {
if command == POINT_DELETE { tl!("Delete Anchor Point").to_string() } else { tl!("Add Anchor Point").to_string() }
}
/// Is `command` the open menu's top point entry?
fn is_point(menu: &CanvasToolMenu, command: &str) -> bool {
menu.point.as_ref().is_some_and(|(c, ..)| c == command)
}
/// Is the open menu's point entry still about the path as it is? The document and its revision are
/// the ones the entry was read from; an edit meanwhile (say a control-channel `path.addAnchor`)
/// shifts the knot indexes, so a stale entry runs nothing.
fn point_current(app: &PhotocraftApp, menu: &CanvasToolMenu) -> bool {
menu.point.as_ref().is_some_and(|(.., stamp)| app.session.active().map(|st| (st.doc.id.0, st.revision)) == Some(*stamp))
}
/// Tools whose plain canvas right-click offers the view menu.
@@ -808,7 +864,9 @@ pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] {
/// Is `command` one of the open menu's rows, and enabled?
pub fn available(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> bool {
(rows(menu).iter().flatten().any(|(_, id)| *id == command) || is_spelling(menu, command) || is_crop(menu, command)) && entry_enabled(app, menu, command)
(is_point(menu, command) && point_current(app, menu))
|| ((rows(menu).iter().flatten().any(|(_, id)| *id == command) || is_spelling(menu, command) || is_crop(menu, command))
&& entry_enabled(app, menu, command))
}
/// Is `command` the transform box's current mode (checked in the transform menu)?
@@ -977,6 +1035,7 @@ pub fn open_transform(app: &mut PhotocraftApp, ctx: &Context, pos: [f32; 2]) ->
sampler_at: None,
color: None,
inside_selection: false,
point: None,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -1064,6 +1123,7 @@ pub fn open_pasteboard(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos:
sampler_at: None,
color: None,
inside_selection: false,
point: None,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -1101,6 +1161,7 @@ pub fn open_guide(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, mods: egui
sampler_at: None,
color: None,
inside_selection: false,
point: None,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -1249,6 +1310,7 @@ pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2]
let has_path = crate::vector_ui::active_path_name(app).is_some() || app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2);
let path_name = crate::vector_ui::active_path_name(app);
let spelling = if text { doc.and_then(|d| spelling_at(app, d)) } else { None };
let point = if point_menu(tool) && has_path { doc.filter(|d| d.iter().all(|v| v.is_finite())).and_then(|d| point_at(app, d)) } else { None };
let inside_selection = tool == Tool::ContentAwareMove && doc.is_some_and(|d| selected_at(app, d));
let sampler = if tool == Tool::ColorSampler { doc.and_then(|d| crate::analysis_ui::sampler_at(app, d)) } else { None };
let sampler_at = sampler.and_then(|i| Some((app.session.active_index()?, *app.session.active()?.color_samplers.get(i)?)));
@@ -1275,6 +1337,7 @@ pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2]
sampler_at,
color,
inside_selection,
point,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -1309,6 +1372,7 @@ pub fn open_view(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32;
sampler_at: None,
color: None,
inside_selection: false,
point: None,
});
crate::canvas_popup::mark_open(ctx, menu_id());
true
@@ -1351,7 +1415,9 @@ pub fn choose(app: &mut PhotocraftApp, ctx: &Context, command: &str) {
if !available(app, &menu, command) {
return;
}
let result = if menu.crop.is_some() {
let result = if is_point(&menu, command) {
menu.point.as_ref().map_or(Ok(()), |(id, params, _)| app.run(id, params.clone()).map(|_| ()))
} else if menu.crop.is_some() {
choose_crop(app, &menu, command)
} else if menu.guide {
choose_guide(app, ctx, command)
@@ -1699,6 +1765,13 @@ pub fn show(app: &mut PhotocraftApp, ctx: &Context) {
selected = Some(command);
}
}
if let Some((command, ..)) = &menu.point {
let item = egui::Button::new(point_label(command)).min_size(egui::vec2(200.0, 20.0));
if ui.add_enabled(available(app, &menu, command), item).clicked() {
selected = Some(command.clone());
}
ui.separator();
}
for row in rows(&menu) {
let Some((label, command)) = *row else {
ui.separator();
@@ -1759,3 +1832,9 @@ mod make_selection_dialog_tests;
#[cfg(test)]
mod type_layer_menu_tests;
#[cfg(test)]
mod delete_anchor_menu_tests;
#[cfg(test)]
mod convert_point_menu_tests;
@@ -0,0 +1,67 @@
//! #12744: the Convert Point tool's right-click opens the same path menu as the Delete Anchor
//! Point tool's (#12771), with no Convert Point entry. The shared steps live in
//! `delete_anchor_menu_tests`.
use egui::Modifiers;
use egui_kittest::kittest::Queryable;
use super::delete_anchor_menu_tests::{add_on_the_edge, delete_the_corner, empty_canvas, opening_changes_nothing, right_clicked, saved, setup};
use super::tests::{harness, right_click, screen_of};
use crate::canvas::{ToolEvent, tool_event};
use crate::direct_select::PathRef;
use crate::state::Tool;
#[test]
fn right_click_on_an_anchor_offers_delete_anchor_point_on_top_and_it_deletes() {
delete_the_corner(Tool::ConvertPoint);
}
#[test]
fn right_click_on_a_segment_offers_add_anchor_point_on_top() {
add_on_the_edge(Tool::ConvertPoint);
}
#[test]
fn right_click_on_empty_canvas_opens_the_path_menu_without_a_top_entry() {
empty_canvas(Tool::ConvertPoint);
}
#[test]
fn the_menu_has_no_convert_point_entry_and_converts_nothing() {
let before = saved(&setup(Tool::ConvertPoint));
for at in [[300.0, 300.0], [200.0, 300.0], [500.0, 100.0]] {
let h = right_clicked(Tool::ConvertPoint, at);
assert!(h.state().ui.canvas_tool_menu.is_some(), "the menu opened at {at:?}");
assert!(h.query_by_label("Convert Point").is_none(), "no Convert Point entry at {at:?}");
assert_eq!(saved(h.state()), before, "no point converted at {at:?}");
}
}
#[test]
fn opening_the_menu_adds_no_history_step_and_converts_no_point() {
opening_changes_nothing(Tool::ConvertPoint);
}
/// Every anchor selected (a Direct Selection marquee), then Delete Anchor Point from the menu on
/// (300,300): the anchors shift, so no old anchor index stays selected, as after a Delete Anchor
/// Point click: `app.run` drops a Direct Selection whose revision moved on (`cancel_stale`).
#[test]
fn deleting_from_the_menu_leaves_no_shifted_anchor_selected() {
let mut app = setup(Tool::DirectSelection);
tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 250.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(&mut app, ToolEvent::Move { x: 750.0, y: 550.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(&mut app, ToolEvent::Up { x: 750.0, y: 550.0 }, Modifiers::NONE);
assert!(app.ui.direct_selection.anchors.len() > 3, "every anchor is selected");
app.ui.tool = Tool::ConvertPoint;
let mut h = harness(app);
assert!(!h.state().ui.direct_selection.anchors.is_empty(), "Convert Point keeps the selection");
let p = screen_of(h.state(), [300.0, 300.0]);
right_click(&mut h, p, Modifiers::NONE);
// Chosen without a frame after it, which would drop a selection with no owner, as for a click.
let ctx = h.ctx.clone();
super::choose(h.state_mut(), &ctx, "path.deleteAnchor");
assert_eq!(saved(h.state()).subpaths[0].knots.len(), 3, "the anchor is gone");
let ds = &h.state().ui.direct_selection;
assert!(ds.target.is_none_or(|t| t == PathRef::Saved(0)), "the selection is on no other path");
assert!(ds.anchors.is_empty(), "no stale anchor index: {:?}", ds.anchors);
}
@@ -0,0 +1,206 @@
//! #12771: with the Delete Anchor Point tool a right-click on the canvas opens the Pen's path menu
//! with one entry for what is under the pointer on top: Delete Anchor Point on an anchor, Add
//! Anchor Point on a segment, nothing on empty canvas. Opening it records no history step and
//! leaves the anchor selection alone. The helpers are shared with the Convert Point tests (#12744).
use egui::Modifiers;
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use photocraft_doc::vector::Path;
use serde_json::json;
use super::tests::{harness, right_click, screen_of};
use super::{PEN_MENU, rows};
use crate::PhotocraftApp;
use crate::canvas::{ToolEvent, tool_event};
use crate::state::Tool;
/// 800 x 600 with a saved, targeted **Path 1**: a closed square (100,300)(300,300)(300,500)
/// (100,500), an open line (400,300)->(700,300) and an open S-curve (400,450)->(700,450).
pub(super) fn setup(tool: Tool) -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 800, "height": 600})).unwrap();
let path = json!({"subpaths": [
{"closed": true, "knots": [[100, 300], [300, 300], [300, 500], [100, 500]]},
{"closed": false, "knots": [[400, 300], [700, 300]]},
{"closed": false, "knots": [
{"anchor": [400, 450], "in": [400, 450], "out": [500, 350]},
{"anchor": [700, 450], "in": [600, 550], "out": [700, 450]},
]},
]});
app.run("path.set", json!({"name": "Path 1", "path": path})).unwrap();
app.ui.selected_path = Some("Path 1".into());
app.ui.tool = tool;
app
}
/// A harness over [`setup`] with the right-click at document point `at` done.
pub(super) fn right_clicked(tool: Tool, at: [f32; 2]) -> Harness<'static, PhotocraftApp> {
let mut h = harness(setup(tool));
let p = screen_of(h.state(), at);
right_click(&mut h, p, Modifiers::NONE);
h
}
pub(super) fn saved(app: &PhotocraftApp) -> Path {
app.session.active().unwrap().doc.paths.iter().find(|p| p.name == "Path 1").unwrap().path.clone()
}
pub(super) fn steps(app: &PhotocraftApp) -> usize {
app.session.active().unwrap().history.entries().len()
}
/// The path menu is open and its rows are the Pen's, in the Pen's order.
fn assert_path_menu(h: &Harness<'static, PhotocraftApp>, tool: Tool) {
assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.tool == tool && rows(m) == PEN_MENU), "{tool:?}: the path menu is open");
}
/// The first entry of the open menu is `label`, above Create Vector Mask, and is the only one of
/// the two Add / Delete Anchor Point entries shown.
pub(super) fn assert_top_entry(h: &Harness<'static, PhotocraftApp>, tool: Tool, label: &str) {
assert_path_menu(h, tool);
let other = if label == "Delete Anchor Point" { "Add Anchor Point" } else { "Delete Anchor Point" };
assert!(h.query_by_label(other).is_none(), "{other} is not offered");
assert!(h.get_by_label(label).rect().top() < h.get_by_label("Create Vector Mask").rect().top(), "{label} heads the menu");
}
/// The context-menu contract after a menu edit: undo restores the whole original path and redo
/// replays the edit (the path as it was right after the menu choice, `edited`).
fn assert_undo_redo(h: &mut Harness<'static, PhotocraftApp>, original: &Path, edited: &Path) {
h.state_mut().run("edit.undo", json!({})).unwrap();
assert_eq!(&saved(h.state()), original, "undo restores the whole path");
h.state_mut().run("edit.redo", json!({})).unwrap();
assert_eq!(&saved(h.state()), edited, "redo replays the menu edit");
}
/// Right-click the corner (300,300), choose Delete Anchor Point: the square stays closed with
/// three anchors and the history gains one step by that name.
pub(super) fn delete_the_corner(tool: Tool) {
let mut h = right_clicked(tool, [300.0, 300.0]);
assert_top_entry(&h, tool, "Delete Anchor Point");
let before = steps(h.state());
let original = saved(h.state());
h.get_by_label("Delete Anchor Point").click();
h.run_steps(2);
let path = saved(h.state());
assert!(path.subpaths[0].closed, "the square stays closed");
assert_eq!(path.subpaths[0].knots.len(), 3);
assert!(path.subpaths[0].knots.iter().all(|k| (k.anchor.x, k.anchor.y) != (300.0, 300.0)), "(300,300) is gone");
assert_eq!(steps(h.state()), before + 1);
assert_eq!(h.state().session.active().unwrap().history.undo_label(), Some("Delete Anchor Point"));
assert_undo_redo(&mut h, &original, &path);
}
/// Right-click the top edge (200,300): Add Anchor Point heads the menu and adds one there.
pub(super) fn add_on_the_edge(tool: Tool) {
let mut h = right_clicked(tool, [200.0, 300.0]);
assert_top_entry(&h, tool, "Add Anchor Point");
let before = steps(h.state());
let original = saved(h.state());
h.get_by_label("Add Anchor Point").click();
h.run_steps(2);
let path = saved(h.state());
assert_eq!(path.subpaths[0].knots.len(), 5);
let added = &path.subpaths[0].knots[1].anchor;
assert!((added.x - 200.0).abs() < 3.0 && (added.y - 300.0).abs() < 3.0, "the anchor lands where it was clicked: {added:?}");
assert_eq!(steps(h.state()), before + 1);
assert_eq!(h.state().session.active().unwrap().history.undo_label(), Some("Add Anchor Point"));
assert_undo_redo(&mut h, &original, &path);
}
/// Right-click empty canvas with the path targeted: the path menu with no top entry.
pub(super) fn empty_canvas(tool: Tool) {
let h = right_clicked(tool, [500.0, 100.0]);
assert_path_menu(&h, tool);
assert!(h.query_by_label("Delete Anchor Point").is_none() && h.query_by_label("Add Anchor Point").is_none(), "no top entry");
assert!(h.query_by_label("Create Vector Mask").is_some());
}
/// Opening the menu on an anchor and on a segment adds no history step, changes no path and
/// leaves the Direct Selection state as it was (here: one anchor selected).
pub(super) fn opening_changes_nothing(tool: Tool) {
for at in [[300.0, 300.0], [200.0, 300.0]] {
let mut app = setup(tool);
// Direct Selection picks the anchor at (100,300); the tool under test takes over.
app.ui.tool = Tool::DirectSelection;
tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 300.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 300.0 }, Modifiers::NONE);
assert_eq!(app.ui.direct_selection.anchors, vec![[0, 0]], "an anchor is selected");
app.ui.tool = tool;
let (path, n) = (saved(&app), steps(&app));
let mut h = harness(app);
// The selection once the first frames have run: Convert Point keeps Direct Selection's,
// Delete Anchor Point already drops it today, menu or not.
let selection = h.state().ui.direct_selection.clone();
assert_eq!(!selection.anchors.is_empty(), tool == Tool::ConvertPoint, "{tool:?}: selection before the right-click");
let p = screen_of(h.state(), at);
right_click(&mut h, p, Modifiers::NONE);
assert!(h.state().ui.canvas_tool_menu.is_some(), "the menu opened at {at:?}");
assert_eq!(steps(h.state()), n, "no history step at {at:?}");
assert_eq!(h.state().ui.direct_selection, selection, "the anchor selection is as it was at {at:?}");
assert_eq!(saved(h.state()), path, "no path changed at {at:?}");
}
}
#[test]
fn right_click_on_an_anchor_offers_delete_anchor_point_on_top_and_it_deletes() {
delete_the_corner(Tool::DeleteAnchorPoint);
}
#[test]
fn right_click_on_a_segment_offers_add_anchor_point_on_top() {
add_on_the_edge(Tool::DeleteAnchorPoint);
}
#[test]
fn right_click_on_empty_canvas_opens_the_path_menu_without_a_top_entry() {
empty_canvas(Tool::DeleteAnchorPoint);
}
#[test]
fn opening_the_menu_adds_no_history_step_and_keeps_the_anchor_selection() {
opening_changes_nothing(Tool::DeleteAnchorPoint);
}
/// The entry is about the path as it was when the menu opened: an edit meanwhile (a control call
/// inserting an anchor before the corner) shifts the knots, so choosing it deletes nothing.
#[test]
fn a_point_entry_expires_once_the_path_is_edited() {
let mut h = right_clicked(Tool::DeleteAnchorPoint, [300.0, 300.0]);
assert_top_entry(&h, Tool::DeleteAnchorPoint, "Delete Anchor Point");
h.state_mut().run("path.addAnchor", json!({"name": "Path 1", "subpath": 0, "knot": 0, "t": 0.5})).unwrap();
let after_add = saved(h.state());
assert_eq!(after_add.subpaths[0].knots.len(), 5, "the anchor went in");
let n = steps(h.state());
let ctx = h.ctx.clone();
super::choose(h.state_mut(), &ctx, "path.deleteAnchor");
assert_eq!(saved(h.state()), after_add, "the stale entry deleted nothing");
assert_eq!(steps(h.state()), n, "and added no history step");
}
/// The expired entry is drawn disabled, not just rejected on click (#12771, CodeRabbit).
#[test]
fn an_expired_point_entry_is_drawn_disabled() {
let mut h = right_clicked(Tool::DeleteAnchorPoint, [300.0, 300.0]);
assert!(!h.get_by_label("Delete Anchor Point").accesskit_node().is_disabled(), "enabled while the path is as it was");
h.state_mut().run("path.addAnchor", json!({"name": "Path 1", "subpath": 0, "knot": 0, "t": 0.5})).unwrap();
h.run();
assert!(h.get_by_label("Delete Anchor Point").accesskit_node().is_disabled(), "disabled once the path is edited");
}
/// A shape with only its own position lock is ignored by the anchor tools' click (#12730, #12825),
/// so the menu offers no point entry on its corner either: the rest of the menu is as before.
#[test]
fn a_shape_with_only_its_position_lock_gets_no_point_entry() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 800, "height": 600})).unwrap();
let shape = app.run("shape.create", json!({"kind": "rect", "rect": [450, 100, 200, 150], "fill": "#3070c0"})).unwrap()["layer"].as_u64().unwrap();
app.run("layer.select", json!({"layer": shape})).unwrap();
app.run("layer.lockLayers", json!({"position": true})).unwrap();
app.ui.tool = Tool::DeleteAnchorPoint;
let mut h = harness(app);
let p = screen_of(h.state(), [650.0, 250.0]);
right_click(&mut h, p, Modifiers::NONE);
assert!(h.state().ui.canvas_tool_menu.is_some(), "the menu opens");
assert!(h.query_by_label("Delete Anchor Point").is_none() && h.query_by_label("Add Anchor Point").is_none(), "no point entry on the locked shape");
}
@@ -526,7 +526,7 @@ fn zoom_app(w: u32, h: u32) -> PhotocraftApp {
app
}
fn screen_of(app: &PhotocraftApp, doc: [f32; 2]) -> Pos2 {
pub(super) fn screen_of(app: &PhotocraftApp, doc: [f32; 2]) -> Pos2 {
crate::canvas::ViewXform::active(app).unwrap().to_screen(doc[0], doc[1])
}
@@ -62,6 +62,7 @@ fn type_layers_offer_free_transform_but_not_distort_or_perspective() {
sampler_at: None,
color: None,
inside_selection: false,
point: None,
};
assert!(crate::canvas_tool_menu::available(&app, &menu, "edit.freeTransform"));
assert!(!crate::canvas_tool_menu::available(&app, &menu, "edit.transform.distort"));