diff --git a/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs b/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs index f06e549e8f1..5f314478f37 100644 --- a/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs +++ b/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs @@ -27,6 +27,14 @@ fn key(app: &mut PhotocraftApp, ctx: &egui::Context, key: Key, modifiers: Modifi ctx.end_pass().textures_delta.clear(); } +/// Several key presses in one frame, in this order. +fn keys(app: &mut PhotocraftApp, ctx: &egui::Context, presses: &[(Key, Modifiers)]) { + let events = presses.iter().map(|&(key, modifiers)| egui::Event::Key { key, physical_key: Some(key), pressed: true, repeat: false, modifiers }).collect(); + ctx.begin_pass(egui::RawInput { events, ..Default::default() }); + crate::shortcuts::handle(app, ctx); + ctx.end_pass().textures_delta.clear(); +} + fn brush(app: &PhotocraftApp) -> (f32, f32, f32) { let b = &app.session.tools.brush; (b.smoothing.amount, b.opacity, b.flow) @@ -39,6 +47,7 @@ fn alt_digits_set_the_smoothing() { let mut a = app(tool); let (_, opacity, flow) = brush(&a); for (k, want) in [(Key::Num3, 0.3), (Key::Num1, 0.1), (Key::Num9, 0.9), (Key::Num0, 1.0)] { + a.session.tools.brush.smoothing.enabled = false; key(&mut a, &ctx, k, Modifiers::ALT); let (smoothing, o, f) = brush(&a); assert!((smoothing - want).abs() < 1e-6, "{tool:?} {k:?}: {smoothing}"); @@ -74,9 +83,8 @@ fn plain_and_shift_digits_still_set_opacity_and_flow() { let ctx = egui::Context::default(); let mut a = app(Tool::Brush); let smoothing = brush(&a).0; - key(&mut a, &ctx, Key::Num4, Modifiers::NONE); - key(&mut a, &ctx, Key::Num5, Modifiers::NONE); - key(&mut a, &ctx, Key::Num3, Modifiers::SHIFT); + // One frame, so the two digits are "typed quickly" however slow the test runner is. + keys(&mut a, &ctx, &[(Key::Num4, Modifiers::NONE), (Key::Num5, Modifiers::NONE), (Key::Num3, Modifiers::SHIFT)]); let (s, o, f) = brush(&a); assert!((o - 0.45).abs() < 1e-6 && (f - 0.3).abs() < 1e-6, "{o} {f}"); assert_eq!(s, smoothing); @@ -98,3 +106,25 @@ fn tools_without_a_smoothing_field_ignore_alt_digits() { assert_eq!(layer(&a), opacity, "{tool:?}: the layer's Opacity stays"); } } + +#[test] +fn an_alt_digit_ends_a_two_digit_opacity() { + // 4, ⌥3, 5 in one frame: the presses run in order and ⌥3 ends the "45", so 5 sets 50%. + let ctx = egui::Context::default(); + let mut a = app(Tool::Brush); + keys(&mut a, &ctx, &[(Key::Num4, Modifiers::NONE), (Key::Num3, Modifiers::ALT), (Key::Num5, Modifiers::NONE)]); + let (s, o, _) = brush(&a); + assert!((s - 0.3).abs() < 1e-6, "{s}"); + assert!((o - 0.5).abs() < 1e-6, "{o}"); +} + +#[test] +fn alt_with_other_modifiers_does_not_set_the_smoothing() { + let ctx = egui::Context::default(); + let mut a = app(Tool::Brush); + let smoothing = brush(&a).0; + for m in [Modifiers::ALT | Modifiers::SHIFT, Modifiers::ALT | Modifiers::CTRL, Modifiers::ALT | Modifiers::COMMAND] { + key(&mut a, &ctx, Key::Num3, m); + assert_eq!(brush(&a).0, smoothing, "{m:?}"); + } +} diff --git a/crates/ui-egui/src/input/opacity_keys.rs b/crates/ui-egui/src/input/opacity_keys.rs index 01c32fd67a2..41c571f3122 100644 --- a/crates/ui-egui/src/input/opacity_keys.rs +++ b/crates/ui-egui/src/input/opacity_keys.rs @@ -68,12 +68,17 @@ fn has_smoothing(tool: Tool) -> bool { matches!(tool, Tool::Brush | Tool::Pencil | Tool::Eraser) } +/// One digit press of this frame: a plain or ⇧ digit, or an ⌥ digit for Smoothing. +enum Press { + Digit(u8, bool), + Smoothing(u8), +} + /// Consumes this frame's unmodified (or ⇧) digit presses and applies them, and ⌥+digit presses -/// for the tools with a Smoothing field. +/// for the tools with a Smoothing field, in the order they were typed. pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) { let smoothing = has_smoothing(app.ui.tool); - let mut alt_presses: Vec = Vec::new(); - let presses: Vec<(u8, bool)> = ctx.input_mut(|i| { + let presses: Vec = ctx.input_mut(|i| { let mut out = Vec::new(); i.events.retain(|e| match e { // ⌥+digit: one tens value each (1 = 10% … 0 = 100%), never two digits. @@ -83,7 +88,7 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) { match physical_key.and_then(digit).or_else(|| digit(*key)) { Some(d) => { if !repeat { - alt_presses.push(d); + out.push(Press::Smoothing(d)); } false } @@ -97,7 +102,7 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) { // A held key's auto-repeat is still one digit, not a second one typed // quickly (holding 4 set 44%, #953); it is consumed all the same. if !repeat { - out.push((d, modifiers.shift)); + out.push(Press::Digit(d, modifiers.shift)); } false } @@ -108,13 +113,17 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) { }); out }); - for d in alt_presses { - let amount = if d == 0 { 1.0 } else { f64::from(d) / 10.0 }; - let _ = app.run("tools.setBrush", json!({ "brush": { "smoothing": { "enabled": true, "amount": amount } } })); - } let now = crate::gpu_canvas::now_ms(); - for (d, shift) in presses { - press(app, d, shift, now); + for p in presses { + match p { + Press::Digit(d, shift) => press(app, d, shift, now), + Press::Smoothing(d) => { + // An ⌥ digit ends a two-digit Opacity value: 4, ⌥3, 5 is 50%, not 45%. + app.opacity_keys = None; + let amount = if d == 0 { 1.0 } else { f64::from(d) / 10.0 }; + let _ = app.run("tools.setBrush", json!({ "brush": { "smoothing": { "enabled": true, "amount": amount } } })); + } + } } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e3e90cbfab6..e9555fe0975 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1050,13 +1050,13 @@ mod menu_item_labels_tests; #[cfg(test)] mod pen_path_ownership_tests; -#[cfg(test)] -mod brush_alt_digit_smoothing_tests; #[cfg(test)] mod artboard_options_bar_tests; #[cfg(test)] mod background_eraser_bar_angle_tests; #[cfg(test)] +mod brush_alt_digit_smoothing_tests; +#[cfg(test)] mod brush_opacity_minimum_tests; #[cfg(test)] mod burn_default_tip_tests;