Skip to content

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

Layer masks: Reveal Selection drops the selection, as Photoshop does (#5251)

abc2cb7ad75b5db2b1
diff --git a/crates/automation/tests/agent_tasks.rs b/crates/automation/tests/agent_tasks.rs
index 271993d2b..e1585e204 100644
--- a/crates/automation/tests/agent_tasks.rs
+++ b/crates/automation/tests/agent_tasks.rs
@@ -166,7 +166,7 @@ async fn agent_completes_ten_scripted_tasks() {
let outside = pixel(&c, 1, 1).await;
assert!(inside[0] > 0.99 && inside[1] < 0.01, "task 5: centre is red {inside:?}");
assert!(outside[0] < 0.1, "task 5: corner shows the photo {outside:?}");
- run(&c, "select.deselect", json!({})).await;
+ assert_eq!(inspect(&c).await["hasSelection"], false, "task 5: the selection became the mask");
// 6. Save a selection to a channel and load it back.
run(&c, "select.rect", json!({"x": 4, "y": 6, "width": 20, "height": 10})).await;
@@ -413,7 +413,7 @@ async fn agent_completes_ten_more_scripted_tasks() {
assert_eq!(top_layer(&doc)["hasMask"], true, "task 17");
let boundary = pixel(&c, 20, 40).await;
assert!(boundary[3] > 0.05 && boundary[3] < 0.95, "task 17: the feathered edge keeps partial alpha {boundary:?}");
- run(&c, "select.deselect", json!({})).await;
+ assert_eq!(doc["hasSelection"], false, "task 17: the selection became the mask");
// 18. Expand a selection and read its bounds back.
run(&c, "select.rect", json!({"x": 20, "y": 20, "width": 20, "height": 20})).await;
diff --git a/crates/engine/src/adjust_cmds/tests.rs b/crates/engine/src/adjust_cmds/tests.rs
index 8fa053e86..9f614732d 100644
--- a/crates/engine/src/adjust_cmds/tests.rs
+++ b/crates/engine/src/adjust_cmds/tests.rs
@@ -353,7 +353,6 @@ fn invert_with_the_mask_targeted_inverts_the_mask() {
let mut s = session(depth, "rgb");
s.execute("select.rect", json!({"x": 24, "y": 0, "width": 24, "height": 32})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
let (pixels, off_canvas) = (rgba(&s, 3, 3), mask_at(&s, -500, 3));
let steps = s.active().unwrap().history.past_len();
s.execute("image.adjustments.invert", json!({"target": "mask"})).unwrap();
@@ -372,6 +371,31 @@ fn invert_with_the_mask_targeted_inverts_the_mask() {
}
}
+/// Photoshop: a mask made from the selection (Reveal Selection, the Layers panel's mask button)
+/// takes the selection with it, as Hide Selection does. So ⌘I on the new mask right away inverts
+/// all of it; with the selection kept it inverted only the inside, leaving the mask all black.
+/// Undo brings the selection back with the mask gone, in one step.
+#[test]
+fn reveal_selection_drops_the_selection_so_invert_flips_the_whole_mask() {
+ for depth in DEPTHS {
+ let mut s = session(depth, "rgb");
+ s.execute("select.rect", json!({"x": 24, "y": 0, "width": 24, "height": 32})).unwrap();
+ let selection = s.active().unwrap().doc.selection.clone();
+ let steps = s.active().unwrap().history.past_len();
+ s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
+ assert!(s.active().unwrap().doc.selection.is_none(), "depth {depth}: the selection became the mask");
+ assert_eq!(s.active().unwrap().history.past_len(), steps + 1, "one step");
+ assert_eq!((mask_at(&s, 3, 3), mask_at(&s, 30, 3)), (0.0, 1.0));
+ s.execute("image.adjustments.invert", json!({"target": "mask"})).unwrap();
+ assert_eq!((mask_at(&s, 3, 3), mask_at(&s, 30, 3)), (1.0, 0.0), "depth {depth}: inside and outside both flip");
+ s.undo();
+ s.undo();
+ let st = s.active().unwrap();
+ assert_eq!(st.doc.selection, selection, "undo restores the selection");
+ assert!(st.doc.layer(st.active_layer.unwrap()).unwrap().mask.is_none(), "and removes the mask");
+ }
+}
+
// ---------- Image › Adjustments as smart filters (FILE-215-4) ----------
/// A painted pixel layer converted to a smart object, over the painted Background.
@@ -492,7 +516,6 @@ fn a_targeted_mask_enables_editing_it_on_any_layer() {
s.execute("layer.delete", json!({})).unwrap();
s.execute("select.rect", json!({"x": 24, "y": 0, "width": 24, "height": 32})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
let pixels = rgba(&s, 23, 3);
s.execute("filter.blur.gaussianBlur", json!({"radius": 3, "target": "mask"})).unwrap();
assert!(mask_at(&s, 23, 3) > 0.0 && mask_at(&s, 23, 3) < 1.0, "the mask's edge is blurred");
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 46361d951..a954eca7e 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -805,7 +805,10 @@ fn build() -> Vec<CommandSpec> {
|s, p| {
let sel = s.active().and_then(|d| d.doc.selection.clone()).ok_or(EngineError::Other("no selection".into()))?;
let mask = LayerMask { surface: sel, ..LayerMask::reveal_all() };
- set_mask(s, p, "Add Layer Mask", Some(mask))
+ // The selection becomes the mask, so Photoshop drops it (as Hide Selection does): a
+ // following Invert (⌘I) on the targeted mask then inverts all of it, not just the
+ // part inside the old selection. Same undo step as the mask.
+ set_mask_and_deselect(s, p, "Add Layer Mask", mask)
}
),
cmd!("layer.layerMask.delete", "Delete", ["Layer", "Layer Mask"], None, r##"{"layer":id?}"##, has_layer, |s, p| set_mask(
@@ -1444,6 +1447,15 @@ fn arrange(s: &mut Session, p: &Value, delta: i32) -> Result<Value> {
/// Add (`Some`) or remove (`None`) a layer mask. Adding acts on every selected layer that can take
/// one ([`crate::layer_multi_cmds::mask_targets`]), in one history step.
fn set_mask(s: &mut Session, p: &Value, label: &str, mask: Option<LayerMask>) -> Result<Value> {
+ set_mask_with(s, p, label, mask, false)
+}
+
+/// [`set_mask`] that also drops the selection the mask was made from, in the same history step.
+fn set_mask_and_deselect(s: &mut Session, p: &Value, label: &str, mask: LayerMask) -> Result<Value> {
+ set_mask_with(s, p, label, Some(mask), true)
+}
+
+fn set_mask_with(s: &mut Session, p: &Value, label: &str, mask: Option<LayerMask>, deselect: bool) -> Result<Value> {
let ids = if mask.is_some() { crate::layer_multi_cmds::mask_targets(s, p)? } else { vec![layer_param(s, p)?] };
s.edit(label, |doc, _| {
let mask = mask.clone().map(|mask| mask_at_depth(mask, doc.depth)).transpose()?;
@@ -1453,6 +1465,9 @@ fn set_mask(s: &mut Session, p: &Value, label: &str, mask: Option<LayerMask>) ->
}
doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?.mask = mask.clone();
}
+ if deselect {
+ doc.selection = None;
+ }
Ok(())
})?;
Ok(Value::Null)
diff --git a/crates/engine/src/file_cmds/tests.rs b/crates/engine/src/file_cmds/tests.rs
index 99ddce15d..9b6567c2a 100644
--- a/crates/engine/src/file_cmds/tests.rs
+++ b/crates/engine/src/file_cmds/tests.rs
@@ -586,7 +586,6 @@ fn flatten_all_layer_effects_and_masks() {
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 40})).unwrap();
s.execute("select.inverse", json!({})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
assert!(composite(&s, 15, 15)[1] > 0.99 && composite(&s, 15, 15)[0] > 0.99, "white shows through the mask");
let r = s.execute("file.scripts.flattenAllMasks", json!({})).unwrap();
assert_eq!(r["flattened"], 1);
@@ -633,7 +632,6 @@ fn flatten_all_layer_effects_keeps_masks() {
.unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 40})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
// Layer 2: a red square at the bottom-right with the same overlay and a *disabled* mask.
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("select.rect", json!({"x": 30, "y": 30, "width": 8, "height": 8})).unwrap();
@@ -652,7 +650,6 @@ fn flatten_all_layer_effects_keeps_masks() {
.unwrap();
s.execute("select.rect", json!({"x": 34, "y": 34, "width": 2, "height": 2})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
s.edit("disable", |doc, _| {
if let Some(m) = doc.layer_mut(id2).and_then(|l| l.mask.as_mut()) {
m.enabled = false;
diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs
index a9b656430..9cd6ff460 100644
--- a/crates/engine/src/filters.rs
+++ b/crates/engine/src/filters.rs
@@ -460,7 +460,6 @@ mod tests {
if reveal_selection {
s.execute("select.all", json!({})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
} else {
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
}
diff --git a/crates/engine/src/mask_view_cmds/tests.rs b/crates/engine/src/mask_view_cmds/tests.rs
index 33e36b142..c73ca0d0a 100644
--- a/crates/engine/src/mask_view_cmds/tests.rs
+++ b/crates/engine/src/mask_view_cmds/tests.rs
@@ -10,7 +10,6 @@ fn session() -> (Session, u64, u64) {
s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();
s.execute("select.rect", json!({"x": 20, "y": 0, "width": 20, "height": 20})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
let st = s.active_mut().unwrap();
st.saved_revision = st.revision;
(s, masked, bg)
diff --git a/crates/engine/src/rasterize_style_cmds/tests.rs b/crates/engine/src/rasterize_style_cmds/tests.rs
index 9b5ef7d35..57db9adba 100644
--- a/crates/engine/src/rasterize_style_cmds/tests.rs
+++ b/crates/engine/src/rasterize_style_cmds/tests.rs
@@ -78,7 +78,6 @@ fn fill_opacity_masks_and_layer_opacity_survive_the_bake() {
s.execute("layer.setProps", json!({"layer": id, "fill": 0.4, "opacity": 0.7})).unwrap();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 34, "height": 48})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
let square = json!({"subpaths": [{"closed": true, "knots": [[4, 4], [60, 4], [60, 26], [4, 26]]}]});
s.execute("layer.vectorMask.add", json!({"layer": id, "path": square})).unwrap();
bake_and_check(&mut s, id, depth);
diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rs
index be0639418..ecc812a51 100644
--- a/crates/engine/src/selection_cmds.rs
+++ b/crates/engine/src/selection_cmds.rs
@@ -877,7 +877,6 @@ mod tests {
let mut s = session();
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
s.execute("view.layerMask", json!({"mode": "gray"})).unwrap();
s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap();
assert_eq!(coverage(&s, 2, 2), 1.0, "gray mask view selects the mask");
diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rs
index ea77e022b..b535fd152 100644
--- a/crates/engine/src/smartselect_cmds.rs
+++ b/crates/engine/src/smartselect_cmds.rs
@@ -1007,7 +1007,6 @@ mod tests {
fn refine_edge_works_on_layer_mask_without_selection() {
let mut s = blurred_edge_selected();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
assert!(s.active().unwrap().doc.selection.is_none());
assert!(s.active().unwrap().doc.layers[0].mask.is_some());
assert!(s.is_enabled("select.refineEdge"));
diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rs
index 4c2a9a907..c11de7c81 100644
--- a/crates/engine/src/vector_cmds/tests.rs
+++ b/crates/engine/src/vector_cmds/tests.rs
@@ -646,7 +646,6 @@ fn rasterizing_a_vector_mask_keeps_what_the_masks_show() {
if let Some((feather, density)) = pm {
s.execute("select.rect", json!({"x": 0, "y": 0, "width": 40, "height": 64})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
s.edit("mask settings", |doc, _| {
let m = doc.layer_mut(id).unwrap().mask.as_mut().unwrap();
(m.feather, m.density) = (feather, density);
diff --git a/crates/engine/tests/layer_mask_depth.rs b/crates/engine/tests/layer_mask_depth.rs
index 082761286..7093164a5 100644
--- a/crates/engine/tests/layer_mask_depth.rs
+++ b/crates/engine/tests/layer_mask_depth.rs
@@ -45,7 +45,12 @@ fn newly_created_layer_masks_follow_document_depth() {
assert_eq!(m.value(20, 20), white);
if selected {
assert_eq!(m.value(0, 0), 0.0);
- assert_eq!(d.doc.selection, before.selection, "the selection is unchanged");
+ if command == "layer.layerMask.revealSelection" {
+ // The selection became the mask: Photoshop drops it, as Hide Selection does.
+ assert!(d.doc.selection.is_none(), "{command}: the selection is dropped");
+ } else {
+ assert_eq!(d.doc.selection, before.selection, "the selection is unchanged");
+ }
} else {
assert_eq!(m.surface.tile_count(), 0, "uniform masks stay sparse");
assert_eq!(m.value(-300_000, 300_000), white);
@@ -133,7 +138,8 @@ fn mask_creation_preserves_selection_coverage_and_existing_masks() {
before.selection.as_ref().unwrap().read_region(before.bounds()),
"{depth}-bit coverage is unchanged"
);
- assert_eq!(after.selection, before.selection);
+ let kept = if command == "layer.layerMask.revealSelection" { None } else { before.selection.clone() };
+ assert_eq!(after.selection, kept, "{command}");
assert_eq!(after.layers[0].mask, before.layers[0].mask, "creating a mask does not requantize an existing mask");
}
}
diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
index 50ff96f08..eda8224fa 100644
--- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs
+++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
@@ -223,7 +223,6 @@ fn dragging_the_mask_thumbnail_to_trash_asks_to_apply_or_delete_it() {
s.execute("layer.layerMask.delete", json!({"layer": masked})).unwrap();
s.execute("select.rect", json!({"x": 100, "y": 0, "width": 100, "height": 100})).unwrap();
s.execute("layer.layerMask.revealSelection", json!({"layer": masked})).unwrap();
- s.execute("select.deselect", json!({})).unwrap();
let mut h = harness(s, 0, 1.0, 290.0);
let drag_mask = |h: &mut Harness<'_, PhotocraftApp>| {
let from = recorded(&h.ctx, masked).unwrap().0.into_iter().find(|(k, _)| *k == MaskKind::Pixel).unwrap().1.center();