* claim: #5164 * Browser: drag a placement row onto another to move it into that component (#5164) --------- Co-authored-by: Colin Faulkingham <colinfaulkingham@Colins-Mac-mini.local>
This commit is contained in:
committed by
GitHub
parent
46a6686380
commit
6f9788ccba
@@ -1357,6 +1357,14 @@ fn component_rows_inner(
|
||||
{
|
||||
acts.drop = Some(DropAction::Component { to: id, folder: d.folder, keys: d.keys.clone() });
|
||||
}
|
||||
|
||||
// A placement row dropped on the design row moves out to the design.
|
||||
if let Some(resp) = &r.resp
|
||||
&& let Some(d) = occurrence_drop_target(ui, resp, |d| occurrence_can_land(app, d, id))
|
||||
{
|
||||
let occurrence = d.path.last().copied().unwrap_or(0);
|
||||
acts.drop = Some(DropAction::Reparent { occurrence, path: d.path.clone(), parent: id, parent_path: None });
|
||||
}
|
||||
if !open {
|
||||
return;
|
||||
}
|
||||
@@ -1482,6 +1490,11 @@ fn occurrence_rows_inner(
|
||||
ui.label(inherited.trim_start());
|
||||
}
|
||||
});
|
||||
// Dragging the row moves the placement it stands for. A press on its fold arrow, eye or
|
||||
// radio is not a drag of the row, so those controls keep their own jobs.
|
||||
if resp.drag_started() && !r.on_control {
|
||||
resp.dnd_set_drag_payload(OccurrenceDrag { path: path.to_vec(), component: placement.component });
|
||||
}
|
||||
}
|
||||
if r.fold {
|
||||
toggle(app, key, true);
|
||||
@@ -1519,6 +1532,14 @@ fn occurrence_rows_inner(
|
||||
{
|
||||
acts.drop = Some(DropAction::Component { to: placement.component, folder: d.folder, keys: d.keys.clone() });
|
||||
}
|
||||
|
||||
// A placement row dropped on this one moves into this row's component.
|
||||
if let Some(resp) = &r.resp
|
||||
&& let Some(d) = occurrence_drop_target(ui, resp, |d| occurrence_can_land(app, d, placement.component))
|
||||
{
|
||||
let occurrence = d.path.last().copied().unwrap_or(0);
|
||||
acts.drop = Some(DropAction::Reparent { occurrence, path: d.path.clone(), parent: placement.component, parent_path: Some(path.to_vec()) });
|
||||
}
|
||||
if !open {
|
||||
return;
|
||||
}
|
||||
@@ -2149,6 +2170,15 @@ struct Drag {
|
||||
keys: Vec<String>,
|
||||
}
|
||||
|
||||
/// A placement row dragged in the tree: the exact path it sits at, and the component it places.
|
||||
#[derive(Clone, Debug)]
|
||||
struct OccurrenceDrag {
|
||||
/// The placement's path from the design root (its own occurrence id last).
|
||||
path: Vec<u64>,
|
||||
/// The component the placement is an instance of.
|
||||
component: u64,
|
||||
}
|
||||
|
||||
/// The items of a component folder, in model order.
|
||||
fn entries(app: &SolveApp, comp: u64, folder: &str) -> Vec<Entry> {
|
||||
let doc = &app.session.doc;
|
||||
@@ -2644,6 +2674,35 @@ fn drop_target(ui: &egui::Ui, resp: &egui::Response, accept: impl Fn(&Drag) -> b
|
||||
resp.dnd_release_payload::<Drag>().filter(|d| accept(d))
|
||||
}
|
||||
|
||||
/// Whether a placement dragged from `d` may land in component `parent`: not where it already is,
|
||||
/// and not in itself or a component it holds. The command refuses those too; this keeps the row
|
||||
/// from showing a drop mark it would refuse.
|
||||
fn occurrence_can_land(app: &SolveApp, d: &OccurrenceDrag, parent: u64) -> bool {
|
||||
let Some(id) = d.path.last().copied() else { return false };
|
||||
let Some(o) = app.session.doc.occurrences.iter().find(|o| o.id == id) else { return false };
|
||||
if parent == 0 {
|
||||
// The design row takes any placement not already at the design root: there is no cycle
|
||||
// to make at the top, and everything is inside the design.
|
||||
return o.parent != 0;
|
||||
}
|
||||
o.parent != parent && !app.session.doc.inside_component(parent, d.component)
|
||||
}
|
||||
|
||||
/// While a matching placement drag hovers the row, mark where it would land; on release, the payload.
|
||||
fn occurrence_drop_target(ui: &egui::Ui, resp: &egui::Response, accept: impl Fn(&OccurrenceDrag) -> bool) -> Option<std::sync::Arc<OccurrenceDrag>> {
|
||||
let t = Tokens::get();
|
||||
let hovering = resp.dnd_hover_payload::<OccurrenceDrag>().filter(|d| accept(d));
|
||||
if hovering.is_some() {
|
||||
let r = resp.rect;
|
||||
ui.painter().rect_stroke(r.shrink(0.5), t.radius_sm.saturating_sub(1), Stroke::new(1.5, t.accent), egui::StrokeKind::Inside);
|
||||
}
|
||||
// Taking a payload takes it whatever its type: leave other drags (appearance swatches) be.
|
||||
if !egui::DragAndDrop::has_payload_of_type::<OccurrenceDrag>(ui.ctx()) {
|
||||
return None;
|
||||
}
|
||||
resp.dnd_release_payload::<OccurrenceDrag>().filter(|d| accept(d))
|
||||
}
|
||||
|
||||
/// A drop, applied after the tree is drawn.
|
||||
enum DropAction {
|
||||
/// Into a group (before an item of it), or out of groups.
|
||||
@@ -2652,6 +2711,8 @@ enum DropAction {
|
||||
Order { comp: u64, folder: &'static str, keys: Vec<String>, order: Vec<String> },
|
||||
/// Into another component (bodies and sketches).
|
||||
Component { to: u64, folder: &'static str, keys: Vec<String> },
|
||||
/// A placement moved into another component (or the design, `parent` 0).
|
||||
Reparent { occurrence: u64, path: Vec<u64>, parent: u64, parent_path: Option<Vec<u64>> },
|
||||
}
|
||||
|
||||
fn apply_drop(app: &mut SolveApp, d: DropAction) {
|
||||
@@ -2670,6 +2731,25 @@ fn apply_drop(app: &mut SolveApp, d: DropAction) {
|
||||
_ => Ok(serde_json::Value::Null),
|
||||
};
|
||||
}
|
||||
DropAction::Reparent { occurrence, path, parent, parent_path } => {
|
||||
let mut payload = json!({ "occurrence": occurrence, "path": path, "parent": parent });
|
||||
if let Some(pp) = parent_path {
|
||||
payload["parent_path"] = json!(pp);
|
||||
}
|
||||
match app.run("occurrence.reparent", payload) {
|
||||
Ok(out) => {
|
||||
let to: Vec<u64> = out
|
||||
.get("path")
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.map(|a| a.iter().filter_map(serde_json::Value::as_u64).collect())
|
||||
.unwrap_or_default();
|
||||
if !to.is_empty() {
|
||||
close_placement(app, &to);
|
||||
}
|
||||
}
|
||||
Err(e) => app.set_status(e, true),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -457,3 +457,44 @@ fn a_folded_folders_menu_items_move_to_the_placements_menu() {
|
||||
h.app.preferences.browser_bodies_folders = BodiesFolders::Always;
|
||||
assert!(!has(&h, "component.from_bodies"), "Always: on the folder's own menu");
|
||||
}
|
||||
|
||||
/// #5164: dragging a placement row onto another moves it into that component. The command
|
||||
/// underneath is `occurrence.reparent`, which keeps the placement's world position. Dragging it
|
||||
/// back onto the design row moves it out to the design.
|
||||
#[test]
|
||||
fn dragging_an_occurrence_row_onto_another_moves_it_into_that_component() {
|
||||
let mut h = Harness::new();
|
||||
// Component A with a body, at the design root.
|
||||
let a = h.app.run("component.create", json!({"name":"A"})).unwrap();
|
||||
let (_a_comp, a_occ) = (a["component"].as_u64().unwrap(), a["occurrence"].as_u64().unwrap());
|
||||
h.app.run("solid.box", json!({"length":10,"width":10,"height":10})).unwrap();
|
||||
// Back to the design root so B is a sibling of A, not a child of it.
|
||||
h.app.run("component.activate", json!({"component":0})).unwrap();
|
||||
// Component B with a body, at the design root.
|
||||
let b = h.app.run("component.create", json!({"name":"B"})).unwrap();
|
||||
let (b_comp, b_occ) = (b["component"].as_u64().unwrap(), b["occurrence"].as_u64().unwrap());
|
||||
h.app.run("solid.box", json!({"length":10,"width":10,"height":10,"corner":[30,0,0]})).unwrap();
|
||||
h.app.run("component.activate", json!({"component":0})).unwrap();
|
||||
h.frames(3);
|
||||
|
||||
// Both rows sit at the root. Drag A's row onto B's row.
|
||||
let from = crate::scenario::handle_at(&format!("row:occurrence:{a_occ}")).unwrap();
|
||||
let to = crate::scenario::handle_at(&format!("row:occurrence:{b_occ}")).unwrap();
|
||||
let drag = h.call("ui.drag", json!({"x0":from.x,"y0":from.y,"x1":to.x,"y1":to.y,"steps":12}));
|
||||
assert_eq!(drag["ok"], true);
|
||||
h.frames(2);
|
||||
|
||||
// A's occurrence is now a child of B's occurrence.
|
||||
let o = h.app.session.doc.occurrences.iter().find(|o| o.id == a_occ).unwrap();
|
||||
assert_eq!(o.parent, b_comp, "A's occurrence moved into component B");
|
||||
assert_eq!(h.app.session.doc.occurrences.iter().find(|o| o.id == b_occ).unwrap().parent, 0, "B's occurrence stays at the design");
|
||||
|
||||
// Drag A's row, now under B, back onto the design row.
|
||||
let design = crate::scenario::handle_at("row:Untitled").unwrap();
|
||||
let from = crate::scenario::handle_at(&format!("row:occurrence:{b_occ}/{a_occ}")).unwrap();
|
||||
let drag = h.call("ui.drag", json!({"x0":from.x,"y0":from.y,"x1":design.x,"y1":design.y,"steps":12}));
|
||||
assert_eq!(drag["ok"], true);
|
||||
h.frames(2);
|
||||
let o = h.app.session.doc.occurrences.iter().find(|o| o.id == a_occ).unwrap();
|
||||
assert_eq!(o.parent, 0, "A's occurrence moved back out to the design");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user