From d40c84a82a8d9dd0faea2e8cfbc924d5ed85f066 Mon Sep 17 00:00:00 2001 From: DuBearte <130190089+DuBearte@users.noreply.github.com> Date: Sat, 7 Nov 2026 09:34:03 -0500 Subject: [PATCH 1/2] Alt+digit sets the Brush, Pencil and Eraser Smoothing (Alt+3 = 30%, Alt+0 = 100%) Fixes #11892. Co-Authored-By: Claude Opus 5.5 --- .../src/brush_alt_digit_smoothing_tests.rs | 100 ++++++++++++++++++ crates/ui-egui/src/input/opacity_keys.rs | 29 ++++- crates/ui-egui/src/lib.rs | 2 + 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs 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..f06e549e8f1 --- /dev/null +++ b/crates/ui-egui/src/brush_alt_digit_smoothing_tests.rs @@ -0,0 +1,100 @@ +//! #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(); +} + +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)] { + 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; + key(&mut a, &ctx, Key::Num4, Modifiers::NONE); + key(&mut a, &ctx, Key::Num5, Modifiers::NONE); + key(&mut a, &ctx, 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"); + } +} diff --git a/crates/ui-egui/src/input/opacity_keys.rs b/crates/ui-egui/src/input/opacity_keys.rs index 7f02c0c77b9..fff2cf30084 100644 --- a/crates/ui-egui/src/input/opacity_keys.rs +++ b/crates/ui-egui/src/input/opacity_keys.rs @@ -3,6 +3,7 @@ //! Eraser they set the brush's Opacity (⇧: Flow; swapped while airbrush build-up is on); with the Gradient or Paint Bucket their //! Opacity; with tools that have no opacity of their own (Move, the selection tools, Type…) the //! active layer's Opacity (⇧: Fill). 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; @@ -57,11 +58,33 @@ 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) +} + +/// Consumes this frame's unmodified (or ⇧) digit presses and applies them, and ⌥+digit presses +/// for the tools with a Smoothing field. 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 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 { + alt_presses.push(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)) { @@ -80,6 +103,10 @@ 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); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 1ba53441ad7..89ba54a4c16 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -989,6 +989,8 @@ mod menu_item_labels_tests; #[cfg(test)] mod pen_path_ownership_tests; +#[cfg(test)] +mod brush_alt_digit_smoothing_tests; #[cfg(test)] mod doc_tab_menu_tests; #[cfg(test)] From 28e04e0d949446cdec734c030832cd43cc395796 Mon Sep 17 00:00:00 2001 From: David Mann Date: Wed, 11 Nov 2026 02:00:11 +0100 Subject: [PATCH 2/2] Brush Alt+digit Smoothing (#12034): keep key order, end a two-digit Opacity, fix fmt The plain and Alt digit presses of one frame now run in the order they were typed, so the Actions recorder sees them in that order, and an Alt digit ends a pending two-digit Opacity value (4, Alt+3, 5 sets 50%, not 45%). The two-digit test now sends its digits in one frame, so it no longer depends on the runner's timing. New tests: the key order, Alt with Shift/Ctrl/Cmd, and Smoothing turned on from off. The lib.rs mod line is in rustfmt order. Co-Authored-By: DuBearte Co-Authored-By: Claude Opus 5.5 --- .../src/brush_alt_digit_smoothing_tests.rs | 36 +++++++++++++++++-- crates/ui-egui/src/input/opacity_keys.rs | 31 ++++++++++------ crates/ui-egui/src/lib.rs | 4 +-- 3 files changed, 55 insertions(+), 16 deletions(-) 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;