From e8302b575757cfc5d9136e8a5b6199f503739145 Mon Sep 17 00:00:00 2001 From: Colin Faulkingham Date: Tue, 27 Oct 2026 12:36:55 +0000 Subject: [PATCH] Sketch: delete dimensions from the right-click menu (#7155) (#7170) * claim: #7155 * fix(ui): delete sketch dimensions from the context menu --- crates/ui-egui/src/context_menu.rs | 10 +++- crates/ui-egui/src/menu_tests.rs | 82 ++++++++++++++++++++++++++++++ crates/ui-egui/src/viewport.rs | 29 ++++++----- 3 files changed, 106 insertions(+), 15 deletions(-) diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs index bbe2c82e9..96118ef69 100644 --- a/crates/ui-egui/src/context_menu.rs +++ b/crates/ui-egui/src/context_menu.rs @@ -995,6 +995,9 @@ fn sketch_entity_items(app: &SolveApp) -> Vec { if curves.is_empty() && points.is_empty() { if dims.len() == 1 { v.push(cmd(app, "sketch.toggle_driven", "Toggle Driven Dimension").with(json!({ "constraint": dims[0] }))); + } + if app.session.selection.iter().any(|s| matches!(s, Sel::SketchConstraint { .. })) { + v.push(act("ui.delete", "Delete", "delete").key("Del")); v.push(Item::sep()); } if crate::sketch_text::selected_entity(app).is_some() { @@ -1716,7 +1719,12 @@ pub fn open(app: &mut SolveApp, at: Pos2, held: crate::tool_modifiers::Held) { _ => None, }); crate::select_other::close(app); - if app.session.active_sketch.is_none() + if let Some(sel) = crate::viewport::sketch_constraint_at(app, at, app.viewport.hover.as_ref()) { + if !in_selection(app, &sel) { + let _ = app.run("select.set", json!({ "items": [sel] })); + app.menu.picked = Some((at, before, app.session.selection.clone())); + } + } else if app.session.active_sketch.is_none() && let Some(h) = app.viewport.hover.clone() && let Some((_, sel)) = crate::viewport::candidate(app, &[h]) && !in_selection(app, &sel) diff --git a/crates/ui-egui/src/menu_tests.rs b/crates/ui-egui/src/menu_tests.rs index 3d84c601d..f9eb3323b 100644 --- a/crates/ui-egui/src/menu_tests.rs +++ b/crates/ui-egui/src/menu_tests.rs @@ -347,6 +347,88 @@ fn sketch_entities_offer_the_constraints_that_fit() { assert!(ss.sketch.constraints.iter().any(|c| format!("{:?}", c.kind).contains("Parallel")), "{:?}", ss.sketch.constraints); } +#[test] +fn sketch_dimension_menu_deletes_only_the_dimension_and_supports_undo() { + for (driven, preselected) in [(false, false), (false, true), (true, false), (true, true)] { + let mut h = crate::scenario::Harness::new(); + h.app.home.open = false; + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + let line = h.app.run("sketch.line", json!({"points":[[0,0],[30,0]],"connect":false,"infer":false})).unwrap()["curves"][0] + .as_str() + .unwrap() + .to_owned(); + let horizontal = h.app.run("sketch.constrainer", json!({"entities":[line]})).unwrap()["constraint"].as_str().unwrap().to_owned(); + let dimension = h.app.run("sketch.dimension", json!({"entities":[line],"value":30})).unwrap(); + let id = dimension["created"]["dimensions"][0].as_str().unwrap(); + if driven { + h.app.run("sketch.toggle_driven", json!({"constraint":id})).unwrap(); + } + let saved = h.app.session.doc.to_json(); + let undo = h.app.session.undo.len(); + h.call("ui.view", json!({"view":"top"})); + h.call("ui.view", json!({"view":"fit"})); + if preselected { + h.step(&json!({"click":{"dimension":id}})).unwrap(); + } + h.step(&json!({"click":{"dimension":id},"button":"right"})).unwrap(); + assert_eq!(h.app.session.selection, vec![solidcraft_engine::Sel::SketchConstraint { id: id.to_owned() }]); + let menu = h.call("ui.menu", json!({})); + assert!(menu["result"]["items"].as_array().unwrap().iter().any(|i| i["id"] == "ui.delete" && i["enabled"] == true), "{menu}"); + assert_eq!(h.call("ui.menuPick", json!({"item":"ui.delete"}))["ok"], true); + let after = h.app.run("sketch.inspect", json!({})).unwrap(); + assert_eq!(after["curves"].as_array().unwrap().len(), 1); + assert_eq!(after["curves"][0]["id"], line); + assert_eq!(after["constraints"].as_array().unwrap().len(), 1); + assert_eq!(after["constraints"][0]["id"], horizontal); + assert_eq!(h.app.session.undo.len(), undo + 1); + h.app.run("edit.undo", json!({})).unwrap(); + assert_eq!(h.app.session.doc.to_json(), saved); + h.app.run("edit.redo", json!({})).unwrap(); + assert_eq!(h.app.run("sketch.inspect", json!({})).unwrap(), after); + } +} + +#[test] +fn sketch_constraint_menu_keeps_the_selection_and_respects_pick_filters() { + let mut h = crate::scenario::Harness::new(); + h.app.home.open = false; + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + let line = + h.app.run("sketch.line", json!({"points":[[0,0],[30,0]],"connect":false,"infer":false})).unwrap()["curves"][0].as_str().unwrap().to_owned(); + let horizontal = h.app.run("sketch.constrainer", json!({"entities":[line]})).unwrap()["constraint"].as_str().unwrap().to_owned(); + let dimension = h.app.run("sketch.dimension", json!({"entities":[line],"value":30})).unwrap(); + let id = dimension["created"]["dimensions"][0].as_str().unwrap(); + h.call("ui.view", json!({"view":"top"})); + h.call("ui.view", json!({"view":"fit"})); + + // An empty selection, including a dimension whose picking is disabled, has no Delete. + assert!(!context_menu::items(&h.app, &Target::Viewport).iter().any(|i| i.id == "ui.delete")); + h.app.ui.pick_off.push("dimensions".into()); + h.step(&json!({"click":{"dimension":id},"button":"right"})).unwrap(); + assert!(h.app.session.selection.is_empty()); + assert!(!context_menu::items(&h.app, &Target::Viewport).iter().any(|i| i.id == "ui.delete")); + h.call("ui.menu", json!({"close":true})); + h.app.ui.pick_off.clear(); + + // A non-dimensional constraint can be deleted too. Right-clicking a dimension already + // in a multi-selection retains all the selected constraints for the same Delete action. + for ids in [vec![horizontal.clone()], vec![horizontal.clone(), id.to_owned()]] { + let selection: Vec<_> = ids.iter().map(|id| solidcraft_engine::Sel::SketchConstraint { id: id.clone() }).collect(); + h.app.session.selection = selection.clone(); + if ids.len() > 1 { + h.step(&json!({"click":{"dimension":id},"button":"right"})).unwrap(); + assert_eq!(h.app.session.selection, selection); + } + let item = find(&context_menu::items(&h.app, &Target::Viewport), "Delete"); + assert!(item.enabled); + context_menu::run_item(&mut h.app, &item, pos2(0.0, 0.0)); + let after = h.app.run("sketch.inspect", json!({})).unwrap(); + assert_eq!(after["curves"].as_array().unwrap().len(), 1); + assert_eq!(after["constraints"].as_array().unwrap().len(), 2 - ids.len()); + h.app.run("edit.undo", json!({})).unwrap(); + } +} + #[test] fn component_menu_activates_grounds_and_pastes() { let mut app = sample_app(); diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 3ce8c1fa9..41488a2e7 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -2683,6 +2683,18 @@ fn tool_hover(app: &SolveApp, hits: &[Hit]) -> Option { hits.iter().find(|h| crate::sketch_tools::takes(cmd, h)).cloned() } +/// A sketch annotation under either a left-click or a context-menu click. Dimension text +/// wins over geometry; dimension lines yield to points and curves, and glyphs to midpoints. +pub(crate) fn sketch_constraint_at(app: &SolveApp, pos: Pos2, hit: Option<&Hit>) -> Option { + app.session.active_sketch?; + let off = |kind: &str| app.ui.pick_off.iter().any(|x| x == kind); + let on_geometry = matches!(hit, Some(Hit::SketchPoint { .. } | Hit::SketchMidpoint { .. } | Hit::SketchCurve { .. })); + let dimension = + crate::dim_view::hit_text(pos).or_else(|| if on_geometry { None } else { crate::dim_view::hit(pos) }).filter(|_| !off("dimensions")); + let glyph = crate::sketch_tools::glyph_at(pos).filter(|_| !off("constraints") && !matches!(hit, Some(Hit::SketchMidpoint { .. }))); + dimension.or(glyph).map(|id| Sel::SketchConstraint { id }) +} + /// What a click would pick: the first hit the open dialog's active input accepts, or with no /// dialog the first hit (sketch entities only belong to the active sketch). pub fn candidate(app: &SolveApp, hits: &[Hit]) -> Option<(Hit, Sel)> { @@ -4018,23 +4030,12 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { app.dialog = Some(dialog); } } else { - // Dimensions and constraint glyphs sit on top of the geometry. - let off = |k: &str| app.ui.pick_off.iter().any(|x| x == k); - let sketching = app.session.active_sketch.is_some(); - // A dimension's text wins; its lines only where no sketch point or curve is (a - // diameter's line runs through the centre point). - let on_geometry = - matches!(cand.as_ref().map(|c| &c.0), Some(Hit::SketchPoint { .. } | Hit::SketchMidpoint { .. } | Hit::SketchCurve { .. })); - let on_dim = crate::dim_view::hit_text(p) - .or_else(|| if on_geometry { None } else { crate::dim_view::hit(p) }) - .filter(|_| sketching && !off("dimensions")); - let on_midpoint = matches!(cand.as_ref().map(|c| &c.0), Some(Hit::SketchMidpoint { .. })); - let on_glyph = crate::sketch_tools::glyph_at(p).filter(|_| sketching && !off("constraints") && !on_midpoint); + let annotation = sketch_constraint_at(app, p, cand.as_ref().map(|c| &c.0)); if crate::sketch_text_handles::over(app, &proj, p) { // A text's resize or rotation handle: the text stays selected. } else if !crate::sketch3d::click(app, &proj, p) { - match on_dim.or(on_glyph) { - Some(id) => select(app, Some(Sel::SketchConstraint { id }), add, allow_toggle), + match annotation { + Some(sel) => select(app, Some(sel), add, allow_toggle), // Clicking again on the same spot selects the next thing under it (not // the second click of a double-click, which has its own actions). None if !add && !resp.double_clicked() && crate::select_other::cycle_click(app, &proj, p) => {}