From 745a24bbc85f8732d9bc19ee875fb233f49a094c Mon Sep 17 00:00:00 2001 From: raykholo Date: Sun, 25 Oct 2026 01:26:15 +0000 Subject: [PATCH] Sketch: undo while drawing a spline removes the last point, not the sketch (#5304) (#5441) * claim: #5304 * Sketch: undo and Backspace remove the last pending spline point (#5304) While a spline tool has points placed, Cmd/Ctrl+Z and Backspace take back the last point instead of undoing sketch history or deleting the selection. Undo reaches sketch history again once no spline points remain. --- crates/ui-egui/src/sketch_shortcut_tests.rs | 53 +++++++++++++++++++++ crates/ui-egui/src/toolbar.rs | 7 ++- crates/ui-egui/src/tools.rs | 11 +++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/sketch_shortcut_tests.rs b/crates/ui-egui/src/sketch_shortcut_tests.rs index 9a330c99b..f5bcdeb8f 100644 --- a/crates/ui-egui/src/sketch_shortcut_tests.rs +++ b/crates/ui-egui/src/sketch_shortcut_tests.rs @@ -17,6 +17,59 @@ fn press(h: &mut Harness, key: &str) { h.settle(); } +#[test] +fn spline_undo_removes_pending_points_before_sketch_history() { + for cmd in ["sketch.spline.fit_point", "sketch.spline.control_point", "sketch.spline.control_point_5"] { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.rectangle.two_point", json!({"p0":[-20,-20],"p1":[-10,-10]})).unwrap(); + h.settle(); + h.step(&json!({"start":cmd})).unwrap(); + for point in [[2, 2, 0], [8, 2, 0], [9, 7, 0]] { + h.step(&json!({"click":{"world":point}})).unwrap(); + } + let points = h.app.tool.as_ref().unwrap().pts.clone(); + assert_eq!(points.len(), 3); + let before = (h.app.session.doc.to_json(), h.app.session.undo.len(), h.app.session.revision); + for remaining in (0..3).rev() { + h.step(&json!({"key":"Z","cmd":true})).unwrap(); + h.settle(); + assert_eq!(h.app.tool.as_ref().unwrap().pts, points[..remaining], "{cmd}"); + assert_eq!((h.app.session.doc.to_json(), h.app.session.undo.len(), h.app.session.revision), before, "{cmd}"); + } + assert_eq!(tool(&h), Some(cmd)); + h.step(&json!({"key":"Z","cmd":true})).unwrap(); + h.settle(); + assert_eq!(h.app.session.undo.len(), before.1 - 1, "history resumes once the spline is empty"); + let sketch = h.app.session.active_sketch.unwrap(); + assert!(h.app.session.model.state().sketch(sketch).unwrap().sketch.curves.is_empty()); + } +} + +#[test] +fn spline_backspace_removes_pending_points_and_keeps_the_tool_ready() { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.rectangle.two_point", json!({"p0":[-20,-20],"p1":[-10,-10]})).unwrap(); + h.settle(); + h.step(&json!({"start":"sketch.spline.fit_point"})).unwrap(); + for point in [[2, 2, 0], [8, 2, 0], [9, 7, 0]] { + h.step(&json!({"click":{"world":point}})).unwrap(); + } + let points = h.app.tool.as_ref().unwrap().pts.clone(); + assert_eq!(points.len(), 3); + let before = (h.app.session.doc.to_json(), h.app.session.undo.len(), h.app.session.revision); + for remaining in (0..3).rev() { + press(&mut h, "Backspace"); + assert_eq!(h.app.tool.as_ref().unwrap().pts, points[..remaining]); + assert_eq!((h.app.session.doc.to_json(), h.app.session.undo.len(), h.app.session.revision), before); + } + assert_eq!(tool(&h), Some("sketch.spline.fit_point")); + press(&mut h, "Escape"); + assert!(tool(&h).is_none()); + assert_eq!((h.app.session.doc.to_json(), h.app.session.undo.len(), h.app.session.revision), before); +} + #[test] fn sketch_keys_outside_a_sketch_sketch_on_the_selected_plane_and_start_their_tool() { for (key, cmd) in diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs index c821a1aff..7a8a7582d 100644 --- a/crates/ui-egui/src/toolbar.rs +++ b/crates/ui-egui/src/toolbar.rs @@ -2029,7 +2029,11 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) { }); } _ if !shown => {} - Key::Z => drop(app.run("edit.undo", json!({}))), + Key::Z => { + if !crate::tools::undo_spline_point(app) { + drop(app.run("edit.undo", json!({}))); + } + } Key::Y => drop(app.run("edit.redo", json!({}))), // Copy, Cut and Paste (`edit_clipboard::handle`). Key::C | Key::X | Key::V if !mods.shift && !mods.alt => {} @@ -2101,6 +2105,7 @@ pub fn shortcuts(app: &mut SolveApp, ctx: &egui::Context) { crate::context_menu::rename_selection(app, at); } Key::F6 => app.animate_view("fit"), + Key::Backspace if crate::tools::undo_spline_point(app) => {} Key::Delete | Key::Backspace => { if !crate::timeline_left::delete_selected_folders(app) { crate::viewport::delete_selection(app); diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 638232dc7..d31f9f3c0 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -652,6 +652,17 @@ fn run_pick(app: &mut SolveApp, t: &Tool) { let _ = app.run(t.cmd, params); } +/// Take back a pending spline point before undo or Backspace reaches the sketch. +pub fn undo_spline_point(app: &mut SolveApp) -> bool { + let Some(t) = app.tool.as_mut() else { return false }; + if !matches!(t.cmd, "sketch.spline.fit_point" | "sketch.spline.control_point" | "sketch.spline.control_point_5") || t.pts.is_empty() { + return false; + } + t.pts.pop(); + crate::sketch_dims::reset(t); + true +} + /// Right click / Esc: end the current chain (line) or cancel the tool. pub fn finish(app: &mut SolveApp) { crate::dim_view::cancel();