From fa25d217fbeb3cac7bc53c110a0e2e2b12e8518c Mon Sep 17 00:00:00 2001 From: prekabreki Date: Thu, 8 Oct 2026 14:29:21 +0200 Subject: [PATCH] Clear, Cut and Image > Adjustments refuse a pixel-locked layer (#1103) clear_area, destructive_adjust and auto_adjust took the layer's surface directly. They now go through commands::paint_surface, which refuses a pixel- or fully-locked layer with the same message painting uses. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/commands.rs | 3 +- crates/engine/src/edit_cmds.rs | 7 ++-- crates/engine/tests/pixel_lock_edits.rs | 48 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 crates/engine/tests/pixel_lock_edits.rs diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index 6da63b698e1..67678a871af 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -218,8 +218,7 @@ fn destructive_adjust(s: &mut Session, label: &str, adj: Adjustment, p: &Value) s.edit(label, |doc, _| { let sel = doc.selection.clone(); let mode = doc.mode; - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - let surf = l.surface_mut().ok_or_else(|| EngineError::Other("not a pixel layer".into()))?; + let surf = paint_surface(doc, id, &Value::Null)?; pixels::adjust_surface(surf, &adj, sel.as_ref(), mode); Ok(()) })?; diff --git a/crates/engine/src/edit_cmds.rs b/crates/engine/src/edit_cmds.rs index 73b47faf1d2..9c0554e6df7 100644 --- a/crates/engine/src/edit_cmds.rs +++ b/crates/engine/src/edit_cmds.rs @@ -130,9 +130,8 @@ fn clear_selected(doc: &mut Document, id: LayerId, background: [f32; 4]) -> Resu /// Cut / Clear on a layer: makes the selected pixels transparent. The Background can't hold /// transparency, so there the area is filled with the background colour instead (Photoshop). pub(crate) fn clear_area(doc: &mut Document, id: LayerId, area: Rect, sel: Option<&Surface>, background: [f32; 4]) -> Result<()> { - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - let bg = crate::extra_cmds::is_background(l); - let surf = l.surface_mut().ok_or(EngineError::Other("the active layer has no pixels".into()))?; + let bg = crate::extra_cmds::is_background(doc.layer(id).ok_or(EngineError::NoLayer(id))?); + let surf = crate::commands::paint_surface(doc, id, &Value::Null)?; if bg { crate::pixels::fill_surface(surf, area, background, sel, true); } else { @@ -369,7 +368,7 @@ fn auto_adjust(s: &mut Session, kind: &str) -> Result { s.edit(label, |doc, _| { let sel = doc.selection.clone(); let mode = doc.mode; - let surf = doc.layer_mut(id).and_then(|l| l.surface_mut()).ok_or(EngineError::NoLayer(id))?; + let surf = crate::commands::paint_surface(doc, id, &Value::Null)?; crate::pixels::adjust_surface(surf, &adj, sel.as_ref(), mode); Ok(()) })?; diff --git a/crates/engine/tests/pixel_lock_edits.rs b/crates/engine/tests/pixel_lock_edits.rs new file mode 100644 index 00000000000..72d3fad945c --- /dev/null +++ b/crates/engine/tests/pixel_lock_edits.rs @@ -0,0 +1,48 @@ +//! Clear, Cut and Image › Adjustments refuse a pixel-locked layer and leave it unchanged, as +//! painting does (#1103). + +use photocraft_engine::Session; +use serde_json::json; + +/// A layer with a red stroke across it, pixels locked. +fn locked() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 10], [20, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap(); + s.execute("layer.lockLayers", json!({"pixels": true})).unwrap(); + s +} + +fn px(s: &Session) -> [f32; 4] { + let st = s.active().unwrap(); + st.active_layer.and_then(|id| st.doc.layer(id)).unwrap().surface().unwrap().rgba(12, 10) +} + +#[test] +fn locked_layer_edits_are_refused() { + for (cmd, p) in [ + ("edit.clear", json!({})), + ("edit.cut", json!({})), + ("image.adjustments.invert", json!({})), + ("image.autoTone", json!({})), + ("image.autoContrast", json!({})), + ("image.autoColor", json!({})), + ] { + let mut s = locked(); + let before = px(&s); + let r = s.execute(cmd, p); + assert!(r.is_err(), "{cmd} edited a locked layer: {r:?}"); + assert_eq!(px(&s), before, "{cmd} changed a locked layer's pixels"); + } +} + +#[test] +fn unlocked_layer_edits_still_work() { + let mut s = locked(); + s.execute("layer.lockLayers", json!({"pixels": false})).unwrap(); + s.execute("image.adjustments.invert", json!({})).unwrap(); + assert_eq!(px(&s), [0.0, 1.0, 1.0, 1.0]); + s.execute("edit.clear", json!({})).unwrap(); + assert_eq!(px(&s)[3], 0.0); +}