diff --git a/crates/ui-egui/src/tools/vector_ui/host.rs b/crates/ui-egui/src/tools/vector_ui/host.rs index c91fa8b7c3..066bae8a85 100644 --- a/crates/ui-egui/src/tools/vector_ui/host.rs +++ b/crates/ui-egui/src/tools/vector_ui/host.rs @@ -308,6 +308,9 @@ fn edit_draft(app: &mut PhotocraftApp, edit: EditEvent) -> bool { fn set_draft(app: &mut PhotocraftApp, value: EditSubpath) { let n = value.knots.len(); let selection_on_draft = app.ui.direct_selection.target == Some(PathRef::Pen); + // A refused draft whose document changed since takes no press, only Delete and Backspace: it stays refused, or + // `cancel_stale_pen` would drop all of it on the next pass instead of the one anchor (#15488). + let stale = app.ui.pen.as_ref().and_then(|p| p.owner.as_ref()).is_some_and(|o| !o.valid(app)); let Some(pen) = ensure_draft(app) else { return }; if pen.draft == value { return; @@ -325,7 +328,7 @@ fn set_draft(app: &mut PhotocraftApp, value: EditSubpath) { } } pen.draft = value; - pen.commit_failed = false; + pen.commit_failed &= stale; if selection_on_draft { let ds = &mut app.ui.direct_selection; if grew { diff --git a/crates/ui-egui/src/tools/vector_ui/pen_resume_tests.rs b/crates/ui-egui/src/tools/vector_ui/pen_resume_tests.rs index c8fc52e9a0..0da2cd1948 100644 --- a/crates/ui-egui/src/tools/vector_ui/pen_resume_tests.rs +++ b/crates/ui-egui/src/tools/vector_ui/pen_resume_tests.rs @@ -735,3 +735,38 @@ fn freeform_selection_does_not_claim_a_failed_pen_or_curvature_draft() { } } } + +/// A refused Pen draft is kept (#15488): under another tool, Delete is that tool's (the active layer goes); back under +/// the Pen that drew it, Delete takes back one anchor at a time, as while drawing, and only the last anchor drops the +/// draft. +#[test] +fn a_kept_pen_draft_takes_delete_only_under_its_own_tool() { + for tool in [Tool::Pen, Tool::CurvaturePen] { + let mut app = app(); + app.run("layer.new.layer", json!({})).unwrap(); + app.ui.tool = tool; + app.ui.tool_options.vector_mode = craft::draw::ui::shape::VectorMode::Path; + three_anchors(&mut app); + app.session.authorize = Some(|id, _| if id == "path.set" { Err(photocraft_engine::EngineError::Other("refused path fixture".into())) } else { Ok(()) }); + assert!(!pen_commit(&mut app, false), "the fixture must actually refuse the commit"); + app.session.authorize = None; + assert_eq!(app.ui.pen.as_ref().unwrap().draft.knots.len(), 3); + let layers = app.session.active().unwrap().doc.layers.len(); + let kept = app.ui.pen.as_ref().unwrap().draft.clone(); + + switch_tool(&mut app, Tool::AddAnchorPoint); + press(&mut app, egui::Key::Delete, egui::Modifiers::NONE); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers - 1, "{tool:?}: Delete is the other tool's: the layer goes"); + assert_eq!(app.ui.pen.as_ref().map(|p| &p.draft), Some(&kept), "{tool:?}: the kept draft is untouched"); + + switch_tool(&mut app, tool); + for left in [2, 1] { + press(&mut app, egui::Key::Delete, egui::Modifiers::NONE); + assert_eq!(app.ui.pen.as_ref().map(|p| p.draft.knots.len()), Some(left), "{tool:?}: Delete takes back one anchor ({})", app.ui.status); + } + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers - 1, "{tool:?}: no other layer went"); + press(&mut app, egui::Key::Delete, egui::Modifiers::NONE); + assert!(app.ui.pen.is_none(), "{tool:?}: the last anchor drops the draft"); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers - 1, "{tool:?}: the last Delete is the drawing tool's too"); + } +}