diff --git a/crates/engine/src/cmd/appearance.rs b/crates/engine/src/cmd/appearance.rs index ac993f7ab5..bc2485962d 100644 --- a/crates/engine/src/cmd/appearance.rs +++ b/crates/engine/src/cmd/appearance.rs @@ -1687,6 +1687,8 @@ fn move_item(s: &mut Session, p: &Value) -> Result { let from = index_param(p, "from", C)?; let contents = p.get("contents").map(|_| index_param(p, "contents", C)).transpose()?; let mut landed = to; + let active = s.active_appearance_item.as_ref().and_then(|a| a.objects.first().copied()); + let mut active_landed = None; // When every object's stack comes out as it was, the document and its undo history stay as they // are. let before = s.doc()?.doc.clone(); @@ -1694,6 +1696,9 @@ fn move_item(s: &mut Session, p: &Value) -> Result { for id in &ids { let Some(n) = d.node_mut(*id) else { continue }; landed = n.appearance.move_item(from, to).ok_or_else(|| bad(C, format!("no item at index {from}")))?; + if active == Some(*id) { + active_landed = Some(landed); + } if let Some(k) = contents.filter(|_| n.contents_label().is_some()) { n.appearance.set_contents_at(k); } @@ -1703,13 +1708,14 @@ fn move_item(s: &mut Session, p: &Value) -> Result { let labels = super::graph::capture_label_changes(&before, d, &ids, true, None); Ok(count_changed(d, &before, &ids) + usize::from(kept || painted || labels)) })?; - // The moved row stays the active one; the rows it passed shift by one. + // Track the row in its own object's stack, since other stacks may clamp to a different index. + let at = active_landed.unwrap_or(landed); s.remap_appearance_item(&ids, |a| { Some(if a == from { - landed - } else if from < a && a <= landed { + at + } else if from < a && a <= at { a - 1 - } else if landed <= a && a < from { + } else if at <= a && a < from { a + 1 } else { a diff --git a/crates/engine/src/tests/appearance.rs b/crates/engine/src/tests/appearance.rs index 9a29f9e026..44b49f148d 100644 --- a/crates/engine/src/tests/appearance.rs +++ b/crates/engine/src/tests/appearance.rs @@ -145,6 +145,23 @@ fn item_rows_follow_remove_and_move() { assert_eq!(s.appearance_item(), None); } +#[test] +fn moving_a_single_row_keeps_the_active_objects_landing() { + let mut s = session(); + let short = rect(&mut s, 50.0); + let long = rect(&mut s, 200.0); + run(&mut s, "appearance.addFill", json!({})); + run(&mut s, "select.set", json!({"ids": [short.0, long.0]})); + run(&mut s, "appearance.setActiveItem", json!({"index": 0})); + + let moved = run(&mut s, "appearance.moveItem", json!({"ids": [short.0, long.0], "from": 0, "to": 2})); + + assert_eq!(moved["index"], 2, "the command still reports the last target's landing"); + assert!(matches!(node(&s, short).appearance.items[1], AppearanceItem::Fill(_))); + assert!(matches!(node(&s, long).appearance.items[0], AppearanceItem::Stroke(_))); + assert_eq!(s.appearance_item(), Some(1), "the active row follows its own object's landing"); +} + #[test] fn transparency_goes_to_the_targeted_item() { let mut s = session();