From 64a55392d8d774c64b2d710baf35d361b18ea595 Mon Sep 17 00:00:00 2001 From: HiroKenEz <59478073+HiroKenEz@users.noreply.github.com> Date: Sun, 1 Nov 2026 09:19:23 +0000 Subject: [PATCH] Sketch: a Line chain survives Undo (#10016) (#10019) * claim: #10016 * Sketch: a Line chain survives Undo (#10016) Undo could take away the segment the chain was drawing on from, and every further click was then refused with "unknown sketch point". The chain now remembers where each segment started and goes back to the last one still there. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/sketch_line_arc.rs | 58 ++++++++- .../ui-egui/src/sketch_moving_click_tests.rs | 15 ++- crates/ui-egui/src/sketch_shortcut_tests.rs | 110 ++++++++++++++++++ crates/ui-egui/src/tools.rs | 7 +- 4 files changed, 181 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/sketch_line_arc.rs b/crates/ui-egui/src/sketch_line_arc.rs index 4ee31e69b..3c45ff68f 100644 --- a/crates/ui-egui/src/sketch_line_arc.rs +++ b/crates/ui-egui/src/sketch_line_arc.rs @@ -35,15 +35,60 @@ pub struct LineChain { rest: Option<(Pos2, f64)>, /// Just switched back to lines: says so until the pointer leaves. back_to_lines: bool, + /// Where each segment started and the curve the chain was on there, oldest first: where it + /// goes back to when Undo takes that segment away (#10016). + back: Vec<((Vec2, Option), Option)>, } impl LineChain { - /// Curve `id` now ends the chain, with the pointer still on its end. - pub fn placed(&mut self, id: &str) { - *self = LineChain { curve: Some(id.to_string()), ..LineChain::default() }; + /// Curve `id`, drawn from `start`, now ends the chain, with the pointer still on its end. + pub fn placed(&mut self, id: &str, start: (Vec2, Option)) { + let mut back = std::mem::take(&mut self.back); + back.push((start, self.curve.take())); + *self = LineChain { curve: Some(id.to_string()), back, ..LineChain::default() }; } } +/// Undo can take away the segment a chain is drawing on from (#10016). The chain then goes back to +/// the end of the last segment still there, or to where it began (as lines: there is nothing to +/// be tangent to), and ends if that point has gone too. Clicking on from a point that no longer +/// exists would only be refused. +pub fn rejoin(app: &SolveApp, t: &mut Tool) { + if t.cmd != LINE && !in_arc_mode(t) { + return; + } + let Some(Some(current)) = t.pts.last().map(|p| p.1.clone()) else { return }; + let state = app.session.world_state(); + let Some(ss) = app.session.active_sketch.and_then(|id| state.sketch(id)) else { return }; + let at = |id: &str| ss.sketch.resolve_point(id).and_then(|p| ss.sketch.point(p)); + if at(¤t).is_some() { + return; + } + let mut back = std::mem::take(&mut t.chain.back); + let mut to = None; + while let Some(((point, id), curve)) = back.pop() { + match id { + None => to = Some(((point, None), curve)), + Some(id) => match at(&id) { + Some(point) => to = Some(((point, Some(id)), curve)), + None => continue, + }, + } + break; + } + let (pts, curve) = to.map_or((Vec::new(), None), |(point, curve)| (vec![point], curve)); + if curve.is_none() + && t.cmd != LINE + && let Some(line) = Tool::for_command(LINE) + { + *t = line; + } + t.pts = pts; + t.lined.clear(); + crate::sketch_dims::reset(t); + t.chain = LineChain { curve, back, ..LineChain::default() }; +} + /// Whether the tool is a Line tool's chain drawing tangent arcs, not the Tangent Arc tool itself. pub fn in_arc_mode(t: &Tool) -> bool { t.cmd == TANGENT_ARC && t.chain.curve.is_some() @@ -108,7 +153,7 @@ fn switch(app: &mut SolveApp) { let Some(mut next) = next else { return }; next.pts = old.pts.clone(); next.hover = old.hover.clone(); - next.chain = LineChain { curve: old.chain.curve.clone(), back_to_lines: !to_arc, ..LineChain::default() }; + next.chain = LineChain { curve: old.chain.curve.clone(), back_to_lines: !to_arc, back: old.chain.back.clone(), ..LineChain::default() }; app.tool = Some(next); let hint = if to_arc { app.tr("ui.line_arc_hint").to_string() } else { crate::tools::hint(LINE) }; app.set_status(hint, false); @@ -139,8 +184,11 @@ pub fn place_arc(app: &mut SolveApp, tool: &mut Tool, end: &(Vec2, Option Vec<([f64; 2], [f64; 2])> { + use solidcraft_engine::sketch::Shape; + let sid = h.app.session.active_sketch.unwrap(); + let state = h.app.session.model.state(); + let sketch = &state.sketch(sid).unwrap().sketch; + (0..sketch.curves.len()) + .filter_map(|c| match sketch.shape(c)? { + Shape::Line { a, b } => Some(([a.x, a.y], [b.x, b.y])), + _ => None, + }) + .collect() +} + +fn assert_line(line: ([f64; 2], [f64; 2]), start: [f64; 2], end: [f64; 2]) { + let near = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6; + assert!(near(line.0, start) && near(line.1, end), "{line:?}, expected {start:?} to {end:?}"); +} + +/// The Undo button runs `edit.undo` and leaves the tool on: the chain goes back to the end of the +/// last segment still there, and the next click draws from it (#10016). +#[test] +fn undo_in_a_line_chain_goes_back_to_the_last_segment_still_there() { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.settle(); + line_chain(&mut h, &[[-20, 0, 0], [0, 0, 0], [0, 15, 0]]); + assert_eq!(lines(&h).len(), 2); + + h.app.run("edit.undo", json!({})).unwrap(); + h.settle(); + assert_eq!(lines(&h).len(), 1); + h.step(&json!({"click":{"world":[20, 15, 0]}})).unwrap(); + h.settle(); + let drawn = lines(&h); + assert_eq!(drawn.len(), 2, "the click drew a line: {:?}", h.app.session.log.last()); + assert_line(drawn[0], [-20.0, 0.0], [0.0, 0.0]); + assert_line(drawn[1], [0.0, 0.0], [20.0, 15.0]); + let chain = h.app.tool.as_ref().unwrap(); + assert_eq!(chain.cmd, "sketch.line", "the tool stays on"); + assert_eq!(chain.pts.len(), 1, "and the chain carries on from the new end"); + + // Undo twice: back past the first segment, to where the chain began. + h.app.run("edit.undo", json!({})).unwrap(); + h.app.run("edit.undo", json!({})).unwrap(); + h.settle(); + assert!(lines(&h).is_empty()); + h.step(&json!({"click":{"world":[5, 5, 0]}})).unwrap(); + h.settle(); + let drawn = lines(&h); + assert_eq!(drawn.len(), 1, "{:?}", h.app.session.log.last()); + assert_line(drawn[0], [-20.0, 0.0], [5.0, 5.0]); +} + +/// A chain begun on a point Undo then takes away has nowhere to go back to: it ends, the tool +/// stays on, and the next click begins a new chain with no error. +#[test] +fn a_chain_whose_first_point_is_undone_ends_and_the_tool_stays_on() { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.line", json!({"points":[[-20, -20], [-10, -20]]})).unwrap(); + h.settle(); + h.step(&json!({"start":"sketch.line"})).unwrap(); + h.step(&json!({"click":{"world":[-10, -20, 0]}})).unwrap(); + assert!(h.app.tool.as_ref().unwrap().pts[0].1.is_some(), "the click snapped to the line's end"); + h.app.run("edit.undo", json!({})).unwrap(); + h.settle(); + assert!(lines(&h).is_empty()); + h.step(&json!({"click":{"world":[5, 5, 0]}})).unwrap(); + h.settle(); + let chain = h.app.tool.as_ref().unwrap(); + assert_eq!(chain.cmd, "sketch.line"); + assert_eq!(chain.pts.len(), 1, "the click began a new chain: {:?}", h.app.session.log.last()); + assert!(chain.pts[0].1.is_none() && (chain.pts[0].0.x - 5.0).abs() < 1e-6, "{:?}", chain.pts); + assert!(lines(&h).is_empty()); + h.step(&json!({"click":{"world":[15, 5, 0]}})).unwrap(); + h.settle(); + assert_eq!(lines(&h).len(), 1, "{:?}", h.app.session.log.last()); +} + +/// Esc ends a chain for good: a new chain begun on its last point doesn't fall back into the old +/// one when Undo takes that point away. +#[test] +fn undo_does_not_bring_back_a_chain_ended_with_esc() { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.settle(); + line_chain(&mut h, &[[-20, 0, 0], [0, 0, 0], [0, 15, 0]]); + press(&mut h, "Escape"); + assert_eq!(tool(&h), Some("sketch.line")); + assert!(h.app.tool.as_ref().unwrap().pts.is_empty()); + h.step(&json!({"click":{"world":[0, 15, 0]}})).unwrap(); + assert!(h.app.tool.as_ref().unwrap().pts[0].1.is_some(), "the new chain begins on the old one's end"); + h.app.run("edit.undo", json!({})).unwrap(); + h.settle(); + h.step(&json!({"click":{"world":[5, 5, 0]}})).unwrap(); + h.settle(); + let chain = h.app.tool.as_ref().unwrap(); + assert_eq!(chain.pts.len(), 1); + assert!(chain.pts[0].1.is_none(), "a new chain, from the click, not the old one's point: {:?}", chain.pts); + assert_eq!(lines(&h).len(), 1, "only the first segment is left"); +} diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 7b134c477..56b99357f 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -215,6 +215,10 @@ fn midpoint_at(app: &SolveApp, proj: &Proj, pos: Pos2, radius: f32) -> Option<(V pub fn on_hover(app: &mut SolveApp, proj: &Proj, pos: Pos2, shift: bool) { crate::sketch3d::on_hover(app, proj, pos); + if let Some(mut tool) = app.tool.take() { + crate::sketch_line_arc::rejoin(app, &mut tool); + app.tool = Some(tool); + } let midpoint = (shift && app.tool.as_ref().is_some_and(|t| t.kind == Kind::HorizontalVertical)) .then(|| midpoint_at(app, proj, pos, f32::INFINITY)) .flatten(); @@ -501,6 +505,7 @@ fn dimension_picks(tool: &Tool, id: String, midpoint: Option<&str>, midpoint_tar /// the typed values as dimension constraints). pub fn place(app: &mut SolveApp, p: (Vec2, Option)) { let Some(mut tool) = app.tool.take() else { return }; + crate::sketch_line_arc::rejoin(app, &mut tool); if tool.kind == Kind::Ext { crate::sketch_tools::place_point(app, &mut tool, p); // A placed text opens Edit Text, which ends the tool. @@ -551,7 +556,7 @@ pub fn place(app: &mut SolveApp, p: (Vec2, Option)) { tool.pts[last].1 = Some(format!("{id}.end")); // The chain needs only its current endpoint, even after many segments. tool.pts = vec![tool.pts[last].clone()]; - tool.chain.placed(id); + tool.chain.placed(id, a.clone()); } crate::sketch_dims::reset(&mut tool); } else {