Keyframes: moving keys keeps the selection order

keys.move (and nudging) rebuilt the key selection in layer and property
order, so the order the keys were selected in was lost after a move. A
Quick Offset that follows staggers layers in that order (#1012).

The move now goes through move_key_refs, which maps each selected key to
its new time and returns the selection in its original order, with the
same collisions (a key landing on another replaces it), roving retiming
and locked-layer handling as before. key_gesture is the drag-step logic
(start again from the keys and project as they were before the drag)
split out of shift_keys so other key drags can share it.

Test: moving_keys_keeps_the_selection_order (fails before: the keys come
back sorted by layer).
This commit is contained in:
tylerjenningsw
2026-10-12 17:37:29 -04:00
parent 470ea7a3af
commit 7643f803e0
2 changed files with 81 additions and 47 deletions
+51 -30
View File
@@ -862,37 +862,40 @@ pub(crate) fn edit_keys(s: &mut Session, label: &str, merge: Option<&str>, f: im
}) })
} }
/// Move the selected keys by `d` comp seconds, snapped to the comp's frames in comp time (keys /// The keys a key drag's step applies to, the drag's total and the project as it was before the
/// of time-stretched layers move with the pointer, not by the layer-time distance). Keys of /// drag. Steps of one drag (one `merge` key) start again from the keys as they were then, so a key
/// locked layers stay. A key landing on another key of its property replaces it. /// passed over survives; `d` adds to the drag's total, or is that total (`absolute`).
pub(crate) fn shift_keys(s: &mut Session, label: &str, merge: Option<&str>, d: Tick) -> Result<Value> { pub(crate) fn key_gesture(
let cid = s.active_comp_id().ok_or(EngineError::NoComp)?; s: &Session,
// A drag (steps with one merge key) starts again from the keys before it each step. merge: Option<&str>,
d: Tick,
absolute: bool,
) -> (Vec<KeyRef>, Tick, Option<std::sync::Arc<effectcraft_project::Project>>) {
let gesture = merge.is_some_and(|m| s.history.merge_key.as_deref() == Some(m)) && s.key_move.as_ref().map(|g| g.merge.as_str()) == merge; let gesture = merge.is_some_and(|m| s.history.merge_key.as_deref() == Some(m)) && s.key_move.as_ref().map(|g| g.merge.as_str()) == merge;
let (from, total, base) = match (&s.key_move, s.history.undo.last()) { match (&s.key_move, s.history.undo.last()) {
(Some(g), Some((_, base))) if gesture => (g.from.clone(), g.total + d, Some(base.clone())), (Some(g), Some((_, base))) if gesture => (g.from.clone(), if absolute { d } else { g.total + d }, Some(base.clone())),
_ => (s.state.selected_keys.clone(), d, None), _ => (s.state.selected_keys.clone(), d, None),
};
s.key_move = merge.map(|m| crate::KeyMove { merge: m.to_string(), from: from.clone(), total });
s.edit(label, merge, |proj, st| {
if let Some(b) = base {
*proj = (*b).clone();
} }
let comp = proj.comp_mut(cid).ok_or(EngineError::NoComp)?; }
let fr = comp.frame_rate;
/// Move keys `from` to the layer times `to` gives (from a key's layer and layer time). A key
/// landing on another key of its property replaces it, roving keys follow their neighbours and
/// keys of locked layers stay. Returns the keys' new references in `from`'s order: the selection
/// keeps the order it was made in.
pub(crate) fn move_key_refs(comp: &mut effectcraft_project::Comp, from: &[KeyRef], to: impl Fn(&Layer, Tick) -> Result<Tick>) -> Result<Vec<KeyRef>> {
let mut remapped: std::collections::HashMap<KeyRef, KeyRef> = Default::default();
let mut groups: std::collections::BTreeMap<(LayerId, Uid), Vec<Tick>> = Default::default(); let mut groups: std::collections::BTreeMap<(LayerId, Uid), Vec<Tick>> = Default::default();
for k in &from { for k in from {
groups.entry((k.layer, k.prop)).or_default().push(k.time); groups.entry((k.layer, k.prop)).or_default().push(k.time);
} }
let mut sel = vec![];
for ((lid, uid), times) in groups { for ((lid, uid), times) in groups {
let Some(l) = comp.layer_mut(lid) else { continue }; let Some(l) = comp.layer_mut(lid) else { continue };
let to = |t: Tick| l.layer_time(fr.snap_nearest(l.comp_time(t) + total)); let at = |time| KeyRef { layer: lid, prop: uid, time };
let moves: Vec<(Tick, Tick)> = times.iter().map(|t| (*t, to(*t))).collect();
if l.switches.locked { if l.switches.locked {
sel.extend(times.iter().map(|t| KeyRef { layer: lid, prop: uid, time: *t })); remapped.extend(times.iter().map(|t| (at(*t), at(*t))));
continue; continue;
} }
let moves: Vec<(Tick, Tick)> = times.iter().map(|t| Ok((*t, to(l, *t)?))).collect::<Result<_>>()?;
let Some(pr) = l.props.find_mut(uid) else { continue }; let Some(pr) = l.props.find_mut(uid) else { continue };
// Take the moving keys out, then put them back at their new times. // Take the moving keys out, then put them back at their new times.
let mut moved = vec![]; let mut moved = vec![];
@@ -900,27 +903,45 @@ pub(crate) fn shift_keys(s: &mut Session, label: &str, merge: Option<&str>, d: T
if let Some(i) = key_at(&pr.keys, *old) { if let Some(i) = key_at(&pr.keys, *old) {
let mut k = pr.keys.remove(i); let mut k = pr.keys.remove(i);
k.time = *new; k.time = *new;
moved.push(k); moved.push((*old, k));
} }
} }
for k in moved { // Target time -> the key's time before the move.
sel.push(KeyRef { layer: lid, prop: uid, time: k.time }); let mut targets: std::collections::HashMap<Tick, Tick> = Default::default();
for (old, k) in moved {
targets.insert(k.time, old);
match pr.keys.binary_search_by(|x| x.time.cmp(&k.time)) { match pr.keys.binary_search_by(|x| x.time.cmp(&k.time)) {
Ok(j) => pr.keys[j] = k, Ok(j) => pr.keys[j] = k,
Err(j) => pr.keys.insert(j, k), Err(j) => pr.keys.insert(j, k),
} }
} }
// Roving keys follow their neighbours: keep the selection on them. // Resolve stored indices before roving keys change their times.
let before: Vec<Tick> = pr.keys.iter().map(|k| k.time).collect(); let tracked: Vec<(Tick, usize)> = targets.into_iter().filter_map(|(t, old)| key_at(&pr.keys, t).map(|i| (old, i))).collect();
if effectcraft_keyframe::retime_roving(&mut pr.keys, pr.spatial) { effectcraft_keyframe::retime_roving(&mut pr.keys, pr.spatial);
for r in sel.iter_mut().filter(|r| r.layer == lid && r.prop == uid) { for (old, i) in tracked {
if let Some(i) = before.iter().position(|t| *t == r.time) { if let Some(key) = pr.keys.get(i) {
r.time = pr.keys[i].time; remapped.insert(at(old), at(key.time));
} }
} }
} }
let mut seen = std::collections::HashSet::new();
Ok(from.iter().filter_map(|k| remapped.get(k).copied()).filter(|k| seen.insert(*k)).collect())
} }
st.selected_keys = sel;
/// Move the selected keys by `d` comp seconds, snapped to the comp's frames in comp time (keys
/// of time-stretched layers move with the pointer, not by the layer-time distance). Keys of
/// locked layers stay. A key landing on another key of its property replaces it.
pub(crate) fn shift_keys(s: &mut Session, label: &str, merge: Option<&str>, d: Tick) -> Result<Value> {
let cid = s.active_comp_id().ok_or(EngineError::NoComp)?;
let (from, total, base) = key_gesture(s, merge, d, false);
s.key_move = merge.map(|m| crate::KeyMove { merge: m.to_string(), from: from.clone(), total });
s.edit(label, merge, |proj, st| {
if let Some(b) = base {
*proj = (*b).clone();
}
let comp = proj.comp_mut(cid).ok_or(EngineError::NoComp)?;
let fr = comp.frame_rate;
st.selected_keys = move_key_refs(comp, &from, |l, t| Ok(l.layer_time(fr.snap_nearest(l.comp_time(t) + total))))?;
Ok(json!(st.selected_keys.len())) Ok(json!(st.selected_keys.len()))
}) })
} }
@@ -111,3 +111,16 @@ fn attribute_edit_deduplicates_legacy_refs_and_drops_stale_refs_without_snapping
let opacity = s.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.find(prop).unwrap(); let opacity = s.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.find(prop).unwrap();
assert!(opacity.keys.iter().all(|k| k.out_interp == Interp::Linear)); assert!(opacity.keys.iter().all(|k| k.out_interp == Interp::Linear));
} }
#[test]
fn moving_keys_keeps_the_selection_order() {
let (mut s, first) = setup();
let second = s.execute_checked("layer.newSolid", json!({"name": "Other", "color": "#fff"})).unwrap()["layer"].as_u64().unwrap();
for layer in [first, second] {
add(&mut s, layer, "transform/opacity", 0, json!(0));
}
let (a, b) = (uid(&s, first, "transform/opacity"), uid(&s, second, "transform/opacity"));
s.execute_checked("keys.select", json!({"keys": [{"layer": second, "prop": b, "time": 0}, {"layer": first, "prop": a, "time": 0}]})).unwrap();
s.execute_checked("keys.move", json!({"delta": 2.0 / 30.0})).unwrap();
assert_eq!(s.state.selected_keys, vec![key(second, b, 2), key(first, a, 2)], "not sorted by layer");
}