Skip to content

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

Clear, Cut and Image › Adjustments refuse a pixel-locked layer (#1103) (#1194)

prekabrekia32ba211c9fc
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 648fe6d83..9c39b750c 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -63,11 +63,10 @@ pub(crate) fn has_paintable(s: &Session) -> std::result::Result<(), String> {
/// Surface a paint command writes to: the layer's pixels, or its mask with `"target":"mask"`.
pub(crate) fn paint_surface<'a>(doc: &'a mut Document, id: LayerId, p: &Value) -> Result<&'a mut photocraft_raster::Surface> {
- let locks = doc.effective_locks(id);
- let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
- if !is_mask_target(p) && (locks.pixels || locks.all) {
- return Err(EngineError::Other(format!("Could not complete your request because the layer \"{}\" is locked", l.name)));
+ if !is_mask_target(p) {
+ check_pixels_unlocked(doc, id)?;
}
+ let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?;
if is_mask_target(p) {
l.mask.as_mut().map(|m| &mut m.surface).ok_or_else(|| EngineError::Other("layer has no mask".into()))
} else {
@@ -75,6 +74,16 @@ pub(crate) fn paint_surface<'a>(doc: &'a mut Document, id: LayerId, p: &Value) -
}
}
+/// Refuse a layer whose pixels are locked (directly or through a group), with Photoshop's message.
+pub(crate) fn check_pixels_unlocked(doc: &Document, id: LayerId) -> Result<()> {
+ let locks = doc.effective_locks(id);
+ let l = doc.layer(id).ok_or(EngineError::NoLayer(id))?;
+ if locks.pixels || locks.all {
+ return Err(EngineError::Other(format!("Could not complete your request because the layer \"{}\" is locked", l.name)));
+ }
+ Ok(())
+}
+
pub(crate) fn is_mask_target(p: &Value) -> bool {
p.get("target").and_then(Value::as_str) == Some("mask")
}
@@ -226,8 +235,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 e58d538b3..bcbcf8b6c 100644
--- a/crates/engine/src/edit_cmds.rs
+++ b/crates/engine/src/edit_cmds.rs
@@ -138,9 +138,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 {
@@ -412,7 +411,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(())
})?;
@@ -537,8 +536,10 @@ pub fn specs() -> Vec<CommandSpec> {
}
vec![
spec!("edit.cut", "Cut", &["Edit"], Some("Cmd+X"), "{}", has_pixels, |s, _| {
- let r = copy(s, false)?;
+ // Refuse a locked layer before copying, so a refused Cut leaves the clipboard alone.
let id = active_id(s)?;
+ crate::commands::check_pixels_unlocked(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?;
+ let r = copy(s, false)?;
let bg = s.tools.background;
s.edit("Cut Pixels", |doc, _| clear_selected(doc, id, bg))?;
Ok(r)
diff --git a/crates/engine/tests/pixel_lock_edits.rs b/crates/engine/tests/pixel_lock_edits.rs
new file mode 100644
index 000000000..a773b414f
--- /dev/null
+++ b/crates/engine/tests/pixel_lock_edits.rs
@@ -0,0 +1,61 @@
+//! 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 a_refused_cut_leaves_the_clipboard_alone() {
+ let mut s = locked();
+ // Copy the whole canvas first, then select a smaller area: a Cut that went through would
+ // replace the clipboard with a smaller clip.
+ s.execute("edit.copy", json!({})).unwrap();
+ let before = s.clipboard.as_ref().map(|c| c.bounds);
+ assert!(before.is_some());
+ s.execute("select.rect", json!({"x": 2, "y": 2, "width": 10, "height": 10})).unwrap();
+ assert!(s.execute("edit.cut", json!({})).is_err());
+ assert_eq!(s.clipboard.as_ref().map(|c| c.bounds), before, "a refused Cut replaced the clipboard");
+}
+
+#[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);
+}