From 9b8fa3d3573cd4869ee28b28ee164cc0e9e4045e Mon Sep 17 00:00:00 2001 From: phanoob Date: Fri, 23 Oct 2026 14:48:11 +0000 Subject: [PATCH] Browser: a body picked in the viewport is opened to and scrolled to (#3005) (#3081) * claim: #3005 Co-Authored-By: Claude Opus 5.5 * Browser: a body picked in the viewport is shown in the Browser (#3005) Co-Authored-By: Claude Opus 5.5 * Merge main; viewport::select takes a toggle flag now Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Phanoob Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/browser.rs | 48 ++++++++++++++++++++--- crates/ui-egui/src/browser_model_tests.rs | 25 ++++++++++++ crates/ui-egui/src/viewport.rs | 1 + 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rs index a58100877..df2846300 100644 --- a/crates/ui-egui/src/browser.rs +++ b/crates/ui-egui/src/browser.rs @@ -68,7 +68,9 @@ pub struct TreeState { pub collapsed: BTreeSet, /// Folders opened by the user (folders that start closed). pub expanded: BTreeSet, - /// Scroll to this body's row and open its folders (Find in Browser). + /// Scroll to this body's row (by the name it has in the world, which tells the placements + /// of a part apart) and open the components, placements and folders above it: Find in + /// Browser, and a body picked in the viewport (#3005). pub reveal: Option, /// A native feature to reveal in its placed component's Features folder. pub(super) reveal_feature: Option, @@ -529,6 +531,31 @@ fn sel_of_key(k: &str) -> Option { None } +/// The body a selection item is on: a body, or a face or edge of one (by its name in the +/// world). +pub(crate) fn picked_body(sel: &Sel) -> Option<&str> { + match sel { + Sel::Body { name } => Some(name), + Sel::Face { body, .. } | Sel::Edge { body, .. } => Some(body), + _ => None, + } +} + +/// A pick in the viewport: the Browser, when shown, opens down to the picked body's row and +/// scrolls to it (#3005). The body of the last item picked. +pub(crate) fn reveal_pick(app: &mut SolveApp) { + if app.ui.show_browser + && let Some(body) = app.session.selection.last().and_then(picked_body) + { + app.tree.reveal = Some(body.to_string()); + } +} + +/// Whether the body being revealed is in this placement or one below it: it opens. +fn reveals_in_occurrence(app: &SolveApp, path: &[u64]) -> bool { + app.tree.reveal.as_ref().is_some_and(|r| app.session.occurrence_bodies(path, true).is_ok_and(|bodies| bodies.contains(r))) +} + pub(crate) fn is_open(app: &SolveApp, key: &str, default: bool) -> bool { if default { !app.tree.collapsed.contains(key) } else { app.tree.expanded.contains(key) } } @@ -947,6 +974,9 @@ fn rows_scrolling(app: &mut SolveApp, ui: &mut egui::Ui, acts: &mut Actions, shr ui.spacing_mut().item_spacing.y = 0.0; component_rows(app, ui, 0, 0, acts); }); + // The row was reached and scrolled to, or isn't in the tree (a body a later feature removes): + // either way the reveal is done. + app.tree.reveal = None; } fn finish_browser(app: &mut SolveApp, ui: &mut egui::Ui, acts: Actions) { @@ -977,9 +1007,14 @@ fn component_name(app: &SolveApp, id: u64) -> String { fn component_rows(app: &mut SolveApp, ui: &mut egui::Ui, id: u64, depth: usize, acts: &mut Actions) { let t = Tokens::get(); let key = format!("c{id}"); + let bodies = crate::context_menu::component_bodies(app, id); + if let Some(r) = app.tree.reveal.clone() + && (bodies.contains(&r) || model_children(app, id).into_iter().any(|o| reveals_in_occurrence(app, &[o]))) + { + app.tree.collapsed.remove(&key); + } let open = is_open(app, &key, true); let active = app.session.active_component == id; - let bodies = crate::context_menu::component_bodies(app, id); let visible = bodies.is_empty() || bodies.iter().any(|b| !app.session.visibility.hidden_bodies.contains(b)); let grounded = app.session.doc.occurrence_of(id).is_some_and(|o| o.grounded); let faded = app.session.active_component != 0 && !app.session.doc.component_within(id, app.session.active_component) && !active; @@ -1082,6 +1117,9 @@ fn occurrence_key(path: &[u64]) -> String { fn occurrence_rows(app: &mut SolveApp, ui: &mut egui::Ui, path: Vec, depth: usize, acts: &mut Actions) { let Some(placement) = model_placement(app, &path) else { return }; let key = occurrence_key(&path); + if reveals_in_occurrence(app, &path) { + app.tree.collapsed.remove(&key); + } let open = is_open(app, &key, true); let active = app.session.active_occurrence_path.as_deref() == Some(path.as_slice()) || (app.session.active_occurrence_path.is_none() @@ -1838,11 +1876,11 @@ fn folder_rows_at(app: &mut SolveApp, ui: &mut egui::Ui, comp: u64, path: Option let key = format!("{prefix}/{folder}"); if let Some(r) = app.tree.reveal.clone() && folder == "bodies" - && all.iter().any(|e| e.key == r) + && let Some(e) = all.iter().find(|e| e.row_key == format!("b:{r}")) { app.tree.collapsed.remove(&key); app.tree.collapsed.remove(&format!("c{comp}")); - for g in app.session.doc.browser_groups.iter().filter(|g| g.items.contains(&r)) { + for g in app.session.doc.browser_groups.iter().filter(|g| g.items.contains(&e.key)) { app.tree.collapsed.remove(&format!("g{}", g.id)); } } @@ -1990,7 +2028,7 @@ fn entry_row( toggle(app, &pattern_key, true); } if folder == "bodies" - && app.tree.reveal.as_deref() == Some(e.key.as_str()) + && app.tree.reveal.as_ref().is_some_and(|b| e.row_key.strip_prefix("b:") == Some(b.as_str())) && let Some(rect) = r.rect { ui.scroll_to_rect(rect, Some(egui::Align::Center)); diff --git a/crates/ui-egui/src/browser_model_tests.rs b/crates/ui-egui/src/browser_model_tests.rs index c2c3f41a8..b0b0c9e96 100644 --- a/crates/ui-egui/src/browser_model_tests.rs +++ b/crates/ui-egui/src/browser_model_tests.rs @@ -81,3 +81,28 @@ fn body_index_and_placements_follow_geometry_undo_and_document_identity() { assert!(super::model_children(&h.app, 0).is_empty()); assert!(super::entries(&h.app, component, "bodies").is_empty()); } + +/// #3005: a face picked in the viewport, on the second placement of a part whose rows are +/// closed: that placement opens down to its body's row (which the browser lights for the pick, +/// #3009); the first placement stays closed. +#[test] +fn a_face_picked_in_the_viewport_opens_the_browser_to_its_placements_body() { + let (mut h, _, occurrence) = fixture(); + let copy = h.app.run("occurrence.copy", json!({"occurrence":occurrence,"translate":[50,0,0]})).unwrap()["occurrence"].as_u64().unwrap(); + let first = super::model_placement(&mut h.app, &[occurrence]).unwrap().bodies[0].world.clone(); + let second = super::model_placement(&mut h.app, &[copy]).unwrap().bodies[0].world.clone(); + h.app.ui.show_browser = true; + let (one, two) = (super::occurrence_key(&[occurrence]), super::occurrence_key(&[copy])); + h.app.tree.collapsed.extend([one.clone(), two.clone(), format!("{two}/bodies")]); + h.frames(2); + assert!(!h.app.tree.order.contains(&format!("b:{second}")), "the body's row is closed away"); + let face = Sel::Face { body: second.clone(), index: 0, point: solidcraft_engine::geom::Vec3::new(55.0, 5.0, 10.0) }; + crate::viewport::select(&mut h.app, Some(face), false, false); + assert_eq!(h.app.tree.reveal.as_deref(), Some(second.as_str())); + h.frames(2); + assert!(h.app.tree.reveal.is_none(), "revealed"); + assert!(!h.app.tree.collapsed.contains(&two) && !h.app.tree.collapsed.contains(&format!("{two}/bodies"))); + assert!(h.app.tree.collapsed.contains(&one), "the other placement stays as it was"); + assert!(h.app.tree.order.contains(&format!("b:{second}")), "the row is drawn"); + assert!(!h.app.tree.order.contains(&format!("b:{first}")), "the other placement's row stays closed away"); +} diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 1f6cd647f..58abe9abe 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -3608,6 +3608,7 @@ pub(crate) fn select(app: &mut SolveApp, sel: Option, add: bool, toggle: bo let _ = app.run("select.set", json!({ "items": rest })); } else { let _ = app.run("select.set", json!({"items": items, "add": add})); + crate::browser::reveal_pick(app); } } None if !add => {