Skip to content

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

fix(engine): Layer via Copy and Cut deselect, as Photoshop does (#1096) (#1696)

MarekSpodBiedry1433c3c520fe
diff --git a/crates/engine/src/edit_cmds.rs b/crates/engine/src/edit_cmds.rs
index bcbcf8b6c..49031f711 100644
--- a/crates/engine/src/edit_cmds.rs
+++ b/crates/engine/src/edit_cmds.rs
@@ -268,6 +268,8 @@ fn layer_via(s: &mut Session, cut: bool) -> Result<Value> {
*crate::pixels_mut(&mut l)? = clip.surface;
let nid = doc.insert_above(Some(id), l);
*active = Some(nid);
+ // Photoshop deselects: a following Free Transform moves the whole new layer (#1096).
+ doc.selection = None;
Ok(nid)
})?;
Ok(json!({"layer": nid.0}))
@@ -917,6 +919,25 @@ mod tests {
assert!(d.layers[2].name.ends_with("copy"));
}
+ /// #1096: Layer via Copy and Cut deselect, as Photoshop does, so a following Free Transform
+ /// moves the whole new layer; one undo brings back both the selection and the old layers.
+ #[test]
+ fn layer_via_copy_and_cut_deselect_in_one_step() {
+ for cmd in ["layer.new.layerViaCopy", "layer.new.layerViaCut"] {
+ let mut s = session();
+ s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 20})).unwrap();
+ let (layers, steps) = (s.active().unwrap().doc.layers.len(), s.active().unwrap().history.past_len());
+ s.execute(cmd, json!({})).unwrap();
+ let st = s.active().unwrap();
+ assert!(st.doc.selection.is_none(), "{cmd}: deselected");
+ assert_eq!((st.doc.layers.len(), st.history.past_len()), (layers + 1, steps + 1), "{cmd}");
+ assert!(s.undo());
+ let st = s.active().unwrap();
+ assert_eq!(st.doc.layers.len(), layers, "{cmd}: undo removes the layer");
+ assert!(st.doc.selection.is_some(), "{cmd}: and restores the selection");
+ }
+ }
+
fn active_content(s: &Session) -> LayerContent {
let st = s.active().unwrap();
st.doc.layer(st.active_layer.unwrap()).unwrap().content.clone()