Skip to content

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

Fill layers: pin the preference-off precedence for selection and explicit path

abc2cbd685aa1484c9
diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs
index 6330525d8..1ea70ecb2 100644
--- a/crates/engine/src/layer_multi_cmds.rs
+++ b/crates/engine/src/layer_multi_cmds.rs
@@ -2983,6 +2983,56 @@ mod layers_panel_parity_tests {
assert!(s.active().unwrap().doc.layer(id).unwrap().mask.is_some());
}
+ /// With the option off only the default reveal-all mask goes away: an active selection still
+ /// becomes the layer mask and an explicit path still becomes the vector mask (path first).
+ #[test]
+ fn fill_layers_without_default_masks_keep_selection_and_explicit_path_masks() {
+ const KINDS: [(&str, &str); 3] = [
+ ("layer.newFillLayer.solidColor", r##"{"color": "#112233"}"##),
+ ("layer.newFillLayer.gradient", "{}"),
+ ("layer.newFillLayer.pattern", r#"{"pattern": "Checkerboard"}"#),
+ ];
+ let path = json!({"subpaths": [{"closed": true, "knots": [[2, 2], [10, 2], [10, 10], [2, 10]]}]});
+ let (mut s, _) = new_session();
+ option(&mut s, "useDefaultMasksOnFillLayers", false);
+ // Creates the fill layer; returns its raster mask and whether it has a vector mask.
+ let create = |s: &mut Session, cmd: &str, params: &str, path: Option<&Value>| {
+ let mut p: Value = serde_json::from_str(params).unwrap();
+ if let Some(path) = path {
+ p["path"] = path.clone();
+ }
+ let id = LayerId(s.execute(cmd, p).unwrap()["layer"].as_u64().unwrap());
+ let l = s.active().unwrap().doc.layer(id).unwrap();
+ (l.mask.clone(), l.vector_mask.is_some())
+ };
+
+ for (cmd, params) in KINDS {
+ let (mask, vector) = create(&mut s, cmd, params, None);
+ assert!(mask.is_none() && !vector, "{cmd}: no selection, no path: no mask at all");
+ }
+
+ s.execute("select.rect", json!({"x": 4, "y": 4, "width": 8, "height": 6})).unwrap();
+ for (cmd, params) in KINDS {
+ let (mask, vector) = create(&mut s, cmd, params, None);
+ let mask = mask.unwrap_or_else(|| panic!("{cmd}: the selection still becomes the layer mask"));
+ assert!(!vector, "{cmd}");
+ assert!(mask.surface.pixel(6, 6)[0] > 0.99, "{cmd}: inside the selection reveals");
+ assert!(mask.surface.pixel(20, 12)[0] < 0.01, "{cmd}: outside the selection hides");
+ }
+
+ // An explicit path wins over the selection, as with the option on.
+ for selected in [true, false] {
+ if !selected {
+ s.execute("select.deselect", json!({})).unwrap();
+ }
+ for (cmd, params) in KINDS {
+ let (mask, vector) = create(&mut s, cmd, params, Some(&path));
+ assert!(vector, "{cmd}: the explicit path becomes the vector mask (selection: {selected})");
+ assert!(mask.is_none(), "{cmd}: no raster mask next to the vector mask (selection: {selected})");
+ }
+ }
+ }
+
#[test]
fn expand_new_effects_is_view_state_and_keeps_edit_history_clean() {
let (mut s, ids) = new_session();