diff --git a/crates/ui-egui/src/timeline.rs b/crates/ui-egui/src/timeline.rs index e10f49ff6..5b61a8d23 100644 --- a/crates/ui-egui/src/timeline.rs +++ b/crates/ui-egui/src/timeline.rs @@ -370,6 +370,12 @@ pub(crate) fn clipboard_buttons(app: &mut SolveApp, ui: &mut egui::Ui, id: u64) } } +/// Where each feature's result is in `results`, by feature id: the first result of an id, as +/// `Model::result` finds it. +fn result_index(results: &[solidcraft_engine::doc::FeatureResult]) -> std::collections::HashMap { + results.iter().enumerate().rev().map(|(k, r)| (r.id, k)).collect() +} + /// The common prefix of a feature tooltip and its copied status message. pub(crate) fn status_text(name: &str, ty: &str, error: Option<&str>, warning: Option<&str>) -> String { let mut text = format!("{name} ({ty})"); @@ -718,6 +724,9 @@ fn timeline_contents(app: &mut SolveApp, ui: &mut egui::Ui, floating: bool) { let mut marker_rect: Option = None; // Each sketch's solve state, read once for the strip (#7245). let sketch_states = crate::sketch_state::SketchStates::of(app); + // Each feature's result, found once for the strip: a search per chip made a long + // timeline cost the square of its length every frame. + let results = result_index(&app.session.model.results); for (i, (id, name, icon, suppressed, ty, kind)) in feats.iter().enumerate() { let sketch_state = matches!(kind, TimelineKind::Sketch).then(|| sketch_states.get(*id)).flatten(); if i == marker { @@ -731,7 +740,10 @@ fn timeline_contents(app: &mut SolveApp, ui: &mut egui::Ui, floating: bool) { } crate::scenario::publish_handle(&format!("timeline:{name}"), br.center()); let resp = ui.interact(br, ui.id().with(("feat", *id)), Sense::click_and_drag()); - let res = app.session.model.result(*id); + // A feature without a result has none; an index that no longer holds this id (a + // command run from an earlier chip evaluated again) is searched for. + let res = + results.get(id).and_then(|k| app.session.model.results.get(*k).filter(|r| r.id == *id).or_else(|| app.session.model.result(*id))); let err = res.and_then(|r| r.error.clone()); let warn = res.and_then(|r| r.warning.clone()); let rolled = i >= marker; @@ -1568,6 +1580,31 @@ mod tests { /// A feature with a warning mark offers Copy Warning Message, which copies what its tooltip /// says; a feature with nothing to report doesn't offer it. In both timelines. + /// The strip finds each chip's result in one table: the same result `Model::result` gives, + /// the first one when an id has two, and none for a feature without one. + #[test] + fn each_chip_finds_its_own_result() { + let mut h = two_boxes(false); + h.app.run("solid.fillet", json!({"edges": [[5, 0, 10]], "radius": 20, "fit": "reduce", "body": "One"})).unwrap(); + h.frames(2); + let model = &h.app.session.model; + let index = super::result_index(&model.results); + let ids: Vec = h.app.session.doc.features.iter().map(|f| f.id).collect(); + assert!(ids.len() >= 3 && model.results.iter().any(|r| r.warning.is_some()), "a warned feature among others"); + for id in &ids { + let found = index.get(id).and_then(|k| model.results.get(*k)); + assert!(found.is_some_and(|r| std::ptr::eq(r, model.result(*id).unwrap())), "feature {id}"); + } + assert!(!index.contains_key(&u64::MAX), "a feature without a result has none"); + let mut twice = model.results.clone(); + let first = twice.iter().position(|r| r.warning.is_some()).unwrap(); + let mut copy = twice[first].clone(); + copy.warning = None; + twice.push(copy); + let index = super::result_index(&twice); + assert_eq!(index.get(&twice[first].id), Some(&first), "the first result of an id wins, as in Model::result"); + } + #[test] fn the_feature_menu_copies_a_warning_message() { for left in [false, true] { diff --git a/crates/ui-egui/src/workspace.rs b/crates/ui-egui/src/workspace.rs index 78269d02d..e34bedf71 100644 --- a/crates/ui-egui/src/workspace.rs +++ b/crates/ui-egui/src/workspace.rs @@ -570,8 +570,11 @@ fn active_body<'a>(app: &'a crate::SolveApp, state: &'a solidcraft_engine::doc:: Sel::Face { body, .. } | Sel::Edge { body, .. } => Some(body.as_str()), _ => None, }); + // The owners in one pass: asked body by body, each one searched the timeline, and the + // ribbon asks this several times a frame. selected.or_else(|| { - state.bodies.iter().rev().find(|b| app.session.doc.model_body_component(b) == app.session.active_component).map(|b| b.name.as_str()) + let owners = app.session.doc.model_body_components(state); + state.bodies.iter().zip(owners).rev().find(|(_, c)| *c == app.session.active_component).map(|(b, _)| b.name.as_str()) }) } @@ -895,6 +898,29 @@ mod tests { assert_eq!(destination(spec), Some(("INSERT", "FILES"))); } + /// Without a body picked, the active body is the last one the active component owns, whatever + /// the other components hold after it. + #[test] + fn without_a_selection_the_active_body_is_the_active_components_last() { + let mut h = crate::scenario::Harness::new(); + h.app.run("solid.box", json!({"body_name":"Top1", "operation":"new", "length":10, "width":10, "height":10})).unwrap(); + let sub = h.app.run("component.create", json!({"name":"Sub"})).unwrap()["component"].as_u64().unwrap(); + h.app.run("component.activate", json!({"component":sub})).unwrap(); + h.app.run("solid.box", json!({"body_name":"Sub1", "operation":"new", "length":5, "width":5, "height":5})).unwrap(); + h.app.run("component.activate", json!({"component":0})).unwrap(); + h.app.run("solid.box", json!({"body_name":"Top2", "operation":"new", "length":4, "width":4, "height":4})).unwrap(); + h.app.run("component.activate", json!({"component":sub})).unwrap(); + h.app.run("solid.box", json!({"body_name":"Sub2", "operation":"new", "length":3, "width":3, "height":3})).unwrap(); + h.app.run("select.set", json!({"items":[]})).unwrap(); + let active = |h: &crate::scenario::Harness| active_body(&h.app, &h.app.session.model.state()).map(str::to_owned); + assert_eq!(active(&h).as_deref(), Some("Sub2")); + h.app.run("component.activate", json!({"component":0})).unwrap(); + h.app.run("select.set", json!({"items":[]})).unwrap(); + assert_eq!(active(&h).as_deref(), Some("Top2"), "the top level's last body, not a later one in Sub"); + h.app.run("select.set", json!({"items":[{"type":"body","name":"Sub1"}]})).unwrap(); + assert_eq!(active(&h).as_deref(), Some("Sub1"), "a picked body wins"); + } + #[test] fn contextual_tabs_follow_the_active_body_and_allow_other_tabs() { let mut h = crate::scenario::Harness::new();