diff --git a/crates/engine/src/cmd/delete.rs b/crates/engine/src/cmd/delete.rs index 9a3bc89b7..2367dd845 100644 --- a/crates/engine/src/cmd/delete.rs +++ b/crates/engine/src/cmd/delete.rs @@ -1,7 +1,7 @@ //! Delete for everything that can be selected, in one undo step: sketch entities (while //! sketching), timeline features and sketches (with the features that depend on them), //! construction planes, bodies (a Remove feature from here on in the timeline), occurrences and -//! components, and canvases. `dry_run` reports what would go and what would fail without +//! components, canvases and joints. `dry_run` reports what would go and what would fail without //! changing anything, so the UI can ask first. use serde_json::{Value, json}; @@ -13,7 +13,7 @@ use crate::params::{bad, bool_}; use crate::{EngineError, Result, Sel, Session}; pub static COMMANDS: &[CommandSpec] = &[CommandSpec::new("selection.delete", "Delete", delete).icon("delete").params( - "items?: [selection items]; components?, occurrences?, canvases?: [ids or names]; dry_run?: bool → deleted, would_fail, skipped (one undo step)", + "items?: [selection items]; components?, occurrences?, canvases?, joints?: [ids or names]; dry_run?: bool → deleted, deleted_joints, would_fail, skipped (one undo step)", )]; /// Run another command's body inside this one (so the whole delete is one undo step). @@ -52,7 +52,24 @@ fn apply(s: &mut Session, p: &Value) -> Result { } Some(_) => return Err(bad(cmd, "`items` must be a list of selection items")), }; + if let Some(value) = p.get("joints").filter(|v| !v.is_null()) { + let Some(joints) = value.as_array() else { + return Err(bad(cmd, "`joints` must be a list of ids or names")); + }; + if joints.len() > 10_000 || joints.iter().any(|j| !j.is_string() && j.as_u64().is_none()) { + return Err(bad(cmd, "`joints` must contain at most 10000 ids or names")); + } + } let before = s.model.clone(); + let mut joints = p + .get("joints") + .and_then(Value::as_array) + .into_iter() + .flatten() + .map(|value| super::joints::joint_id(s, Some(value), cmd)) + .collect::>>()?; + joints.sort_unstable(); + joints.dedup(); let features_before: Vec<(u64, String)> = s.doc.features.iter().map(|f| (f.id, f.name.clone())).collect(); let mut skipped: Vec = Vec::new(); let mut skip = |why: String| { @@ -94,6 +111,10 @@ fn apply(s: &mut Session, p: &Value) -> Result { } features.dedup(); let mut done = false; + for id in &joints { + sub(s, "joint.delete", json!({"joint": id}))?; + done = true; + } if !entities.is_empty() { if s.active_sketch.is_none() { skip("sketch entities are deleted while editing their sketch".into()); @@ -168,7 +189,7 @@ fn apply(s: &mut Session, p: &Value) -> Result { .filter(|r| r.error.is_some() && before.result(r.id).is_some_and(|b| b.error.is_none() && !b.skipped)) .map(|r| r.name.clone()) .collect(); - Ok(json!({ "deleted": deleted, "bodies": bodies, "would_fail": would_fail, "skipped": skipped })) + Ok(json!({ "deleted": deleted, "deleted_joints": joints, "bodies": bodies, "would_fail": would_fail, "skipped": skipped })) } #[cfg(test)] @@ -251,6 +272,80 @@ mod tests { } } + fn joint_assembly() -> Session { + let mut s = Session::default(); + for (name, x) in [("A", 0), ("B", 30), ("C", 60)] { + run(&mut s, "component.activate", json!({"component": "root"})); + run(&mut s, "component.create", json!({"name": name})); + run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 10, "corner": [x, 0, 0]})); + } + run(&mut s, "component.activate", json!({"component": "root"})); + run(&mut s, "occurrence.ground", json!({"occurrence": "A:1", "grounded": true})); + for (name, other, face) in [("Hinge", "B:1", [40, 10, 0]), ("Glue", "C:1", [70, 10, 0])] { + run( + &mut s, + "joint.create", + json!({"type": "revolute", "name": name, + "a": {"occurrence": "A:1", "face": [10, 10, 10]}, "b": {"occurrence": other, "face": face}}), + ); + } + run(&mut s, "joint.motion_link", json!({"a": "Hinge", "b": "Glue", "ratio": 1})); + s + } + + #[test] + fn selected_joints_and_their_link_are_deleted_in_one_undo_step_with_a_read_only_dry_run() { + let mut s = joint_assembly(); + let features = serde_json::to_value(&s.doc.features).unwrap(); + let hinge = s.doc.assembly.joints[0].id; + let dry = run(&mut s, "selection.delete", json!({"joints": [hinge, "Hinge", "Glue"], "dry_run": true})); + assert_eq!(dry["deleted_joints"].as_array().unwrap().len(), 2); + assert_eq!(s.doc.assembly.joints.len(), 2); + assert_eq!(s.doc.assembly.links.len(), 1); + assert_eq!(serde_json::to_value(&s.doc.features).unwrap(), features); + run(&mut s, "selection.delete", json!({"joints": [hinge, "Hinge", "Glue"]})); + assert!(s.doc.assembly.joints.is_empty() && s.doc.assembly.links.is_empty()); + assert_eq!(serde_json::to_value(&s.doc.features).unwrap(), features); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc.assembly.joints.len(), 2); + assert_eq!(s.doc.assembly.links.len(), 1); + assert_eq!(serde_json::to_value(&s.doc.features).unwrap(), features); + run(&mut s, "edit.redo", json!({})); + assert!(s.doc.assembly.joints.is_empty() && s.doc.assembly.links.is_empty()); + } + + #[test] + fn an_unknown_joint_refuses_the_whole_delete_without_losing_features_or_other_joints() { + let mut s = joint_assembly(); + let before = serde_json::to_value(&s.doc.features).unwrap(); + let feature = s.doc.features[0].id; + assert!(s.execute("selection.delete", &json!({"items": [{"type": "feature", "id": feature}], "joints": ["Hinge", "missing"]})).is_err()); + for joints in [json!("Hinge"), json!([null]), json!([-1]), json!([1.5]), json!(vec!["Hinge"; 10_001])] { + assert!(s.execute("selection.delete", &json!({"joints": joints})).is_err()); + } + assert_eq!(serde_json::to_value(&s.doc.features).unwrap(), before); + assert_eq!(s.doc.assembly.joints.len(), 2); + assert_eq!(s.doc.assembly.links.len(), 1); + } + + #[test] + fn numeric_joint_ids_do_not_delete_a_different_joint_with_that_numeric_name() { + for batch in [false, true] { + let mut s = joint_assembly(); + let hinge = s.doc.assembly.joints[0].id; + let glue = s.doc.assembly.joints[1].id; + run(&mut s, "joint.edit", json!({"joint": hinge, "name": glue.to_string()})); + let features = serde_json::to_value(&s.doc.features).unwrap(); + let (command, params) = if batch { ("selection.delete", json!({"joints": [glue]})) } else { ("joint.delete", json!({"joint": glue})) }; + run(&mut s, command, params); + assert_eq!(s.doc.assembly.joints.len(), 1); + assert_eq!(s.doc.assembly.joints[0].id, hinge, "a numeric id must not match another joint's name"); + assert_eq!(serde_json::to_value(&s.doc.features).unwrap(), features); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc.assembly.joints.len(), 2); + } + } + #[test] fn occurrences_components_and_sketch_entities() { let (mut s, _) = part(); diff --git a/crates/engine/src/cmd/joints.rs b/crates/engine/src/cmd/joints.rs index e7c071c45..bac77a780 100644 --- a/crates/engine/src/cmd/joints.rs +++ b/crates/engine/src/cmd/joints.rs @@ -497,7 +497,12 @@ pub(crate) fn joint_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result Some(Value::String(x)) => x.clone(), _ => return Err(bad(cmd, "`joint` must be an id or a name")), }; - s.doc.assembly.joints.iter().find(|j| j.id.to_string() == key || j.name == key).map(|j| j.id).ok_or_else(|| bad(cmd, format!("no joint `{key}`"))) + let joint = if let Some(Value::Number(number)) = v { + number.as_u64().and_then(|id| s.doc.assembly.joints.iter().find(|joint| joint.id == id)) + } else { + s.doc.assembly.joints.iter().find(|joint| joint.id.to_string() == key || joint.name == key) + }; + joint.map(|joint| joint.id).ok_or_else(|| bad(cmd, format!("no joint `{key}`"))) } /// A joint value: numbers are degrees for angles and mm for distances; text is an expression. diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs index 46429a9c5..9f0a6315e 100644 --- a/crates/ui-egui/src/context_menu.rs +++ b/crates/ui-egui/src/context_menu.rs @@ -643,7 +643,7 @@ fn joint_items(app: &SolveApp, id: u64) -> Vec { act("ui.findJoint", "Find in Window", "").with(json!({ "joint": id })), cmd(app, "joint.edit", if j.suppressed { "Unsuppress" } else { "Suppress" }).with(json!({ "joint": id, "suppressed": !j.suppressed })), Item::sep(), - cmd(app, "joint.delete", "Delete").key("Del").with(json!({ "joint": id })), + act("ui.delete", "Delete", "delete").key("Del").with(json!({ "joints": [id] })), act("ui.rename", "Rename", "").with(json!({ "joint": id })), Item::sep(), // Hiding a joint hides only its glyph: it still constrains and moves the parts. diff --git a/crates/ui-egui/src/delete.rs b/crates/ui-egui/src/delete.rs index 1f1260152..571e75cea 100644 --- a/crates/ui-egui/src/delete.rs +++ b/crates/ui-egui/src/delete.rs @@ -21,11 +21,11 @@ pub fn selection_params(app: &SolveApp) -> Value { let items: Vec<&solidcraft_engine::Sel> = app.session.selection.iter().filter(|s| !matches!(s, solidcraft_engine::Sel::Body { name } if app.ui.locked_bodies.contains(name))).collect(); let occurrences: Vec = app.tree.picked_components.iter().filter_map(|c| app.session.doc.occurrence_of(*c).map(|o| o.id)).collect(); - json!({ "items": items, "occurrences": occurrences, "canvases": app.tree.picked_canvases }) + json!({ "items": items, "occurrences": occurrences, "canvases": app.tree.picked_canvases, "joints": app.tree.picked_joints }) } fn is_empty(p: &Value) -> bool { - ["items", "occurrences", "components", "canvases"].iter().all(|k| p.get(*k).and_then(Value::as_array).is_none_or(|a| a.is_empty())) + ["items", "occurrences", "components", "canvases", "joints"].iter().all(|k| p.get(*k).and_then(Value::as_array).is_none_or(|a| a.is_empty())) } fn shared_occurrences(app: &SolveApp, params: &Value) -> bool { @@ -53,6 +53,11 @@ pub fn delete_selection(app: &mut SolveApp) { /// other features go with it or would fail. The dry run that finds out evaluates the history /// after what is deleted, so it may compute off the window thread (#2678). pub fn request(app: &mut SolveApp, params: Value) { + if params.get("joints").and_then(Value::as_array).is_some_and(|a| !a.is_empty()) + && (app.dialog.is_some() || app.tool.is_some() || app.computing_since().is_some() || app.file_load.busy()) + { + return; + } if is_empty(¶ms) { return; } @@ -87,6 +92,7 @@ fn run(app: &mut SolveApp, params: &Value) { Ok(r) => { app.tree.picked_components.clear(); app.tree.picked_canvases.clear(); + app.tree.picked_joints.clear(); if let Some(s) = r["skipped"].as_array().and_then(|a| a.first()).and_then(Value::as_str) { app.set_status(format!("deleted; {s}"), false); } diff --git a/crates/ui-egui/src/dialogs_assembly.rs b/crates/ui-egui/src/dialogs_assembly.rs index 045894c35..16270ba85 100644 --- a/crates/ui-egui/src/dialogs_assembly.rs +++ b/crates/ui-egui/src/dialogs_assembly.rs @@ -1229,7 +1229,7 @@ fn joint_menu( } ui.separator(); if ui.button("Delete").clicked() { - *run = Some(("joint.delete", json!({ "joint": id }))); + *run = Some(("selection.delete", json!({ "joints": [id] }))); ui.close(); } } @@ -1248,8 +1248,15 @@ pub fn timeline_joints(app: &mut SolveApp, ui: &mut egui::Ui, p: &egui::Painter, let mut run: Option<(&'static str, Value)> = None; for (id, name, kind, suppressed) in joints { let br = egui::Rect::from_min_size(egui::pos2(x, r.top() + 6.0), vec2(28.0, 28.0)); + crate::scenario::publish_handle(&format!("timeline-joint:{name}"), br.center()); let resp = ui.interact(br, ui.id().with(("joint", id)), egui::Sense::click()); - let bg = if resp.hovered() { t.hover } else { t.timeline_item }; + let bg = if app.tree.picked_joints.contains(&id) { + t.accent_soft + } else if resp.hovered() { + t.hover + } else { + t.timeline_item + }; p.rect(br, 3.0, bg, Stroke::new(1.0, t.border), egui::StrokeKind::Inside); crate::icons::paint(p, br.shrink(4.0), "joint", if suppressed { t.border } else { t.icon }, t.icon_fill, t.accent); if suppressed { @@ -1258,12 +1265,21 @@ pub fn timeline_joints(app: &mut SolveApp, ui: &mut egui::Ui, p: &egui::Painter, let resp = resp.on_hover_text(format!("{name} ({kind:?} joint)\nDouble-click to edit · right-click for more")); if resp.double_clicked() { open = edit_joint(app, id); + } else if resp.clicked() { + pick_joint(app, id, ui.input(|i| i.modifiers.command || i.modifiers.ctrl)); + } + if resp.secondary_clicked() && !app.tree.picked_joints.contains(&id) { + pick_joint(app, id, false); } resp.context_menu(|ui| joint_menu(app, ui, (id, kind, suppressed), &mut open, &mut run)); x += 32.0; } if let Some((c, prm)) = run { - let _ = app.run(c, prm); + if c == "selection.delete" { + crate::delete::request(app, prm); + } else { + let _ = app.run(c, prm); + } } if let Some(d) = open { app.tool = None; @@ -1286,7 +1302,10 @@ pub fn timeline_joints_vertical(app: &mut SolveApp, ui: &mut egui::Ui) { let mut run = None; for (id, name, kind, suppressed) in joints { let (rect, response) = ui.allocate_exact_size(vec2(ui.available_width(), 30.0), egui::Sense::click()); - if response.hovered() { + crate::scenario::publish_handle(&format!("timeline-joint:{name}"), rect.center()); + if app.tree.picked_joints.contains(&id) { + ui.painter().rect_filled(rect, 3.0, t.accent_soft); + } else if response.hovered() { ui.painter().rect_filled(rect, 3.0, t.hover); } let icon = egui::Rect::from_min_size(egui::pos2(rect.left() + 4.0, rect.top() + 4.0), vec2(22.0, 22.0)); @@ -1304,11 +1323,20 @@ pub fn timeline_joints_vertical(app: &mut SolveApp, ui: &mut egui::Ui) { let response = response.on_hover_text(format!("{name} ({kind:?} joint)\nDouble-click to edit · right-click for more")); if response.double_clicked() { open = edit_joint(app, id); + } else if response.clicked() { + pick_joint(app, id, ui.input(|i| i.modifiers.command || i.modifiers.ctrl)); + } + if response.secondary_clicked() && !app.tree.picked_joints.contains(&id) { + pick_joint(app, id, false); } response.context_menu(|ui| joint_menu(app, ui, (id, kind, suppressed), &mut open, &mut run)); } if let Some((command, params)) = run { - let _ = app.run(command, params); + if command == "selection.delete" { + crate::delete::request(app, params); + } else { + let _ = app.run(command, params); + } } if let Some(dialog) = open { app.tool = None; @@ -1377,7 +1405,7 @@ pub fn browser_joints(app: &mut SolveApp, ui: &mut egui::Ui, depth: usize) { // The same menus as the rest of the browser. if let Some(at) = r.secondary { if !app.tree.picked_joints.contains(&id) { - app.tree.picked_joints = vec![id]; + pick_joint(app, id, false); } crate::context_menu::open_for(app, at, crate::context_menu::Target::Joint { id }); } @@ -1391,6 +1419,8 @@ pub fn browser_joints(app: &mut SolveApp, ui: &mut egui::Ui, depth: usize) { /// A click on a joint's browser row: it becomes the pick (or, with Ctrl, toggles in or out of /// it). Picked joints draw highlighted in the viewport, even hidden ones. pub fn pick_joint(app: &mut SolveApp, id: u64, add: bool) { + app.tree.picked_timeline_groups.clear(); + app.tree.timeline_anchor = None; if !add { let was = app.tree.picked_joints == [id]; app.tree.picked_joints.clear(); diff --git a/crates/ui-egui/src/joint_browser_tests.rs b/crates/ui-egui/src/joint_browser_tests.rs index 9d055bec4..7709eb8f2 100644 --- a/crates/ui-egui/src/joint_browser_tests.rs +++ b/crates/ui-egui/src/joint_browser_tests.rs @@ -119,3 +119,205 @@ fn the_menu_and_the_v_key_hide_and_show_picked_joints() { context_menu::toggle_visibility(&mut h.app); assert!(h.app.session.joint_visible(hinge) && h.app.session.joint_visible(glue)); } + +mod delete { + //! Joint history and Browser selection must be the target of the Delete key. + use serde_json::{Value, json}; + + use crate::scenario::Harness; + + fn assembly() -> Harness { + let mut h = Harness::new(); + h.app.home.open = false; + for (name, x) in [("A", 0), ("B", 30), ("C", 60)] { + h.app.run("component.activate", json!({"component": "root"})).unwrap(); + h.app.run("component.create", json!({"name": name})).unwrap(); + h.app.run("solid.box", json!({"length": 20, "width": 20, "height": 10, "corner": [x, 0, 0]})).unwrap(); + } + h.app.run("component.activate", json!({"component": "root"})).unwrap(); + h.app.run("occurrence.ground", json!({"occurrence": "A:1", "grounded": true})).unwrap(); + for (name, kind, other, face) in [("Hinge", "revolute", "B:1", [40, 10, 0]), ("Glue", "rigid", "C:1", [70, 10, 0])] { + h.app + .run( + "joint.create", + json!({"type": kind, "name": name, + "a": {"occurrence": "A:1", "face": [10, 10, 10]}, "b": {"occurrence": other, "face": face}}), + ) + .unwrap(); + } + h.frames(3); + h + } + + fn joint(h: &Harness, name: &str) -> u64 { + h.app.session.doc.assembly.joints.iter().find(|j| j.name == name).unwrap().id + } + + fn click(h: &mut Harness, handle: &str) { + h.step(&json!({"click": {"handle": handle}})).unwrap(); + h.frames(3); + } + + fn features(h: &Harness) -> Value { + serde_json::to_value(&h.app.session.doc.features).unwrap() + } + + #[test] + fn a_joint_history_click_replaces_the_stale_feature_before_delete_in_both_layouts() { + for left in [false, true] { + let mut h = assembly(); + h.app.ui.show_browser = false; + h.app.preferences.timeline_left = left; + h.frames(3); + let hinge = joint(&h, "Hinge"); + let before = features(&h); + let feature = h.app.session.doc.features.last().unwrap(); + let (id, name) = (feature.id, feature.name.clone()); + click(&mut h, &format!("timeline:{name}")); + assert!(h.app.session.selection.iter().any(|s| matches!(s, solidcraft_engine::Sel::Feature { id: f } if *f == id))); + click(&mut h, "timeline-joint:Hinge"); + assert_eq!(h.app.tree.picked_joints, [hinge], "the clicked joint is the selection; left={left}"); + assert!(h.app.session.selection.is_empty(), "the old feature must not remain a Delete target"); + h.step(&json!({"key": "Delete"})).unwrap(); + assert_eq!(features(&h), before, "Delete must leave every feature intact"); + assert!(h.app.session.doc.assembly.joints.iter().all(|j| j.id != hinge)); + assert_eq!(h.app.session.doc.assembly.joints.len(), 1); + h.app.run("edit.undo", json!({})).unwrap(); + assert_eq!(h.app.session.doc.assembly.joints.len(), 2); + assert_eq!(features(&h), before); + h.app.run("edit.redo", json!({})).unwrap(); + assert_eq!(h.app.session.doc.assembly.joints.len(), 1); + assert_eq!(features(&h), before); + } + } + + #[test] + fn browser_joint_multiselection_deletes_in_one_undo_step() { + let mut h = assembly(); + let before = features(&h); + click(&mut h, "row:Hinge"); + h.step(&json!({"click": {"handle": "row:Glue"}, "ctrl": true})).unwrap(); + h.frames(3); + assert_eq!(h.app.tree.picked_joints.len(), 2); + h.step(&json!({"key": "Backspace"})).unwrap(); + assert!(h.app.session.doc.assembly.joints.is_empty()); + assert!(h.app.tree.picked_joints.is_empty()); + assert_eq!(features(&h), before); + h.app.run("edit.undo", json!({})).unwrap(); + assert_eq!(h.app.session.doc.assembly.joints.len(), 2); + assert_eq!(features(&h), before); + } + + #[test] + fn picking_a_feature_after_a_joint_replaces_the_joint_selection() { + for left in [false, true] { + let mut h = assembly(); + h.app.ui.show_browser = false; + h.app.preferences.timeline_left = left; + h.frames(3); + click(&mut h, "timeline-joint:Hinge"); + let name = h.app.session.doc.features.last().unwrap().name.clone(); + click(&mut h, &format!("timeline:{name}")); + assert!(h.app.tree.picked_joints.is_empty(), "joint selection must follow the feature click; left={left}"); + assert!(!h.app.session.selection.is_empty()); + } + } + + #[test] + fn a_joint_menu_delete_uses_its_target_despite_a_stale_feature_selection() { + let mut h = assembly(); + let before = features(&h); + let feature = h.app.session.doc.features.last().unwrap().id; + h.app.run("select.set", json!({"items": [{"type": "feature", "id": feature}]})).unwrap(); + let hinge = joint(&h, "Hinge"); + let items = crate::context_menu::items(&h.app, &crate::context_menu::Target::Joint { id: hinge }); + let item = items.iter().find(|i| i.label == "Delete").unwrap().clone(); + crate::context_menu::run_item(&mut h.app, &item, egui::pos2(0., 0.)); + assert_eq!(h.app.session.doc.assembly.joints.len(), 1); + assert_eq!(features(&h), before); + h.app.run("edit.undo", json!({})).unwrap(); + assert_eq!(h.app.session.doc.assembly.joints.len(), 2); + } + + #[test] + fn history_joint_ctrl_click_toggles_and_the_context_menu_selects_its_target() { + for left in [false, true] { + let mut h = assembly(); + h.app.ui.show_browser = false; + h.app.preferences.timeline_left = left; + h.frames(3); + let (hinge, glue) = (joint(&h, "Hinge"), joint(&h, "Glue")); + let before = features(&h); + click(&mut h, "timeline-joint:Hinge"); + h.step(&json!({"click": {"handle": "timeline-joint:Glue"}, "ctrl": true})).unwrap(); + h.frames(3); + assert_eq!(h.app.tree.picked_joints, [hinge, glue]); + h.step(&json!({"click": {"handle": "timeline-joint:Hinge"}, "ctrl": true})).unwrap(); + h.frames(3); + assert_eq!(h.app.tree.picked_joints, [glue]); + let name = h.app.session.doc.features.last().unwrap().name.clone(); + click(&mut h, &format!("timeline:{name}")); + h.step(&json!({"click": {"handle": "timeline-joint:Hinge"}, "button": "right"})).unwrap(); + h.frames(3); + assert_eq!(h.app.tree.picked_joints, [hinge]); + assert!(h.app.session.selection.is_empty()); + h.step(&json!({"widget": "Delete"})).unwrap(); + assert!(h.app.session.doc.assembly.joints.iter().all(|j| j.id != hinge)); + assert!(h.app.session.doc.assembly.joints.iter().any(|j| j.id == glue)); + assert_eq!(features(&h), before); + } + } + + #[test] + fn joint_delete_does_not_run_while_a_dialog_or_history_command_is_active() { + let mut h = assembly(); + click(&mut h, "row:Hinge"); + let before = features(&h); + h.step(&json!({"start": "solid.box"})).unwrap(); + let hinge = joint(&h, "Hinge"); + h.app.tree.picked_joints.clear(); + crate::dialogs_assembly::pick_joint(&mut h.app, hinge, false); + assert!(h.app.dialog.is_some()); + assert_eq!(h.app.tree.picked_joints, [hinge], "the dialog guard must protect an actual joint selection"); + click(&mut h, "dialog:length"); + h.step(&json!({"key": "Delete"})).unwrap(); + assert_eq!(h.app.session.doc.assembly.joints.len(), 2, "Delete belongs to the focused field"); + assert_eq!(h.app.tree.picked_joints, [hinge]); + crate::delete::delete_selection(&mut h.app); + assert_eq!(h.app.session.doc.assembly.joints.len(), 2, "a dialog must not delete a joint behind it"); + h.step(&json!({"key": "Escape"})).unwrap(); + let hinge = joint(&h, "Hinge"); + h.app.tree.picked_joints.clear(); + crate::dialogs_assembly::pick_joint(&mut h.app, hinge, false); + assert_eq!(h.app.tree.picked_joints, [hinge]); + h.app.apply_off_thread = Some(std::time::Duration::ZERO); + h.app.run_heavy("timeline.roll_to", json!({"position": 1})).unwrap(); + assert!(h.app.computing_since().is_some()); + crate::delete::delete_selection(&mut h.app); + assert_eq!(h.app.session.doc.assembly.joints.len(), 2, "Delete must not race the pending history command"); + assert_eq!(features(&h), before); + } + + #[test] + fn a_ctrl_joint_pick_does_not_leave_a_folder_as_the_delete_target() { + let mut h = assembly(); + h.app.ui.show_browser = false; + h.app.preferences.timeline_left = true; + let feature = h.app.session.doc.features[0].id; + let group = h.app.run("timeline.group", json!({"name": "History", "features": [feature]})).unwrap()["group"].as_u64().unwrap(); + h.app.tree.picked_timeline_groups.push(group); + h.frames(3); + let before = features(&h); + let hinge = joint(&h, "Hinge"); + h.step(&json!({"click": {"handle": "timeline-joint:Hinge"}, "ctrl": true})).unwrap(); + h.frames(3); + assert_eq!(h.app.tree.picked_joints, [hinge]); + assert!(h.app.tree.picked_timeline_groups.is_empty()); + h.step(&json!({"key": "Delete"})).unwrap(); + assert!(h.app.session.doc.assembly.joints.iter().all(|joint| joint.id != hinge)); + assert!(h.app.session.doc.timeline_groups.iter().any(|folder| folder.id == group), "Delete must preserve the old folder"); + assert_eq!(features(&h), before); + h.app.run("edit.undo", json!({})).unwrap(); + assert_eq!(h.app.session.doc.assembly.joints.len(), 2); + } +} diff --git a/crates/ui-egui/src/timeline.rs b/crates/ui-egui/src/timeline.rs index e3b831980..de7539525 100644 --- a/crates/ui-egui/src/timeline.rs +++ b/crates/ui-egui/src/timeline.rs @@ -402,6 +402,8 @@ pub fn timeline(app: &mut SolveApp, ui: &mut egui::Ui) { } else if ui.input(|i| i.modifiers.command || i.modifiers.ctrl) { toggle_feature(app, *id); } else { + app.tree.picked_joints.clear(); + app.tree.picked_timeline_groups.clear(); let _ = app.run("select.set", json!({"items": [{"type": "feature", "id": id}]})); } } diff --git a/crates/ui-egui/src/timeline_left.rs b/crates/ui-egui/src/timeline_left.rs index 192cca55b..f7db54a45 100644 --- a/crates/ui-egui/src/timeline_left.rs +++ b/crates/ui-egui/src/timeline_left.rs @@ -223,6 +223,7 @@ fn drag_x(items: &DragItems, pointer_x: f32) -> f32 { } fn select_only(app: &mut SolveApp, item: TimelineItem) { + app.tree.picked_joints.clear(); match item { TimelineItem::Group(id) => { app.tree.picked_timeline_groups.clear(); @@ -239,6 +240,9 @@ fn select_only(app: &mut SolveApp, item: TimelineItem) { fn select_item(app: &mut SolveApp, ui: &egui::Ui, item: TimelineItem, order: &[TimelineItem]) { let modifiers = ui.input(|input| input.modifiers); + if !modifiers.command && !modifiers.ctrl { + app.tree.picked_joints.clear(); + } if modifiers.shift && let Some(anchor) = app.tree.timeline_anchor.as_deref() && let (Some(from), Some(to)) = (order.iter().position(|entry| entry.key() == anchor), order.iter().position(|entry| *entry == item))