Skip to content

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 (#5880)

FinnNk8c3c3237f412
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index cd7f5d606..9b4d4a747 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -754,7 +754,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 {
@@ -767,7 +766,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.rs
index 7cdb59521..aa2ce627f 100644
--- a/crates/engine/src/edit_cmds.rs
+++ b/crates/engine/src/edit_cmds.rs
@@ -394,7 +394,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;
@@ -403,8 +402,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.rs
index 1b71e966a..418b0051d 100644
--- a/crates/engine/src/layer_multi_cmds.rs
+++ b/crates/engine/src/layer_multi_cmds.rs
@@ -885,9 +885,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);
@@ -1386,7 +1386,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();
@@ -1408,7 +1407,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)}))
@@ -1823,8 +1821,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);
@@ -1847,6 +1845,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}));
}