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

Selecting a layer is not a history step, so each state kept the target it
had when it was created. Undoing a step therefore jumped back to whatever
was selected when the previous step was taken: rasterize a shape with the
Background active, select the rasterized layer, paint, Ctrl+Z, and the
Background became active again.

The state a step leaves behind now targets what was selected just before
the step (Session::edit and compound steps set the current state's target
before recording), as in Photoshop: undo of a stroke or fill keeps the
painted layer, undo of New Layer or Merge goes back to the layers selected
before it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-09 02:59:29 +02:00
1 parent d2ad238e82
commit 98a24d1269
6 files changed
+78 -10

No files matched your search

+3 -1
View File
@@ -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;
+2 -1
View File
@@ -1493,8 +1493,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);
}
+5 -1
View File
@@ -152,7 +152,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>,
@@ -449,6 +449,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)?;
@@ -463,6 +466,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 {
@@ -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());
}
+4 -3
View File
@@ -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>,
+6 -4
View File
@@ -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");
}
}