diff --git a/crates/ui-egui/src/brush_resize_hardness_rate_tests.rs b/crates/ui-egui/src/brush_resize_hardness_rate_tests.rs new file mode 100644 index 00000000000..f7a00549980 --- /dev/null +++ b/crates/ui-egui/src/brush_resize_hardness_rate_tests.rs @@ -0,0 +1,110 @@ +//! #17569: Alt+right-drag (or Ctrl+Alt+drag) changes the brush hardness by 0.25 % per screen point, +//! rounded to a whole percent: 400 points take it from 0 % to 100 %. The size axis and the opacity +//! axis (with "Vary Round Brush Hardness on HUD" off) keep their own rates. + +use craft::behavior::PointerButton; +use craft::keymap::Mods; +use craft::ui::runtime::windows::{Released, WindowHost}; +use egui::Modifiers; +use serde_json::json; + +use super::*; + +use crate::canvas::testing; + +fn ctrl_alt() -> Modifiers { + Modifiers { alt: true, ctrl: true, ..Default::default() } +} + +fn mods_of(m: Modifiers) -> Mods { + Mods { cmd: false, ctrl: m.ctrl, alt: m.alt, shift: m.shift } +} + +/// A 200 x 100 white document with the Pencil active at 100 % zoom and `hardness` / `opacity` on its +/// brush, so a screen point is a document point. +fn app(hardness: f32, opacity: 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": opacity}})).unwrap(); + app.ui.tool = Tool::Pencil; + app.sync_views(); + // The Ctrl+Alt chord is a Mac gesture: off macOS the window host reports Ctrl as the command key. + testing::on_a_mac(&app); + app +} + +/// Runs frames until the window host has delivered the queued input. +fn settle(app: &mut PhotocraftApp, host: &WindowHost) { + for _ in 0..64 { + testing::frame(app); + if host.injected_idle() { + return; + } + } + panic!("the injected input was not taken"); +} + +/// The resize drag with Ctrl+Alt and the primary button, from the canvas point at document 100,50, +/// moved by `right` and `down` **screen points** in four steps and released. +fn drag(app: &mut PhotocraftApp, right: f64, down: f64) { + testing::shown(app); + let host = WindowHost::of(&testing::ctx(app)); + let start = testing::to_screen(app, [100.0, 50.0]); + let at = |k: f32| start + egui::vec2(right as f32 * k / 4.0, down as f32 * k / 4.0); + host.inject_click(egui::ViewportId::ROOT, start, PointerButton::Primary, 1, mods_of(ctrl_alt()), true); + settle(app, &host); + for k in 1..=4 { + host.inject_move(egui::ViewportId::ROOT, at(k as f32), mods_of(ctrl_alt()), 0.0); + settle(app, &host); + } + host.inject_release(egui::ViewportId::ROOT, Some(at(4.0)), Released::Button(PointerButton::Primary)); + settle(app, &host); +} + +/// The brush's hardness as a whole percent. +fn hardness(app: &PhotocraftApp) -> i32 { + (app.session.tools.brush.hardness * 100.0).round() as i32 +} + +/// The brush's opacity as a whole percent. +fn opacity(app: &PhotocraftApp) -> i32 { + (app.session.tools.brush.opacity * 100.0).round() as i32 +} + +/// Done when 1: 0.25 % per screen point, rounded to a whole percent. +#[test] +fn the_hardness_moves_a_quarter_percent_per_screen_point() { + let mut a = app(0.0, 1.0); + drag(&mut a, 0.0, 50.0); + assert_eq!(hardness(&a), 13, "50 points down from 0 %"); + + let mut a = app(0.0, 1.0); + drag(&mut a, 0.0, 150.0); + assert_eq!(hardness(&a), 38, "150 points down from 0 %"); + + // 38 % - 50 x 0.25 % = 25.5 %, rounded half up. + let mut a = app(0.38, 1.0); + drag(&mut a, 0.0, -50.0); + assert_eq!(hardness(&a), 26, "50 points up from 38 %"); +} + +/// Done when 2: the size axis is the one that changes on a rightward drag, and it is unchanged. +#[test] +fn a_rightward_drag_only_changes_the_size() { + let mut a = app(0.0, 1.0); + let size = a.session.tools.brush.size; + drag(&mut a, 30.0, 0.0); + assert!(a.session.tools.brush.size > size, "right grows the brush"); + assert_eq!(hardness(&a), 0, "no vertical travel, no hardness change"); +} + +/// Done when 2: with the preference off the vertical drag changes the opacity at today's rate +/// (200 points from 0 % to 100 %), not the hardness. +#[test] +fn with_the_preference_off_the_vertical_drag_keeps_the_opacity_rate() { + let mut a = app(0.0, 0.5); + a.run("prefs.set", json!({"path": "tools.varyRoundBrushHardnessOnHud", "value": false})).unwrap(); + drag(&mut a, 0.0, -50.0); + assert_eq!(opacity(&a), 25, "50 points of a 200-point travel, from 50 %"); + assert_eq!(hardness(&a), 0, "the hardness is left alone"); +} diff --git a/crates/ui-egui/src/tools/brush_resize.rs b/crates/ui-egui/src/tools/brush_resize.rs index 2860661c9a6..d5c19b96fda 100644 --- a/crates/ui-egui/src/tools/brush_resize.rs +++ b/crates/ui-egui/src/tools/brush_resize.rs @@ -20,9 +20,10 @@ use photocraft_engine::paint::MAX_BRUSH_SIZE; use crate::PhotocraftApp; use crate::state::Tool; -/// Screen points of vertical drag that take the hardness from 0 % to 100 %: Photoshop's rate, -/// where 50 points down from 0 % give 13 % (#11891). -const HARDNESS_TRAVEL: f32 = 385.0; +/// Screen points of vertical drag that take the hardness from 0 % to 100 %: Photoshop's rate of +/// 0.25 % per screen point (#11891, #17569), where 50 points down from 0 % give 13 % and 150 points +/// give 38 %. +const HARDNESS_TRAVEL: f32 = 400.0; /// Screen points of vertical drag that take the opacity from 0 % to 100 % when "Vary Round Brush /// Hardness on HUD" is off. const OPACITY_TRAVEL: f32 = 200.0; @@ -89,7 +90,10 @@ impl PaintResize { let Some(space) = cx.space() else { return false }; let brush = &app.session.tools.brush; let level = if varies_hardness(app) { brush.hardness } else { brush.opacity }; - let start = BrushDimensions::new(f64::from(brush.size), f64::from(level)); + // The drag starts from the whole percent the readout shows (#17569): the brush keeps the + // level as `f32`, where 38 % is a hair below 0.38, which would round a 25.5 % result down. + let level = (f64::from(level) * 100.0).round() / 100.0; + let start = BrushDimensions::new(f64::from(brush.size), level); if self.resize.begin(&space.view, pointer.pos, start, &options(app)).is_none() { return false; } @@ -170,3 +174,7 @@ impl PaintResize { #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "../brush_resize_hardness_rate_tests.rs"] +mod brush_resize_hardness_rate_tests;