PhotoCraft: An open-source, photo editor written by scratch in pure Rust
New adjustment and fill layers get a white mask (#1858)
Jason Xiang175603a644f1
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 85c82e82c..53ac05c72 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -248,7 +248,8 @@ pub(crate) fn selection_mask(doc: &Document) -> Option<LayerMask> {/// Mask a fill or adjustment layer being created, as Photoshop does: the path selected in the/// Paths panel (`"path"`: `"work"`, a saved path's name or a path object) becomes its vector/// mask, which makes a Solid Color fill a shape (#1419); without one, the selection becomes its-/// layer mask ([`selection_mask`]). A path that isn't there is an error.+/// layer mask ([`selection_mask`]); without either, a white mask reveals the whole layer (#1768).+/// A path that isn't there is an error.pub(crate) fn mask_new_layer(doc: &Document, l: &mut Layer, p: &Value, cmd: &str) -> Result<()> {let path = match p.get("path") {None | Some(Value::Null) => None,@@ -260,14 +261,14 @@ pub(crate) fn mask_new_layer(doc: &Document, l: &mut Layer, p: &Value, cmd: &str};match path {Some(path) => l.vector_mask = Some(photocraft_doc::VectorMask::new(path)),- None => l.mask = selection_mask(doc),+ None => l.mask = Some(selection_mask(doc).unwrap_or_else(LayerMask::reveal_all)),}Ok(())}/// The `"path"` param of the new fill and adjustment layer commands ([`mask_new_layer`]).pub(crate) const NEW_LAYER_PATH: &str =- r##""path":"work"|saved path name|{…path}? (the active path: becomes the vector mask; else the selection is the layer mask)"##;+ r##""path":"work"|saved path name|{…path}? (the active path: becomes the vector mask; else the selection is the layer mask, or white with no selection)"##;fn new_adjustment(s: &mut Session, adj: Adjustment, p: &Value, cmd: &str) -> Result<Value> {let label = format!("New {} Layer", adj.label());diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex 15e34597a..f5c701029 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -158,16 +158,48 @@ fn new_adjustment_and_fill_layers_are_masked_by_the_selection() {}#[test]-fn new_adjustment_and_fill_layers_have_no_mask_without_a_selection() {- let mut s = session_with_doc();- for (id, params) in [- ("layer.newAdjustmentLayer.invert", json!({})),- ("layer.newFillLayer.solidColor", json!({"color": "#000000"})),- ("layer.newFillLayer.gradient", json!({})),- ("layer.newFillLayer.pattern", json!({"pattern": "Checkerboard"})),- ] {- let layer = LayerId(s.execute(id, params).unwrap()["layer"].as_u64().unwrap());- assert!(s.active().unwrap().doc.layer(layer).unwrap().mask.is_none(), "{id} got a mask with no selection");+fn new_adjustment_and_fill_layers_have_white_masks_without_a_selection() {+ for depth in [8, 16, 32] {+ for (id, params) in [+ ("layer.newAdjustmentLayer.invert", json!({})),+ ("layer.newFillLayer.solidColor", json!({"color": "#000000"})),+ ("layer.newFillLayer.gradient", json!({})),+ ("layer.newFillLayer.pattern", json!({"pattern": "Checkerboard"})),+ ] {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 64, "height": 48, "depth": depth})).unwrap();+ let history = s.active().unwrap().history.entries().len();+ let layer = LayerId(s.execute(id, params).unwrap()["layer"].as_u64().unwrap());+ let d = s.active().unwrap();+ let mask = d.doc.layer(layer).unwrap().mask.as_ref().unwrap_or_else(|| panic!("{id} at {depth}-bit: no mask"));+ assert!(mask.enabled && mask.linked, "{id}");+ assert_eq!(mask.surface.tile_count(), 0, "a reveal-all mask does not allocate canvas-sized pixels");+ for (x, y) in [(0, 0), (63, 47), (-1, -1), (300_000, 300_000)] {+ assert_eq!(mask.value(x, y), 1.0, "{id}: the default mask reveals all pixels");+ }+ assert_eq!(d.history.entries().len(), history + 1, "the mask belongs to the layer's one history step");+ s.execute("edit.undo", json!({})).unwrap();+ assert!(s.active().unwrap().doc.layer(layer).is_none());+ s.execute("edit.redo", json!({})).unwrap();+ assert_eq!(s.active().unwrap().doc.layer(layer).unwrap().mask.as_ref().unwrap().value(20, 20), 1.0);+ }+ }+}++#[test]+fn a_new_adjustment_mask_can_be_painted_immediately() {+ for depth in [8, 16, 32] {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 64, "height": 48, "depth": depth})).unwrap();+ s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap();+ assert_eq!(px(&mut s, 20, 20), vec![0.0, 0.0, 0.0, 1.0]);+ s.execute("paint.stroke", json!({"points": [[20, 20]], "size": 10, "hardness": 1.0, "color": "#000000", "target": "mask"})).unwrap();+ assert_eq!(px(&mut s, 20, 20), vec![1.0, 1.0, 1.0, 1.0]);+ assert_eq!(px(&mut s, 2, 2), vec![0.0, 0.0, 0.0, 1.0]);+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(px(&mut s, 20, 20), vec![0.0, 0.0, 0.0, 1.0]);+ s.execute("edit.redo", json!({})).unwrap();+ assert_eq!(px(&mut s, 20, 20), vec![1.0, 1.0, 1.0, 1.0]);}}