From 7e43e330c4c73b29e5fbfac99809192651db9a98 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 04:04:03 +0100 Subject: [PATCH] Color Replacement: size follows pen pressure from the first use and after Reset Tool (#17785) A first pick of the tool now starts its "Size:" control at Pen Pressure, as Reset Tool already did (#11931), so a pen stroke is the tip size times the pressure. Tests for both and for the stroke width at 0.25, 0.5 and full pressure; the Brush tool keeps its own default. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas/paint_mouse.rs | 9 +- ...replacement_size_pressure_default_tests.rs | 90 +++++++++++++++++++ crates/ui-egui/src/lib.rs | 2 + 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/color_replacement_size_pressure_default_tests.rs diff --git a/crates/ui-egui/src/canvas/paint_mouse.rs b/crates/ui-egui/src/canvas/paint_mouse.rs index 56ed8e03bc..59fbc29aac 100644 --- a/crates/ui-egui/src/canvas/paint_mouse.rs +++ b/crates/ui-egui/src/canvas/paint_mouse.rs @@ -82,7 +82,12 @@ pub fn sync_tool_brush(app: &mut PhotocraftApp) { }; // The Remove tool starts at its measured 19 px; the rest is the current brush (#12548). let size = if tool == Tool::Remove { REMOVE_TOOL_SIZE } else { current.size }; - BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, opacity, flow, size, ..current } + let mut b = BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, opacity, flow, size, ..current }; + // Color Replacement's size follows pen pressure from its first use, as after Reset Tool (#17785). + if tool == Tool::ColorReplacement { + crate::bg_eraser_popup::set_size_control(&mut b, crate::bg_eraser_popup::PEN_PRESSURE); + } + b } }; // The picked preset was the outgoing tool's brush: forget it, so Update Brush can't overwrite @@ -173,7 +178,7 @@ pub(crate) fn fresh_brush(tool: Tool, current: &BrushSettings) -> BrushSettings Tool::Smudge => (brush.size, brush.hardness, brush.spacing) = SMUDGE_TOOL_TIP, Tool::ColorReplacement => { (brush.size, brush.hardness, brush.spacing) = COLOR_REPLACEMENT_TIP; - // Its popup's "Size:" starts at Pen Pressure (#11931). + // Its popup's "Size:" starts at Pen Pressure, so strokes thin with lighter pressure (#11931, #17785). crate::bg_eraser_popup::set_size_control(&mut brush, crate::bg_eraser_popup::PEN_PRESSURE); } Tool::CloneStamp => { diff --git a/crates/ui-egui/src/color_replacement_size_pressure_default_tests.rs b/crates/ui-egui/src/color_replacement_size_pressure_default_tests.rs new file mode 100644 index 0000000000..728444227d --- /dev/null +++ b/crates/ui-egui/src/color_replacement_size_pressure_default_tests.rs @@ -0,0 +1,90 @@ +//! #17785: the Color Replacement tool's size follows pen pressure out of the box and after Reset Tool, as in the +//! reference application: a pen stroke's width is the tip size times the pressure (41 px at 0.5 -> about 23 rows, at +//! 0.25 about 11), the mouse paints at full size. The Brush tool keeps its own default, which ignores pressure. + +use egui::Modifiers; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::bg_eraser_popup::{PEN_PRESSURE, size_control}; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +/// A 200×100 red document, foreground green, with `tool` picked and reset, then its tip set to 41 px with no +/// smoothing (the rest of the reset brush kept). +fn app(tool: Tool) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 100, "background": "#ff0000"})).unwrap(); + app.run("tools.setColors", json!({"foreground": "#00ff00", "background": "#0000ff"})).unwrap(); + app.ui.tool = tool; + crate::paint_mouse::sync_tool_brush(&mut app); + reset(&mut app); + app.run("tools.setBrush", json!({"brush": {"size": 41, "smoothing": {"amount": 0.0}}})).unwrap(); + app +} + +fn reset(app: &mut PhotocraftApp) { + let ctx = app.ctx().clone(); + crate::menus::invoke(app, &ctx, crate::tools::tool_reset::RESET, json!({})).unwrap(); +} + +/// A horizontal stroke across the middle at a constant `pressure` (1.0 is what the mouse sends). +fn stroke(app: &mut PhotocraftApp, pressure: f32) { + let m = Modifiers::NONE; + tool_event(app, ToolEvent::Down { x: 30.0, y: 50.0, pressure }, m); + for x in (40..=170).step_by(10) { + tool_event(app, ToolEvent::Move { x: f64::from(x), y: 50.0, pressure }, m); + } + tool_event(app, ToolEvent::Up { x: 170.0, y: 50.0 }, m); + assert!(!app.ui.status_error, "{}", app.ui.status); +} + +/// How many rows of the column in the stroke's middle no longer hold the document's red. +fn rows_changed(app: &PhotocraftApp) -> usize { + let st = app.session.active().unwrap(); + let surface = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + (0..100).filter(|&y| (surface.rgba(100, y)[1]) > 0.02).count() +} + +fn width_at(tool: Tool, pressure: f32) -> usize { + let mut app = app(tool); + stroke(&mut app, pressure); + rows_changed(&app) +} + +#[test] +fn a_fresh_tool_and_reset_tool_put_size_on_pen_pressure() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 50, "height": 50})).unwrap(); + app.ui.tool = Tool::ColorReplacement; + crate::paint_mouse::sync_tool_brush(&mut app); + assert_eq!(size_control(&app.session.tools.brush), PEN_PRESSURE, "a fresh tool"); + // Turned off, Reset Tool puts it back on Pen Pressure. + let mut brush = app.session.tools.brush.clone(); + crate::bg_eraser_popup::set_size_control(&mut brush, crate::bg_eraser_popup::OFF); + app.session.tools.brush = brush; + app.session.tools.brush_changed(); + assert_ne!(size_control(&app.session.tools.brush), PEN_PRESSURE); + reset(&mut app); + assert_eq!(size_control(&app.session.tools.brush), PEN_PRESSURE, "after Reset Tool"); +} + +#[test] +fn a_pen_stroke_is_the_tip_size_times_the_pressure() { + let (quarter, half, mouse) = (width_at(Tool::ColorReplacement, 0.25), width_at(Tool::ColorReplacement, 0.5), width_at(Tool::ColorReplacement, 1.0)); + // The reference: 41 px at 0.25 -> 11 rows, 0.5 -> 23, the mouse 43. + assert!(quarter.abs_diff(11) <= 2, "0.25 pressure paints about 11 rows, got {quarter}"); + assert!(half.abs_diff(23) <= 2, "0.5 pressure paints about 23 rows, got {half}"); + assert!(mouse.abs_diff(43) <= 3, "the mouse paints the full tip, got {mouse}"); + assert!(quarter < half && half < mouse, "lighter pressure paints thinner: {quarter} < {half} < {mouse}"); +} + +#[test] +fn the_brush_tool_keeps_a_full_size_stroke_at_low_pressure() { + let mut app = app(Tool::Brush); + assert_ne!(size_control(&app.session.tools.brush), PEN_PRESSURE, "the Brush tool's size is not on Pen Pressure"); + assert!(!app.session.tools.brush.pressure_size); + stroke(&mut app, 0.25); + let light = rows_changed(&app); + assert!(light >= width_at(Tool::Brush, 1.0) - 2, "the Brush tool ignores pressure by default: {light} rows at 0.25"); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 48a22a45c1..6e58a53b39 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -874,6 +874,8 @@ mod pen_variants_tests; #[cfg(test)] mod color_replacement_digit_keys_tests; #[cfg(test)] +mod color_replacement_size_pressure_default_tests; +#[cfg(test)] mod color_replacement_tests; #[cfg(test)] mod eyedropper_shift_sampler_tests;