From e3c08471b6661fd344cb2852a12bf82dae7053ab Mon Sep 17 00:00:00 2001 From: ReDead <104050379+ReDead@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:48:53 +0000 Subject: [PATCH] Sketch: diameter box appears for a second concentric circle (#8899) (#8904) * claim: #8899 Co-authored-by: ReDead * Sketch: keep Diameter box when placing a second concentric circle (#8899) A centre-point circle's press starts drag-to-draw; releasing on the same centre (or close to the press) must keep the stage-1 Diameter field instead of resetting it and risking a second on-click that finishes the shape. Co-authored-by: ReDead --------- Co-authored-by: Cursor Agent Co-authored-by: ReDead --- crates/ui-egui/src/sketch_dims.rs | 20 ++++++- crates/ui-egui/src/sketch_dims_tests.rs | 80 +++++++++++++++++++++++++ crates/ui-egui/src/viewport.rs | 34 +++++++---- 3 files changed, 122 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/sketch_dims.rs b/crates/ui-egui/src/sketch_dims.rs index 5c64b4de3..58a8502ea 100644 --- a/crates/ui-egui/src/sketch_dims.rs +++ b/crates/ui-egui/src/sketch_dims.rs @@ -359,6 +359,21 @@ pub fn reset(t: &mut Tool) { t.dims_focus = false; } +/// After a draw-drag press that only placed the first point, a short release (or one that lands +/// on the same sketch point, #8899) must keep the stage-1 value boxes rather than wiping them +/// with [`reset`] and waiting for the next frame to rebuild them. +pub fn keep_stage_one(t: &mut Tool) { + if t.pts.len() != 1 { + return; + } + if t.dims_stage != 1 || t.dims.is_empty() { + t.dim_history.clear(); + t.dims = boxes_for(t.cmd, 1); + t.dims_stage = 1; + } + t.dims_focus = !t.dims.is_empty(); +} + pub fn has_locked(t: &Tool, label: &str) -> bool { t.dims.iter().chain(&t.dim_history).any(|dimension| dimension.locked && dimension.label == label) } @@ -477,7 +492,10 @@ fn boxes(app: &SolveApp, ui: &mut egui::Ui, proj: &Proj, t: &mut Tool) -> Option let sid = app.session.active_sketch?; let st = app.session.world_state(); let plane = st.sketch(sid)?.plane; - let e = shown(app, t, t.hover.as_ref()?); + // When the pointer is over the value box itself after releasing on a shared centre (#8899), + // the tool may have no hover for a frame: still show the boxes from the first point. + let hover = t.hover.clone().or_else(|| t.pts.last().map(|(p, id)| (*p + Vec2::X, id.clone()))); + let e = shown(app, t, hover.as_ref()?); let px = 2.0 * app.cam.half_height() / f64::from(proj.rect.height().max(1.0)); let lv = live(app, t, e, px); let focus = std::mem::take(&mut t.dims_focus); diff --git a/crates/ui-egui/src/sketch_dims_tests.rs b/crates/ui-egui/src/sketch_dims_tests.rs index 4aaaa672c..a20556ed5 100644 --- a/crates/ui-egui/src/sketch_dims_tests.rs +++ b/crates/ui-egui/src/sketch_dims_tests.rs @@ -556,6 +556,86 @@ fn a_box_typed_name_equals_value_defines_the_parameter_driving_the_dimension() { assert_eq!(h.app.status.as_ref().map(|(text, _, _)| text.as_str()), Some("Parameters `h`, `w` defined")); } +/// #8899: after a centre-point circle is placed, another circle started on the same centre must +/// keep its Diameter field after the button is released. A press places the centre for +/// drag-to-draw (#1630); releasing on that same centre (a click, or a drag that ends there) +/// must not clear the Diameter box (#2448 / #8899). +#[test] +fn a_second_circle_on_the_same_centre_keeps_its_diameter_box_after_release() { + for (label, drive) in [("short-drag", "short"), ("zero-move", "zero"), ("click", "click")] { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane": "XY"})).unwrap(); + h.app.cam.distance = 120.0; + h.settle(); + h.app.run("sketch.circle.center", json!({"center": [0, 0], "radius": 10})).unwrap(); + h.settle(); + h.step(&json!({"start": "sketch.circle.center"})).unwrap(); + + let p = h.call("ui.at", json!({"sketch": [0.0, 0.0]}))["result"].clone(); + let (x, y) = (p[0].as_f64().unwrap(), p[1].as_f64().unwrap()); + match drive { + "short" => { + h.call("ui.drag", json!({"x0": x - 12.0, "y0": y, "x1": x, "y1": y, "steps": 4})); + } + "zero" => { + h.call("ui.drag", json!({"x0": x, "y0": y, "x1": x, "y1": y, "steps": 1, "hold": 5})); + } + _ => { + h.call("ui.click", json!({"x": x, "y": y})); + } + } + h.settle(); + + let tool = h.app.tool.as_ref().unwrap_or_else(|| panic!("{label}: tool ended; status={:?}", h.app.status)); + assert_eq!(tool.pts.len(), 1, "{label}: pts={:?} status={:?}", tool.pts, h.app.status); + assert_eq!( + tool.dims.iter().map(|d| d.label).collect::>(), + ["Diameter"], + "{label}: dims={:?} hover={:?} status={:?}", + tool.dims, + tool.hover, + h.app.status + ); + assert!( + h.ctx.memory(|m| m.area_rect(egui::Id::new(("sc_dim_area", 0usize)))).is_some(), + "{label}: Diameter field missing; focused={:?} hover={:?}", + h.ctx.memory(|m| m.focused()), + tool.hover + ); + let state = h.app.session.world_state(); + let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch; + assert_eq!(sk.curves.len(), 1, "{label}: unexpected curves {}", sk.curves.len()); + } +} + +/// #8899: same as above, but the first circle was drawn with a typed Diameter (so egui's text +/// edit for `sc_dim` still has state when the second centre is placed). +#[test] +fn a_typed_first_circle_then_same_centre_keeps_the_diameter_box() { + let mut h = drawing("sketch.circle.center"); + type_value(&mut h, "20"); + h.step(&json!({"key": "Enter"})).unwrap(); + assert_eq!(h.app.tool.as_ref().map(|t| t.cmd), Some("sketch.circle.center")); + assert!(h.app.tool.as_ref().unwrap().pts.is_empty()); + + let p = h.call("ui.at", json!({"sketch": [0.0, 0.0]}))["result"].clone(); + let (x, y) = (p[0].as_f64().unwrap(), p[1].as_f64().unwrap()); + h.call("ui.drag", json!({"x0": x, "y0": y, "x1": x, "y1": y, "steps": 1, "hold": 8})); + h.settle(); + // Do not move away: the Diameter field must remain with the keyboard at the shared centre. + let tool = h.app.tool.as_ref().expect("tool"); + assert_eq!(tool.pts.len(), 1, "pts={:?} status={:?}", tool.pts, h.app.status); + assert_eq!(tool.dims.iter().map(|d| d.label).collect::>(), ["Diameter"]); + assert!( + h.ctx.memory(|m| m.area_rect(egui::Id::new(("sc_dim_area", 0usize)))).is_some(), + "Diameter field gone; focused={:?} hover={:?} dims={:?}", + h.ctx.memory(|m| m.focused()), + tool.hover, + tool.dims + ); + assert_focus(&h, 0); +} + /// A box that is not a dimension (a polygon's sides, a rounded rectangle's corner radius) has /// nothing for a parameter to drive: `name = ...` there is refused, not quietly dropped. #[test] diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 4cfa1f1d0..43dcea044 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -61,6 +61,10 @@ pub struct ViewportState { /// way (#2448): the release places the first point instead and the shape goes on by clicks, /// rather than a sliver of a shape. pub(crate) draw_drag: Option, + /// The draw-drag release above already placed or kept the tool's point this frame: ignore + /// egui's ordinary `clicked` for the same release so a second on-click cannot finish the + /// shape (or wipe its Diameter box) when the centre is an existing sketch point (#8899). + pub(crate) draw_drag_handled: bool, /// A tool's click moved on the way (#2448): egui takes a press that moves past its /// click distance, or is held past its click time, as a drag, so its release is the click. pub(crate) tool_click: bool, @@ -4308,6 +4312,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { } // A two-point shape being dragged out (#1630) follows the pointer and ends where it is let go, // even over the shape's own dimension box, which rides along beside the pointer. + app.viewport.draw_drag_handled = false; if let Some(from) = app.viewport.draw_drag { let at = ui.input(|i| i.pointer.latest_pos()).filter(|p| rect.contains(*p) && !crate::shell::covered(app, *p)); if resp.drag_stopped() || !primary_down { @@ -4315,17 +4320,23 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { if let Some(p) = at && app.tool.as_ref().is_some_and(|t| t.pts.len() == 1) { - // Let go close to the press, it was a click that moved on the way (#2448): the - // first point goes where it was let go, as for the other tools, and the shape - // goes on by clicks. - if p.distance(from) < DRAW_DRAG_MIN - && let Some(t) = app.tool.as_mut() - { - t.pts.clear(); - crate::sketch_dims::reset(t); - } crate::tools::on_hover(app, &proj, p, mods.shift); - crate::tools::on_click(app, &proj, p, mods.shift); + let release = sketch_point_at(app, &proj, p); + let first = app.tool.as_ref().and_then(|t| t.pts.first().map(|pt| pt.0)); + // Same sketch point as the press (a second circle on an existing centre, #8899), + // or let go close to the press (#2448): keep a single first point and its value + // boxes. Clearing and re-clicking used to wipe the Diameter box on release. + let keep_first = p.distance(from) < DRAW_DRAG_MIN || matches!((first, release.as_ref()), (Some(a), Some((b, _))) if a == *b); + if keep_first { + if let Some(pt) = release + && let Some(t) = app.tool.as_mut() + { + t.pts = vec![pt]; + crate::sketch_dims::keep_stage_one(t); + } + } else { + crate::tools::on_click(app, &proj, p, mods.shift); + } // A line dragged out is that one line: no chain goes on from its end (#7247). if p.distance(from) >= DRAW_DRAG_MIN && let Some(t) = app.tool.as_mut().filter(|t| t.cmd == "sketch.line") @@ -4333,6 +4344,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { t.pts.clear(); crate::sketch_dims::reset(t); } + app.viewport.draw_drag_handled = true; } } else if let Some(p) = at { crate::tools::on_hover(app, &proj, p, mods.shift); @@ -4425,7 +4437,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { { dialog.restore_value_focus = true; } - if resp.clicked() && !held && !reference_pick && !joint_handle_pointer { + if resp.clicked() && !held && !reference_pick && !joint_handle_pointer && !app.viewport.draw_drag_handled { if app.tool.is_some() { crate::tools::on_click(app, &proj, p, mods.shift); } else if app.dialog.as_ref().is_some_and(|d| d.wants_picks()) {