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 <[email protected]>
This commit is contained in:
prekabrekiandClaude Opus 5.5 authored and Brandon Thomas committed 2026-10-08 14:44:20 -04:00
1 parent c833294e34
commit fa25d217fb
3 files changed
+52 -6

No files matched your search

+1 -2
View File
@@ -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(())
})?;
+3 -4
View File
@@ -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<Value> {
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(())
})?;
+48
View File
@@ -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);
}