Skip to content

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

fix(ui): allow removing the last polygonal lasso point (#1262)

Nigel Ihza Firdausy5054308d0b88
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 7e93f02b2..259db9703 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1817,12 +1817,14 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
{
crate::canvas_tool_menu::open_transform(app, [p.x, p.y]);
}
+ let lasso_retracted = tool == Tool::Lasso && response.secondary_clicked() && crate::lasso_ui::undo_last_vertex(app);
if tool == Tool::Lasso {
crate::lasso_ui::canvas_input(app, &ctx, &xf, &response);
(buttons.started, buttons.dragged, buttons.stopped, buttons.clicked) = (false, false, false, false);
}
// Right-click with the Move tool, or ⌘/Ctrl+right-click: the layers under the pointer.
if response.secondary_clicked()
+ && !lasso_retracted
&& !transforming
&& crate::layer_pick_ui::is_gesture(tool, mods)
&& let Some(p) = response.interact_pointer_pos()
@@ -1832,6 +1834,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
crate::layer_pick_ui::open(app, [p.x, p.y], d[0], d[1]);
}
if response.secondary_clicked()
+ && !lasso_retracted
&& !transforming
&& !crate::layer_pick_ui::is_gesture(tool, mods)
&& let Some(p) = response.interact_pointer_pos()
diff --git a/crates/ui-egui/src/lasso_ui.rs b/crates/ui-egui/src/lasso_ui.rs
index 889714818..2328f748b 100644
--- a/crates/ui-egui/src/lasso_ui.rs
+++ b/crates/ui-egui/src/lasso_ui.rs
@@ -20,6 +20,29 @@ pub fn active(app: &PhotocraftApp) -> bool {
app.drag.as_ref().is_some_and(|d| d.tool == Tool::Lasso && d.lasso.is_some())
}
+/// Between polygonal clicks, Backspace/right-click retracts the last fixed vertex.
+/// Do not affect a freehand stroke whose primary button is still held.
+pub fn waiting_for_vertex(app: &PhotocraftApp) -> bool {
+ app.ui.tool == Tool::Lasso
+ && app
+ .drag
+ .as_ref()
+ .is_some_and(|d| d.tool == Tool::Lasso && d.lasso.as_ref().is_some_and(|l| !l.down && l.document == app.session.active().map(|st| st.doc.id)))
+}
+
+pub fn undo_last_vertex(app: &mut PhotocraftApp) -> bool {
+ cancel_stale(app);
+ if !waiting_for_vertex(app) {
+ return false;
+ }
+ if app.drag.as_ref().is_some_and(|d| d.points.len() <= 1) {
+ app.drag = None;
+ } else if let Some(d) = app.drag.as_mut() {
+ d.points.pop();
+ }
+ true
+}
+
/// A lasso drag that moves the selection (started inside it, `canvas::selection_drag_kind`):
/// the canvas's own selection-move path handles it, this module only feeds it the events.
fn moving_selection(app: &PhotocraftApp) -> bool {
diff --git a/crates/ui-egui/src/lasso_ui/tests.rs b/crates/ui-egui/src/lasso_ui/tests.rs
index 7639170ed..97480f6b3 100644
--- a/crates/ui-egui/src/lasso_ui/tests.rs
+++ b/crates/ui-egui/src/lasso_ui/tests.rs
@@ -199,3 +199,54 @@ fn a_drag_inside_the_selection_moves_it() {
assert!(h.state().drag.is_none());
assert_eq!(bounds(h.state()), Some(photocraft_geom::Rect::new(110, 105, 160, 145)));
}
+
+#[test]
+fn retracting_polygonal_vertices_keeps_the_document_unchanged() {
+ let mut app = app();
+ begin(&mut app, Modifiers::NONE);
+ event(&mut app, "up", 120.0, 50.0, Modifiers::ALT);
+ assert!(waiting_for_vertex(&app));
+ let layer = app.session.active().unwrap().active_layer.unwrap();
+ assert_eq!(app.drag.as_ref().unwrap().points.len(), 3);
+ assert!(undo_last_vertex(&mut app));
+ assert_eq!(app.drag.as_ref().unwrap().points.len(), 2);
+ assert!(undo_last_vertex(&mut app));
+ assert_eq!(app.drag.as_ref().unwrap().points.len(), 1);
+ assert!(undo_last_vertex(&mut app));
+ assert!(!active(&app));
+ assert!(!undo_last_vertex(&mut app));
+ let st = app.session.active().unwrap();
+ assert!(st.doc.layer(layer).is_some());
+ assert!(st.doc.selection.is_none());
+}
+
+#[test]
+fn real_canvas_backspace_and_right_click_retract_polygonal_points() {
+ let mut h = harness();
+ mouse(&mut h, "down", 50.0, 50.0, Modifiers::NONE);
+ mouse(&mut h, "move", 120.0, 50.0, Modifiers::NONE);
+ mouse(&mut h, "up", 120.0, 50.0, Modifiers::ALT);
+ mouse(&mut h, "down", 180.0, 100.0, Modifiers::ALT);
+ mouse(&mut h, "up", 180.0, 100.0, Modifiers::ALT);
+ assert_eq!(h.state().drag.as_ref().unwrap().points.len(), 3);
+ let layer = h.state().session.active().unwrap().active_layer.unwrap();
+
+ h.event(Event::Key { key: egui::Key::Backspace, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::ALT });
+ h.run_steps(2);
+ assert_eq!(h.state().drag.as_ref().unwrap().points.len(), 2);
+ assert!(h.state().session.active().unwrap().doc.layer(layer).is_some());
+
+ let state = h.state();
+ let view = &state.ui.views[0];
+ let xf = ViewXform { rect: crate::rulers::content_rect(state, state.last_canvas_rect), zoom: view.zoom, center: view.center, flip: false };
+ let pos = xf.to_screen(155.0, 100.0);
+ h.event(Event::PointerMoved(pos));
+ h.event(Event::PointerButton { pos, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::ALT });
+ h.run_steps(1);
+ h.event(Event::PointerButton { pos, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::ALT });
+ h.run_steps(2);
+ assert_eq!(h.state().drag.as_ref().unwrap().points.len(), 1);
+ assert!(h.state().ui.canvas_tool_menu.is_none());
+ assert!(h.state().session.active().unwrap().doc.layer(layer).is_some());
+ assert!(h.state().session.active().unwrap().doc.selection.is_none());
+}
diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs
index 078c9f3b7..b413c44ea 100644
--- a/crates/ui-egui/src/shortcuts.rs
+++ b/crates/ui-egui/src/shortcuts.rs
@@ -296,6 +296,15 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {
if crate::magnetic_lasso_ui::keys(app, ctx) {
return;
}
+ // While placing polygonal lasso vertices, Backspace/Delete retracts the last point;
+ // it must win over the regular Edit › Clear shortcut, which edits document pixels.
+ if crate::lasso_ui::waiting_for_vertex(app) {
+ let mods = ctx.input(|i| i.modifiers);
+ if !mods.command && !mods.ctrl && !mods.shift && ctx.input_mut(|i| i.consume_key(mods, Key::Backspace) || i.consume_key(mods, Key::Delete)) {
+ crate::lasso_ui::undo_last_vertex(app);
+ return;
+ }
+ }
// Inline type editing eats text and navigation keys; ⌘-shortcuts still reach the menus.
let editing = crate::type_tool::handle_keys(app, ctx);
// Registry, UI and menu-catalogue shortcuts (see [`crate::shortcut_dispatch::bindings`]).