Skip to content

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

Undo: a paint stroke's undo keeps the painted layer active (#1356) (#1523)

David Mann4dcb2e4088c3
diff --git a/crates/engine/src/analysis_cmds.rs b/crates/engine/src/analysis_cmds.rs
index 9bf9b08ab..21b527798 100644
--- a/crates/engine/src/analysis_cmds.rs
+++ b/crates/engine/src/analysis_cmds.rs
@@ -174,12 +174,14 @@ fn set_quiet(s: &mut Session, f: impl FnOnce(&mut Document)) -> Result<()> {
/// Run several commands as one history step labelled `label`.
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) = (st.doc.clone(), st.history.clone());
+ let (before, history, prior) = (st.doc.clone(), st.history.clone(), st.layer_target());
let r = f(s);
let st = s.active_mut().ok_or(EngineError::NoDocument)?;
st.history = history;
match r {
Ok(v) => {
+ // As in `Session::edit`: undo targets what was selected just before the step (#1356).
+ st.history.set_current_layers(prior);
st.history.record(label, before, st.layer_target());
st.history.trim(&st.doc);
st.coalesce = None;
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs
index 9277f5979..84cfddfab 100644
--- a/crates/engine/src/layer_multi_cmds.rs
+++ b/crates/engine/src/layer_multi_cmds.rs
@@ -1501,8 +1501,9 @@ mod tests {
assert_eq!(sel(&s), vec![m]);
s.undo();
assert_eq!(doc(&s).layer_count(), before);
- assert_eq!(sel(&s), vec![c], "undo leaves a valid selection");
+ assert_eq!(sel(&s), vec![a, c], "undo selects the layers that were merged");
// One layer selected: Merge Down.
+ s.execute("layer.select", json!({"layer": c.0})).unwrap();
s.execute("layer.mergeLayers", json!({})).unwrap();
assert_eq!(doc(&s).layer_count(), before - 1);
}
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 9f6184a6e..7b209455a 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -154,7 +154,7 @@ pub struct DocState {
pub active_layer: Option<LayerId>,
/// Every selected layer in the Layers panel (⌘/⇧-click), in selection order. Like
/// `active_layer`, selecting is not a history step, but undo and redo restore the layers each
- /// state targeted when it was created; see [`DocState::selected_layers`].
+ /// state targeted when the next step was taken; see [`DocState::selected_layers`].
pub selected_layers: Vec<LayerId>,
/// Anchor of ⇧-click range selection (the last plainly or ⌘-clicked layer).
pub layer_anchor: Option<LayerId>,
@@ -451,6 +451,9 @@ impl Session {
let restrict = self.color_restrict;
let st = self.active_mut().ok_or(EngineError::NoDocument)?;
let before = st.doc.clone();
+ // Selecting layers is not a step, so the state this edit leaves behind targets what was
+ // selected just before it (#1356): undoing a stroke keeps the painted layer active.
+ let prior = st.layer_target();
let mut doc = (*before).clone();
let mut active = st.active_layer;
let r = f(&mut doc, &mut active)?;
@@ -465,6 +468,7 @@ impl Session {
let st = self.active_mut().ok_or(EngineError::NoDocument)?;
let layers = st.layer_target();
if key.is_none() || st.coalesce != key || !st.history.can_undo() {
+ st.history.set_current_layers(prior);
st.history.record(label, before, layers);
st.history.trim(&st.doc);
} else {
diff --git a/crates/engine/tests/undo_keeps_active_layer.rs b/crates/engine/tests/undo_keeps_active_layer.rs
new file mode 100644
index 000000000..32c7d4057
--- /dev/null
+++ b/crates/engine/tests/undo_keeps_active_layer.rs
@@ -0,0 +1,58 @@
+//! Undoing a paint stroke keeps the painted layer active (#1356), as in Photoshop: selecting a
+//! layer is not a history step, so the state a step leaves behind targets what was selected just
+//! before the step, not what was selected when the state before it was made.
+
+use photocraft_doc::LayerId;
+use photocraft_engine::Session;
+use serde_json::json;
+
+fn target(s: &Session) -> (Option<LayerId>, Vec<LayerId>) {
+ let st = s.active().unwrap();
+ (st.active_layer, st.selected_layers.clone())
+}
+
+fn one(id: LayerId) -> (Option<LayerId>, Vec<LayerId>) {
+ (Some(id), vec![id])
+}
+
+#[test]
+fn undoing_a_stroke_on_a_rasterized_shape_keeps_it_active() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
+ let bg = s.active().unwrap().doc.layers[0].id;
+ let id = LayerId(s.execute("shape.create", json!({"kind": "ellipse", "rect": [4, 4, 40, 30]})).unwrap()["layer"].as_u64().unwrap());
+ // Rasterize the shape layer while the Background is the active layer (Layers panel context
+ // menu), then select the rasterized layer and paint on it.
+ s.execute("layer.select", json!({"layer": bg.0})).unwrap();
+ s.execute("layer.rasterize.layer", json!({"layer": id.0})).unwrap();
+ s.execute("layer.select", json!({"layer": id.0})).unwrap();
+ s.execute("paint.stroke", json!({"points": [[4, 40], [60, 40]], "size": 6})).unwrap();
+ assert!(s.undo());
+ assert_eq!(target(&s), one(id), "undo of the stroke left the painted layer (background is {bg:?})");
+ assert!(s.redo());
+ assert_eq!(target(&s), one(id), "redo targets the painted layer");
+ // Undoing the stroke and then the rasterize goes back to what was active before each step.
+ assert!(s.undo());
+ assert!(s.undo());
+ assert_eq!(target(&s), one(bg), "the rasterize was taken with the background active");
+}
+
+#[test]
+fn undoing_a_fill_keeps_the_filled_layer_active() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16})).unwrap();
+ let bg = s.active().unwrap().doc.layers[0].id;
+ s.execute("layer.new.layer", json!({})).unwrap();
+ let new = target(&s).0.unwrap();
+ s.execute("layer.select", json!({"layer": bg.0})).unwrap();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ assert!(s.undo());
+ assert_eq!(target(&s), one(bg), "the fill was on the background");
+ // Undoing New Layer still goes back to the layer active before it.
+ assert!(s.undo());
+ assert_eq!(target(&s), one(bg));
+ // Redo returns New Layer's state as it was left: the background had been selected in it.
+ assert!(s.redo());
+ assert_eq!(target(&s), one(bg));
+ assert!(s.active().unwrap().doc.layer(new).is_some());
+}
diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rs
index c3f494fca..097fb3b94 100644
--- a/crates/ops/src/lib.rs
+++ b/crates/ops/src/lib.rs
@@ -15,9 +15,10 @@ use std::sync::Arc;
use photocraft_doc::{Document, LayerId};
-/// The layers a state targeted when it was created: the active ("key") layer and every selected
-/// layer. Undo and redo bring them back with the state's document, as the reference app does;
-/// selecting layers is not a step of its own, so it doesn't change a state's target.
+/// The layers a state targeted: the active ("key") layer and every selected layer. Undo and redo
+/// bring them back with the state's document, as the reference app does. Selecting layers is not
+/// a step of its own, so the session updates the current state's target with
+/// [`History::set_current_layers`] just before recording the next step (#1356).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LayerTarget {
pub active: Option<LayerId>,
diff --git a/crates/ui-egui/src/layer_reveal_tests.rs b/crates/ui-egui/src/layer_reveal_tests.rs
index 5db920ffa..e9c7cd1ae 100644
--- a/crates/ui-egui/src/layer_reveal_tests.rs
+++ b/crates/ui-egui/src/layer_reveal_tests.rs
@@ -97,7 +97,7 @@ fn a_new_active_layer_opens_its_groups_and_scrolls_into_view() {
h.run_steps(4);
assert!(!expanded(h.state(), group), "{theme:?}: stays closed");
// Undo and other commands that change the active layer reveal it too: undoing a new
- // layer deep in the list makes the top layer active again.
+ // layer made deep in the list makes the layer active before it active again (#1356).
h.state_mut().run("layer.select", json!({"layer": top.0})).unwrap();
h.run_steps(3);
h.state_mut().run("layer.select", json!({"layer": inner.0})).unwrap();
@@ -105,11 +105,13 @@ fn a_new_active_layer_opens_its_groups_and_scrolls_into_view() {
assert!(expanded(h.state(), group));
h.state_mut().run("layer.new.layer", json!({"name": "Scratch"})).unwrap();
h.run_steps(4);
- assert!(!in_view(&h, row(&h, "Stack 29").unwrap()));
+ h.state_mut().run("layer.select", json!({"layer": top.0})).unwrap();
+ h.run_steps(4);
+ assert!(!in_view(&h, row(&h, "Deep inside").unwrap()));
h.state_mut().run("edit.undo", json!({})).unwrap();
h.run_steps(4);
- assert_eq!(h.state().session.active().unwrap().active_layer, Some(top), "{theme:?}: undo made the top layer active");
- assert!(in_view(&h, row(&h, "Stack 29").unwrap()), "{theme:?}: undo revealed it");
+ assert_eq!(h.state().session.active().unwrap().active_layer, Some(inner), "{theme:?}: undo made the inner layer active");
+ assert!(in_view(&h, row(&h, "Deep inside").unwrap()), "{theme:?}: undo revealed it");
}
}