Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Video layers: edits keep the timeline work area (#4170) (#4171)

David Manne4bfa7907f77
diff --git a/crates/engine/src/video_cmds.rs b/crates/engine/src/video_cmds.rs
index dbb51d82d..2a9f8ae8b 100644
--- a/crates/engine/src/video_cmds.rs
+++ b/crates/engine/src/video_cmds.rs
@@ -72,6 +72,17 @@ fn has_doc(s: &Session) -> std::result::Result<(), String> {
s.active().map(|_| ()).ok_or_else(|| "no document".into())
}
+/// Grow the timeline to at least `frames` frames (never shrink it). Only a work area that reached
+/// the old end follows the new end; a narrowed one stays where the user put it (#4170).
+fn grow_duration(t: &mut Timeline, frames: usize) {
+ let old = t.duration;
+ t.duration = t.duration.max(frames.max(1));
+ if t.work_end >= old {
+ t.work_end = t.duration;
+ }
+ t.clamp();
+}
+
/// Edit the active video layer's [`VideoData`] at the playhead, then re-sync the displayed frame.
fn edit_video(s: &mut Session, label: &str, f: impl FnOnce(&mut VideoData, usize) -> Result<()>) -> Result<Value> {
let count = s.edit(label, |doc, active| {
@@ -83,9 +94,7 @@ fn edit_video(s: &mut Session, label: &str, f: impl FnOnce(&mut VideoData, usize
f(v, cur)?;
let n = v.frames.len();
if let Some(t) = &mut doc.timeline {
- t.duration = t.duration.max(n.max(1));
- t.work_end = t.work_end.max(t.duration);
- t.clamp();
+ grow_duration(t, n);
}
sync(doc);
Ok(n)
@@ -295,8 +304,7 @@ fn new_from_file(s: &mut Session, p: &Value) -> Result<Value> {
if doc.timeline.is_none() {
doc.timeline = Some(Timeline::new(n, fps));
} else if let Some(t) = &mut doc.timeline {
- t.duration = t.duration.max(n);
- t.clamp();
+ grow_duration(t, n);
}
let name = doc.next_layer_name(std::path::Path::new(&path).file_stem().and_then(|s| s.to_str()).unwrap_or("Video"));
let mut l = Layer::raster(name, fmt);
diff --git a/crates/engine/src/video_cmds/tests.rs b/crates/engine/src/video_cmds/tests.rs
index 3380d3b9d..22d639d49 100644
--- a/crates/engine/src/video_cmds/tests.rs
+++ b/crates/engine/src/video_cmds/tests.rs
@@ -430,3 +430,71 @@ fn restoring_blank_video_frames_still_produces_blank_frames() {
s.execute("layer.videoLayers.restoreFrame", json!({})).unwrap();
assert_eq!(layer_px(&s, id)[3], 0.0);
}
+
+fn work_area(s: &Session) -> (usize, usize, usize) {
+ let t = s.active().unwrap().doc.timeline.as_ref().unwrap();
+ (t.work_start, t.work_end, t.duration)
+}
+
+#[test]
+fn video_edits_keep_a_narrowed_work_area() {
+ // #4170: a video edit pushed the work-area end to the full duration.
+ for (cmd, params) in [
+ ("layer.videoLayers.showAlteredVideo", json!({})),
+ ("layer.videoLayers.restoreFrame", json!({})),
+ ("layer.videoLayers.interpretFootage", json!({"fps": 24})),
+ ] {
+ let mut s = session();
+ s.execute("timeline.create", json!({"duration": 30, "fps": 30})).unwrap();
+ s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap();
+ s.execute("timeline.setProps", json!({"workStart": 2, "workEnd": 10})).unwrap();
+ assert_eq!(work_area(&s), (2, 10, 30));
+ s.execute(cmd, params).unwrap();
+ assert_eq!(work_area(&s), (2, 10, 30), "{cmd} moved the work area");
+ }
+}
+
+#[test]
+fn a_full_work_area_grows_with_the_duration() {
+ let mut s = session();
+ s.execute("timeline.create", json!({"duration": 30, "fps": 30})).unwrap();
+ s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap();
+ assert_eq!(work_area(&s), (0, 30, 30));
+ s.execute("layer.videoLayers.insertBlankFrame", json!({})).unwrap();
+ assert_eq!(work_area(&s), (0, 31, 31));
+ // A narrowed work area stays put when the duration grows.
+ s.execute("timeline.setProps", json!({"workStart": 2, "workEnd": 10})).unwrap();
+ s.execute("layer.videoLayers.insertBlankFrame", json!({})).unwrap();
+ assert_eq!(work_area(&s), (2, 10, 32));
+}
+
+#[test]
+fn longer_footage_grows_only_a_full_work_area() {
+ let dir = tmpdir("workarea");
+ for i in 0..5 {
+ write_png(&format!("{dir}/f{i:02}.png"), 16, 12, "#808080");
+ }
+ // A work area covering everything follows the longer footage.
+ let mut s = session();
+ s.execute("timeline.create", json!({"duration": 3, "fps": 30})).unwrap();
+ s.execute("layer.videoLayers.newVideoLayerFromFile", json!({"path": dir})).unwrap();
+ assert_eq!(work_area(&s), (0, 5, 5));
+ // A narrowed one stays put.
+ let mut s = session();
+ s.execute("timeline.create", json!({"duration": 3, "fps": 30})).unwrap();
+ s.execute("timeline.setProps", json!({"workStart": 1, "workEnd": 2})).unwrap();
+ s.execute("layer.videoLayers.newVideoLayerFromFile", json!({"path": dir})).unwrap();
+ assert_eq!(work_area(&s), (1, 2, 5));
+}
+
+#[test]
+fn grow_duration_never_shrinks_and_keeps_the_work_area_valid() {
+ let mut t = Timeline::new(10, 30.0);
+ grow_duration(&mut t, 4);
+ assert_eq!((t.duration, t.work_end), (10, 10));
+ grow_duration(&mut t, 0);
+ assert_eq!((t.duration, t.work_end), (10, 10));
+ t.work_end = 6;
+ grow_duration(&mut t, 12);
+ assert_eq!((t.work_start, t.work_end, t.duration), (0, 6, 12));
+}