From 34d0a4dbc014bf83e4173fd609d1e8fe3ae1cd59 Mon Sep 17 00:00:00 2001 From: kubuntugamer Date: Mon, 30 Nov 2026 00:27:21 -0600 Subject: [PATCH] Background Eraser: show the Opacity row in the resize readout The reference's Alt+right-drag display has a third row, "Opacity : 50 %", after Diameter and Hardness for the Background Eraser only. It stays at 50 % through the drag and for other Tolerance values (not measured); every other painting tool keeps the framework's two rows. Fixes #18053 --- crates/ui-egui/src/canvas/paint_tool.rs | 2 +- crates/ui-egui/src/tools/brush_resize.rs | 23 +++- ...ize_background_eraser_opacity_row_tests.rs | 116 ++++++++++++++++++ 3 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 crates/ui-egui/src/tools/brush_resize_background_eraser_opacity_row_tests.rs diff --git a/crates/ui-egui/src/canvas/paint_tool.rs b/crates/ui-egui/src/canvas/paint_tool.rs index d3ce9b3552..0d7ba055f5 100644 --- a/crates/ui-egui/src/canvas/paint_tool.rs +++ b/crates/ui-egui/src/canvas/paint_tool.rs @@ -373,7 +373,7 @@ impl CanvasTool for PaintTool<'_> { scope.with_app(|app| { crate::brush_cursor::sync_tip(app); // The readout request lasts one pass: a pass without pointer input repeats it. - app.brush_resize.hud(cx); + app.brush_resize.hud(app, cx); let Some(stroke) = state.stroke.as_ref() else { return }; let stale_mixer = stroke.tool == Tool::MixerBrush && app.live_stroke.is_some() && app.session.active_index().is_none_or(|idx| live_stroke(app, idx).is_none()); diff --git a/crates/ui-egui/src/tools/brush_resize.rs b/crates/ui-egui/src/tools/brush_resize.rs index d5c19b96fd..ec6dfff9e4 100644 --- a/crates/ui-egui/src/tools/brush_resize.rs +++ b/crates/ui-egui/src/tools/brush_resize.rs @@ -29,6 +29,10 @@ const HARDNESS_TRAVEL: f32 = 400.0; const OPACITY_TRAVEL: f32 = 200.0; /// The overlay alpha of the preview tint. const TINT_ALPHA: u8 = 110; +/// The Background Eraser's third readout row: the reference shows "Opacity : 50 %" there, the +/// tool's Tolerance after Reset Tool and not an opacity the tool has (#18053). Unmeasured for other +/// Tolerance values, so the row stays at 50 until they are (#18053). +const BACKGROUND_ERASER_READOUT_OPACITY: f64 = 50.0; /// The brush resize drag of the painting tools, kept in `PhotocraftApp::brush_resize`. The painting tool takes it /// out of the app for each call, so its methods get the app as an argument. @@ -98,7 +102,7 @@ impl PaintResize { return false; } self.key += 1; - self.hud(cx); + self.hud(app, cx); true } @@ -108,7 +112,7 @@ impl PaintResize { let Some(space) = cx.space() else { return }; let step = self.resize.track(&space.view, pointer.pos); self.apply(app, step); - self.hud(cx); + self.hud(app, cx); } /// The button came up: the last values stay. @@ -126,13 +130,20 @@ impl PaintResize { /// Asks for the diameter and level readout beside the pointer while a resize runs. `hud`, not `readout`: Show /// Transformation Values never hides it. The request lasts one pass, so the tool repeats it every pass. - pub(crate) fn hud(&self, cx: &mut ToolContext<'_>) { + /// The Background Eraser gets a third row, Opacity (#18053). + pub(crate) fn hud(&self, app: &PhotocraftApp, cx: &mut ToolContext<'_>) { if !self.resize.active() { return; } let words = cx.words::(); - let rows = self.resize.readout_rows(&cx.units(), &words); + let mut rows = self.resize.readout_rows(&cx.units(), &words); + // The reference's third row is there beside a Hardness row; with "Vary Round Brush Hardness on HUD" off the + // level row is already Opacity, and a second one would repeat it. + if app.ui.tool == Tool::BackgroundEraser && varies_hardness(app) { + let opacity = cx.units().percent(BACKGROUND_ERASER_READOUT_OPACITY); + rows.push((words.opacity.clone(), opacity)); + } cx.hud(rows); } @@ -175,6 +186,10 @@ impl PaintResize { #[cfg(test)] mod tests; +#[cfg(test)] +#[path = "brush_resize_background_eraser_opacity_row_tests.rs"] +mod background_eraser_opacity_row_tests; + #[cfg(test)] #[path = "../brush_resize_hardness_rate_tests.rs"] mod brush_resize_hardness_rate_tests; diff --git a/crates/ui-egui/src/tools/brush_resize_background_eraser_opacity_row_tests.rs b/crates/ui-egui/src/tools/brush_resize_background_eraser_opacity_row_tests.rs new file mode 100644 index 0000000000..5551976baf --- /dev/null +++ b/crates/ui-egui/src/tools/brush_resize_background_eraser_opacity_row_tests.rs @@ -0,0 +1,116 @@ +//! #18053: the Background Eraser's Ctrl+Alt+drag (Alt+right-drag) readout has a third row, +//! "Opacity : 50 %", after Diameter and Hardness, as the reference application does. The row does +//! not follow the drag or the tool's Tolerance (not measured), and the gesture still changes only +//! the diameter and the hardness, erases nothing and adds no history state. + +use serde_json::json; + +use super::*; + +use crate::canvas::{ToolEvent, testing}; + +/// A 200 x 100 white document at 100 % zoom with `tool` active and a 40 px, `hardness`, 100 % opaque +/// brush. The window is made a Mac's: off macOS the host reports Ctrl as the command key, and the +/// Ctrl+Alt chord needs Ctrl its own key. +fn app(tool: Tool, hardness: f32) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 100, "background": "white"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 40, "hardness": hardness, "opacity": 1.0}})).unwrap(); + app.ui.tool = tool; + app.sync_views(); + testing::on_a_mac(&app); + app +} + +fn chord() -> egui::Modifiers { + egui::Modifiers { alt: true, ctrl: true, ..Default::default() } +} + +/// The rows the readout asked for at `ev`, as (label, value) pairs. +fn readout(app: &mut PhotocraftApp, ev: ToolEvent) -> Vec<(String, String)> { + crate::canvas::tool_event_response(app, ev, chord()) + .readout + .map(|r| r.rows.into_iter().map(|(label, value)| (label.as_str().to_owned(), value)).collect()) + .unwrap_or_default() +} + +fn values(rows: &[(String, String)]) -> Vec<&str> { + rows.iter().map(|(_, value)| value.as_str()).collect() +} + +/// Done when 1: after Reset Tool's brush, three rows in the order Diameter, Hardness, Opacity, and +/// the Opacity row reads 50 %. +#[test] +fn the_background_eraser_readout_has_a_fifty_percent_opacity_row() { + let mut a = app(Tool::BackgroundEraser, 1.0); + let got = readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!( + got, + vec![("Diameter:".to_owned(), "40 px".to_owned()), ("Hardness:".to_owned(), "100%".to_owned()), ("Opacity:".to_owned(), "50%".to_owned()),], + "three rows, in the reference's order" + ); + readout(&mut a, ToolEvent::Up { x: 100.0, y: 50.0 }); +} + +/// Done when 2: horizontal and vertical movement leave the Opacity row at 50 %, the diameter and +/// the hardness following their own axes. +#[test] +fn the_opacity_row_stays_at_fifty_percent_through_the_drag() { + let mut a = app(Tool::BackgroundEraser, 0.5); + readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + let right = readout(&mut a, ToolEvent::Move { x: 130.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&right), vec!["100 px", "50%", "50%"], "rightward: a wider tip, the same rows"); + // 400 screen points take the hardness from 0 % to 100 %, so 20 points down add 5 %. + let down = readout(&mut a, ToolEvent::Move { x: 130.0, y: 70.0, pressure: 1.0 }); + assert_eq!(values(&down), vec!["100 px", "55%", "50%"], "downward: a softer tip, the opacity row unchanged"); + let up = readout(&mut a, ToolEvent::Move { x: 130.0, y: 30.0, pressure: 1.0 }); + assert_eq!(values(&up), vec!["100 px", "45%", "50%"], "upward: harder, the opacity row unchanged"); + readout(&mut a, ToolEvent::Up { x: 130.0, y: 30.0 }); +} + +/// Done when 3: the row stays at 50 % for another Tolerance. The 50 is the Tolerance after Reset +/// Tool, but the reference's row was not measured for other values, so it does not follow it yet. +#[test] +fn another_tolerance_leaves_the_row_at_fifty_percent() { + let mut a = app(Tool::BackgroundEraser, 1.0); + a.ui.tool_options.bg_tolerance = 85.0; + let got = readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&got), vec!["40 px", "100%", "50%"], "Tolerance 85 %: the row still reads 50 %"); + readout(&mut a, ToolEvent::Up { x: 100.0, y: 50.0 }); +} + +/// Done when 2: the gesture changes only the diameter and the hardness, erases nothing and adds no +/// history state. +#[test] +fn the_gesture_erases_nothing_and_adds_no_history_state() { + let mut a = app(Tool::BackgroundEraser, 0.5); + let history = a.session.active().unwrap().history.entries().len(); + readout(&mut a, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + readout(&mut a, ToolEvent::Move { x: 130.0, y: 70.0, pressure: 1.0 }); + readout(&mut a, ToolEvent::Up { x: 130.0, y: 70.0 }); + + assert_ne!(a.session.tools.brush.size, 40.0, "the diameter changed"); + assert!(a.session.tools.brush.hardness > 0.5, "the hardness changed"); + assert_eq!(a.session.tools.brush.opacity, 1.0, "the brush's own opacity did not"); + let st = a.session.active().unwrap(); + assert_eq!(st.history.entries().len(), history, "no history state"); + let surface = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + assert_eq!(surface.pixel(100, 50)[3], 1.0, "the canvas was not erased"); +} + +/// Done when 1: every other painting tool keeps the framework's two rows, and with "Vary Round +/// Brush Hardness on HUD" off the Background Eraser's second row is already Opacity, so no third row +/// repeats it. +#[test] +fn only_the_background_eraser_with_the_hardness_row_gets_the_third_row() { + let mut pencil = app(Tool::Pencil, 1.0); + let got = readout(&mut pencil, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&got), vec!["40 px", "100%"], "the Pencil keeps two rows"); + readout(&mut pencil, ToolEvent::Up { x: 100.0, y: 50.0 }); + + let mut eraser = app(Tool::BackgroundEraser, 1.0); + eraser.run("prefs.set", json!({"path": "tools.varyRoundBrushHardnessOnHud", "value": false})).unwrap(); + let got = readout(&mut eraser, ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }); + assert_eq!(values(&got), vec!["40 px", "100%"], "the level row is Opacity, and there is no second one"); + readout(&mut eraser, ToolEvent::Up { x: 100.0, y: 50.0 }); +}