PhotoCraft: An open-source, photo editor written by scratch in pure Rust
refactor(engine): drop the second damage pass after single-edit duplicates and pastes
Finn Newickfbaf6795cfdc
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 68d00a7da..924f6e5f8 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -648,7 +648,6 @@ fn build() -> Vec<CommandSpec> {return crate::layer_multi_cmds::duplicate_selected(s, in_place, "Duplicate Layers");}let id = layer_param(s, p)?;- let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone();let nid = s.edit("Duplicate Layer", |doc, active| {let mut dup = layer_copy(doc, id)?;if let Some(name) = &name {@@ -661,7 +660,6 @@ fn build() -> Vec<CommandSpec> {*active = Some(nid);Ok(nid)})?;- crate::layer_multi_cmds::note_insert(s, &before);Ok(json!({ "layer": nid.0 }))}),diff --git a/crates/engine/src/edit_cmds.rs b/crates/engine/src/edit_cmds.rsindex dcb89aa98..1340c7094 100644--- a/crates/engine/src/edit_cmds.rs+++ b/crates/engine/src/edit_cmds.rs@@ -390,7 +390,6 @@ fn paste(s: &mut Session, p: &Value, in_place: bool) -> Result<Value> {let moved = shifted(&clip.surface, dx, dy);let target = PixelFormat::new(fmt.mode, fmt.sample, true);let surf = if moved.format() == target { moved } else { moved.convert(target) };- let before = d.doc.clone();let id = s.edit("Paste", |doc, active| {let mut l = Layer::raster(doc.next_layer_name("Layer"), target);*crate::pixels_mut(&mut l)? = surf;@@ -399,8 +398,7 @@ fn paste(s: &mut Session, p: &Value, in_place: bool) -> Result<Value> {doc.selection = None;Ok(id)})?;- // Only where the pasted pixels land needs recompositing, not the whole document (#2901).- crate::layer_multi_cmds::note_insert(s, &before);+ // `edit` reports only where the pasted pixels land as damage, not the whole document (#2901).Ok(json!({"layer": id.0, "offset": [dx, dy]}))}diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 9a79a7205..31733f12e 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -884,9 +884,9 @@ pub(crate) fn note_inert_insert(s: &mut Session, id: LayerId) {}}-/// Report the pixels an edit from `before` that added layers changed ([`step_damage`]), for-/// commands made of more than one edit, so the canvas recomposites only where the new layers-/// draw instead of the whole document.+/// Report the pixels a command made of more than one edit changed since `before` ([`step_damage`]),+/// so the canvas recomposites only where its new layers draw instead of the whole document.+/// A single [`Session::edit`] already reports its own damage: don't call this after one (#5695).pub(crate) fn note_insert(s: &mut Session, before: &Document) {let Some(st) = s.active_mut() else { return };st.last_damage = step_damage(before, &st.doc);@@ -1378,7 +1378,6 @@ pub fn delete_selected(s: &mut Session) -> Result<Value> {/// this path, #2777).pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Result<Value> {let sel = selected(s);- let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone();let old_active = s.active().and_then(|d| d.active_layer);let (copies, active) = s.edit(label, |doc, active| {let mut copies = Vec::new();@@ -1400,7 +1399,6 @@ pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Resul*active = a;Ok((copies, a))})?;- note_insert(s, &before);let out = copies.iter().map(|l| l.0).collect::<Vec<_>>();reselect(s, copies, active);Ok(json!({"layers": out, "layer": active.map(|l| l.0)}))@@ -1807,8 +1805,8 @@ mod tests {x.px.iter().zip(&y.px).enumerate().all(|(i, (p, q))| p == q || damage.contains(i as i32 % w, i as i32 / w))}- /// Duplicate, paste and delete, and their undo and redo, recomposite only where the layer- /// draws; undoing a duplicate used to recomposite the whole document.+ /// Duplicate (one layer or several), paste and delete, and their undo and redo, recomposite+ /// only where the layers draw; undoing a duplicate used to recomposite the whole document.#[test]fn duplicate_paste_and_delete_report_where_the_layer_draws() {let mut s = session(8);@@ -1831,6 +1829,12 @@ mod tests {s.execute("edit.copy", json!({})).unwrap();s.execute("select.deselect", json!({})).unwrap();step(&mut s, "edit.paste", json!({}));+ // Duplicating several selected layers reports where the copies draw, too.+ let b = rect_layer(&mut s, Rect::new(30, 30, 40, 40));+ s.execute("layer.select", json!({"layer": a.0})).unwrap();+ s.execute("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap();+ step(&mut s, "layer.duplicate", json!({}));+ assert_eq!(damage(&s), Some(Rect::new(10, 10, 40, 40)), "both copies, nothing else");step(&mut s, "layer.delete", json!({"layer": a.0}));}