Fix #1288: keep edits to a video frame when the playhead moves (#1395)

Paint, fills and filters edit a video layer's displayed raster, but the
frame stack kept the old pixels, so scrubbing or Render Video re-synced the
stale frame over the edit. Write the displayed content back into the
playhead's frame (video_cmds::store) before any navigation, frame edit,
new video layer or render.


Claude-Session: https://claude.ai/code/session_01UHgFEHEsefoQoBParH47JA

Co-authored-by: Claude <[email protected]>
This commit is contained in:
BFlatandClaude authored and GitHub committed 2026-10-09 07:37:51 +07:00
1 parent 88a47dc7c7
commit 8208761a7c
3 files changed
+68 -3

No files matched your search

+1
View File
@@ -32,6 +32,7 @@ fn has_timeline(s: &Session) -> std::result::Result<(), String> {
fn with_timeline(s: &mut Session, f: impl FnOnce(&mut Option<Timeline>)) -> Result<Value> {
let st = s.active_mut().ok_or(EngineError::NoDocument)?;
let mut doc = (*st.doc).clone();
crate::video_cmds::store(&mut doc);
f(&mut doc.timeline);
if let Some(t) = &mut doc.timeline {
t.clamp();
+29 -3
View File
@@ -1,6 +1,7 @@
//! Layer › Video Layers (and Rasterize › Video). A video layer holds a stack of frames; the
//! layer's displayed raster content is the frame at the timeline playhead, kept in sync by [`sync`]
//! after any edit or navigation. Headless and scriptable. Frame persistence in `.pcraft` is a
//! after any edit or navigation. Paint, fills and filters edit that displayed content, so [`store`]
//! writes it back into the playhead's frame before anything re-syncs. Headless and scriptable. Frame persistence in `.pcraft` is a
//! follow-up; a saved document keeps the current frame as the layer's content.
use photocraft_doc::{Document, Layer, LayerContent, Timeline, VideoData};
@@ -18,7 +19,7 @@ pub fn sync(doc: &mut Document) {
fn sync_layers(layers: &mut [Layer], cur: usize) {
for l in layers.iter_mut() {
let frame = l.video.as_ref().filter(|v| !v.frames.is_empty()).map(|v| v.frames[cur.min(v.frames.len() - 1)].clone());
let frame = l.video.as_ref().and_then(|v| v.frames.get(cur.min(v.frames.len().saturating_sub(1)))).cloned();
if let Some(f) = frame
&& let LayerContent::Raster(s) = &mut l.content
{
@@ -30,6 +31,27 @@ fn sync_layers(layers: &mut [Layer], cur: usize) {
}
}
/// Keep edits made to every video layer's displayed content (paint, fills, filters) by writing it
/// back into its frame at the playhead. Call before moving the playhead or re-syncing.
pub fn store(doc: &mut Document) {
let cur = doc.timeline.as_ref().map_or(0, |t| t.current);
store_layers(&mut doc.layers, cur);
}
fn store_layers(layers: &mut [Layer], cur: usize) {
for l in layers.iter_mut() {
if let (Some(v), LayerContent::Raster(s)) = (&mut l.video, &l.content) {
let at = cur.min(v.frames.len().saturating_sub(1));
if let Some(f) = v.frames.get_mut(at) {
*f = s.clone();
}
}
if let Some(ch) = l.children_mut() {
store_layers(ch, cur);
}
}
}
/// The active layer, if it's a video layer.
fn active_video(s: &Session) -> std::result::Result<(), String> {
let st = s.active().ok_or("no document")?;
@@ -46,6 +68,7 @@ fn has_doc(s: &Session) -> std::result::Result<(), String> {
/// 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| {
store(doc);
let cur = doc.timeline.as_ref().map_or(0, |t| t.current);
let id = active.ok_or(EngineError::NoDocument)?;
let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
@@ -69,6 +92,7 @@ fn blank_of(fmt: photocraft_color::PixelFormat) -> Surface {
fn new_blank(s: &mut Session, _p: &Value) -> Result<Value> {
let id = s.edit("New Blank Video Layer", |doc, active| {
store(doc);
let fmt = doc.pixel_format();
if doc.timeline.is_none() {
doc.timeline = Some(Timeline::new(1, 30.0));
@@ -200,6 +224,7 @@ fn new_from_file(s: &mut Session, p: &Value) -> Result<Value> {
let frames = load_frames(&path, fmt)?;
let n = frames.len();
let id = s.edit("New Video Layer from File", |doc, active| {
store(doc);
if doc.timeline.is_none() {
doc.timeline = Some(Timeline::new(n, fps));
} else if let Some(t) = &mut doc.timeline {
@@ -298,7 +323,8 @@ fn render_video(s: &mut Session, p: &Value) -> Result<Value> {
let format = p.get("format").and_then(Value::as_str).unwrap_or("png");
std::fs::create_dir_all(dir).map_err(|e| EngineError::Other(format!("mkdir `{dir}`: {e}")))?;
let st = s.active().ok_or(EngineError::NoDocument)?;
let base = (*st.doc).clone();
let mut base = (*st.doc).clone();
store(&mut base);
let frames = base.timeline.as_ref().map_or(1, |t| t.duration).max(1);
let fps = base.timeline.as_ref().map_or(30.0, |t| t.fps).max(1.0);
let stem = base.name.rsplit_once('.').map_or(base.name.as_str(), |(a, _)| a).to_string();
+38
View File
@@ -136,3 +136,41 @@ fn inspect_reports_video_layer() {
assert_eq!(vid["frames"], 2);
assert_eq!(vid["source"]["file"].as_str(), Some(dir.as_str()));
}
fn layer_px(s: &Session, id: LayerId) -> Vec<f32> {
match &s.active().unwrap().doc.layer(id).unwrap().content {
LayerContent::Raster(r) => r.pixel(2, 2),
_ => panic!("not a raster"),
}
}
/// #1288: a fill on a video frame survives scrubbing away and back, and reaches Render Video.
#[test]
fn edits_to_a_frame_survive_scrubbing_and_render() {
let mut s = session();
let r = s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap();
let id = LayerId(r["layer"].as_u64().unwrap());
s.execute("layer.videoLayers.insertBlankFrame", json!({})).unwrap();
s.execute("timeline.setFrame", json!({"frame": 0})).unwrap();
s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
s.execute("timeline.setFrame", json!({"frame": 1})).unwrap();
assert!(layer_px(&s, id)[3] < 0.01, "frame 1 stays blank");
s.execute("timeline.setFrame", json!({"frame": 0})).unwrap();
let p = layer_px(&s, id);
assert!(p[0] > 0.99 && p[3] > 0.99, "frame 0 keeps its fill: {p:?}");
// Duplicate Frame copies the edited frame.
s.execute("layer.videoLayers.duplicateFrame", json!({})).unwrap();
s.execute("timeline.setFrame", json!({"frame": 1})).unwrap();
assert!(layer_px(&s, id)[0] > 0.99);
// Render Video picks up an edit made at the playhead without scrubbing first.
s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap();
let out = tmpdir("edited");
s.execute("file.export.renderVideo", json!({"dir": out, "format": "png"})).unwrap();
let stem = s.active().unwrap().doc.name.rsplit_once('.').map_or(s.active().unwrap().doc.name.clone(), |(a, _)| a.to_string());
let f1 = std::fs::read(format!("{out}/{stem}_0001.png")).unwrap();
let doc = photocraft_io::import("f1.png", &f1).unwrap().document;
let flat = crate::file_cmds::flattened(&doc, doc.pixel_format());
let q = flat.pixel(2, 2);
assert!(q[2] > 0.99 && q[0] < 0.01, "rendered frame 1 is blue: {q:?}");
}