diff --git a/crates/ui-egui/src/input/shortcut_dispatch.rs b/crates/ui-egui/src/input/shortcut_dispatch.rs index af137c2c5dd..b5dcd9821b5 100644 --- a/crates/ui-egui/src/input/shortcut_dispatch.rs +++ b/crates/ui-egui/src/input/shortcut_dispatch.rs @@ -698,6 +698,14 @@ fn pick_tool(app: &mut PhotocraftApp, letter: char, step: PickStep, plain: bool, /// through [`dispatch_as`]. fn run_pressed(app: &mut PhotocraftApp, ctx: &egui::Context, f: &Fired) { let id = f.command.id.as_str(); + // A held primary stroke takes the brush size and hardness keys without modifying the stored tip. + if f.source == FireSource::Key + && matches!(id, "tools.decreaseBrushSize" | "tools.increaseBrushSize" | "tools.decreaseBrushHardness" | "tools.increaseBrushHardness") + && crate::canvas::paint_tool::stroking(app).is_some_and(|stroke| matches!(stroke.tool, Tool::Brush | Tool::Pencil | Tool::Eraser | Tool::MixerBrush)) + && ctx.input(|input| input.pointer.primary_down() && !input.pointer.button_down(egui::PointerButton::Secondary)) + { + return; + } if f.source == FireSource::Key && id == crate::panels::QUICK_MASK && app.ui.transform.as_ref().is_some_and(|transform| { @@ -834,3 +842,7 @@ pub(crate) mod tests; #[cfg(test)] #[path = "quick_mask_free_transform_tests.rs"] mod quick_mask_free_transform_tests; + +#[cfg(test)] +#[path = "size_keys_during_stroke_tests.rs"] +mod size_keys_during_stroke_tests; diff --git a/crates/ui-egui/src/input/size_keys_during_stroke_tests.rs b/crates/ui-egui/src/input/size_keys_during_stroke_tests.rs new file mode 100644 index 00000000000..0ed70d7e143 --- /dev/null +++ b/crates/ui-egui/src/input/size_keys_during_stroke_tests.rs @@ -0,0 +1,109 @@ +//! Brush size and hardness shortcuts during a captured primary stroke (#17735). + +use egui::{Event, Key, Modifiers, PointerButton, Pos2}; +use egui_kittest::Harness; +use serde_json::json; + +use super::tests::{app_harness, tap}; +use crate::PhotocraftApp; +use crate::canvas::{paint_tool, testing}; +use crate::state::Tool; + +const BRUSH_KEYS: [(Key, Modifiers, (f32, f32)); 4] = [ + (Key::OpenBracket, Modifiers::NONE, (15.0, 0.5)), + (Key::CloseBracket, Modifiers::NONE, (20.0, 0.5)), + (Key::OpenBracket, Modifiers::SHIFT, (20.0, 0.25)), + (Key::CloseBracket, Modifiers::SHIFT, (20.0, 0.5)), +]; + +fn harness(tool: Tool, secondary_erase: bool) -> Harness<'static, Box> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + testing::still_clock(&mut app); + app.run("file.new", json!({"width": 120, "height": 60, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + if tool == Tool::Eraser || secondary_erase { + app.run("edit.fill", json!({"color": "#000000"})).unwrap(); + } + if secondary_erase { + app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "erase"})).unwrap(); + } + crate::tool_memory::select(&mut app, tool); + crate::paint_mouse::sync_tool_brush(&mut app); + app.run("tools.setColors", json!({"foreground": "#ff0000"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 20, "hardness": 0.5, "smoothing": {"amount": 0.0}, "mixer": {"wet": 0.0}}})).unwrap(); + if tool == Tool::MixerBrush { + app.run("brush.mixer.load", json!({})).unwrap(); + } + app_harness(app) +} + +fn tip(app: &PhotocraftApp) -> (f32, f32) { + (app.session.tools.brush.size, app.session.tools.brush.hardness) +} + +fn pointer(h: &mut Harness<'_, Box>, pos: Pos2, button: PointerButton, pressed: bool) { + h.event(Event::PointerButton { pos, button, pressed, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +#[test] +fn size_and_hardness_keys_are_dropped_during_primary_paint_strokes() { + for tool in [Tool::Brush, Tool::Pencil, Tool::Eraser, Tool::MixerBrush] { + let mut h = harness(tool, false); + let start = testing::to_screen(h.state(), [20.0, 30.0]); + let middle = testing::to_screen(h.state(), [60.0, 30.0]); + let end = testing::to_screen(h.state(), [95.0, 30.0]); + h.event(Event::PointerMoved(start)); + h.run_steps(1); + + let before = tip(h.state()); + let steps = h.state().session.active().unwrap().history.past_len(); + pointer(&mut h, start, PointerButton::Primary, true); + h.event(Event::PointerMoved(middle)); + h.run_steps(1); + assert_eq!(paint_tool::stroking(h.state()).expect("a captured stroke").tool, tool); + let journal = h.state().session.journal.len(); + + for (key, mods, _) in BRUSH_KEYS { + tap(&mut h, key, mods); + assert_eq!(tip(h.state()), before, "{tool:?}: {mods:?} {key:?} changed the held tip"); + assert_eq!(h.state().session.journal.len(), journal, "{tool:?}: the shortcut was recorded"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), steps); + } + + h.event(Event::PointerMoved(end)); + h.run_steps(1); + pointer(&mut h, end, PointerButton::Primary, false); + h.run_steps(2); + assert!(paint_tool::stroking(h.state()).is_none()); + assert_eq!(tip(h.state()), before, "{tool:?}: a dropped key took effect after release"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), steps + 1, "{tool:?}: one stroke edit"); + } +} + +#[test] +fn all_four_keys_still_adjust_an_idle_brush() { + let mut h = harness(Tool::Brush, false); + assert_eq!(tip(h.state()), (20.0, 0.5)); + for (key, mods, expected) in BRUSH_KEYS { + tap(&mut h, key, mods); + assert_eq!(tip(h.state()), expected, "{mods:?} {key:?}"); + } +} + +#[test] +fn a_secondary_erase_stroke_does_not_take_the_brush_size_keys() { + let mut h = harness(Tool::Brush, true); + let start = testing::to_screen(h.state(), [20.0, 30.0]); + let end = testing::to_screen(h.state(), [95.0, 30.0]); + h.event(Event::PointerMoved(start)); + h.run_steps(1); + pointer(&mut h, start, PointerButton::Secondary, true); + assert!(paint_tool::stroking(h.state()).expect("a captured right-erase stroke").erase); + tap(&mut h, Key::CloseBracket, Modifiers::NONE); + assert_eq!(tip(h.state()), (25.0, 0.5)); + h.event(Event::PointerMoved(end)); + h.run_steps(1); + pointer(&mut h, end, PointerButton::Secondary, false); + assert_eq!(tip(h.state()), (25.0, 0.5)); +}