Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Context menu: a right-click on a face of a selected body keeps the selection (#5849) (#5915)

James Barnette2eaa15ea04a0
diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs
index ff58d941d0..96948454b0 100644
--- a/crates/ui-egui/src/context_menu.rs
+++ b/crates/ui-egui/src/context_menu.rs
@@ -1567,9 +1567,34 @@ fn context_target_selected(selection: &[Sel], target: &Sel) -> bool {
})
}
+/// The body a viewport pick belongs to: a face, edge or vertex carries its body's name. The
+/// origin point is a vertex of no body, so an empty name is not a body.
+fn pick_body(sel: &Sel) -> Option<&str> {
+ match sel {
+ Sel::Face { body, .. } | Sel::Edge { body, .. } | Sel::Vertex { body, .. } if !body.is_empty() => Some(body),
+ _ => None,
+ }
+}
+
+/// Is the pick already part of the selection? Beside the item itself (and an edge's midpoint,
+/// above), a face, edge or vertex of a selected body counts, whether the body is selected on its
+/// own or through an occurrence: the menu is then about the bodies, and the right-click leaves
+/// the selection alone (#5849).
+fn in_selection(app: &SolveApp, sel: &Sel) -> bool {
+ if context_target_selected(&app.session.selection, sel) {
+ return true;
+ }
+ let Some(body) = pick_body(sel) else { return false };
+ app.session.selection.iter().any(|x| match x {
+ Sel::Body { name } => name == body,
+ Sel::Occurrence { path } => app.session.occurrence_bodies(path, true).is_ok_and(|b| b.iter().any(|n| n == body)),
+ _ => false,
+ })
+}
+
/// Open the viewport menu at a screen position. A right-click on something not selected
/// selects it first, so the menu is about what is under the cursor; a face or edge already in a
-/// multi-selection keeps the whole selection (#3733). `held`: the keys held at that click.
+/// multi-selection keeps the whole selection (#3733), as does one of a selected body (#5849). `held`: the keys held at that click.
pub fn open(app: &mut SolveApp, at: Pos2, held: crate::tool_modifiers::Held) {
app.menu.opened = Some((at, held));
app.menu.picked = None;
@@ -1581,11 +1606,10 @@ pub fn open(app: &mut SolveApp, at: Pos2, held: crate::tool_modifiers::Held) {
_ => None,
});
crate::select_other::close(app);
- let selected = |app: &SolveApp, sel: &Sel| app.session.selection.iter().any(|x| crate::selection::same_item(x, sel));
if app.session.active_sketch.is_none()
&& let Some(h) = app.viewport.hover.clone()
&& let Some((_, sel)) = crate::viewport::candidate(app, &[h])
- && !selected(app, &sel)
+ && !in_selection(app, &sel)
&& !matches!(sel, Sel::Plane { .. } | Sel::Axis { .. })
{
let _ = app.run("select.set", json!({ "items": [sel] }));
@@ -1595,7 +1619,7 @@ pub fn open(app: &mut SolveApp, at: Pos2, held: crate::tool_modifiers::Held) {
&& let Some((_, sel)) = crate::viewport::candidate(app, &[h])
&& (matches!(sel, Sel::SketchCurve { .. } | Sel::SketchPoint { .. } | Sel::SketchMidpoint { .. })
|| crate::sketch_text::entity_for_selection(app, &sel).is_some())
- && !context_target_selected(&app.session.selection, &sel)
+ && !in_selection(app, &sel)
{
let _ = app.run("select.set", json!({ "items": [sel] }));
app.menu.picked = Some((at, before, app.session.selection.clone()));
diff --git a/crates/ui-egui/src/menu_tests.rs b/crates/ui-egui/src/menu_tests.rs
index 94b799a4a9..f7bc909050 100644
--- a/crates/ui-egui/src/menu_tests.rs
+++ b/crates/ui-egui/src/menu_tests.rs
@@ -160,6 +160,37 @@ fn right_click_on_a_selected_face_keeps_the_multi_selection() {
assert_eq!(app.session.selection.len(), 1, "a face outside the selection is picked: {:?}", app.session.selection);
}
+/// A right-click on a face of a selected body keeps the whole selection: the body is what is
+/// selected, so the menu is about the bodies and the face under the cursor does not replace them
+/// (#5849).
+#[test]
+fn right_click_on_a_face_of_a_selected_body_keeps_the_bodies() {
+ let mut app = sample_app();
+ let first = body(&app);
+ app.run("solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [200, 0, 0]})).unwrap();
+ let second = app.session.model.state().bodies.iter().map(|b| b.name.clone()).find(|n| *n != first).expect("a second body");
+ let both = vec![solidcraft_engine::Sel::Body { name: first.clone() }, solidcraft_engine::Sel::Body { name: second.clone() }];
+ app.run("select.set", json!({"items": [{"type": "body", "name": first}, {"type": "body", "name": second}]})).unwrap();
+ // A face of the first body: both bodies stay selected, and the menu stays the bodies' menu.
+ app.viewport.hover = Some(Hit::Face { body: first.clone(), index: 0, point: Vec3::new(9.0, 9.0, 9.0) });
+ context_menu::open(&mut app, pos2(400.0, 300.0), Default::default());
+ assert_eq!(app.session.selection, both, "the right-click keeps the bodies it is about");
+ assert!(!context_menu::items(&app, &Target::Viewport).iter().any(|i| i.label == "Create Sketch"), "the bodies' menu, not the face menu");
+ // An edge and a vertex of a selected body count the same way.
+ app.viewport.hover = Some(Hit::Edge { body: first.clone(), index: 0, mid: Vec3::new(9.0, 9.0, 9.0) });
+ context_menu::open(&mut app, pos2(400.0, 300.0), Default::default());
+ assert_eq!(app.session.selection, both, "an edge of a selected body keeps the bodies");
+ // A face of a body outside the selection is still picked, so the menu is about that face.
+ app.run("select.set", json!({"items": [{"type": "body", "name": first}]})).unwrap();
+ app.viewport.hover = Some(Hit::Face { body: second, index: 0, point: Vec3::new(9.0, 9.0, 9.0) });
+ context_menu::open(&mut app, pos2(400.0, 300.0), Default::default());
+ assert!(
+ matches!(app.session.selection.as_slice(), [solidcraft_engine::Sel::Face { .. }]),
+ "a face of an unselected body is picked: {:?}",
+ app.session.selection
+ );
+}
+
#[test]
fn face_menu_hides_and_finds_the_body() {
let mut app = sample_app();