Skip to content

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

Pen and Path Selection: Delete deletes the finished work path (#5702) (#5732)

Michael Higgins56d3b7f983af
diff --git a/crates/ui-egui/src/pen_draw_tests.rs b/crates/ui-egui/src/pen_draw_tests.rs
index d8bb39d82..155450eac 100644
--- a/crates/ui-egui/src/pen_draw_tests.rs
+++ b/crates/ui-egui/src/pen_draw_tests.rs
@@ -171,3 +171,51 @@ fn dragging_on_the_first_anchor_closes_with_its_handles_in_one_step() {
assert!(sp.closed && sp.knots.len() == 3);
assert!(near([k.in_ctrl.x, k.in_ctrl.y], [0.0, 20.0]) && near([k.out_ctrl.x, k.out_ctrl.y], [40.0, 20.0]), "{k:?}");
}
+
+/// #5702: a finished work path showed again whenever the Pen came back, and ⌫ / Delete reached
+/// Edit › Clear (the pixels under it) instead, so the path looked impossible to delete. With the
+/// Pen the key deletes the path, in one undoable step, and leaves the pixels; other tools keep
+/// Edit › Clear.
+#[test]
+fn delete_with_the_pen_deletes_the_finished_work_path_not_the_pixels() {
+ let mut h = harness();
+ h.state_mut().run("layer.new.layer", json!({})).unwrap();
+ h.state_mut()
+ .session
+ .edit("paint", |doc, a| {
+ doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 200, 150), &[1.0, 0.0, 0.0, 1.0]);
+ Ok(())
+ })
+ .unwrap();
+ for p in [[20.0, 20.0], [120.0, 20.0], [120.0, 100.0]] {
+ click(&mut h, p);
+ }
+ h.key_press(Key::Enter);
+ h.run_steps(2);
+ let work = |h: &Harness<'static, PhotocraftApp>| h.state().session.active().unwrap().doc.work_path.is_some();
+ let red = |h: &Harness<'static, PhotocraftApp>| {
+ let st = h.state().session.active().unwrap();
+ st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(60, 60) == [1.0, 0.0, 0.0, 1.0]
+ };
+ assert!(work(&h) && h.state().ui.pen.is_none(), "a finished work path");
+ let steps = h.state().session.active().unwrap().history.past_len();
+
+ h.key_press(Key::Delete);
+ h.run_steps(2);
+ assert!(!work(&h), "Delete deletes the work path");
+ assert!(red(&h), "and leaves the pixels under it");
+ assert_eq!(h.state().session.active().unwrap().history.past_len(), steps + 1, "one History step");
+ h.state_mut().run("edit.undo", json!({})).unwrap();
+ assert!(work(&h), "Undo brings it back");
+
+ h.state_mut().ui.tool = Tool::PathSelection;
+ h.key_press(Key::Backspace);
+ h.run_steps(2);
+ assert!(!work(&h), "Backspace with Path Selection deletes it too");
+ h.state_mut().run("edit.undo", json!({})).unwrap();
+
+ h.state_mut().ui.tool = Tool::Brush;
+ h.key_press(Key::Delete);
+ h.run_steps(2);
+ assert!(work(&h), "another tool's Delete is Edit › Clear, not Delete Path");
+}
diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs
index a2f3f17c1..e8645a151 100644
--- a/crates/ui-egui/src/shortcuts.rs
+++ b/crates/ui-egui/src/shortcuts.rs
@@ -477,6 +477,19 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {
crate::vector_ui::pen_commit(app, false);
return;
}
+ // A finished work path shown by the Pen or Path Selection: ⌫/Delete deletes it, as in
+ // Photoshop (#5702), never Edit › Clear on the pixels under it.
+ if focus != Focus::DeletingWidget && crate::vector_ui::delete_key_deletes_work_path(app) {
+ let mods = ctx.input(|i| i.modifiers);
+ if !mods.command && !mods.ctrl && !mods.shift && !mods.alt && ctx.input_mut(|i| i.consume_key(mods, Key::Backspace) || i.consume_key(mods, Key::Delete))
+ {
+ if let Err(e) = app.run("path.delete", json!({"name": "work"})) {
+ app.ui.status = e;
+ app.ui.status_error = true;
+ }
+ return;
+ }
+ }
// Magnetic Lasso: ↩ closes, Esc cancels, ⌫ removes a fastening point (not Edit › Clear),
// [ ] change the detection width (not the brush size).
if crate::magnetic_lasso_ui::keys(app, ctx) {
diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs
index f68a4a6a4..9682ba2ed 100644
--- a/crates/ui-egui/src/vector_ui.rs
+++ b/crates/ui-egui/src/vector_ui.rs
@@ -725,6 +725,19 @@ pub(crate) fn active_shape_path(app: &PhotocraftApp) -> Option<(u64, Path)> {
}
}
+/// Does ⌫ / Delete delete the work path? With the Pen, Freeform Pen or Path Selection tool, no
+/// path being drawn and the work path the one these tools show and edit (no shape layer or
+/// targeted vector mask in front of it), as in Photoshop (#5702). It was drawn whenever one of
+/// these tools was active, but the key reached Edit › Clear, so it looked impossible to delete.
+/// Direct Selection and the Curvature Pen delete selected anchors in Photoshop, not the path.
+pub(crate) fn delete_key_deletes_work_path(app: &PhotocraftApp) -> bool {
+ matches!(app.ui.tool, Tool::Pen | Tool::FreeformPen | Tool::PathSelection)
+ && app.ui.pen.is_none()
+ && app.ui.transform.is_none()
+ && app.ui.view.shows(app.ui.view.show.target_path)
+ && matches!(target_path(app), Some((PathTarget::Work, _)))
+}
+
/// The path Path Selection edits: the targeted vector mask, the active shape layer's path, else
/// the work path.
fn target_path(app: &PhotocraftApp) -> Option<(PathTarget, Path)> {