Skip to content

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

Layer comps: an edited layout becomes the Last Document State (#1862)

Jason Xianga4a2583280c4
diff --git a/crates/engine/src/comps_cmds.rs b/crates/engine/src/comps_cmds.rs
index 3857236f6..5eb1fab75 100644
--- a/crates/engine/src/comps_cmds.rs
+++ b/crates/engine/src/comps_cmds.rs
@@ -161,11 +161,28 @@ fn update_comp(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"updated": ids}))
}
+/// Whether the live layout still matches the properties the last applied comp restores.
+/// Compare only on comp application: ordinary edits must not scan pixel bounds each frame.
+fn matches_last_comp(doc: &Document, now: &[CompLayerState]) -> bool {
+ let Some(comp) = doc.last_applied_comp.and_then(|id| doc.comp(id)) else { return false };
+ let saved: std::collections::HashMap<_, _> = comp.states.iter().map(|st| (st.layer, st)).collect();
+ now.len() == saved.len()
+ && now.iter().all(|live| {
+ saved.get(&live.layer).is_some_and(|saved| {
+ (!comp.apply_visibility || saved.visible.is_none() || saved.visible == live.visible)
+ && (!comp.apply_position || saved.position.is_none() || saved.position == live.position)
+ && (!comp.apply_appearance || saved.appearance.is_none() || saved.appearance == live.appearance)
+ })
+ })
+}
+
fn apply(s: &mut Session, id: u32, label: &str) -> Result<Value> {
s.edit(label, |doc, _| {
let comp = doc.comp(id).cloned().ok_or_else(|| EngineError::Other(format!("no layer comp with id {id}")))?;
- // Applying over the document's own state first remembers it as the Last Document State.
- if doc.last_applied_comp.is_none_or(|c| doc.comp(c).is_none()) {
+ // Ordinary edits can leave last_applied_comp pointing at an outdated layout (#919).
+ // Preserve the edited layout, but keep the original backup while cycling unedited comps.
+ let states = capture_states(doc);
+ if !matches_last_comp(doc, &states) {
doc.last_document_state = Some(LayerComp {
id: 0,
name: "Last Document State".into(),
@@ -173,7 +190,7 @@ fn apply(s: &mut Session, id: u32, label: &str) -> Result<Value> {
apply_visibility: true,
apply_position: true,
apply_appearance: true,
- states: capture_states(doc),
+ states,
});
}
apply_comp(doc, &comp, false);
diff --git a/crates/engine/src/comps_cmds/tests.rs b/crates/engine/src/comps_cmds/tests.rs
index cf7497279..22ef1a469 100644
--- a/crates/engine/src/comps_cmds/tests.rs
+++ b/crates/engine/src/comps_cmds/tests.rs
@@ -83,11 +83,6 @@ fn undo_redo_and_last_document_state() {
assert_eq!(doc(&s).layer_comps[0].name, "Layer Comp 1");
s.execute("layer.translate", json!({"layer": a.0, "dx": 3, "dy": 0})).unwrap();
// A fresh document state (not a comp) is remembered when a comp is applied over it.
- s.edit("forget", |d, _| {
- d.last_applied_comp = None;
- Ok(())
- })
- .unwrap();
s.execute("layerComp.apply", json!({})).unwrap();
assert_eq!(pos(&s, a), Some((4, 4)));
assert!(doc(&s).last_document_state.is_some());
@@ -107,6 +102,60 @@ fn undo_redo_and_last_document_state() {
assert_eq!(doc(&s).layer_comps.len(), 1);
}
+#[test]
+fn ordinary_layout_edits_become_the_last_document_state() {
+ for depth in [8, 16, 32] {
+ for (command, mut params) in [
+ ("layer.translate", json!({"dx": 9, "dy": 3})),
+ ("layer.setProps", json!({"visible": false})),
+ ("layer.setProps", json!({"opacity": 0.25})),
+ ("layer.setProps", json!({"blend": "multiply"})),
+ ] {
+ let (mut s, a, _) = session(depth);
+ s.execute("layerComp.new", json!({})).unwrap();
+ params["layer"] = json!(a.0);
+ s.execute(command, params).unwrap();
+ let edited = capture_states(doc(&s));
+ s.execute("layerComp.apply", json!({})).unwrap();
+ assert_ne!(capture_states(doc(&s)), edited);
+ assert!(s.is_enabled("layerComp.restoreLastDocumentState"));
+ s.execute("layerComp.restoreLastDocumentState", json!({})).unwrap();
+ assert_eq!(capture_states(doc(&s)), edited, "{depth}-bit");
+ }
+ }
+}
+
+#[test]
+fn cycling_comps_keeps_the_latest_edited_layout() {
+ let (mut s, a, _) = session(8);
+ let one = s.execute("layerComp.new", json!({"name": "One"})).unwrap()["comp"].clone();
+ s.execute("layer.translate", json!({"layer": a.0, "dx": 10, "dy": 0})).unwrap();
+ let two = s.execute("layerComp.new", json!({"name": "Two"})).unwrap()["comp"].clone();
+ for dx in [7, 13] {
+ s.execute("layer.translate", json!({"layer": a.0, "dx": dx, "dy": 0})).unwrap();
+ let edited = capture_states(doc(&s));
+ s.execute("layerComp.apply", json!({"comp": one})).unwrap();
+ s.execute("layerComp.apply", json!({"comp": two})).unwrap();
+ s.execute("layerComp.apply", json!({"comp": two})).unwrap();
+ s.execute("layerComp.restoreLastDocumentState", json!({})).unwrap();
+ assert_eq!(capture_states(doc(&s)), edited);
+ }
+}
+
+#[test]
+fn unrecorded_properties_do_not_replace_the_backup_on_reapply() {
+ let (mut s, a, _) = session(8);
+ s.execute("layerComp.new", json!({"position": false, "appearance": false})).unwrap();
+ s.execute("layer.translate", json!({"layer": a.0, "dx": 10, "dy": 0})).unwrap();
+ s.execute("layer.setProps", json!({"layer": a.0, "visible": false, "opacity": 0.5})).unwrap();
+ let edited = capture_states(doc(&s));
+ s.execute("layerComp.apply", json!({})).unwrap();
+ assert!(doc(&s).layer(a).unwrap().visible);
+ s.execute("layerComp.apply", json!({})).unwrap();
+ s.execute("layerComp.restoreLastDocumentState", json!({})).unwrap();
+ assert_eq!(capture_states(doc(&s)), edited);
+}
+
#[test]
fn manage_comps_and_cycle() {
let (mut s, _, b) = session(8);