diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 2d8cf66c4..5f0629f65 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -336,7 +336,15 @@ fn copy(s: &mut Session, p: &Value) -> Result { let (cid, ids) = layers_p(s, p)?; s.state.clip_is_keys = false; let comp = s.project.comp(cid).ok_or(crate::EngineError::NoComp)?; - let layers: Vec = comp.layers.iter().filter(|l| ids.contains(&l.id)).cloned().collect(); + // In selection order, not stack order: After Effects pastes the first layer selected on top. + let mut layers: Vec = vec![]; + for id in &ids { + if !layers.iter().any(|l| l.id == *id) + && let Some(l) = comp.layer(*id) + { + layers.push(l.clone()); + } + } clear_clipboards(s); s.state.clipboard = layers; Ok(json!(s.state.clipboard.len())) @@ -619,7 +627,9 @@ fn paste(s: &mut Session, p: &Value) -> Result { let new = s.edit("Paste", None, |proj, st| { let mut next = proj.next_id; let comp = proj.comp_mut(cid).ok_or(crate::EngineError::NoComp)?; - let at = st.selected_layers.first().and_then(|id| comp.layers.iter().position(|l| l.id == *id)).unwrap_or(0); + // Above the topmost selected layer (the top of the stack without one), in copy order, as + // After Effects pastes: with layer 2 selected, three pasted layers become 2, 3 and 4. + let at = comp.layers.iter().position(|l| st.selected_layers.contains(&l.id)).unwrap_or(0); let mut created = vec![]; let mut map = vec![]; for (k, mut l) in clip.into_iter().enumerate() { @@ -956,8 +966,24 @@ pub fn specs() -> Vec { vec![ cmd!("edit.undo", "Undo", ["Edit"], Some("Cmd+Z"), "{}", can_undo, undo), cmd!("edit.redo", "Redo", ["Edit"], Some("Cmd+Shift+Z"), "{}", can_redo, redo), - cmd!("edit.cut", "Cut", ["Edit"], Some("Cmd+X"), "{layers?} (keyframes, effects or shape items when they are selected)", layers_or_keys, cut), - cmd!("edit.copy", "Copy", ["Edit"], Some("Cmd+C"), "{layers?} (keyframes, effects or shape items when they are selected)", layers_or_keys, copy), + cmd!( + "edit.cut", + "Cut", + ["Edit"], + Some("Cmd+X"), + "{layers?} (layers in selection order; keyframes, effects or shape items when they are selected)", + layers_or_keys, + cut + ), + cmd!( + "edit.copy", + "Copy", + ["Edit"], + Some("Cmd+C"), + "{layers?} (layers in selection order; keyframes, effects or shape items when they are selected)", + layers_or_keys, + copy + ), cmd!( "edit.copyWithPropertyLinks", "Copy with Property Links", @@ -982,7 +1008,7 @@ pub fn specs() -> Vec { "Paste", ["Edit"], Some("Cmd+V"), - "{} (layers, keyframes at the CTI, effects, shape items into the selected shape layers, or property links / expressions)", + "{} (layers above the topmost selected layer, in copy order; keyframes at the CTI, effects, shape items into the selected shape layers, or property links / expressions)", has_clip, paste ), diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index a083f599b..3e8e7fef2 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -92,7 +92,15 @@ pub fn specs() -> Vec { has_replace_source, replace_source ), - cmd!("layer.select", "Select Layers", [], None, "{layers: [id|name|#n], add?, toggle?}", has_comp, select), + cmd!( + "layer.select", + "Select Layers", + [], + None, + "{layers: [id|name|#n] (kept in this order: the selection is ordered, and add or toggle appends), add?, toggle?}", + has_comp, + select + ), cmd!("layer.selectNext", "Select Next Layer", [], Some("Cmd+ArrowDown"), "{add?}", has_comp, select_next), cmd!("layer.selectPrevious", "Select Previous Layer", [], Some("Cmd+ArrowUp"), "{add?}", has_comp, select_prev), cmd!("layer.extendSelectionNext", "Extend Selection to Next Layer", [], Some("Cmd+Shift+ArrowDown"), "{}", has_comp, |s, _| select_step( diff --git a/crates/engine/src/cmd/layer/select.rs b/crates/engine/src/cmd/layer/select.rs index efe038a0a..d8e1077cd 100644 --- a/crates/engine/src/cmd/layer/select.rs +++ b/crates/engine/src/cmd/layer/select.rs @@ -45,7 +45,16 @@ pub(super) fn select_step(s: &mut Session, p: &Value, dir: i64) -> Result if comp.layers.is_empty() { return Ok(Value::Null); } - let cur = s.state.selected_layers.first().and_then(|id| comp.layers.iter().position(|l| l.id == *id)); + let add = b_p(p, "add").unwrap_or(false); + let mut placed = s.state.selected_layers.iter().filter_map(|id| comp.layers.iter().position(|l| l.id == *id).map(|i| (i, *id))); + // A plain step goes on from the first selected layer. Extending grows the end it steps from: + // the lowest selected layer going down, the topmost going up. + let from = match (add, dir > 0) { + (false, _) => placed.next(), + (true, true) => placed.max_by_key(|(i, _)| *i), + (true, false) => placed.min_by_key(|(i, _)| *i), + }; + let cur = from.map(|(i, _)| i); let n = comp.layers.len() as i64; // The next layer that can be selected (locked and hidden shy layers are stepped over); at // the end of the stack the selection stays. @@ -59,10 +68,14 @@ pub(super) fn select_step(s: &mut Session, p: &Value, dir: i64) -> Result .find(|i| crate::cmd::selectable(comp, &comp.layers[*i as usize])); let Some(i) = next.or(cur.map(|i| i as i64)) else { return Ok(Value::Null) }; let id = comp.layers[i as usize].id; - let add = b_p(p, "add").unwrap_or(false); if add { if !s.state.selected_layers.contains(&id) { - s.state.selected_layers.insert(0, id); + // Next to the layer it extends from (after it going down, before it going up), so a + // range grown from one layer stays in stack order, top first, as a Shift-click range + // is; the order Ctrl/Cmd-clicks picked elsewhere in the selection is kept. + let k = from.and_then(|(_, f)| s.state.selected_layers.iter().position(|l| *l == f)); + let at = k.map_or(0, |k| if dir > 0 { k + 1 } else { k }); + s.state.selected_layers.insert(at, id); } } else { s.state.selected_layers = vec![id]; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 41bb98dd6..f32150924 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -56,6 +56,7 @@ mod replace_source; mod rig3d; pub(crate) mod roto; mod scale_comp; +mod selection_order; mod sequence; mod session; mod set_selected_keys; diff --git a/crates/engine/src/tests/selection_order.rs b/crates/engine/src/tests/selection_order.rs new file mode 100644 index 000000000..c5a4a51f1 --- /dev/null +++ b/crates/engine/src/tests/selection_order.rs @@ -0,0 +1,139 @@ +//! Ordered layer selection (After Effects parity): the selection keeps the order layers were +//! selected in, Edit ▸ Copy keeps that order, and Edit ▸ Paste puts the layers above the topmost +//! selected layer in that order (checked against After Effects 2026), selecting them in it. + +use serde_json::json; + +use crate::Session; + +/// A comp with layers L1..L6, L1 on top; their ids, top first. +fn setup() -> (Session, Vec) { + let mut s = Session::default(); + s.execute_checked("comp.new", json!({"width": 64, "height": 64, "frameRate": 30, "duration": 10})).unwrap(); + for i in (1..=6).rev() { + s.execute_checked("layer.newSolid", json!({"name": format!("L{i}"), "color": "#fff"})).unwrap(); + } + let ids = s.active_comp().unwrap().layers.iter().map(|l| l.id.0).collect(); + (s, ids) +} + +fn names(s: &Session) -> Vec { + s.active_comp().unwrap().layers.iter().map(|l| l.name.clone()).collect() +} + +fn selected_names(s: &Session) -> Vec { + let c = s.active_comp().unwrap(); + s.state.selected_layers.iter().map(|l| c.layer(*l).unwrap().name.clone()).collect() +} + +/// Click the first, Ctrl/Cmd-click the rest (layers by 1-based stack index). +fn click(s: &mut Session, ids: &[u64], order: &[usize]) { + for (k, i) in order.iter().enumerate() { + s.execute_checked("layer.select", json!({"layers": [ids[i - 1]], "toggle": k > 0})).unwrap(); + } +} + +#[test] +fn ctrl_click_keeps_the_click_order() { + let (mut s, ids) = setup(); + assert_eq!(names(&s), ["L1", "L2", "L3", "L4", "L5", "L6"]); + click(&mut s, &ids, &[5, 2, 4]); + assert_eq!(selected_names(&s), ["L5", "L2", "L4"]); + // Ctrl-clicking a selected layer drops it; clicking it again appends it. + click(&mut s, &ids, &[5, 2, 4, 2, 2]); + assert_eq!(selected_names(&s), ["L5", "L4", "L2"]); + // An explicit list is kept in its order, and so is `add`. + s.execute_checked("layer.select", json!({"layers": [ids[2], ids[0]]})).unwrap(); + s.execute_checked("layer.select", json!({"layers": [ids[5], ids[2]], "add": true})).unwrap(); + assert_eq!(selected_names(&s), ["L3", "L1", "L6"]); +} + +/// The cases run in After Effects 2026: copy L3, L4, L5 in some order, select L2, paste. The +/// copies become layers 2, 3 and 4 in copy order (L2 moves down to 5) and are selected in it. +#[test] +fn paste_goes_above_the_topmost_selected_layer_in_copy_order() { + for (copied, pasted) in [([3, 4, 5], ["L3", "L4", "L5"]), ([5, 4, 3], ["L5", "L4", "L3"]), ([4, 3, 5], ["L4", "L3", "L5"])] { + let (mut s, ids) = setup(); + click(&mut s, &ids, &copied); + s.execute_checked("edit.copy", json!({})).unwrap(); + click(&mut s, &ids, &[2]); + s.execute_checked("edit.paste", json!({})).unwrap(); + let c = s.active_comp().unwrap(); + let src: Vec = c.layers.iter().skip(1).take(3).map(|l| source_name(&s, l.id.0)).collect(); + assert_eq!(src, pasted, "copied {copied:?}: {:?}", names(&s)); + assert_eq!(names(&s)[0], "L1"); + assert_eq!(&names(&s)[4..], ["L2", "L3", "L4", "L5", "L6"]); + let sel: Vec = s.state.selected_layers.iter().map(|l| l.0).collect(); + assert_eq!(sel, c.layers.iter().skip(1).take(3).map(|l| l.id.0).collect::>()); + } +} + +/// With no layer selected the copies go on top; with several selected, above the topmost one +/// whatever the order they were selected in. +#[test] +fn paste_without_or_with_several_selected_layers() { + let (mut s, ids) = setup(); + click(&mut s, &ids, &[3, 4, 5]); + s.execute_checked("edit.copy", json!({})).unwrap(); + s.execute_checked("layer.select", json!({"layers": []})).unwrap(); + s.execute_checked("edit.paste", json!({})).unwrap(); + assert_eq!(&names(&s)[3..], ["L1", "L2", "L3", "L4", "L5", "L6"]); + let (mut s, ids) = setup(); + click(&mut s, &ids, &[3, 4, 5]); + s.execute_checked("edit.copy", json!({})).unwrap(); + click(&mut s, &ids, &[6, 2]); + s.execute_checked("edit.paste", json!({})).unwrap(); + assert_eq!(names(&s)[0], "L1"); + assert_eq!(&names(&s)[4..], ["L2", "L3", "L4", "L5", "L6"]); +} + +/// Sequence Layers lines the layers up in selection order, not stack order. +#[test] +fn sequence_layers_follows_the_selection_order() { + let (mut s, ids) = setup(); + for id in &ids { + s.execute_checked("layer.timing", json!({"layers": [id], "out": 2.0})).ok(); + } + click(&mut s, &ids, &[4, 1, 3]); + s.execute_checked("layer.sequence", json!({})).unwrap(); + let c = s.active_comp().unwrap(); + let start = |i: usize| c.layers[i - 1].in_point.seconds(); + assert!(start(1) > start(4), "L1 follows L4"); + assert!(start(3) > start(1), "L3 follows L1"); +} + +fn source_name(s: &Session, layer: u64) -> String { + let c = s.active_comp().unwrap(); + let l = c.layer(effectcraft_project::LayerId(layer)).unwrap(); + // A pasted copy is named after its original (`L3` → `L3 2` or similar); the original's name is its prefix. + ["L1", "L2", "L3", "L4", "L5", "L6"].iter().find(|n| l.name.starts_with(*n)).map(|n| n.to_string()).unwrap_or_default() +} + +/// Extend Selection to Next / Previous Layer grows a range in stack order, top first, as a +/// Shift-click range is, whichever way it grows; so Copy and Paste keep the stack order. +#[test] +fn a_keyboard_extended_range_copies_and_pastes_top_first() { + for (from, step) in [(2, "layer.extendSelectionNext"), (4, "layer.extendSelectionPrevious")] { + let (mut s, ids) = setup(); + click(&mut s, &ids, &[from]); + s.execute_checked(step, json!({})).unwrap(); + s.execute_checked(step, json!({})).unwrap(); + assert_eq!(selected_names(&s), ["L2", "L3", "L4"], "{step}"); + s.execute_checked("edit.copy", json!({})).unwrap(); + click(&mut s, &ids, &[6]); + s.execute_checked("edit.paste", json!({})).unwrap(); + let c = s.active_comp().unwrap(); + let pasted: Vec = c.layers.iter().skip(5).take(3).map(|l| source_name(&s, l.id.0)).collect(); + assert_eq!(pasted, ["L2", "L3", "L4"], "{step}: {:?}", names(&s)); + } + // Down then up from L3: each step grows its own end of the range. + let (mut s, ids) = setup(); + click(&mut s, &ids, &[3]); + s.execute_checked("layer.extendSelectionNext", json!({})).unwrap(); + s.execute_checked("layer.extendSelectionPrevious", json!({})).unwrap(); + assert_eq!(selected_names(&s), ["L2", "L3", "L4"]); + // Ctrl/Cmd-clicked layers keep their order; the step lands next to the end it grew from. + click(&mut s, &ids, &[5, 2]); + s.execute_checked("layer.extendSelectionNext", json!({})).unwrap(); + assert_eq!(selected_names(&s), ["L5", "L6", "L2"]); +} diff --git a/crates/ui-egui/src/panels/timeline/tests.rs b/crates/ui-egui/src/panels/timeline/tests.rs index 174de58b2..24b378d3c 100644 --- a/crates/ui-egui/src/panels/timeline/tests.rs +++ b/crates/ui-egui/src/panels/timeline/tests.rs @@ -1019,6 +1019,34 @@ fn a_box_over_both_columns_selects_the_rows_it_crosses_and_the_keys_inside() { assert_eq!(ls, [layers[0], layers[1]]); } +/// A box selects its layers in stack order (top first) whichever way it was drawn, as After +/// Effects orders a marquee selection (Adobe's Quick Offset notes, #1012); only Ctrl/Cmd-clicks +/// give the selection another order. +#[test] +fn a_box_selects_layers_in_stack_order_either_way() { + let mut s = effectcraft_engine::Session::default(); + s.execute("comp.new", json!({"name": "C", "width": 64, "height": 64, "duration": 4, "frameRate": 30})).unwrap(); + for name in ["D", "C", "B", "A"] { + s.execute("layer.newSolid", json!({"name": name, "color": "#fff"})).unwrap(); + } + // Stack order, top first: A, B, C, D. + let comp = s.active_comp().unwrap().clone(); + let layers: Vec = comp.layers.iter().map(|l| l.id.0).collect(); + let rows: Vec = layers.iter().map(|id| Row { layer: LayerId(*id), depth: 0, kind: RowKind::Layer }).collect(); + let tm = TMap { x0: 205.0, start: 0.0, pps: 100.0 }; + let view = marquee::View { tm, top: 0.0, graph_x0: 200.0, graph_on: false }; + let go = |from: Pos2, to: Pos2| { + let (paint, sel) = marquee::Anchor::press(&view, 0.0, from).selection(&view, 0.0, to); + marquee::targets(&comp, &rows, &tm, 0.0, 20.0, paint, &sel).0 + }; + // Rows are 20 pt: B is y 20..40, D is y 60..80. + let up = go(pos2(10.0, 75.0), pos2(10.0, 25.0)); + assert_eq!(up, [layers[1], layers[2], layers[3]], "bottom to top: B, C, D"); + assert_eq!(go(pos2(10.0, 25.0), pos2(10.0, 75.0)), up, "top to bottom: the same"); + s.execute("layer.select", json!({"layers": up})).unwrap(); + assert_eq!(s.state.selected_layers.iter().map(|l| l.0).collect::>(), [layers[1], layers[2], layers[3]]); +} + /// #4342: the real outline elides nested Contents; dropping below Stroke still targets it. #[test] fn shape_item_drop_resolves_nested_outline_and_dispatches_move() {