diff --git a/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs b/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs new file mode 100644 index 00000000000..5f314478f37 --- /dev/null +++ b/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs @@ -0,0 +1,130 @@ +//! #11892: ⌥+digit sets the Smoothing of the tools that show it (Alt+3 = 30%, Alt+0 = 100%), +//! one tens value per press; Opacity and Flow stay, plain and ⇧ digits work as before, and a +//! tool without a Smoothing field ignores it. + +use egui::{Key, Modifiers}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +fn app(tool: Tool) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 32, "height": 32})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.ui.tool = tool; + app +} + +/// One key press with `modifiers` through the app's shortcut handling. +fn key(app: &mut PhotocraftApp, ctx: &egui::Context, key: Key, modifiers: Modifiers) { + let raw = egui::RawInput { + events: vec![egui::Event::ModifiersChanged(modifiers), egui::Event::Key { key, physical_key: Some(key), pressed: true, repeat: false, modifiers }], + ..Default::default() + }; + ctx.begin_pass(raw); + crate::shortcuts::handle(app, ctx); + 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) +} + +#[test] +fn alt_digits_set_the_smoothing() { + for tool in [Tool::Brush, Tool::Pencil, Tool::Eraser] { + let ctx = egui::Context::default(); + 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}"); + assert_eq!((o, f), (opacity, flow), "{tool:?}: Opacity and Flow stay"); + assert!(a.session.tools.brush.smoothing.enabled); + } + } +} + +#[test] +fn alt_digits_keep_the_other_smoothing_options() { + let ctx = egui::Context::default(); + let mut a = app(Tool::Brush); + a.session.tools.brush.smoothing.pulled_string = true; + a.session.tools.brush.smoothing.catch_up = false; + key(&mut a, &ctx, Key::Num6, Modifiers::ALT); + let s = &a.session.tools.brush.smoothing; + assert!((s.amount - 0.6).abs() < 1e-6); + assert!(s.pulled_string && !s.catch_up); +} + +#[test] +fn two_quick_alt_digits_each_set_their_own_tens() { + let ctx = egui::Context::default(); + let mut a = app(Tool::Brush); + key(&mut a, &ctx, Key::Num4, Modifiers::ALT); + key(&mut a, &ctx, Key::Num5, Modifiers::ALT); + assert!((brush(&a).0 - 0.5).abs() < 1e-6, "{}", brush(&a).0); +} + +#[test] +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; + // 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); +} + +#[test] +fn tools_without_a_smoothing_field_ignore_alt_digits() { + for tool in [Tool::Move, Tool::CloneStamp, Tool::Gradient] { + let ctx = egui::Context::default(); + let mut a = app(tool); + let before = (brush(&a), a.ui.tool_options.fill_opacity); + let layer = |a: &PhotocraftApp| { + let st = a.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().opacity + }; + let opacity = layer(&a); + key(&mut a, &ctx, Key::Num3, Modifiers::ALT); + assert_eq!((brush(&a), a.ui.tool_options.fill_opacity), before, "{tool:?}"); + 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 6ccda869952..41c571f3122 100644 --- a/crates/ui-egui/src/input/opacity_keys.rs +++ b/crates/ui-egui/src/input/opacity_keys.rs @@ -5,6 +5,7 @@ //! Opacity; with tools that have no opacity of their own (Move, the selection tools, Type…) the //! active layer's Opacity (⇧: Fill); with the Background Eraser its Tolerance (#11907). Tools whose //! number is a strength or exposure are left alone. +//! ⌥+digit sets the Smoothing of the tools that show it (Brush, Pencil, Eraser; #11892). use egui::{Event, Key}; use serde_json::json; @@ -62,11 +63,38 @@ fn digit(k: Key) -> Option { }) } -/// Consumes this frame's unmodified (or ⇧) digit presses and applies them. +/// The tools whose options bar has a Smoothing field, which ⌥+digit sets (#11892). +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, in the order they were typed. pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) { - let presses: Vec<(u8, bool)> = ctx.input_mut(|i| { + let smoothing = has_smoothing(app.ui.tool); + 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. + Event::Key { key, physical_key, pressed: true, repeat, modifiers } + if smoothing && modifiers.alt && !modifiers.shift && !modifiers.command && !modifiers.ctrl => + { + match physical_key.and_then(digit).or_else(|| digit(*key)) { + Some(d) => { + if !repeat { + out.push(Press::Smoothing(d)); + } + false + } + None => true, + } + } // The physical key, so ⇧+digit is still a digit whatever symbol the layout puts there. Event::Key { key, physical_key, pressed: true, repeat, modifiers } if !modifiers.command && !modifiers.ctrl && !modifiers.alt => { match physical_key.and_then(digit).or_else(|| digit(*key)) { @@ -74,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 } @@ -86,8 +114,16 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) { out }); 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 e496bb38f97..e9555fe0975 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1055,6 +1055,8 @@ 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;