diff --git a/crates/ui-egui/src/canvas/color_replacement_locked_cursor_tests.rs b/crates/ui-egui/src/canvas/color_replacement_locked_cursor_tests.rs index 242ae33de1..3ac4f4c283 100644 --- a/crates/ui-egui/src/canvas/color_replacement_locked_cursor_tests.rs +++ b/crates/ui-egui/src/canvas/color_replacement_locked_cursor_tests.rs @@ -51,18 +51,17 @@ fn caps_lock_still_gives_the_precise_crosshair_on_an_unlocked_layer() { assert_eq!(cursor(&mut h, 100.0), Some(Cursor::Painted(PaintedCursor::Crosshair))); } +/// Lock Image Pixels and a hidden Lock All layer give the not-allowed cursor since #17788 +/// (`color_replacement_pixel_lock_cursor_tests`). #[test] -fn partial_locks_and_a_hidden_locked_layer_keep_the_tip() { +fn partial_locks_keep_the_tip() { let mut h = pixel_layer(); let tip = cursor(&mut h, 100.0); - for lock in ["pixels", "transparency", "position"] { + for lock in ["transparency", "position"] { h.state_mut().run("layer.lockLayers", json!({ (lock): true })).unwrap(); assert_eq!(cursor(&mut h, 100.0), tip, "{lock}"); h.state_mut().run("layer.lockLayers", json!({ (lock): false })).unwrap(); } - lock_all(&mut h, true); - h.state_mut().run("layer.setProps", json!({"visible": false})).unwrap(); - assert_eq!(cursor(&mut h, 100.0), tip, "hidden Lock All layer"); } #[test] diff --git a/crates/ui-egui/src/canvas/color_replacement_pixel_lock_cursor_tests.rs b/crates/ui-egui/src/canvas/color_replacement_pixel_lock_cursor_tests.rs new file mode 100644 index 0000000000..8b545e0048 --- /dev/null +++ b/crates/ui-egui/src/canvas/color_replacement_pixel_lock_cursor_tests.rs @@ -0,0 +1,148 @@ +//! #17788: the Color Replacement tool shows the not-allowed cursor over a layer with Lock Image +//! Pixels, inside a group with Lock All or Lock Image Pixels or an artboard with Lock All, on that +//! locked group or artboard itself, and on a hidden Lock All layer. Lock Transparent Pixels, Lock +//! Position, the artboard lock and the Background layer keep the tip; Alt keeps the Eyedropper. +//! Driven through real egui input on the real canvas. + +use craft::behavior::{Cursor, CursorIcon, PaintedCursor}; +use craft::ui::runtime::windows::WindowHost; +use egui::Modifiers; +use egui_kittest::Harness; +use serde_json::{Value, json}; + +use crate::PhotocraftApp; +use crate::guide_cursor_tests::{harness, hover}; +use crate::state::Tool; + +type H = Harness<'static, Box>; + +const BLOCKED: Option = Some(Cursor::System(CursorIcon::NotAllowed)); + +/// A red pixel layer with the Color Replacement tool active; returns its id. +fn pixel_layer() -> (H, u64) { + let mut h = harness(Tool::ColorReplacement); + let id = h.state_mut().run("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap(); + h.state_mut().run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + (h, id) +} + +fn run(h: &mut H, id: &str, params: Value) -> Value { + let out = h.state_mut().run(id, params).unwrap(); + h.state_mut().sync_views(); + out +} + +fn cursor_with(h: &mut H, tool: Tool, mods: Modifiers) -> Option { + h.state_mut().ui.tool = tool; + hover(h, 100.0, 150.0, mods); + h.state().canvas_tools.last_response.as_ref().and_then(|response| response.cursor.clone()) +} + +fn cursor(h: &mut H) -> Option { + cursor_with(h, Tool::ColorReplacement, Modifiers::NONE) +} + +fn is_tip(cursor: &Option) -> bool { + matches!(cursor, Some(Cursor::Painted(PaintedCursor::Circle { .. } | PaintedCursor::Outline { .. }))) +} + +fn assert_blocked(h: &mut H, what: &str) { + assert_eq!(cursor(h), BLOCKED, "{what}"); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::NotAllowed, "{what}"); +} + +fn active_layer(h: &H) -> Option { + h.state().session.active().and_then(|state| state.active_layer).map(|id| id.0) +} + +#[test] +fn lock_image_pixels_alone_or_combined_and_back() { + let (mut h, _) = pixel_layer(); + assert!(is_tip(&cursor(&mut h)), "unlocked"); + for locks in [json!({"pixels": true}), json!({"pixels": true, "position": true}), json!({"transparency": true, "pixels": true})] { + run(&mut h, "layer.lockLayers", locks.clone()); + assert_blocked(&mut h, &format!("{locks}")); + run(&mut h, "layer.lockLayers", json!({"transparency": false, "pixels": false, "position": false})); + // The cursor follows the unlock without anything else happening. + assert!(is_tip(&cursor(&mut h)), "unlocked after {locks}"); + } +} + +#[test] +fn a_locked_group_around_the_layer_and_the_group_itself() { + for lock in ["all", "pixels"] { + let (mut h, layer) = pixel_layer(); + let group = run(&mut h, "layer.new.groupFromLayers", json!({"layer": layer}))["layer"].as_u64(); + assert_eq!(active_layer(&h), group, "the new group is active"); + run(&mut h, "layer.lockLayers", json!({ (lock): true })); + assert_blocked(&mut h, &format!("the group with Lock {lock} itself")); + run(&mut h, "layer.select", json!({"layer": layer})); + assert_blocked(&mut h, &format!("a layer in a group with Lock {lock}")); + } +} + +#[test] +fn an_artboard_with_lock_all_and_with_only_its_artboard_lock() { + let (mut h, layer) = pixel_layer(); + let board = run(&mut h, "layer.new.artboardFromLayers", json!({"name": "Board", "background": "transparent"}))["layer"].as_u64(); + let board = board.or_else(|| active_layer(&h)).expect("the artboard's id"); + run(&mut h, "layer.select", json!({"layer": board})); + run(&mut h, "layer.lockLayers", json!({"artboard": true})); + run(&mut h, "layer.select", json!({"layer": layer})); + assert!(is_tip(&cursor(&mut h)), "the artboard lock on the board keeps the tip"); + + run(&mut h, "layer.select", json!({"layer": board})); + run(&mut h, "layer.lockLayers", json!({"artboard": false, "all": true})); + assert_blocked(&mut h, "the artboard with Lock All itself"); + run(&mut h, "layer.select", json!({"layer": layer})); + assert_blocked(&mut h, "a layer on an artboard with Lock All"); +} + +#[test] +fn a_hidden_lock_all_layer() { + let (mut h, _) = pixel_layer(); + run(&mut h, "layer.setProps", json!({"visible": false})); + let hidden = cursor(&mut h); + run(&mut h, "layer.lockLayers", json!({"all": true})); + assert_blocked(&mut h, "hidden Lock All layer"); + run(&mut h, "layer.lockLayers", json!({"all": false})); + assert_eq!(cursor(&mut h), hidden, "hidden, unlocked: as before"); +} + +#[test] +fn partial_locks_and_the_background_keep_the_tip() { + let (mut h, _) = pixel_layer(); + for lock in ["transparency", "position", "artboard"] { + run(&mut h, "layer.lockLayers", json!({ (lock): true })); + assert!(is_tip(&cursor(&mut h)), "Lock {lock}"); + run(&mut h, "layer.lockLayers", json!({ (lock): false })); + } + run(&mut h, "layer.lockLayers", json!({"transparency": true})); + WindowHost::of(&h.ctx).set_caps_lock(Some(true)); + assert_eq!(cursor(&mut h), Some(Cursor::Painted(PaintedCursor::Crosshair)), "Caps Lock on Lock Transparent Pixels"); + + let mut h = harness(Tool::ColorReplacement); + let background = h.state().session.active().and_then(|state| state.doc.background().map(|l| l.id)); + assert!(background.is_some(), "the new document has a Background layer"); + assert!(is_tip(&cursor(&mut h)), "Background layer"); +} + +#[test] +fn alt_over_a_locked_layer_keeps_the_eyedropper() { + let (mut h, _) = pixel_layer(); + let eyedropper = cursor_with(&mut h, Tool::Eyedropper, Modifiers::NONE); + for locks in [json!({"pixels": true}), json!({"all": true})] { + run(&mut h, "layer.lockLayers", locks.clone()); + assert_eq!(cursor_with(&mut h, Tool::ColorReplacement, Modifiers::ALT), eyedropper, "{locks}"); + run(&mut h, "layer.lockLayers", json!({"pixels": false, "all": false})); + } +} + +#[test] +fn other_tools_keep_their_cursors_on_lock_image_pixels() { + let (mut h, _) = pixel_layer(); + let tools = [Tool::Brush, Tool::Eraser, Tool::Pencil, Tool::MixerBrush, Tool::BackgroundEraser]; + let before = tools.map(|tool| cursor_with(&mut h, tool, Modifiers::NONE)); + run(&mut h, "layer.lockLayers", json!({"pixels": true})); + assert_eq!(tools.map(|tool| cursor_with(&mut h, tool, Modifiers::NONE)), before); +} diff --git a/crates/ui-egui/src/canvas/cursor_policy.rs b/crates/ui-egui/src/canvas/cursor_policy.rs index 484b18697a..c90baa8348 100644 --- a/crates/ui-egui/src/canvas/cursor_policy.rs +++ b/crates/ui-egui/src/canvas/cursor_policy.rs @@ -160,7 +160,8 @@ pub(crate) fn alt_loads_mixer(tool: Tool, mods: craft::keymap::Mods) -> bool { } /// Whether the painting tool shows the not-allowed cursor for its current target. The Color -/// Replacement tool shows it on a Lock All pixel layer (#17761) and on a target it refuses (#17784). +/// Replacement tool shows it on a locked layer, group or artboard (#17761, #17788) and on a target +/// it refuses (#17784). pub(super) fn paint_cursor_blocked(app: &PhotocraftApp) -> bool { use photocraft_doc::LayerContent; @@ -192,6 +193,14 @@ pub(super) fn paint_cursor_blocked(app: &PhotocraftApp) -> bool { return true; } } + if tool == Tool::ColorReplacement && matches!(layer.content, LayerContent::Raster(_) | LayerContent::Group(_)) { + // #17788: Lock All on the layer or anything around it, also on a hidden layer, and Lock + // Image Pixels on the shown layer or a group around it (an artboard's was not measured). + // The active group or artboard itself counts as the layer. + let chain = (1..=path.len()).filter_map(|n| state.doc.layer_at(path.get(..n)?)); + let (all, pixels) = chain.fold((false, false), |(all, pixels), l| (all || l.locks.all, pixels || (l.locks.pixels && l.artboard().is_none()))); + return (all || (pixels && state.doc.shown_at(path))) && crate::brush_refusal::pixels_target(&super::paint_target(app)); + } if tool == Tool::MixerBrush { // #11957 checks the active layer's own Lock All for the Mixer Brush. return layer.locks.all; @@ -287,3 +296,7 @@ mod color_replacement_locked_cursor_tests; #[cfg(test)] #[path = "color_replacement_target_cursor_tests.rs"] mod color_replacement_target_cursor_tests; + +#[cfg(test)] +#[path = "color_replacement_pixel_lock_cursor_tests.rs"] +mod color_replacement_pixel_lock_cursor_tests;