PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Layers: Fill and the Paint Bucket edit an adjustment layer's mask when its icon is targeted (#4298) (#4308)
Dan Hiorns908c47021f1b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex d02891826..362eef2a3 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -5450,7 +5450,8 @@ pub fn commit_crop(app: &mut PhotocraftApp) {}}-/// "mask" when the Layers panel targets (or the canvas shows) the active layer's mask, else "pixels".+/// "mask" when the Layers panel targets (or the canvas shows) the active layer's mask, or the layer+/// is an adjustment layer with one (it has no pixels: #4298), else "pixels".pub fn paint_target(app: &PhotocraftApp) -> serde_json::Value {use photocraft_engine::channel_cmds::ChannelTarget;let Some(st) = app.session.active() else { return json!("pixels") };@@ -5464,7 +5465,8 @@ pub fn paint_target(app: &PhotocraftApp) -> serde_json::Value {let has_mask = st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.mask.is_some());// Viewing the mask (⌥-click its thumbnail, #196) paints the mask.let viewing = photocraft_engine::mask_view_cmds::current(st).is_some();- json!(if (app.ui.mask_target || viewing) && has_mask { "mask" } else { "pixels" })+ let mask = app.ui.mask_target || viewing || crate::adjustment_layer_active(st);+ json!(if mask && has_mask { "mask" } else { "pixels" })}#[cfg(test)]@@ -6306,6 +6308,41 @@ mod tests {assert!(app.session.tools.foreground[1] > 0.99 && app.session.tools.foreground[0] < 0.01);}+ /// #4298: an adjustment layer has no pixels, so after its icon thumbnail is clicked (the+ /// Layers panel no longer targets the mask) Fill, the fill keys and the Paint Bucket still+ /// edit its mask, as in Photoshop, instead of failing with "not a pixel layer".+ #[test]+ fn fills_on_an_adjustment_layer_edit_its_mask_when_the_icon_is_targeted() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());+ app.run("file.new", json!({"width": 40, "height": 20, "background": "white"})).unwrap();+ app.run("layer.newAdjustmentLayer.curves", json!({})).unwrap();+ // Click the Curves icon thumbnail: the panel targets the layer, not its mask.+ app.ui.mask_target = false;+ app.sync_mask_targets();+ assert!(!app.ui.mask_target, "the panel's target is the icon");+ let mask_at = |app: &PhotocraftApp| {+ let st = app.session.active().unwrap();+ st.doc.layer(st.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface.sample_channel(10, 10, 0)+ };+ assert_eq!(paint_target(&app), json!("mask"));+ assert!(crate::menus::is_enabled(&app, "edit.fill"), "Edit > Fill (⇧F5) is enabled");+ assert!(crate::menus::is_enabled(&app, "edit.fillForeground"));+ app.ui.tool = Tool::PaintBucket;+ app.run("tools.setColors", json!({"foreground": [0.0, 0.0, 0.0, 1.0], "background": [1.0, 1.0, 1.0, 1.0]})).unwrap();+ tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, egui::Modifiers::NONE);+ tool_event(&mut app, ToolEvent::Up { x: 10.0, y: 10.0 }, egui::Modifiers::NONE);+ assert_eq!(mask_at(&app), 0.0, "the Paint Bucket filled the mask black");+ app.run("edit.fill", json!({"contents": "white"})).unwrap();+ assert_eq!(mask_at(&app), 1.0, "Edit > Fill filled the mask white");+ assert!(!app.ui.mask_target, "filling doesn't move the panel's target");+ // A pixel layer with an untargeted mask still paints its pixels.+ app.run("layer.new.layer", json!({})).unwrap();+ app.run("layer.layerMask.revealAll", json!({})).unwrap();+ app.ui.mask_target = false;+ assert_eq!(paint_target(&app), json!("pixels"));+ assert!(!app.layer_mask_targeted());+ }+#[test]fn alt_with_a_painting_tool_samples_the_foreground_instead_of_painting() {let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex a71d8134a..bc219f5ff 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1810,6 +1810,13 @@ fn read_dropped(f: &(dyn egui::DroppedFile + Send + Sync)) -> Result<Vec<u8>, Stf.bytes()}+/// The active layer is an adjustment layer. It has no pixels, so painting and filling it edit its+/// mask even when its icon thumbnail, not the mask, is the Layers panel's target, as in+/// Photoshop (#4298).+pub(crate) fn adjustment_layer_active(st: &photocraft_engine::DocState) -> bool {+ st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| matches!(l.content, photocraft_doc::LayerContent::Adjustment(_)))+}+/// On the web, dropped-file bytes arrive asynchronously; the web shell handles drops itself.#[cfg(target_arch = "wasm32")]fn read_dropped(_f: &dyn egui::DroppedFile) -> Result<Vec<u8>, String> {@@ -1834,9 +1841,9 @@ impl PhotocraftApp {}}- /// The active layer's mask is what pixel commands edit: the Layers panel targets it, or every- /// colour channel is off and that mask is the picture. An alpha channel or Quick Mask wins,- /// as the engine routes those itself.+ /// The active layer's mask is what pixel commands edit: the Layers panel targets it, the layer+ /// is an adjustment layer (no pixels of its own), or every colour channel is off and that mask+ /// is the picture. An alpha channel or Quick Mask wins, as the engine routes those itself.pub fn layer_mask_targeted(&self) -> bool {let Some(st) = self.session.active() else { return false };let layer_mask = st.channel_view.target == photocraft_engine::channel_cmds::ChannelTarget::LayerMask;@@ -1845,7 +1852,7 @@ impl PhotocraftApp {if !composite || !has_mask {return false;}- if self.ui.mask_target || layer_mask {+ if self.ui.mask_target || layer_mask || adjustment_layer_active(st) {return true;}let colors_off = st.channel_view.visible_colors(photocraft_engine::channel_cmds::color_count(&st.doc)) == 0;diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rsindex ad0e70ca8..e18cb0e9a 100644--- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs+++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs@@ -629,12 +629,13 @@ fn command_i_inverts_the_targeted_mask() {let mask = &l.mask.as_ref().unwrap().surface;assert_eq!((mask.sample_channel(50, 50, 0), mask.sample_channel(150, 10, 0)), (1.0, 0.0), "the mask inverted");assert!(l.content == pixels, "the layer's pixels are untouched");- // An adjustment layer's mask: Invert is enabled only through the mask target.+ // An adjustment layer's mask: with no pixels of its own, Invert edits the mask whether the+ // panel targets the mask or the icon thumbnail (#4298).let app = h.state_mut();app.run("layer.newAdjustmentLayer.curves", json!({})).unwrap();app.run("layer.layerMask.revealAll", json!({})).unwrap();app.ui.mask_target = false;- assert!(!crate::menus::is_enabled(app, "image.adjustments.invert"));+ assert!(crate::menus::is_enabled(app, "image.adjustments.invert"));app.ui.mask_target = true;assert!(crate::menus::is_enabled(app, "image.adjustments.invert"));app.run("image.adjustments.invert", json!({})).unwrap();