From 1429217786e12b5d991ed5d115b88f006cba8a4e Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 27 Nov 2026 16:41:47 +0000 Subject: [PATCH] Show the not-allowed cursor on blocked painting targets (#16815) --- crates/ui-egui/src/canvas/cursor_policy.rs | 28 +++- .../src/canvas/locked_layer_cursor_tests.rs | 138 ++++++++++++++++++ crates/ui-egui/src/canvas/paint_tool.rs | 24 +-- 3 files changed, 178 insertions(+), 12 deletions(-) create mode 100644 crates/ui-egui/src/canvas/locked_layer_cursor_tests.rs diff --git a/crates/ui-egui/src/canvas/cursor_policy.rs b/crates/ui-egui/src/canvas/cursor_policy.rs index 01163ce5a19..cbc336f91b3 100644 --- a/crates/ui-egui/src/canvas/cursor_policy.rs +++ b/crates/ui-egui/src/canvas/cursor_policy.rs @@ -123,9 +123,27 @@ pub(crate) fn alt_loads_mixer(tool: Tool, mods: craft::keymap::Mods) -> bool { mods.alt && !mods.ctrl && tool == Tool::MixerBrush } -/// Whether the Mixer Brush's target layer has Lock All on, so its cursor shows the not-allowed mark. -pub(crate) fn mixer_target_locked(app: &PhotocraftApp) -> bool { - app.session.active().and_then(|st| st.active_layer.and_then(|id| st.doc.layer(id))).is_some_and(|layer| layer.locks.all) +/// Whether the painting tool shows the not-allowed cursor for its current target. +pub(super) fn paint_cursor_blocked(app: &PhotocraftApp) -> bool { + use photocraft_doc::LayerContent; + + let tool = app.ui.tool; + if !matches!(tool, Tool::Brush | Tool::Eraser | Tool::PaintBucket | Tool::MixerBrush) { + return false; + } + let Some(state) = app.session.active() else { return false }; + let Some(path) = state.active_layer.and_then(|id| state.doc.layers.index().path(id)) else { return false }; + let Some(layer) = state.doc.layer_at(path) else { return false }; + if tool == Tool::MixerBrush { + // #11957 checks the active layer's own Lock All for the Mixer Brush. + return layer.locks.all; + } + let blocked = match &layer.content { + LayerContent::Raster(_) => state.doc.locks_at(path).all, + LayerContent::Text(_) => matches!(tool, Tool::Brush | Tool::PaintBucket), + _ => false, + }; + blocked && state.doc.shown_at(path) && crate::brush_refusal::pixels_target(&super::paint_target(app)) } #[cfg(test)] @@ -195,3 +213,7 @@ mod custom_shape_alt_eyedropper_tests; #[cfg(test)] #[path = "magic_eraser_cursor_tests.rs"] mod magic_eraser_cursor_tests; + +#[cfg(test)] +#[path = "locked_layer_cursor_tests.rs"] +mod locked_layer_cursor_tests; diff --git a/crates/ui-egui/src/canvas/locked_layer_cursor_tests.rs b/crates/ui-egui/src/canvas/locked_layer_cursor_tests.rs new file mode 100644 index 00000000000..664423ff9bf --- /dev/null +++ b/crates/ui-egui/src/canvas/locked_layer_cursor_tests.rs @@ -0,0 +1,138 @@ +//! #16815: show the system cursor for painting tools on the blocked layer targets measured in the issue. + +use craft::behavior::{Cursor, CursorIcon}; +use egui::{Modifiers, PointerButton}; +use egui_kittest::Harness; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::dialogs::{alerts, rasterize_prompt::Rasterize}; +use crate::guide_cursor_tests::{harness, hover, screen}; +use crate::state::Tool; + +const IN_IMAGE: f32 = 100.0; +const ON_PASTEBOARD: f32 = -40.0; +const TOOLS: [Tool; 3] = [Tool::Brush, Tool::Eraser, Tool::PaintBucket]; +const BLOCKED: Option = Some(Cursor::System(CursorIcon::NotAllowed)); + +fn pixel_layer() -> Harness<'static, Box> { + let mut h = harness(Tool::Brush); + h.state_mut().run("layer.new.layer", json!({})).unwrap(); + h.state_mut().run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + h +} + +fn cursor_at(h: &mut Harness<'static, Box>, tool: Tool, x: f32, mods: Modifiers) -> Option { + h.state_mut().ui.tool = tool; + hover(h, x, 150.0, mods); + h.state().canvas_tools.last_response.as_ref().and_then(|response| response.cursor.clone()) +} + +fn image_cursor(h: &mut Harness<'static, Box>, tool: Tool) -> Option { + cursor_at(h, tool, IN_IMAGE, Modifiers::NONE) +} + +fn click_image(h: &mut Harness<'static, Box>) { + let pos = screen(h, IN_IMAGE, 150.0); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.run_steps(2); + } +} + +#[test] +fn lock_all_shows_the_blocked_cursor_on_the_image_and_pasteboard() { + let mut h = pixel_layer(); + let normal = TOOLS.map(|tool| image_cursor(&mut h, tool)); + for (tool, cursor) in TOOLS.into_iter().zip(&normal) { + assert!(cursor.is_some() && *cursor != BLOCKED, "{tool:?} has a normal cursor before locking"); + } + let shift_bucket = cursor_at(&mut h, Tool::PaintBucket, ON_PASTEBOARD, Modifiers::SHIFT); + h.state_mut().run("layer.lockLayers", json!({"all": true})).unwrap(); + for tool in TOOLS { + for x in [IN_IMAGE, ON_PASTEBOARD] { + assert_eq!(cursor_at(&mut h, tool, x, Modifiers::NONE), BLOCKED, "{tool:?} at x={x}"); + assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::NotAllowed); + } + } + assert_eq!(cursor_at(&mut h, Tool::PaintBucket, ON_PASTEBOARD, Modifiers::SHIFT), shift_bucket, "Shift changes the pasteboard colour"); + h.state_mut().run("layer.lockLayers", json!({"all": false})).unwrap(); + assert_eq!(TOOLS.map(|tool| image_cursor(&mut h, tool)), normal); +} + +#[test] +fn a_type_layer_blocks_brush_and_bucket_and_their_clicks_still_ask_to_rasterize() { + for tool in [Tool::Brush, Tool::PaintBucket] { + let mut h = pixel_layer(); + h.state_mut().run("type.create", json!({"x": 40, "y": 60, "text": "Type"})).unwrap(); + let cursor = image_cursor(&mut h, tool); + let before = h.state().session.active().unwrap().history.past_len(); + click_image(&mut h); + assert!(alerts::prompt_tagged::(h.state()).is_some(), "{tool:?}: Rasterize prompt"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), before, "{tool:?}"); + assert_eq!(cursor, BLOCKED, "{tool:?}"); + } + let mut h = pixel_layer(); + let eraser = image_cursor(&mut h, Tool::Eraser); + h.state_mut().run("type.create", json!({"x": 40, "y": 60, "text": "Type"})).unwrap(); + assert_eq!(image_cursor(&mut h, Tool::Eraser), eraser); +} + +#[test] +fn locked_pixel_clicks_keep_their_refusal_and_history() { + for tool in TOOLS { + let mut h = pixel_layer(); + h.state_mut().run("layer.lockLayers", json!({"all": true})).unwrap(); + image_cursor(&mut h, tool); + let before = h.state().session.active().unwrap().history.past_len(); + click_image(&mut h); + let app = h.state(); + let dialogs = app.framework.dialogs.open_dialogs(); + let alert = dialogs.iter().any(|dialog| { + dialog + .fields + .as_ref() + .and_then(|fields| fields.get("message")) + .and_then(|message| message.as_str()) + .is_some_and(|message| message.contains("layer is locked")) + }); + assert!(alert || (app.ui.status_error && app.ui.status.contains("locked")), "{tool:?}: {}", app.ui.status); + assert_eq!(app.session.active().unwrap().history.past_len(), before, "{tool:?}"); + } +} + +#[test] +fn hidden_layers_partial_locks_and_groups_keep_each_tools_normal_cursor() { + let mut h = pixel_layer(); + let normal = TOOLS.map(|tool| image_cursor(&mut h, tool)); + for lock in ["pixels", "transparency", "position"] { + h.state_mut().run("layer.lockLayers", json!({ (lock): true })).unwrap(); + assert_eq!(TOOLS.map(|tool| image_cursor(&mut h, tool)), normal, "{lock}"); + h.state_mut().run("layer.lockLayers", json!({ (lock): false })).unwrap(); + } + // Test hiding a fully locked layer, so the hidden and locked rules compete. + h.state_mut().run("layer.lockLayers", json!({"all": true})).unwrap(); + h.state_mut().run("layer.setProps", json!({"visible": false})).unwrap(); + assert_eq!(TOOLS.map(|tool| image_cursor(&mut h, tool)), normal, "hidden Lock All layer"); + + h.state_mut().run("layer.new.group", json!({})).unwrap(); + h.state_mut().run("layer.lockLayers", json!({"all": true})).unwrap(); + assert_eq!(TOOLS.map(|tool| image_cursor(&mut h, tool)), normal, "locked group itself"); +} + +#[test] +fn locked_layers_keep_nonpainting_cursors_and_the_stand_ins() { + let mut h = pixel_layer(); + let tools = [Tool::RectMarquee, Tool::Move, Tool::Pencil]; + let expected = tools.map(|tool| image_cursor(&mut h, tool)); + let eyedropper = image_cursor(&mut h, Tool::Eyedropper); + let quick_mask = image_cursor(&mut h, Tool::Brush); + h.state_mut().run("layer.lockLayers", json!({"all": true})).unwrap(); + assert_eq!(tools.map(|tool| image_cursor(&mut h, tool)), expected); + assert_eq!(image_cursor(&mut h, Tool::MixerBrush), BLOCKED); + assert_eq!(cursor_at(&mut h, Tool::Brush, IN_IMAGE, Modifiers::ALT), eyedropper); + assert_eq!(cursor_at(&mut h, Tool::Brush, IN_IMAGE, Modifiers::COMMAND), expected[1]); + + h.state_mut().run("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + assert_eq!(image_cursor(&mut h, Tool::Brush), quick_mask); +} diff --git a/crates/ui-egui/src/canvas/paint_tool.rs b/crates/ui-egui/src/canvas/paint_tool.rs index 49455feaf0c..5cedcc3f72b 100644 --- a/crates/ui-egui/src/canvas/paint_tool.rs +++ b/crates/ui-egui/src/canvas/paint_tool.rs @@ -16,7 +16,7 @@ use craft::keymap::{Chord, Key, Mods}; use craft::ui::canvas::{CanvasTool, ToolAction, ToolClaims, ToolContext, ToolHostResponse, ToolKey, ToolPath, ToolPointer}; use craft::ui::primitives::Draw; -use super::cursor_policy::{alt_loads_mixer, alt_samples, click_tool_cursor, mixer_target_locked, sample_target}; +use super::cursor_policy::{alt_loads_mixer, alt_samples, click_tool_cursor, paint_cursor_blocked, sample_target}; use super::document_display::CanvasHost; use super::PhotocraftApp; @@ -225,18 +225,22 @@ fn click_tool(app: &mut PhotocraftApp, tool: Tool, at: [f64; 2]) -> bool { } } +fn bucket_targets_pasteboard(app: &PhotocraftApp, at: [f64; 2], mods: Mods) -> bool { + let plain_shift = mods.shift && !mods.alt && !mods.ctrl && !mods.cmd; + let Some(st) = app.session.active() else { return false }; + let (w, h) = (f64::from(st.doc.size.width), f64::from(st.doc.size.height)); + let (x, y) = (at[0], at[1]); + let outside = x.is_finite() && y.is_finite() && (x < 0.0 || y < 0.0 || x >= w || y >= h); + plain_shift && outside && st.doc.selection.is_none() +} + /// Paint Bucket ⇧-click on the pasteboard sets the pasteboard colour to the foreground colour, as /// in the reference application (#12534): the custom colour, and use it, as the pasteboard menu's /// Select Custom Color… does. It is a preference, so no history state and no document change. /// Only a plain ⇧ with no selection: other modifiers and an active selection were not measured and /// keep today's fill. True when it handled the click. pub(crate) fn bucket_pasteboard_click(app: &mut PhotocraftApp, at: [f64; 2], mods: Mods) -> bool { - let plain_shift = mods.shift && !mods.alt && !mods.ctrl && !mods.cmd; - let Some(st) = app.session.active() else { return false }; - let (w, h) = (f64::from(st.doc.size.width), f64::from(st.doc.size.height)); - let (x, y) = (at[0], at[1]); - let outside = x.is_finite() && y.is_finite() && (x < 0.0 || y < 0.0 || x >= w || y >= h); - if !plain_shift || !outside || st.doc.selection.is_some() { + if !bucket_targets_pasteboard(app, at, mods) { return false; } let [r, g, b, _] = app.session.tools.foreground; @@ -489,8 +493,10 @@ impl CanvasTool for PaintTool<'_> { if alt_loads_mixer(tool, cx.mods) && !cx.mods.cmd { return if cx.precise() { Cursor::Painted(PaintedCursor::Crosshair) } else { sample_target() }; } - // Over a layer it can't paint (Lock All), the Mixer Brush shows a no-entry sign (#11957). - if tool == Tool::MixerBrush && mixer_target_locked(app) { + let sets_pasteboard = tool == Tool::PaintBucket + && cx.mods.shift + && cx.pointer.as_ref().and_then(|pointer| doc_point(cx, pointer.pos)).is_some_and(|at| bucket_targets_pasteboard(app, at, cx.mods)); + if !sets_pasteboard && paint_cursor_blocked(app) { return Cursor::System(CursorIcon::NotAllowed); } // The Paint Bucket and the Magic Eraser act on a click: their own pointers, no brush tip.