Skip to content

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

Let Edit > Fill and the fill keys fill an adjustment layer's mask (#2456) (#2471)

MarekSpodBiedry10571be5e280
diff --git a/crates/engine/src/adjust_cmds/tests.rs b/crates/engine/src/adjust_cmds/tests.rs
index 7c23b545e..9f8f07db6 100644
--- a/crates/engine/src/adjust_cmds/tests.rs
+++ b/crates/engine/src/adjust_cmds/tests.rs
@@ -329,6 +329,22 @@ fn mask_at(s: &Session, x: i32, y: i32) -> f32 {
d.doc.layer(d.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0)
}
+/// #2456: Edit > Fill (and the fill keys) fill the targeted mask of an adjustment layer.
+#[test]
+fn fill_works_on_an_adjustment_layers_mask() {
+ let mut s = session(8, "rgb");
+ s.execute("layer.newAdjustmentLayer.curves", json!({})).unwrap();
+ s.execute("layer.layerMask.revealAll", json!({})).unwrap();
+ let mask = json!({"target": "mask"});
+ assert!(!s.is_enabled("edit.fill"), "no pixels to fill without the mask target");
+ assert!(s.is_enabled_with("edit.fill", &mask));
+ assert!(s.is_enabled_with("edit.fillForeground", &mask));
+ s.execute("edit.fill", json!({"contents": "black", "target": "mask"})).unwrap();
+ assert_eq!(mask_at(&s, 3, 3), 0.0, "the mask is filled black");
+ s.execute("edit.fill", json!({"contents": "white", "target": "mask"})).unwrap();
+ assert_eq!(mask_at(&s, 3, 3), 1.0);
+}
+
/// #780: with the layer mask targeted, Invert (⌘I) inverts the mask, past the canvas too, and
/// leaves the layer's pixels alone; one history step.
#[test]
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs
index 5ced9899f..e99ac9cbe 100644
--- a/crates/engine/src/channel_cmds.rs
+++ b/crates/engine/src/channel_cmds.rs
@@ -533,12 +533,15 @@ pub(crate) fn channel_surface_for_filter<'a>(doc: &'a mut Document, layer: Optio
Ok(Some(surf))
}
-/// Commands that edit a targeted layer mask in place of the layer: the Image › Adjustments table
-/// and the filters. A mask target enables them on any layer with a mask, so ⌘I inverts an
-/// adjustment layer's mask as in Photoshop (#780).
+/// Commands that edit a targeted layer mask in place of the layer: the Image › Adjustments table,
+/// the filters and the fills (Edit › Fill and its keys). A mask target enables them on any layer
+/// with a mask, so ⌘I inverts an adjustment layer's mask (#780) and Fill fills it (#2456) as in
+/// Photoshop.
fn edits_mask(id: &str) -> bool {
id.strip_prefix("image.adjustments.").is_some_and(|kind| crate::adjust_params::default_for(kind, ColorMode::Rgb).is_ok())
|| crate::filters::params_for(id, &Value::Null).is_some()
+ || id == "edit.fill"
+ || crate::fill_key_cmds::IDS.contains(&id)
}
/// The precondition of `id` when `p` targets the active layer's mask (`"target":"mask"`): the
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index e21123731..fad98f923 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -393,7 +393,7 @@ fn build() -> Vec<CommandSpec> {
"Fill…",
["Edit"],
Some("Shift+F5"),
- r##"{"contents":"foreground|background|color|contentAware|pattern|history|black|gray|white"="color","color":"#rrggbb|[r,g,b,a]"=foreground (contents=color),"pattern":id|name (contents=pattern),"scale":%=100,"angle":deg,"state":index? (contents=history; default the oldest state),"colorAdaptation":bool=true (contents=contentAware),"mode":"normal|multiply|…"="normal","opacity":0..100=100,"preserveTransparency":bool=false,"target":"pixels"|{"channel":i}|"quickMask"?}"##,
+ r##"{"contents":"foreground|background|color|contentAware|pattern|history|black|gray|white"="color","color":"#rrggbb|[r,g,b,a]"=foreground (contents=color),"pattern":id|name (contents=pattern),"scale":%=100,"angle":deg,"state":index? (contents=history; default the oldest state),"colorAdaptation":bool=true (contents=contentAware),"mode":"normal|multiply|…"="normal","opacity":0..100=100,"preserveTransparency":bool=false,"target":"pixels"|"mask"|"quickMask"|{"channel":i}?}"##,
has_pixel_or_channel,
crate::fill_cmds::fill
),
diff --git a/crates/engine/src/fill_key_cmds.rs b/crates/engine/src/fill_key_cmds.rs
index ed775e3fd..6235b629b 100644
--- a/crates/engine/src/fill_key_cmds.rs
+++ b/crates/engine/src/fill_key_cmds.rs
@@ -14,7 +14,7 @@ use crate::{EngineError, Result, Session};
/// The fill-key command ids (routed to the Channels panel target like `edit.fill`).
pub const IDS: [&str; 4] = ["edit.fillForeground", "edit.fillBackground", "edit.fillForegroundPreserve", "edit.fillBackgroundPreserve"];
-const PARAMS: &str = r##"{"layer":id?,"target":"pixels"|{"channel":i}|"quickMask"?}"##;
+const PARAMS: &str = r##"{"layer":id?,"target":"pixels"|"mask"|"quickMask"|{"channel":i}?}"##;
fn enabled(s: &Session) -> std::result::Result<(), String> {
let edit_fill = crate::commands::find("edit.fill").ok_or_else(|| "Fill is unavailable".to_string())?;