diff --git a/crates/ui-egui/src/canvas/paint_mouse.rs b/crates/ui-egui/src/canvas/paint_mouse.rs index a02d8aaeed8..8d2cedf5d15 100644 --- a/crates/ui-egui/src/canvas/paint_mouse.rs +++ b/crates/ui-egui/src/canvas/paint_mouse.rs @@ -170,6 +170,8 @@ pub(crate) fn fresh_brush(tool: Tool, current: &BrushSettings) -> BrushSettings Tool::Eraser => { (brush.size, brush.hardness, brush.spacing) = ERASER_TOOL_TIP; brush.smoothing.amount = 0.0; + // The preset scales size with pressure independently of the options-bar override (#17664). + crate::bg_eraser_popup::set_size_control(&mut brush, crate::bg_eraser_popup::PEN_PRESSURE); } Tool::BackgroundEraser => (brush.size, brush.hardness, brush.spacing) = BACKGROUND_ERASER_TIP, Tool::Healing => (brush.size, brush.hardness, brush.spacing) = HEALING_BRUSH_TIP, @@ -353,3 +355,7 @@ mod pencil_reset_defaults_tests; #[cfg(test)] #[path = "remove_right_click_tests.rs"] mod remove_right_click_tests; + +#[cfg(test)] +#[path = "../tools/eraser_default_pressure_size_tests.rs"] +mod eraser_default_pressure_size_tests; diff --git a/crates/ui-egui/src/tools/eraser_default_pressure_size_tests.rs b/crates/ui-egui/src/tools/eraser_default_pressure_size_tests.rs new file mode 100644 index 00000000000..6c43c599e51 --- /dev/null +++ b/crates/ui-egui/src/tools/eraser_default_pressure_size_tests.rs @@ -0,0 +1,143 @@ +//! #17664: Eraser preset size pressure is independent of opacity, flow and bar overrides. + +use egui::Modifiers; +use photocraft_engine::paint::{BrushSettings, Control, Dynamic, ShapeDynamics}; +use serde_json::{Value, json}; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 160, "height": 160})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.sync_views(); + app +} + +fn invoke(app: &mut PhotocraftApp, command: &str, params: Value) { + let ctx = app.ctx().clone(); + crate::menus::invoke(app, &ctx, command, params).unwrap(); +} + +fn pick(app: &mut PhotocraftApp, tool: Tool) { + app.ui.tool = tool; + crate::paint_mouse::sync_tool_brush(app); +} + +fn assert_preset(brush: &BrushSettings) { + assert_eq!(brush.shape_dynamics, ShapeDynamics { enabled: true, size: Dynamic::controlled(Control::PenPressure), ..ShapeDynamics::default() }); + assert!(!brush.pressure_size, "the bar override is separate from the preset"); + assert!(!brush.transfer.enabled); + assert_eq!(brush.transfer.opacity.control, Control::Off); + assert_eq!((brush.size, brush.hardness, brush.spacing), (13.0, 1.0, 0.25)); + assert_eq!(brush.smoothing.amount, 0.0); +} + +#[test] +fn first_use_and_reset_enable_preset_pressure_without_changing_other_default_values() { + let mut app = app(); + app.session.tools.brush.opacity = 0.6; + app.session.tools.brush.flow = 0.4; + pick(&mut app, Tool::Eraser); + assert_preset(&app.session.tools.brush); + assert_eq!((app.session.tools.brush.opacity, app.session.tools.brush.flow), (0.6, 0.4), "first use keeps today's inherited opacity and flow"); + app.run("tools.setBrush", json!({"brush": {"shapeDynamics": {"enabled": false, "size": {"control": "off", "jitter": 0.7, "minimum": 0.8}}}})).unwrap(); + invoke(&mut app, "tool.reset", json!({})); + assert_preset(&app.session.tools.brush); + assert_eq!((app.session.tools.brush.opacity, app.session.tools.brush.flow), (1.0, 1.0), "reset keeps today's opacity/flow defaults"); +} + +#[test] +fn reset_tool_still_keeps_the_users_bar_toggles() { + for (size, opacity) in [(false, false), (true, false), (false, true), (true, true)] { + let mut app = app(); + pick(&mut app, Tool::Eraser); + app.run("tools.setBrush", json!({"brush": {"pressureSize": size, "pressureOpacity": opacity, "shapeDynamics": {"enabled": false}}})).unwrap(); + invoke(&mut app, "tool.reset", json!({})); + let brush = &app.session.tools.brush; + assert_eq!((brush.pressure_size, brush.pressure_opacity), (size, opacity)); + assert!(brush.shape_dynamics.enabled); + assert_eq!(brush.shape_dynamics.size.control, Control::PenPressure); + } +} + +fn erase(pressure: Option, opacity: f32, flow: f32, pressure_opacity: bool, preset: bool) -> (usize, usize, f32) { + let mut app = app(); + pick(&mut app, Tool::Eraser); + invoke(&mut app, "tool.reset", json!({})); + app.run("tools.setBrush", json!({"brush": {"size": 40, "opacity": opacity, "flow": flow, "pressureOpacity": pressure_opacity}})).unwrap(); + if !preset { + // Full-size baseline, isolated from the preset control as toolbar override tests must be. + app.run("tools.setBrush", json!({"brush": {"shapeDynamics": {"size": {"control": "off"}}}})).unwrap(); + } + assert!(!app.session.tools.brush.pressure_size); + if let Some(pressure) = pressure { + tool_event(&mut app, ToolEvent::Down { x: 80.0, y: 80.0, pressure }, Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 80.0, y: 80.0 }, Modifiers::NONE); + assert!(!app.ui.status_error, "{}", app.ui.status); + } else { + // Omitted pressure is the engine's real pressure-absent mouse point. + app.run("paint.stroke", json!({"points": [[80, 80]], "erase": true})).unwrap(); + } + assert_eq!((app.session.tools.brush.opacity, app.session.tools.brush.flow), (opacity, flow)); + let state = app.session.active().unwrap(); + let surface = state.doc.layer(state.active_layer.unwrap()).unwrap().surface().unwrap(); + let width = (0..160).filter(|&x| surface.rgba(x, 80)[3] < 1.0).count(); + let height = (0..160).filter(|&y| surface.rgba(80, y)[3] < 1.0).count(); + (width, height, surface.rgba(80, 80)[3]) +} + +#[test] +fn a_40px_eraser_tap_has_the_measured_light_pressure_footprints() { + for (pressure, expected) in [(0.3, 14usize), (0.05, 4)] { + let (width, height, _) = erase(Some(pressure), 1.0, 1.0, false, true); + assert!(width.abs_diff(expected) <= 2 && height.abs_diff(expected) <= 2, "pressure={pressure}: {width}x{height}, expected {expected} +/-2"); + } + let full = erase(Some(1.0), 1.0, 1.0, false, true); + let baseline = erase(Some(1.0), 1.0, 1.0, false, false); + assert_eq!(full, baseline, "pressure one retains today's full-size dab"); + assert!(full.0 >= 40 && full.1 >= 40); + assert_eq!(erase(None, 1.0, 1.0, false, true), baseline, "a mouse still erases at the full size"); +} + +#[test] +fn pressure_changes_size_but_not_flow_or_opacity_without_the_opacity_override() { + let light = erase(Some(0.3), 0.5, 0.4, false, true); + let full = erase(Some(1.0), 0.5, 0.4, false, true); + assert!(light.0 < full.0); + assert!((light.2 - full.2).abs() <= 1.0 / 255.0, "unchanged centre alpha: light={}, full={}", light.2, full.2); + let override_on = erase(Some(0.3), 0.5, 0.4, true, true); + assert!(override_on.2 > light.2, "the existing opacity override still reduces erasure under light pressure"); +} + +#[test] +fn saved_user_brushes_are_restored_instead_of_replaced_by_the_new_default() { + let mut app = app(); + let custom = BrushSettings { + size: 71.0, + opacity: 0.3, + flow: 0.2, + shape_dynamics: ShapeDynamics { size: Dynamic::controlled(Control::Fade), ..Default::default() }, + pressure_opacity: true, + ..Default::default() + }; + app.ui.tool_brushes.push((Tool::Eraser, custom.clone())); + pick(&mut app, Tool::Eraser); + let brush = &app.session.tools.brush; + assert_eq!((brush.size, brush.opacity, brush.flow), (custom.size, custom.opacity, custom.flow)); + assert_eq!(brush.shape_dynamics, custom.shape_dynamics); + assert_eq!(brush.pressure_opacity, custom.pressure_opacity); +} + +#[test] +fn neighboring_tools_keep_their_existing_shape_and_transfer_defaults() { + let current = BrushSettings::default(); + for tool in [Tool::BackgroundEraser, Tool::Healing, Tool::CloneStamp] { + let brush = super::fresh_brush(tool, ¤t); + assert_eq!(brush.shape_dynamics, ShapeDynamics::default(), "{tool:?}"); + assert_eq!(brush.transfer, current.transfer, "{tool:?}"); + } +}