PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix #1229: the Polygonal Lasso tool removes its last vertex on Backspace (follow-up to #1262) (#1338)
BFlatc4d1c1f8d13b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex bdeae2dd2..fba51b63c 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -2113,7 +2113,12 @@ 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);+ let lasso_retracted = response.secondary_clicked()+ && match tool {+ Tool::Lasso => crate::lasso_ui::undo_last_vertex(app),+ Tool::PolygonLasso => polygon_retract(app),+ _ => false,+ };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);@@ -3310,6 +3315,18 @@ fn polygon_click(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers)app.ui.polygon.push([x, y]);}+/// Remove the Polygonal Lasso's last vertex (⌫, Delete or a right-click while drawing, #1229);+/// removing the only one cancels the polygon. False when no polygon is being drawn.+pub fn polygon_retract(app: &mut PhotocraftApp) -> bool {+ if app.ui.polygon.pop().is_none() {+ return false;+ }+ if app.ui.polygon.is_empty() {+ app.ui.polygon_mode.clear();+ }+ true+}+/// A new-selection polygonal (or magnetic) lasso is being drawn, so the selection it will replace/// is hidden.pub fn polygon_replaces_selection(app: &PhotocraftApp) -> bool {diff --git a/crates/ui-egui/src/polygon_lasso_tests.rs b/crates/ui-egui/src/polygon_lasso_tests.rsindex aac2df9ff..62da06b56 100644--- a/crates/ui-egui/src/polygon_lasso_tests.rs+++ b/crates/ui-egui/src/polygon_lasso_tests.rs@@ -286,6 +286,38 @@ fn cmd_drag_inside_the_selection_floats_it_with_the_polygonal_lasso() {assert_eq!(app.ui.polygon.len(), 1);}+#[test]+fn backspace_delete_and_right_click_remove_the_last_vertex() {+ // #1229: ⌫ took Edit › Clear and wiped the layer while a polygon was being drawn.+ let mut h = harness();+ for (x, y) in [(50.0, 50.0), (150.0, 50.0), (150.0, 120.0)] {+ click(&mut h, x, y, Modifiers::NONE);+ // Past egui's double-click window, so each click adds a vertex.+ h.run_steps(20);+ }+ assert_eq!(h.state().ui.polygon.len(), 3);+ let st = h.state().session.active().unwrap();+ let (layer, steps) = (st.active_layer.unwrap(), st.history.past_len());+ for key in [egui::Key::Backspace, egui::Key::Delete] {+ h.event(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });+ h.run_steps(2);+ }+ assert_eq!(h.state().ui.polygon.len(), 1);+ // A right-click removes the last one and cancels the polygon, without a context menu.+ let p = screen(&h, 90.0, 90.0);+ h.event(egui::Event::PointerMoved(p));+ h.run_steps(1);+ h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::NONE });+ h.run_steps(1);+ h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::NONE });+ h.run_steps(2);+ assert!(h.state().ui.polygon.is_empty() && h.state().ui.polygon_mode.is_empty());+ assert!(h.state().ui.canvas_tool_menu.is_none());+ let st = h.state().session.active().unwrap();+ assert!(st.doc.layer(layer).is_some(), "the layer survives");+ assert_eq!(st.history.past_len(), steps, "nothing was edited");+}+/// ⌥ held: a drag draws freehand into the polygon; releasing ⌥ goes back to straight segments,/// and the polygon stays open (a new selection, even though ⌥ was held at the first click).#[test]diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rsindex 86779ac5b..4918f8edb 100644--- a/crates/ui-egui/src/shortcuts.rs+++ b/crates/ui-egui/src/shortcuts.rs@@ -316,6 +316,15 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {return;}}+ // The Polygonal Lasso tool likewise: ⌫/Delete removes the last vertex (#1229), and never+ // reaches Edit › Clear while a polygon is being drawn.+ if !app.ui.polygon.is_empty() {+ 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::canvas::polygon_retract(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`]).