From c4d1c1f8d13bb0000c448ffa6c998b2c5c00ffa1 Mon Sep 17 00:00:00 2001 From: BFlat Date: Fri, 9 Oct 2026 08:07:54 +0700 Subject: [PATCH] Fix #1229: the Polygonal Lasso tool removes its last vertex on Backspace (follow-up to #1262) (#1338) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix #1229: the Polygonal Lasso tool removes its last vertex on Backspace Alt-click polygon mode. The Polygonal Lasso tool itself keeps its points elsewhere (ui.polygon), so there Backspace still reached Edit › Clear and wiped the layer, the data loss reported in #1229. It now removes the last vertex there too (Delete as well), a right-click does the same, and removing the only vertex cancels the polygon. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UHgFEHEsefoQoBParH47JA * Pipette cursor test: Windows draws the precise crosshair itself input_tests::eyedropper_and_alt_sampling_show_a_pipette (#1028) expected the OS crosshair with Other Cursors = Precise, but on Windows the canvas draws its own crosshair and hides the OS cursor (visible_crosshair), so the test failed on every Windows run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UHgFEHEsefoQoBParH47JA --------- Co-authored-by: Claude --- crates/ui-egui/src/canvas.rs | 19 +++++++++++++- crates/ui-egui/src/polygon_lasso_tests.rs | 32 +++++++++++++++++++++++ crates/ui-egui/src/shortcuts.rs | 9 +++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index bdeae2dd2b..fba51b63cf 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.rs index aac2df9ff1..62da06b563 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.rs index 86779ac5bc..4918f8edb6 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`]).