diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rs index 64bd6734f..ee5592ebf 100644 --- a/crates/ui-egui/src/browser.rs +++ b/crates/ui-egui/src/browser.rs @@ -217,6 +217,68 @@ fn find_group(app: &SolveApp, id: u64) -> Option<&solidcraft_engine::doc::Browse app.session.doc.browser_groups.iter().find(|g| g.id == id) } +/// World bodies of a component picked in the tree. The root is the whole design; a nested +/// component is its own placement, including bodies inside it. +pub(crate) fn bodies_of_component(app: &SolveApp, id: u64) -> Vec { + let present: Vec = app.session.world_state().bodies.iter().map(|b| b.name.clone()).collect(); + let mut names = if id == 0 { + present.clone() + } else { + let mut names = Vec::new(); + let path = app.session.doc.first_path(id); + if !path.is_empty() + && let Ok(more) = app.session.occurrence_bodies(&path, true) + { + names.extend(more); + } + names.extend(crate::context_menu::component_bodies(app, id)); + names + }; + names.retain(|n| present.contains(n) && !app.session.body_removed_later(n)); + let mut kept = Vec::new(); + for name in names { + if !kept.contains(&name) { + kept.push(name); + } + } + kept +} + +/// How a browser click changes a dialog input that takes bodies. +enum Take { + /// A second click on the same set removes it. + Toggle, + /// A Shift range only adds. + Add, +} + +/// Put these bodies into the open dialog's active input, the same way as clicking each one +/// ([`pick_from_browser`]). Returns false when that input does not take bodies, so the click +/// keeps its ordinary meaning. A Shift range only adds; a group or component toggles. +fn offer_bodies(app: &mut SolveApp, names: &[String], take: Take) -> bool { + if !dialog_takes_body(app) { + return false; + } + let world = app.session.world_state(); + let picked: Vec = + names.iter().filter(|n| world.body(n).is_some() && !app.session.body_removed_later(n)).map(|n| Sel::Body { name: n.clone() }).collect(); + if picked.is_empty() { + return false; + } + let Some(mut d) = app.dialog.take() else { return false }; + for body in picked { + if !d.active_input().is_some_and(|i| i.accept & crate::selection::BODIES != 0) { + break; + } + if matches!(take, Take::Add) && d.active_input().is_some_and(|i| i.items.contains(&body)) { + continue; + } + d.pick(&app.session, body); + } + app.dialog = Some(d); + true +} + /// The bodies in a group (none for sketch and plane groups). pub fn group_bodies(app: &SolveApp, id: u64) -> Vec { let st = app.session.world_state(); @@ -585,6 +647,18 @@ fn click_select(app: &mut SolveApp, ui: &egui::Ui, key: String, sel: Option let order = app.tree.order.get(..app.tree.order_len).unwrap_or(&[]); let items = shift_range(&app.tree.order_prev, a, &key).or_else(|| shift_range(order, a, &key)); if let Some(items) = items { + // Move/Copy (and any open command whose active input takes bodies) gets every body + // in the range. Otherwise the range is an ordinary selection. + let names: Vec = items + .iter() + .filter_map(|s| match s { + Sel::Body { name } => Some(name.clone()), + _ => None, + }) + .collect(); + if offer_bodies(app, &names, Take::Add) { + return; + } let _ = app.run("select.set", json!({ "items": items, "add": mods.command || mods.ctrl })); return; } @@ -734,7 +808,10 @@ impl Lit { fn of(app: &mut SolveApp) -> Lit { use crate::viewport::Hit; let mut selected = std::collections::HashSet::new(); - for s in &app.session.selection { + // Bodies chosen in an open dialog light up even when the click was in the 3D view, + // which does not rewrite the browser selection. + let dialog_picks = app.dialog.as_ref().map(|d| d.items()).unwrap_or_default(); + for s in app.session.selection.iter().chain(&dialog_picks) { selected.insert(match s { Sel::Body { name } | Sel::Face { body: name, .. } | Sel::Edge { body: name, .. } | Sel::Vertex { body: name, .. } => { Thing::Body(name.clone()) @@ -1230,16 +1307,19 @@ fn component_rows_inner( app.session.visibility.hidden_bodies.retain(|b| !bodies.contains(b)); } } - if r.clicked { - let ctrl = ui.input(|i| i.modifiers.command || i.modifiers.ctrl); - if !ctrl { - app.tree.picked_components.clear(); - let _ = app.run("select.clear", json!({})); - } - if app.tree.picked_components.contains(&id) { - app.tree.picked_components.retain(|c| *c != id); - } else { - app.tree.picked_components.push(id); + if r.clicked && !r.double { + let names = bodies_of_component(app, id); + if !offer_bodies(app, &names, Take::Toggle) { + let ctrl = ui.input(|i| i.modifiers.command || i.modifiers.ctrl); + if !ctrl { + app.tree.picked_components.clear(); + let _ = app.run("select.clear", json!({})); + } + if app.tree.picked_components.contains(&id) { + app.tree.picked_components.retain(|c| *c != id); + } else { + app.tree.picked_components.push(id); + } } } if r.double { @@ -1383,8 +1463,11 @@ fn occurrence_rows_inner( if r.double && app.session.doc.parts.contains_key(&placement.component) { crate::dialogs_part_size::open(app, placement.component); } - if r.clicked { - click_select(app, ui, key.clone(), Some(Sel::Occurrence { path: path.to_vec() })); + if r.clicked && !r.double { + let names = app.session.occurrence_bodies(path, true).unwrap_or_default(); + if !offer_bodies(app, &names, Take::Toggle) { + click_select(app, ui, key.clone(), Some(Sel::Occurrence { path: path.to_vec() })); + } } if r.hovered { let bodies = placement.hover_bodies.get_or_insert_with(|| app.session.occurrence_bodies(path, true).unwrap_or_default()); @@ -1832,9 +1915,10 @@ impl CanvasPanel { } } -/// Does the open dialog's active input take bodies (Combine's target and tools, #4452)? Then a -/// body clicked in the Browser goes there, as a click on it in the view does, rather than -/// replacing the selection: overlapping bodies are often only told apart in the Browser. +/// Does the open dialog's active input take bodies (Combine's target and tools, #4452; +/// Move/Copy's bodies, #4771)? Then a body clicked in the Browser goes there, as a click +/// on it in the view does, rather than replacing the selection: overlapping bodies are +/// often only told apart in the Browser. fn dialog_takes_body(app: &SolveApp) -> bool { app.dialog.as_ref().and_then(|d| d.active_input()).is_some_and(|i| i.accept & crate::selection::BODIES != 0) } @@ -2126,8 +2210,15 @@ fn folder_rows_at( ..Default::default() }, ); - if r.fold || r.clicked { + if r.fold { toggle(app, key, true); + } else if r.clicked { + // The Bodies label is the whole set while a command is asking for bodies. The arrow + // still folds it. With no such command, the label folds, as it always has. + let names: Vec = data.entries().flat_map(|e| e.bodies.clone()).collect(); + if !offer_bodies(app, &names, Take::Toggle) { + toggle(app, key, true); + } } if r.eye { toggle_folder(app, key); @@ -2174,8 +2265,13 @@ fn folder_rows_at( ..Default::default() }, ); - if r.fold || r.clicked { + if r.fold { toggle(app, gkey, true); + } else if r.clicked { + let names: Vec = items.iter().flat_map(|e| e.bodies.clone()).collect(); + if !offer_bodies(app, &names, Take::Toggle) { + toggle(app, gkey, true); + } } if r.eye { toggle_folder(app, gkey); @@ -2311,8 +2407,14 @@ fn entry_row( app.edit_sketch(id); } } else if r.clicked { - if folder == "construction" || (folder == "bodies" && app.dialog.is_some()) { + let shift = ui.input(|i| i.modifiers.shift); + // A body row with a dialog open is a pick, as Combine does (#4452), unless Shift is + // asking for the rows between this one and the anchor. + if folder == "construction" || (folder == "bodies" && !shift && app.dialog.is_some()) { if let Some(s) = e.sel.clone() { + if matches!(s, Sel::Body { .. }) { + app.tree.anchor = Some(e.row_key.clone()); + } pick_from_browser(app, s); } } else { diff --git a/crates/ui-egui/src/browser_model_tests.rs b/crates/ui-egui/src/browser_model_tests.rs index e70985591..ee598b340 100644 --- a/crates/ui-egui/src/browser_model_tests.rs +++ b/crates/ui-egui/src/browser_model_tests.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use serde_json::json; use solidcraft_engine::Sel; -use crate::test_support::component_box as fixture; +use crate::test_support::{click, component_box as fixture}; #[test] fn navigating_and_selecting_reuse_placements_but_rows_read_live_view_state() { @@ -195,3 +195,192 @@ fn construction_rows_include_points_and_axes() { super::prepare_model(&mut h.app); assert!(super::entries(&h.app, 0, "construction").is_empty()); } + +fn body_names(app: &crate::SolveApp) -> Vec { + app.dialog + .as_ref() + .and_then(|d| d.inputs.first()) + .map(|i| { + i.items + .iter() + .filter_map(|s| match s { + Sel::Body { name } => Some(name.clone()), + _ => None, + }) + .collect() + }) + .unwrap_or_default() +} + +fn two_bodies(h: &mut crate::scenario::Harness) { + h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Left"})).unwrap(); + h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [30, 0, 0], "body_name": "Right"})).unwrap(); +} + +fn open_move(h: &mut crate::scenario::Harness) { + h.app.run("select.clear", json!({})).unwrap(); + h.app.tree.picked_components.clear(); + h.app.start("solid.move"); + assert!(h.app.dialog.is_some(), "Move/Copy opens"); + assert!(body_names(&h.app).is_empty(), "nothing was selected, so the body list starts empty"); +} + +/// #4771: with Move/Copy open, each body clicked in the browser joins the body list. +/// Ctrl-click toggles one body and leaves the others. +#[test] +fn browser_move_copy_takes_each_body_clicked_in_the_tree() { + let mut h = crate::scenario::Harness::new(); + two_bodies(&mut h); + open_move(&mut h); + h.frames(2); + click(&mut h, "row:Left"); + assert_eq!(body_names(&h.app), vec!["Left".to_string()]); + h.step(&json!({"click": {"handle": "row:Right"}, "ctrl": true})).unwrap(); + h.frames(2); + let names = body_names(&h.app); + assert!(names.contains(&"Left".into()) && names.contains(&"Right".into()), "{names:?}"); + h.step(&json!({"click": {"handle": "row:Left"}, "ctrl": true})).unwrap(); + h.frames(2); + assert_eq!(body_names(&h.app), vec!["Right".to_string()]); + assert!(h.app.session.selection.is_empty(), "the click fills the dialog and does not replace the selection"); +} + +/// #4771: one click on a body group, or on the Bodies folder, selects that whole set. +/// The fold arrow still collapses the row. +#[test] +fn browser_move_copy_takes_a_group_or_the_bodies_folder_in_one_click() { + let mut h = crate::scenario::Harness::new(); + two_bodies(&mut h); + let group = h.app.run("browser.group", json!({"component": 0, "folder": "bodies", "name": "Pair", "items": ["Left", "Right"]})).unwrap(); + let group = group["group"].as_u64().unwrap(); + open_move(&mut h); + h.frames(2); + click(&mut h, "row:Pair (2)"); + let names = body_names(&h.app); + assert!(names.contains(&"Left".into()) && names.contains(&"Right".into()), "{names:?}"); + assert!(!h.app.tree.collapsed.contains(&format!("c0/g{group}")), "the group label selects; it does not fold"); + click(&mut h, "row:Pair (2)"); + assert!(body_names(&h.app).is_empty(), "clicking the group again clears those bodies"); + click(&mut h, "fold:Pair (2)"); + assert!(h.app.tree.collapsed.contains(&format!("c0/g{group}")), "the arrow still folds the group"); + assert!(body_names(&h.app).is_empty()); + + h.app.tree.collapsed.clear(); + h.frames(2); + click(&mut h, "row:Bodies"); + let names = body_names(&h.app); + assert!(names.contains(&"Left".into()) && names.contains(&"Right".into()), "{names:?}"); + assert!(!h.app.tree.collapsed.contains("c0/bodies"), "the Bodies label selects; it does not fold"); + click(&mut h, "fold:Bodies"); + assert!(h.app.tree.collapsed.contains("c0/bodies")); +} + +/// #4771: one click on a component adds every body it owns. The design row does the same. +#[test] +fn browser_move_copy_takes_a_component_clicked_in_the_tree() { + let (mut h, _, occurrence) = fixture(); + let owned = h.app.session.occurrence_bodies(&[occurrence], true).unwrap(); + assert!(!owned.is_empty(), "the component has a body to move"); + open_move(&mut h); + h.frames(2); + click(&mut h, &format!("row:occurrence:{occurrence}")); + assert_eq!(body_names(&h.app), owned, "the component's bodies are the Move/Copy selection"); + assert!(h.app.tree.occurrence_move.is_none(), "the open body dialog stays; this click does not switch to component Move"); + + h.app.dialog = None; + h.app.run("select.clear", json!({})).unwrap(); + h.frames(2); + click(&mut h, "row:Untitled"); + assert_eq!(h.app.tree.picked_components, vec![0]); + h.app.start("solid.move"); + let names = body_names(&h.app); + assert!(names.iter().any(|n| owned.contains(n)), "Move/Copy starts from the component picked in the tree: {names:?}"); +} + +/// M and the Move/Copy command both start from a body that is already selected, and from a +/// component picked in the tree. One selected component placement still moves as a component. +#[test] +fn browser_move_copy_starts_from_the_selected_body_or_component() { + let mut h = crate::scenario::Harness::new(); + two_bodies(&mut h); + let both = json!({"items": [Sel::Body { name: "Left".into() }, Sel::Body { name: "Right".into() }]}); + h.app.run("select.set", both.clone()).unwrap(); + crate::keymap::press(&mut h.app, "M", false); + let names = body_names(&h.app); + assert!(names.contains(&"Left".into()) && names.contains(&"Right".into()), "M uses the selected bodies: {names:?}"); + // Starting the command takes the selection into the dialog, so the toolbar is tried + // with the bodies selected again. + h.app.dialog = None; + h.app.run("select.set", both).unwrap(); + h.app.start("solid.move"); + let names = body_names(&h.app); + assert!(names.contains(&"Left".into()) && names.contains(&"Right".into()), "the Move/Copy button uses the same selection: {names:?}"); + + h.app.dialog = None; + h.frames(2); + click(&mut h, "row:Untitled"); + crate::keymap::press(&mut h.app, "M", false); + let names = body_names(&h.app); + assert!(names.contains(&"Left".into()) && names.contains(&"Right".into()), "M uses the component picked in the tree: {names:?}"); + + let (mut placed, _, occurrence) = fixture(); + placed.app.run("select.set", json!({"items": [{"type": "occurrence", "path": [occurrence]}]})).unwrap(); + crate::keymap::press(&mut placed.app, "M", false); + assert!(placed.app.dialog.is_none(), "one selected component still moves as a component"); + assert!(placed.app.tree.occurrence_move.is_some()); +} + +/// Shift-click adds every body between the two rows. +#[test] +fn browser_shift_click_adds_the_bodies_in_between_to_move_copy() { + let mut h = crate::scenario::Harness::new(); + two_bodies(&mut h); + h.app.run("solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [60, 0, 0], "body_name": "Far"})).unwrap(); + open_move(&mut h); + h.frames(2); + h.step(&json!({"click": {"handle": "row:Left"}})).unwrap(); + h.frames(1); + h.step(&json!({"click": {"handle": "row:Far"}, "shift": true})).unwrap(); + h.frames(1); + let names = body_names(&h.app); + assert!(names.contains(&"Left".into()) && names.contains(&"Right".into()) && names.contains(&"Far".into()), "{names:?}"); +} + +/// #4452: Combine's active input takes the body, then the next one does. A browser click is the +/// same pick as a click in the view, so the first body is the target and the next is a tool. +#[test] +fn combine_takes_bodies_clicked_in_the_browser() { + let mut h = crate::scenario::Harness::new(); + h.app.run("solid.box", json!({"corner": [0, 0, 0], "length": 20, "width": 20, "height": 20, "body_name": "Block"})).unwrap(); + h.app.run("solid.box", json!({"corner": [10, 10, 10], "length": 20, "width": 20, "height": 20, "body_name": "Cutter"})).unwrap(); + h.app.session.selection.clear(); + h.app.start("solid.combine"); + h.frames(2); + h.step(&json!({"click": {"browser": "Block"}})).unwrap(); + h.frames(1); + h.step(&json!({"click": {"browser": "Cutter"}})).unwrap(); + h.frames(1); + let d = h.app.dialog.as_ref().expect("Combine stays open"); + assert_eq!(d.inputs[0].items, vec![Sel::Body { name: "Block".into() }], "target: {:?}", d.inputs); + assert_eq!(d.inputs[1].items, vec![Sel::Body { name: "Cutter".into() }], "tools: {:?}", d.inputs); + assert!(h.app.session.selection.is_empty(), "the browser click does not replace the selection"); +} + +/// With Move/Copy closed, a body click still selects that body, and a group or folder click +/// still folds. +#[test] +fn browser_rows_still_fold_or_select_when_move_copy_is_not_open() { + let mut h = crate::scenario::Harness::new(); + two_bodies(&mut h); + let group = h.app.run("browser.group", json!({"component": 0, "folder": "bodies", "name": "Pair", "items": ["Left", "Right"]})).unwrap(); + let group = group["group"].as_u64().unwrap(); + h.app.run("select.clear", json!({})).unwrap(); + h.frames(2); + click(&mut h, "row:Left"); + assert!(h.app.dialog.is_none()); + assert_eq!(h.app.session.selection, vec![Sel::Body { name: "Left".into() }]); + click(&mut h, "row:Pair (2)"); + assert!(h.app.tree.collapsed.contains(&format!("c0/g{group}")), "a group click folds when no command wants bodies"); + click(&mut h, "row:Bodies"); + assert!(h.app.tree.collapsed.contains("c0/bodies")); +} diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index a1ebe41d4..0d288fa60 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -745,6 +745,37 @@ impl Dialog { Dialog::new(Kind::ChatContext, inputs) } + /// A component picked in the browser is not a body selection. A command that takes bodies, + /// Move/Copy included, still starts with that component's bodies. + fn take_component_bodies(app: &SolveApp, d: &mut Dialog) { + if app.tree.picked_components.is_empty() { + return; + } + let mut names = Vec::new(); + for id in &app.tree.picked_components { + for name in crate::browser::bodies_of_component(app, *id) { + if !names.contains(&name) { + names.push(name); + } + } + } + let Some(inp) = d.inputs.iter_mut().find(|i| i.accept == BODIES && (i.multi || i.items.is_empty())) else { + return; + }; + for name in names { + let body = Sel::Body { name }; + if !inp.multi { + if inp.items.is_empty() { + inp.items.push(body); + } + break; + } + if !inp.items.contains(&body) { + inp.items.push(body); + } + } + } + pub fn for_command(app: &SolveApp, id: &str) -> Option { let s = &app.session; let has_bodies = !s.world_state().bodies.is_empty(); @@ -998,6 +1029,27 @@ impl Dialog { } continue; } + // A selected component is its bodies when the command takes bodies. One selected + // placement still starts component Move before this dialog is built. + if let Sel::Occurrence { path } = sel + && let Some(inp) = d.inputs.iter_mut().find(|i| i.accept == BODIES && (i.multi || i.items.is_empty())) + { + if let Ok(names) = s.occurrence_bodies(path, true) { + for name in names { + let body = Sel::Body { name }; + if !inp.multi { + if inp.items.is_empty() { + inp.items.push(body); + } + break; + } + if !inp.items.contains(&body) { + inp.items.push(body); + } + } + } + continue; + } // Planar-only inputs take planar faces only. let planar_ok = match sel { Sel::Face { body, index, .. } => planar_face(s, body, *index).is_some(), @@ -1031,6 +1083,7 @@ impl Dialog { } } } + Self::take_component_bodies(app, &mut d); // A primitive goes where a face or vertex is picked, standing on a planar face. if let Kind::Primitive { at, .. } = &mut d.kind { *at = s.selection.iter().find_map(|sel| match sel { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 5dfaefcce..53eaa0dac 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1200,8 +1200,9 @@ impl SolveApp { return; } workspace::select_command_tab(self, id); - // Move a selected assembly placement the same way as its Browser menu, retaining - // the exact instance path and the grounding/rigid-group restrictions. + // One selected placement moves as a component, the same way as its Browser menu, + // retaining the exact instance path and the grounding/rigid-group restrictions. + // A selected body, or a component picked in the tree, opens Move/Copy on those bodies. if id == "solid.move" && let [solidcraft_engine::Sel::Occurrence { path }] = self.session.selection.as_slice() {