Skip to content

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

Fix/scale marker rollback layer state 4975 (#5391)

shmuel biller7b8e5034d04b
diff --git a/crates/engine/src/analysis_cmds.rs b/crates/engine/src/analysis_cmds.rs
index 64af849e5..6ad25951d 100644
--- a/crates/engine/src/analysis_cmds.rs
+++ b/crates/engine/src/analysis_cmds.rs
@@ -177,6 +177,12 @@ fn set_quiet(s: &mut Session, f: impl FnOnce(&mut Document)) -> Result<()> {
pub(crate) fn compound<R>(s: &mut Session, label: &str, f: impl FnOnce(&mut Session) -> Result<R>) -> Result<R> {
let st = s.active().ok_or(EngineError::NoDocument)?;
let (before, history, prior) = (st.doc.clone(), st.history.clone(), st.layer_target());
+ // An inner command may select a layer that disappears when the compound rolls back.
+ // Selection, channel targeting and coalescing must roll back with the document (#4975).
+ let anchor = st.layer_anchor;
+ let channel_view = st.channel_view.clone();
+ let coalesce = st.coalesce.clone();
+ let was_clean = !st.is_dirty();
let journal = s.journal.len();
// Color Sampler points are view state a step may move (crop, rotation): roll them back too, so
// a failed second step can't leave them pinned to pixels the restored document no longer has.
@@ -187,7 +193,7 @@ pub(crate) fn compound<R>(s: &mut Session, label: &str, f: impl FnOnce(&mut Sess
}
let st = s.active_mut().ok_or(EngineError::NoDocument)?;
st.history = history;
- match r {
+ let result = match r {
Ok(v) => {
// As in `Session::edit`: undo targets what was selected just before the step (#1356).
st.history.set_current_layers(prior);
@@ -198,12 +204,26 @@ pub(crate) fn compound<R>(s: &mut Session, label: &str, f: impl FnOnce(&mut Sess
}
Err(e) => {
st.doc = before;
+ st.active_layer = prior.active;
+ st.selected_layers = prior.selected;
+ st.layer_anchor = anchor;
+ st.channel_view = channel_view;
+ st.coalesce = coalesce;
st.color_samplers = samplers;
st.revision += 1;
+ // A failed compound does not make an otherwise untouched document dirty.
+ if was_clean {
+ st.saved_revision = st.revision;
+ }
st.last_damage = None;
Err(e)
}
+ };
+ if result.is_err() {
+ // The app-wide tool colours/mask mode follow the restored channel target.
+ s.sync_tools_mask_target();
}
+ result
}
/// Current time as ISO 8601 UTC (`2026-10-01T12:34:56Z`); empty on the web (no clock).
diff --git a/crates/engine/src/analysis_cmds/tests.rs b/crates/engine/src/analysis_cmds/tests.rs
index e5272d558..124f9abf1 100644
--- a/crates/engine/src/analysis_cmds/tests.rs
+++ b/crates/engine/src/analysis_cmds/tests.rs
@@ -11,6 +11,69 @@ fn d(s: &Session) -> Arc<Document> {
s.active().unwrap().doc.clone()
}
+#[test]
+fn failed_compound_restores_layer_target_history_and_journal() {
+ let mut s = session(80, 60, 8);
+ let background = s.active().unwrap().active_layer.unwrap();
+ s.execute("layer.new.layer", json!({"name": "Selected"})).unwrap();
+ let selected = s.active().unwrap().active_layer.unwrap();
+ {
+ let st = s.active_mut().unwrap();
+ st.selected_layers = vec![background, selected];
+ st.layer_anchor = Some(background);
+ st.coalesce = Some("previous-gesture".into());
+ }
+
+ let st = s.active().unwrap();
+ let prior_doc = st.doc.clone();
+ let prior_history = st.history.past_len();
+ let prior_channel_view = st.channel_view.clone();
+ let prior_mask_targeted = s.tools.mask_targeted;
+ let prior_journal = s.journal.len();
+ let error = compound(&mut s, "Place Scale Marker", |s| -> Result<()> {
+ // Mirrors a scale-bar creation followed by a failed type.create: the
+ // inner command selects a new layer, which rollback must remove.
+ s.execute("layer.new.layer", json!({"name": "Temporary Scale Bar"}))?;
+ s.active_mut().unwrap().channel_view.target = crate::channel_cmds::ChannelTarget::LayerMask;
+ s.sync_tools_mask_target();
+ Err(bad("test", "synthetic second step failed"))
+ })
+ .unwrap_err();
+
+ assert!(error.to_string().contains("synthetic second step failed"));
+ let st = s.active().unwrap();
+ assert!(Arc::ptr_eq(&prior_doc, &st.doc), "the original document is restored");
+ assert_eq!(st.history.past_len(), prior_history, "no undo step remains");
+ assert_eq!(s.journal.len(), prior_journal, "no rolled-back command is journaled");
+ assert_eq!(st.active_layer, Some(selected));
+ assert_eq!(st.selected_layers, vec![background, selected]);
+ assert_eq!(st.layer_anchor, Some(background));
+ assert_eq!(st.channel_view, prior_channel_view);
+ assert_eq!(s.tools.mask_targeted, prior_mask_targeted, "tool mask target follows the restored channel");
+ assert_eq!(st.coalesce.as_deref(), Some("previous-gesture"));
+ assert!(st.doc.layer(selected).is_some());
+
+ // Subsequent ordinary commands target the original layer, not the
+ // nonexistent scale bar.
+ s.execute("layer.setProps", json!({"layer": selected.0, "visible": false})).unwrap();
+ assert!(!s.active().unwrap().doc.layer(selected).unwrap().visible);
+}
+
+#[test]
+fn failed_compound_preserves_a_clean_document() {
+ let mut s = session(80, 60, 8);
+ assert!(!s.active().unwrap().is_dirty());
+ let prior_doc = d(&s);
+ let error = compound(&mut s, "Place Scale Marker", |s| -> Result<()> {
+ s.execute("layer.new.layer", json!({"name": "Temporary Scale Bar"}))?;
+ Err(bad("test", "synthetic second step failed"))
+ })
+ .unwrap_err();
+ assert!(error.to_string().contains("synthetic second step failed"));
+ assert!(Arc::ptr_eq(&prior_doc, &d(&s)));
+ assert!(!s.active().unwrap().is_dirty(), "a rolled-back edit does not need saving");
+}
+
#[test]
fn record_measurements_refuses_an_area_too_large_to_measure() {
// #933: with no selection the whole document is measured, and `width * height` overflowed