diff --git a/crates/engine/src/cmd/brush_cmds/tests.rs b/crates/engine/src/cmd/brush_cmds/tests.rs index 12d7d724e6..7a424e2f03 100644 --- a/crates/engine/src/cmd/brush_cmds/tests.rs +++ b/crates/engine/src/cmd/brush_cmds/tests.rs @@ -468,7 +468,7 @@ fn every_size_change_scales_the_dual_tip_and_keeps_its_proportion() { s.execute("tools.setBrush", json!({"brush": {"size": 100, "dualBrush": {"enabled": true, "size": 20}}})).unwrap(); let sizes = |s: &Session| (s.tools.brush.size, s.tools.brush.dual_brush.size); s.execute("tools.decreaseBrushSize", json!({})).unwrap(); - assert_eq!(sizes(&s), (80.0, 16.0)); + assert_eq!(sizes(&s), (90.0, 18.0)); s.execute("tools.increaseBrushSize", json!({})).unwrap(); assert_eq!(sizes(&s), (100.0, 20.0)); // A coalesced drag (options bar slider, Alt+right-drag) is a run of size-only patches. diff --git a/crates/engine/src/cmd/brush_key_cmds.rs b/crates/engine/src/cmd/brush_key_cmds.rs index eada73490b..f731f2ccce 100644 --- a/crates/engine/src/cmd/brush_key_cmds.rs +++ b/crates/engine/src/cmd/brush_key_cmds.rs @@ -18,11 +18,17 @@ use crate::{EngineError, Result, Session}; /// Photoshop's largest brush diameter, in pixels. pub const MAX_SIZE: f32 = 5000.0; -/// `[` / `]`: 25 % smaller or larger, and at least a pixel either way so small brushes still grow -/// and shrink (2 / 1.25 rounds back to 2, which kept `[` from reaching 1 px, #3229). +/// `[` / `]`: the next diameter in the key's direction, following the measured size table +/// through 1500 px (#12936), then continuing in 100 px steps to the maximum. Intermediate +/// sizes snap in that direction, and the 1 px endpoint stays reachable (#3229). fn size_step(size: f32, up: bool) -> f32 { - let next = if up { (size * 1.25).round().max(size + 1.0) } else { (size / 1.25).round().min(size - 1.0) }; - next.clamp(1.0, MAX_SIZE) + let mut steps = [ + 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 125.0, 150.0, 175.0, + 200.0, 250.0, 300.0, + ] + .into_iter() + .chain((4..=(MAX_SIZE / 100.0) as u32).map(|n| n as f32 * 100.0)); + if up { steps.find(|&next| next > size).unwrap_or(MAX_SIZE) } else { steps.rfind(|&next| next < size).unwrap_or(1.0) } } /// ⇧[ / ⇧]: to the previous or next quarter (0, 25, 50, 75, 100 %). @@ -164,6 +170,10 @@ pub fn specs() -> Vec { ] } +#[cfg(test)] +#[path = "brush_key_cmds/bracket_steps_tests.rs"] +mod bracket_steps_tests; + #[cfg(test)] mod tests { use super::*; @@ -280,7 +290,7 @@ mod tests { let mut s = Session::new(); s.execute("tools.setBrush", json!({"brush": {"size": 40, "hardness": 0.5}})).unwrap(); s.execute("tools.increaseBrushSize", json!({})).unwrap(); - assert_eq!(s.tools.brush.size, 50.0); + assert_eq!(s.tools.brush.size, 45.0); s.execute("tools.decreaseBrushSize", Value::Null).unwrap(); assert_eq!(s.tools.brush.size, 40.0); // Pixel artists: `[` held down reaches 1 px (#3229). diff --git a/crates/engine/src/cmd/brush_key_cmds/bracket_steps_tests.rs b/crates/engine/src/cmd/brush_key_cmds/bracket_steps_tests.rs new file mode 100644 index 0000000000..3d4f808696 --- /dev/null +++ b/crates/engine/src/cmd/brush_key_cmds/bracket_steps_tests.rs @@ -0,0 +1,90 @@ +//! Bracket-key size steps from the measured sequence in #12936 and its specified extensions. + +use super::*; + +/// The commands walk the measured sizes in both directions without changing hardness or spacing. +#[test] +fn commands_follow_the_measured_sequence_in_both_directions() { + let sizes = [ + 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 125.0, 150.0, 175.0, + 200.0, 250.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1100.0, 1200.0, 1300.0, 1400.0, 1500.0, + ]; + let mut s = Session::new(); + s.execute("tools.setBrush", json!({"brush": {"size": 1, "hardness": 0.37, "spacing": 0.42}})).unwrap(); + for (command, expected) in [ + ("tools.increaseBrushSize", sizes.iter().skip(1).copied().collect::>()), + ("tools.decreaseBrushSize", sizes.iter().rev().skip(1).copied().collect()), + ] { + for size in expected { + s.execute(command, json!({})).unwrap(); + assert_eq!(s.tools.brush.size, size, "{command}"); + assert_eq!(s.tools.brush.hardness, 0.37); + assert_eq!(s.tools.brush.spacing, 0.42); + assert_eq!(s.journal.last().map(|(id, _)| id.as_str()), Some(command)); + } + } + assert!(s.documents().is_empty(), "brush keys still work without a document"); +} + +/// Fractional and integer sizes between entries snap to the next entry in the requested direction. +#[test] +fn intermediate_sizes_snap_in_the_keys_direction() { + for (size, smaller, larger) in [ + (1.25, 1.0, 2.0), + (9.5, 9.0, 10.0), + (10.5, 10.0, 15.0), + (12.0, 10.0, 15.0), + (27.0, 25.0, 30.0), + (32.0, 30.0, 35.0), + (49.5, 45.0, 50.0), + (55.0, 50.0, 60.0), + (99.0, 90.0, 100.0), + (100.5, 100.0, 125.0), + (149.0, 125.0, 150.0), + (176.0, 175.0, 200.0), + (225.0, 200.0, 250.0), + (299.0, 250.0, 300.0), + (301.0, 300.0, 400.0), + (1499.0, 1400.0, 1500.0), + (1501.0, 1500.0, 1600.0), + (4999.0, 4900.0, 5000.0), + ] { + assert_eq!(size_step(size, false), smaller, "[ from {size}"); + assert_eq!(size_step(size, true), larger, "] from {size}"); + } +} + +/// The specified extension above 1500 continues in hundreds and saturates at the brush maximum. +#[test] +fn commands_continue_in_hundreds_to_the_maximum_and_back() { + let mut s = Session::new(); + s.execute("tools.setBrush", json!({"brush": {"size": 1500}})).unwrap(); + for size in (1600..=5000).step_by(100) { + s.execute("tools.increaseBrushSize", Value::Null).unwrap(); + assert_eq!(s.tools.brush.size, size as f32); + } + assert_eq!(s.tools.brush.size, MAX_SIZE); + for _ in 0..3 { + s.execute("tools.increaseBrushSize", Value::Null).unwrap(); + assert_eq!(s.tools.brush.size, MAX_SIZE); + } + for size in (1500_u16..=4900).step_by(100).rev() { + s.execute("tools.decreaseBrushSize", Value::Null).unwrap(); + assert_eq!(s.tools.brush.size, size as f32); + } +} + +/// Small sizes still reach 1 px, stay there on decrease and grow again on increase (#3229). +#[test] +fn commands_reach_and_hold_the_one_pixel_minimum() { + let mut s = Session::new(); + s.execute("tools.setBrush", json!({"brush": {"size": 3}})).unwrap(); + for size in [2.0, 1.0, 1.0, 1.0] { + s.execute("tools.decreaseBrushSize", Value::Null).unwrap(); + assert_eq!(s.tools.brush.size, size); + } + s.execute("tools.increaseBrushSize", Value::Null).unwrap(); + assert_eq!(s.tools.brush.size, 2.0); + assert_eq!(size_step(0.5, true), 1.0, "a subpixel brush grows to the first entry"); + assert_eq!(size_step(0.5, false), 1.0, "the bracket-key minimum remains 1 px"); +} diff --git a/crates/ui-egui/src/color_replacement_tests.rs b/crates/ui-egui/src/color_replacement_tests.rs index 87adc1f64c..997b76f6c2 100644 --- a/crates/ui-egui/src/color_replacement_tests.rs +++ b/crates/ui-egui/src/color_replacement_tests.rs @@ -243,7 +243,7 @@ fn it_paints_like_the_brush_tools() { a.run("tools.increaseBrushSize", json!({})).unwrap(); assert!(a.session.tools.brush.size > size); a.run("tools.decreaseBrushSize", json!({})).unwrap(); - assert_eq!(a.session.tools.brush.size, size); + assert_eq!(a.session.tools.brush.size, 10.0, "[ snaps back below the original intermediate 12 px size"); // Alt+right drag resizes and paints nothing. let events = json!([{"kind": "down", "x": 40, "y": 20}, {"kind": "move", "x": 60, "y": 20}, {"kind": "up", "x": 70, "y": 20}]); let (req, _rx) = crate::control::ControlRequest::new("ui.pointer", json!({"tool": "colorReplacement", "button": "right", "alt": true, "events": events})); diff --git a/crates/ui-egui/src/input/shortcut_dispatch/tests.rs b/crates/ui-egui/src/input/shortcut_dispatch/tests.rs index ce7e3fcc39..29217a6af4 100644 --- a/crates/ui-egui/src/input/shortcut_dispatch/tests.rs +++ b/crates/ui-egui/src/input/shortcut_dispatch/tests.rs @@ -465,7 +465,7 @@ fn brush_keys_can_be_rebound() { assert_eq!(brush(&h), (40.0, 0.5), "the old keys are free"); press(&mut h, "Alt+W"); press(&mut h, "Alt+Shift+W"); - assert_eq!(brush(&h), (50.0, 0.75), "the new keys step size and hardness"); + assert_eq!(brush(&h), (45.0, 0.75), "the new keys step size and hardness"); press(&mut h, "["); press(&mut h, "Shift+["); assert_eq!(brush(&h), (40.0, 0.5), "the others keep their defaults");