diff --git a/crates/ui-egui/src/reset_tip_tests.rs b/crates/ui-egui/src/reset_tip_tests.rs index 80583ec222..815bd3c108 100644 --- a/crates/ui-egui/src/reset_tip_tests.rs +++ b/crates/ui-egui/src/reset_tip_tests.rs @@ -123,9 +123,10 @@ fn each_tool_resets_and_first_uses_its_own_tip() { scramble(&mut a); assert_ne!(live(&a), starting(&a, tool), "{tool:?}: the scrambled tip is not its starting tip"); invoke(&mut a, "tool.reset", json!({})); - // The Eraser's Reset Tool keeps Pressure for Size as it was (#17700). + // The Eraser's Reset Tool keeps Pressure for Size as it was (#17700), and so does the Background + // Eraser's (#18058). let mut want = starting(&a, tool); - want.pressure_size |= tool == Tool::Eraser; + want.pressure_size |= matches!(tool, Tool::Eraser | Tool::BackgroundEraser); assert_eq!(live(&a), want, "{tool:?}: Reset Tool"); // Reset All Tools with the tool active, then with it picked later. diff --git a/crates/ui-egui/src/tools/bg_eraser_reset_pressure_tests.rs b/crates/ui-egui/src/tools/bg_eraser_reset_pressure_tests.rs new file mode 100644 index 0000000000..fe3d3a8717 --- /dev/null +++ b/crates/ui-egui/src/tools/bg_eraser_reset_pressure_tests.rs @@ -0,0 +1,80 @@ +//! #18058: Reset Tool on the Background Eraser resets its tip and its options but leaves Pressure for Size as it +//! was, as measured in the reference application. A first use starts with it off, and Reset All Tools still turns +//! it off. + +use serde_json::json; + +use crate::PhotocraftApp; +use crate::control::{ControlRequest, handle}; +use crate::state::Tool; + +fn app() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.sync_views(); + app +} + +/// Run a menu command as the control channel does, then the frame's sync. +fn invoke(app: &mut PhotocraftApp, id: &str, params: serde_json::Value) { + let ctx = app.ctx().clone(); + let (req, _rx) = ControlRequest::new("ui.menu.invoke", json!({"id": id, "params": params})); + handle(app, &ctx, &req); + app.sync_views(); +} + +/// Pick the Background Eraser, and swap its brush in as a frame does. +fn pick_background_eraser(app: &mut PhotocraftApp) { + app.ui.tool = Tool::BackgroundEraser; + crate::paint_mouse::sync_tool_brush(app); + app.sync_views(); +} + +/// Pressure for Size and Pressure for Opacity, as `brush.get` reports them. +fn pressure(app: &mut PhotocraftApp) -> (bool, bool) { + let b = app.run("brush.get", json!({})).unwrap(); + (b["pressureSize"].as_bool().unwrap(), b["pressureOpacity"].as_bool().unwrap()) +} + +/// The Background Eraser away from its defaults, as the reference was measured (Sampling Background Swatch, Limits +/// Discontiguous, Tolerance 100), with Pressure for Size `on`. +fn changed_eraser(on: bool) -> Box { + let mut app = app(); + pick_background_eraser(&mut app); + app.run("tools.setBrush", json!({"brush": {"size": 57, "pressureSize": on, "pressureOpacity": true}})).unwrap(); + let options = &mut app.ui.tool_options; + options.bg_sampling = "backgroundSwatch".into(); + options.bg_limits = "discontiguous".into(); + options.bg_tolerance = 100.0; + options.bg_tolerance_control = "off".into(); + assert_eq!(pressure(&mut app), (on, true), "set before the reset"); + app +} + +#[test] +fn reset_tool_leaves_pressure_for_size_as_it_was_and_resets_the_rest() { + for on in [true, false] { + let mut app = changed_eraser(on); + invoke(&mut app, "tool.reset", json!({})); + assert_eq!(pressure(&mut app), (on, false), "Pressure for Size stays, the other toggle is reset as before"); + let size = app.run("brush.get", json!({})).unwrap()["size"].as_f64().unwrap(); + assert_eq!(size, 13.0, "the tip is back to its starting size"); + let options = &app.ui.tool_options; + let reset = (options.bg_sampling.as_str(), options.bg_limits.as_str(), options.bg_tolerance, options.bg_tolerance_control.as_str()); + assert_eq!(reset, ("continuous", "contiguous", 50.0, "penPressure"), "the options are back to their defaults"); + } +} + +#[test] +fn a_first_use_of_the_background_eraser_starts_with_pressure_for_size_off() { + let mut app = app(); + pick_background_eraser(&mut app); + assert!(!pressure(&mut app).0); +} + +#[test] +fn reset_all_tools_still_turns_pressure_for_size_off() { + let mut app = changed_eraser(true); + invoke(&mut app, "tool.resetAll", json!({"confirmed": true})); + assert!(!pressure(&mut app).0); +} diff --git a/crates/ui-egui/src/tools/tool_reset.rs b/crates/ui-egui/src/tools/tool_reset.rs index f40ccc6c38..55ea6ef18f 100644 --- a/crates/ui-egui/src/tools/tool_reset.rs +++ b/crates/ui-egui/src/tools/tool_reset.rs @@ -91,13 +91,15 @@ fn reset_tool(app: &mut PhotocraftApp, tool: Tool) { crate::paint_mouse::sync_tool_brush(app); app.ui.tool_brushes.retain(|(t, _)| *t != tool); // The Eraser's Reset Tool resets the tip but keeps Pressure for Size and Pressure for - // Opacity as they were (#17700). + // Opacity as they were (#17700). The Background Eraser's keeps Pressure for Size (#18058). let brush = &app.session.tools.brush; - let pressure = (tool == Tool::Eraser).then_some((brush.pressure_size, brush.pressure_opacity)); + let size = matches!(tool, Tool::Eraser | Tool::BackgroundEraser).then_some(brush.pressure_size); + let opacity = (tool == Tool::Eraser).then_some(brush.pressure_opacity); reset_session_brush(app); - if let Some((size, opacity)) = pressure { - app.session.tools.brush.pressure_size = size; - app.session.tools.brush.pressure_opacity = opacity; + if size.is_some() || opacity.is_some() { + let brush = &mut app.session.tools.brush; + brush.pressure_size = size.unwrap_or(brush.pressure_size); + brush.pressure_opacity = opacity.unwrap_or(brush.pressure_opacity); app.session.tools.brush_changed(); } } @@ -483,3 +485,7 @@ mod patch_sample_all_default_tests; #[cfg(test)] #[path = "eraser_reset_keeps_pressure_tests.rs"] mod eraser_reset_keeps_pressure_tests; + +#[cfg(test)] +#[path = "bg_eraser_reset_pressure_tests.rs"] +mod bg_eraser_reset_pressure_tests;