diff --git a/crates/engine/src/cmds_timeline.rs b/crates/engine/src/cmds_timeline.rs index 7e9191f5..36ecf924 100644 --- a/crates/engine/src/cmds_timeline.rs +++ b/crates/engine/src/cmds_timeline.rs @@ -33,8 +33,8 @@ pub fn specs() -> Vec { ), c("timeline.set_label", "Frame Label", "", None, "{label, layer?, frame?}", true, set_label), c("timeline.goto", "Go to Frame", "", None, "{frame}", false, goto), - c("timeline.next_frame", "Next Frame", "Control", Some("."), "{}", false, |s, _| step(s, 1)), - c("timeline.prev_frame", "Previous Frame", "Control", Some(","), "{}", false, |s, _| step(s, -1)), + c("timeline.next_frame", "Next Frame", "Control", Some("."), "{} (on the last frame it stays there)", false, |s, _| step(s, 1)), + c("timeline.prev_frame", "Previous Frame", "Control", Some(","), "{} (on the first frame it stays there)", false, |s, _| step(s, -1)), c("timeline.first_frame", "Go to First Frame", "Control", Some("Home"), "{}", false, |s, _| { s.set_frame(0); Ok(json!({"frame": 0})) @@ -816,9 +816,12 @@ fn goto(s: &mut Session, p: &Value) -> Result { Ok(json!({"frame": s.frame})) } +/// One frame on or back, stopping at the first and the last frame rather than going round +/// (#561), as the arrow keys in the timeline do. fn step(s: &mut Session, d: i64) -> Result { - let n = i64::from(s.frame_count().max(1)); - let f = (i64::from(s.frame) + d).rem_euclid(n); + let (now, last) = (i64::from(s.frame), i64::from(s.frame_count().max(1)) - 1); + // A playhead already past the last frame (on an empty cell) isn't pulled back by Next. + let f = (now + d).clamp(0, last.max(now)); s.set_frame(f as u32); Ok(json!({"frame": s.frame})) } diff --git a/crates/engine/src/timeline_key_tests.rs b/crates/engine/src/timeline_key_tests.rs index 58bf50bc..0d9114af 100644 --- a/crates/engine/src/timeline_key_tests.rs +++ b/crates/engine/src/timeline_key_tests.rs @@ -173,3 +173,21 @@ fn advance_at_the_last_frame_stays_in_range() { let _ = s.execute("timeline.insert_keyframe", &json!({"advance": true})); assert!(s.frame <= MAX_FRAME); } + +/// #561: Next and Previous Frame (`.` and `,`) stop at the last and the first frame instead of +/// going round to the other end. +#[test] +fn stepping_stops_at_the_ends() { + let mut s = Session::default(); + let l = s.layer; + run(&mut s, "timeline.insert_frame", json!({"layer": l.0, "frame": 0, "count": 4})); + let last = s.frame_count() - 1; + run(&mut s, "timeline.goto", json!({"frame": last})); + run(&mut s, "timeline.next_frame", json!({})); + assert_eq!(s.frame, last, "the last frame stays"); + run(&mut s, "timeline.goto", json!({"frame": 0})); + run(&mut s, "timeline.prev_frame", json!({})); + assert_eq!(s.frame, 0, "the first frame stays"); + run(&mut s, "timeline.next_frame", json!({})); + assert_eq!(s.frame, 1, "in between, one frame on"); +} diff --git a/docs/guide/reference/commands.md b/docs/guide/reference/commands.md index 3068d543..1431d722 100644 --- a/docs/guide/reference/commands.md +++ b/docs/guide/reference/commands.md @@ -289,9 +289,9 @@ This page is generated from the command registry, so it always matches the versi ## Control - **Next Frame** `timeline.next_frame` · **.** - Parameters: `{}` + Parameters: `{} (on the last frame it stays there)` - **Previous Frame** `timeline.prev_frame` · **,** - Parameters: `{}` + Parameters: `{} (on the first frame it stays there)` - **Go to First Frame** `timeline.first_frame` · **Home** Parameters: `{}` - **Go to Last Frame** `timeline.last_frame` · **End**