From 4c5f49d9ff100880b8981c140949671f55552602 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:31:30 +0100 Subject: [PATCH 1/3] Image Size: Alt+1 to Alt+8 pick the Resample method (#14789) While the dialog is open, Alt+1..Alt+8 (Option on macOS) set Automatic, Preserve Details, (Preserve Details 2.0, not yet), Bicubic Smoother, Bicubic Sharper, Bicubic, Nearest Neighbor and Bilinear. The press is taken out of the frame before the fields draw, so a focused field never gets the digit. Keys do nothing with Resample off or for a method the mode doesn't offer; Ctrl+Alt (AltGr) is left alone. --- .../image_size_resample_shortcuts_tests.rs | 166 ++++++++++++++++++ crates/ui-egui/src/dialogs/sizing.rs | 62 ++++++- 2 files changed, 224 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs diff --git a/crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs b/crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs new file mode 100644 index 0000000000..1a12e11801 --- /dev/null +++ b/crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs @@ -0,0 +1,166 @@ +//! #14789: while the Image Size dialog is open, Alt+1 to Alt+8 (Option on macOS) pick the +//! Resample method, also while a field has focus, without typing the digit into it. + +use super::*; +use egui::accesskit::Role; +use egui::{Key, Modifiers}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; + +/// An open Image Size dialog for a 200 x 100 px document; `indexed` makes it Indexed Color. +fn image_size_dialog(indexed: bool) -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 100})).unwrap(); + if indexed { + app.session.execute("image.mode.indexedColor", json!({})).unwrap(); + } + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + open(h.state_mut(), "image.imageSize").unwrap(); + h.run_steps(3); + h +} + +fn fields(h: &Harness<'static, PhotocraftApp>) -> Map { + h.state().ui.dialogs.first().map(|d| d.fields.clone()).expect("the dialog is open") +} + +fn resample(h: &Harness<'static, PhotocraftApp>) -> Value { + fields(h).get("resample").cloned().unwrap_or(Value::Null) +} + +fn set_resample(h: &mut Harness<'static, PhotocraftApp>, method: &str) { + if let Some(d) = h.state_mut().ui.dialogs.first_mut() { + d.fields.insert("resample".into(), json!(method)); + } + h.run_steps(2); +} + +/// Presses and releases `key` with `modifiers` in one frame, plus the text a real keyboard types +/// with it: winit sends the digit along with Alt+digit on Windows and Linux. (The harness's own +/// key helpers give each event a frame of its own, which a real keyboard doesn't.) +fn press(h: &mut Harness<'static, PhotocraftApp>, modifiers: Modifiers, key: Key, text: &str) { + let events = &mut h.input_mut().events; + events.push(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }); + events.push(egui::Event::Text(text.into())); + events.push(egui::Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers }); + h.run_steps(2); +} + +/// Each key picks its method: Alt+1 Automatic, Alt+2 Preserve Details, Alt+4 Bicubic Smoother, +/// Alt+5 Bicubic Sharper, Alt+6 Bicubic, Alt+7 Nearest Neighbor, Alt+8 Bilinear. +#[test] +fn each_alt_digit_picks_its_method() { + let mut h = image_size_dialog(false); + for (key, digit, method) in [ + (Key::Num7, "7", "nearest"), + (Key::Num1, "1", "automatic"), + (Key::Num2, "2", "preserveDetails"), + (Key::Num4, "4", "bicubicSmoother"), + (Key::Num5, "5", "bicubicSharper"), + (Key::Num6, "6", "bicubic"), + (Key::Num8, "8", "bilinear"), + ] { + press(&mut h, Modifiers::ALT, key, digit); + assert_eq!(resample(&h), json!(method), "Alt+{digit}"); + } + // The list shows the picked method. + assert!(h.query_all_by_role(Role::ComboBox).any(|n| n.value().as_deref() == Some("Bilinear")), "the Resample list shows Bilinear"); +} + +/// The dialog opens with Width focused and selected: Alt+7 picks Nearest Neighbor and types +/// nothing into it, and the field still takes typing afterwards. +#[test] +fn a_focused_field_does_not_get_the_digit() { + let mut h = image_size_dialog(false); + assert!(h.query_all_by_role(Role::SpinButton).next().expect("the Width field").is_focused(), "Width has focus"); + press(&mut h, Modifiers::ALT, Key::Num7, "7"); + assert_eq!(resample(&h), json!("nearest")); + assert_eq!((num(&fields(&h), "width"), num(&fields(&h), "height")), (200.0, 100.0), "the 7 was not typed into Width"); + assert!(h.query_all_by_role(Role::SpinButton).next().expect("the Width field").is_focused(), "Width keeps the focus"); + // macOS: Option+7 types "¶" along with the key. + press(&mut h, Modifiers::ALT, Key::Num8, "¶"); + assert_eq!(resample(&h), json!("bilinear")); + assert_eq!(num(&fields(&h), "width"), 200.0, "Option's character was not typed into Width"); + h.event(egui::Event::Text("50".into())); + h.run_steps(1); + assert_eq!(num(&fields(&h), "width"), 50.0, "plain typing still edits Width"); +} + +/// Alt+3 is Preserve Details 2.0, which doesn't exist yet (#2942): it does nothing, and Lanczos, +/// which the reference application doesn't have, has no key. +#[test] +fn a_method_that_does_not_exist_does_nothing() { + let mut h = image_size_dialog(false); + set_resample(&mut h, "bicubicSharper"); + press(&mut h, Modifiers::ALT, Key::Num3, "3"); + assert_eq!(resample(&h), json!("bicubicSharper")); + assert_eq!(num(&fields(&h), "width"), 200.0, "the 3 was not typed into Width"); + assert!(RESAMPLE_KEYS.iter().all(|(_, m)| *m != Some("lanczos")), "Lanczos gets no shortcut"); + // Alt+9 and Alt+0 aren't shortcuts. + press(&mut h, Modifiers::ALT, Key::Num9, "9"); + assert_eq!(resample(&h), json!("bicubicSharper")); +} + +/// Indexed Color offers only Automatic and Nearest Neighbor: the other keys do nothing there. +#[test] +fn a_method_the_mode_does_not_offer_does_nothing() { + let mut h = image_size_dialog(true); + set_resample(&mut h, "nearest"); + for (key, digit) in [(Key::Num2, "2"), (Key::Num4, "4"), (Key::Num5, "5"), (Key::Num6, "6"), (Key::Num8, "8")] { + press(&mut h, Modifiers::ALT, key, digit); + assert_eq!(resample(&h), json!("nearest"), "Alt+{digit} is not offered for Indexed Color"); + } + press(&mut h, Modifiers::ALT, Key::Num1, "1"); + assert_eq!(resample(&h), json!("automatic")); + press(&mut h, Modifiers::ALT, Key::Num7, "7"); + assert_eq!(resample(&h), json!("nearest")); + assert_eq!(num(&fields(&h), "width"), 200.0, "no digit was typed into Width"); +} + +/// With Resample off the keys do nothing: Resample stays off and the size stays. +#[test] +fn resample_off_ignores_the_keys() { + let mut h = image_size_dialog(false); + set_resample(&mut h, "none"); + for (key, digit) in [(Key::Num1, "1"), (Key::Num7, "7"), (Key::Num8, "8")] { + press(&mut h, Modifiers::ALT, key, digit); + assert_eq!(resample(&h), json!("none"), "Alt+{digit}"); + } + let f = fields(&h); + assert_eq!((num(&f, "width"), num(&f, "height")), (200.0, 100.0), "{f:?}"); +} + +/// Ctrl+Alt is AltGr on Windows, which types characters on many layouts: it is no shortcut. +#[test] +fn ctrl_alt_digit_is_not_a_shortcut() { + let mut h = image_size_dialog(false); + set_resample(&mut h, "bicubic"); + press(&mut h, Modifiers::ALT | Modifiers::CTRL, Key::Num7, "{"); + assert_eq!(resample(&h), json!("bicubic")); +} + +/// OK resizes with the method a shortcut picked: Nearest Neighbor doubles a hard black/white +/// edge without the grey a blending method puts between them. +#[test] +fn ok_applies_the_picked_method() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 4, "height": 1, "background": "white"})).unwrap(); + app.session.execute("select.rect", json!({"x": 0, "y": 0, "width": 2, "height": 1})).unwrap(); + app.session.execute("edit.fill", json!({"contents": "black"})).unwrap(); + app.session.execute("select.deselect", json!({})).unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_ui_state(|ui, app| crate::dialogs::show(app, ui.ctx()), app); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + let id = open(h.state_mut(), "image.imageSize").unwrap(); + h.run_steps(3); + press(&mut h, Modifiers::ALT, Key::Num7, "7"); + if let Some(d) = h.state_mut().ui.dialog_mut(id) { + d.fields.insert("width".into(), json!(8)); + d.fields.insert("height".into(), json!(2)); + } + crate::dialogs::confirm(h.state_mut(), id).expect("OK runs Image Size"); + let doc = &h.state().session.active().unwrap().doc; + assert_eq!(doc.size, photocraft_doc::Size::new(8, 2)); + let reds: Vec = photocraft_compose::flatten(doc).px.iter().take(8).map(|p| (p[0] * 255.0).round() as u8).collect(); + assert_eq!(reds, [0, 0, 0, 0, 255, 255, 255, 255], "Nearest Neighbor keeps the edge hard"); +} diff --git a/crates/ui-egui/src/dialogs/sizing.rs b/crates/ui-egui/src/dialogs/sizing.rs index 3f05834a88..fd3af57994 100644 --- a/crates/ui-egui/src/dialogs/sizing.rs +++ b/crates/ui-egui/src/dialogs/sizing.rs @@ -318,6 +318,7 @@ fn image_size(ui: &mut egui::Ui, f: &mut Map) { let t = Tokens::get(ui.ctx()); let (ow, oh) = (num(f, "__origW"), num(f, "__origH")); let bpp = num(f, "__bytesPerPixel").max(1.0); + resample_shortcut(ui, f); let resample = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic").to_string(); let resampling = resample != "none"; if !resampling { @@ -452,10 +453,7 @@ fn image_size(ui: &mut egui::Ui, f: &mut Map) { ("bilinear".to_string(), tl!("Bilinear")), ("lanczos".to_string(), tl!("Lanczos (sharp)")), ]; - // Indexed Color: every other method blends colours the colour table doesn't have, - // which the command doesn't use and a save quantises away (#6019). - let indexed = f.get("__indexed").and_then(Value::as_bool) == Some(true); - let opts: Vec<_> = opts.into_iter().filter(|(k, _)| !indexed || k == "automatic" || k == "nearest").collect(); + let opts: Vec<_> = opts.into_iter().filter(|(k, _)| resample_offered(f, k)).collect(); if crate::widgets::dropdown(ui, "is-resample", tl!("Resample"), &mut cur, &opts, 200.0) { f.insert("resample".into(), json!(cur)); } @@ -463,6 +461,58 @@ fn image_size(ui: &mut egui::Ui, f: &mut Map) { }); } +/// Whether the Resample list offers `method`. Indexed Color: every method but Automatic and +/// Nearest Neighbor blends colours the colour table doesn't have, which the command doesn't use +/// and a save quantises away (#6019). +fn resample_offered(f: &Map, method: &str) -> bool { + let indexed = f.get("__indexed").and_then(Value::as_bool) == Some(true); + !indexed || method == "automatic" || method == "nearest" +} + +/// Alt+1 to Alt+8 (Option on macOS) and the Resample method each picks, as in the reference +/// application. Alt+3 is Preserve Details 2.0, which PhotoCraft doesn't have yet (#2942), and +/// Lanczos has no key. +const RESAMPLE_KEYS: [(egui::Key, Option<&str>); 8] = [ + (egui::Key::Num1, Some("automatic")), + (egui::Key::Num2, Some("preserveDetails")), + (egui::Key::Num3, None), + (egui::Key::Num4, Some("bicubicSmoother")), + (egui::Key::Num5, Some("bicubicSharper")), + (egui::Key::Num6, Some("bicubic")), + (egui::Key::Num7, Some("nearest")), + (egui::Key::Num8, Some("bilinear")), +]; + +/// Takes an Alt+1 to Alt+8 press out of this frame's input and picks its Resample method, when +/// Resample is on and the list offers it. It runs before the fields are drawn, so the press (and +/// the digit, or Option's character on macOS, the key also types) never reaches a focused field. +/// Ctrl+Alt is left alone: on Windows it's AltGr, which types characters on many layouts. +fn resample_shortcut(ui: &egui::Ui, f: &mut Map) { + let picked = ui.ctx().input_mut(|i| { + let mut picked = None; + i.events.retain(|e| { + let egui::Event::Key { key, pressed, modifiers, .. } = e else { return true }; + if !modifiers.alt || modifiers.ctrl || modifiers.command || modifiers.mac_cmd { + return true; + } + let Some(&(_, method)) = RESAMPLE_KEYS.iter().find(|(k, _)| k == key) else { return true }; + if *pressed { + picked = Some(method); + } + false + }); + if picked.is_some() { + i.events.retain(|e| !matches!(e, egui::Event::Text(_))); + } + picked + }); + let Some(Some(method)) = picked else { return }; + let resampling = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic") != "none"; + if resampling && resample_offered(f, method) { + f.insert("resample".into(), json!(method)); + } +} + fn canvas_size(ui: &mut egui::Ui, f: &mut Map) { let t = Tokens::get(ui.ctx()); let (ow, oh) = (num(f, "__origW"), num(f, "__origH")); @@ -582,3 +632,7 @@ fn anchor_grid(ui: &mut egui::Ui, current: &str, dw: f64, dh: f64) -> Option<&'s #[cfg(test)] mod tests; + +#[cfg(test)] +#[path = "image_size_resample_shortcuts_tests.rs"] +mod image_size_resample_shortcuts_tests; From 1d7edc34285b3a8ba4e929e742a411ba5a0dd570 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sun, 15 Nov 2026 09:32:19 +0100 Subject: [PATCH 2/3] Image Size: keep the Resample shortcut lines clear of the sibling Image Size PRs (#14789) --- crates/ui-egui/src/dialogs/sizing.rs | 107 ++++++++++++++------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/crates/ui-egui/src/dialogs/sizing.rs b/crates/ui-egui/src/dialogs/sizing.rs index fd3af57994..8b1b086a9f 100644 --- a/crates/ui-egui/src/dialogs/sizing.rs +++ b/crates/ui-egui/src/dialogs/sizing.rs @@ -90,6 +90,59 @@ pub(crate) fn pref_resample(i: photocraft_engine::prefs::Interpolation) -> &'sta Interpolation::PreserveDetails => "preserveDetails", } } + +/// Whether the Resample list offers `method`. Indexed Color: every method but Automatic and +/// Nearest Neighbor blends colours the colour table doesn't have, which the command doesn't use +/// and a save quantises away (#6019). +fn resample_offered(f: &Map, method: &str) -> bool { + let indexed = f.get("__indexed").and_then(Value::as_bool) == Some(true); + !indexed || method == "automatic" || method == "nearest" +} + +/// Alt+1 to Alt+8 (Option on macOS) and the Resample method each picks, as in the reference +/// application. Alt+3 is Preserve Details 2.0, which PhotoCraft doesn't have yet (#2942), and +/// Lanczos has no key. +const RESAMPLE_KEYS: [(egui::Key, Option<&str>); 8] = [ + (egui::Key::Num1, Some("automatic")), + (egui::Key::Num2, Some("preserveDetails")), + (egui::Key::Num3, None), + (egui::Key::Num4, Some("bicubicSmoother")), + (egui::Key::Num5, Some("bicubicSharper")), + (egui::Key::Num6, Some("bicubic")), + (egui::Key::Num7, Some("nearest")), + (egui::Key::Num8, Some("bilinear")), +]; + +/// Takes an Alt+1 to Alt+8 press out of this frame's input and picks its Resample method, when +/// Resample is on and the list offers it. It runs before the fields are drawn, so the press (and +/// the digit, or Option's character on macOS, the key also types) never reaches a focused field. +/// Ctrl+Alt is left alone: on Windows it's AltGr, which types characters on many layouts. +fn resample_shortcut(ui: &egui::Ui, f: &mut Map) { + let picked = ui.ctx().input_mut(|i| { + let mut picked = None; + i.events.retain(|e| { + let egui::Event::Key { key, pressed, modifiers, .. } = e else { return true }; + if !modifiers.alt || modifiers.ctrl || modifiers.command || modifiers.mac_cmd { + return true; + } + let Some(&(_, method)) = RESAMPLE_KEYS.iter().find(|(k, _)| k == key) else { return true }; + if *pressed { + picked = Some(method); + } + false + }); + if picked.is_some() { + i.events.retain(|e| !matches!(e, egui::Event::Text(_))); + } + picked + }); + let Some(Some(method)) = picked else { return }; + let resampling = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic") != "none"; + if resampling && resample_offered(f, method) { + f.insert("resample".into(), json!(method)); + } +} + const ANCHORS: [&str; 9] = ["topLeft", "top", "topRight", "left", "center", "right", "bottomLeft", "bottom", "bottomRight"]; pub fn is_sizing(command: &str) -> bool { @@ -316,9 +369,9 @@ fn set_unit(f: &mut Map, key: &str, unit: &str) { fn image_size(ui: &mut egui::Ui, f: &mut Map) { let t = Tokens::get(ui.ctx()); + resample_shortcut(ui, f); let (ow, oh) = (num(f, "__origW"), num(f, "__origH")); let bpp = num(f, "__bytesPerPixel").max(1.0); - resample_shortcut(ui, f); let resample = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic").to_string(); let resampling = resample != "none"; if !resampling { @@ -461,58 +514,6 @@ fn image_size(ui: &mut egui::Ui, f: &mut Map) { }); } -/// Whether the Resample list offers `method`. Indexed Color: every method but Automatic and -/// Nearest Neighbor blends colours the colour table doesn't have, which the command doesn't use -/// and a save quantises away (#6019). -fn resample_offered(f: &Map, method: &str) -> bool { - let indexed = f.get("__indexed").and_then(Value::as_bool) == Some(true); - !indexed || method == "automatic" || method == "nearest" -} - -/// Alt+1 to Alt+8 (Option on macOS) and the Resample method each picks, as in the reference -/// application. Alt+3 is Preserve Details 2.0, which PhotoCraft doesn't have yet (#2942), and -/// Lanczos has no key. -const RESAMPLE_KEYS: [(egui::Key, Option<&str>); 8] = [ - (egui::Key::Num1, Some("automatic")), - (egui::Key::Num2, Some("preserveDetails")), - (egui::Key::Num3, None), - (egui::Key::Num4, Some("bicubicSmoother")), - (egui::Key::Num5, Some("bicubicSharper")), - (egui::Key::Num6, Some("bicubic")), - (egui::Key::Num7, Some("nearest")), - (egui::Key::Num8, Some("bilinear")), -]; - -/// Takes an Alt+1 to Alt+8 press out of this frame's input and picks its Resample method, when -/// Resample is on and the list offers it. It runs before the fields are drawn, so the press (and -/// the digit, or Option's character on macOS, the key also types) never reaches a focused field. -/// Ctrl+Alt is left alone: on Windows it's AltGr, which types characters on many layouts. -fn resample_shortcut(ui: &egui::Ui, f: &mut Map) { - let picked = ui.ctx().input_mut(|i| { - let mut picked = None; - i.events.retain(|e| { - let egui::Event::Key { key, pressed, modifiers, .. } = e else { return true }; - if !modifiers.alt || modifiers.ctrl || modifiers.command || modifiers.mac_cmd { - return true; - } - let Some(&(_, method)) = RESAMPLE_KEYS.iter().find(|(k, _)| k == key) else { return true }; - if *pressed { - picked = Some(method); - } - false - }); - if picked.is_some() { - i.events.retain(|e| !matches!(e, egui::Event::Text(_))); - } - picked - }); - let Some(Some(method)) = picked else { return }; - let resampling = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic") != "none"; - if resampling && resample_offered(f, method) { - f.insert("resample".into(), json!(method)); - } -} - fn canvas_size(ui: &mut egui::Ui, f: &mut Map) { let t = Tokens::get(ui.ctx()); let (ow, oh) = (num(f, "__origW"), num(f, "__origH")); From cc51a09f814825dc50e1a58d404072a272b96d1d Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:38:24 +0100 Subject: [PATCH 3/3] Image Size: a key that does nothing keeps an earlier pick, and other typing in the frame stays (#14789) Several Alt+digit presses in one frame pick the last offered method; an Alt+3 or a method the mode doesn't offer no longer cancels an earlier pick. Only the text right after each swallowed press (its digit) is dropped, so ordinary typing batched into the same frame still reaches the focused field. --- .../image_size_resample_shortcuts_tests.rs | 42 +++++++++++++++++++ crates/ui-egui/src/dialogs/sizing.rs | 28 ++++++++----- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs b/crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs index 1a12e11801..3dfa4ac7df 100644 --- a/crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs +++ b/crates/ui-egui/src/dialogs/image_size_resample_shortcuts_tests.rs @@ -164,3 +164,45 @@ fn ok_applies_the_picked_method() { let reds: Vec = photocraft_compose::flatten(doc).px.iter().take(8).map(|p| (p[0] * 255.0).round() as u8).collect(); assert_eq!(reds, [0, 0, 0, 0, 255, 255, 255, 255], "Nearest Neighbor keeps the edge hard"); } + +/// Queues `key` with `modifiers` as a real keyboard sends it (press, its text, release) into the +/// next frame's input, without running it. +fn queue(h: &mut Harness<'static, PhotocraftApp>, modifiers: Modifiers, key: Key, text: &str) { + let events = &mut h.input_mut().events; + events.push(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }); + events.push(egui::Event::Text(text.into())); + events.push(egui::Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers }); +} + +/// Several shortcuts in one frame: one that does nothing (Alt+3, or a method Indexed Color +/// doesn't offer) doesn't undo an earlier one that picked a method. +#[test] +fn a_key_that_does_nothing_keeps_the_earlier_pick_in_the_same_frame() { + let mut h = image_size_dialog(false); + set_resample(&mut h, "bicubic"); + queue(&mut h, Modifiers::ALT, Key::Num7, "7"); + queue(&mut h, Modifiers::ALT, Key::Num3, "3"); + h.run_steps(2); + assert_eq!(resample(&h), json!("nearest"), "Alt+3 after Alt+7 keeps Nearest Neighbor"); + let mut h = image_size_dialog(true); + set_resample(&mut h, "automatic"); + queue(&mut h, Modifiers::ALT, Key::Num7, "7"); + queue(&mut h, Modifiers::ALT, Key::Num5, "5"); + h.run_steps(2); + assert_eq!(resample(&h), json!("nearest"), "Indexed Color: Alt+5 after Alt+7 keeps Nearest Neighbor"); + assert_eq!(num(&fields(&h), "width"), 200.0, "neither digit was typed"); +} + +/// Ordinary typing that arrives in the same frame as a shortcut still reaches the focused field; +/// only the shortcut's own digit is dropped. +#[test] +fn typing_in_the_same_frame_as_a_shortcut_is_kept() { + let mut h = image_size_dialog(false); + assert!(h.query_all_by_role(Role::SpinButton).next().expect("the Width field").is_focused(), "Width has focus"); + h.input_mut().events.push(egui::Event::Text("5".into())); + queue(&mut h, Modifiers::ALT, Key::Num7, "7"); + h.input_mut().events.push(egui::Event::Text("0".into())); + h.run_steps(2); + assert_eq!(resample(&h), json!("nearest")); + assert_eq!(num(&fields(&h), "width"), 50.0, "the typed 5 and 0 replaced the selected 200; the shortcut's 7 didn't"); +} diff --git a/crates/ui-egui/src/dialogs/sizing.rs b/crates/ui-egui/src/dialogs/sizing.rs index 8b1b086a9f..afbc4890c0 100644 --- a/crates/ui-egui/src/dialogs/sizing.rs +++ b/crates/ui-egui/src/dialogs/sizing.rs @@ -113,32 +113,38 @@ const RESAMPLE_KEYS: [(egui::Key, Option<&str>); 8] = [ (egui::Key::Num8, Some("bilinear")), ]; -/// Takes an Alt+1 to Alt+8 press out of this frame's input and picks its Resample method, when -/// Resample is on and the list offers it. It runs before the fields are drawn, so the press (and -/// the digit, or Option's character on macOS, the key also types) never reaches a focused field. -/// Ctrl+Alt is left alone: on Windows it's AltGr, which types characters on many layouts. +/// Takes Alt+1 to Alt+8 presses out of this frame's input and picks the Resample method of the +/// last one that Resample (on) and the list offer; the others do nothing. It runs before the +/// fields are drawn, so the press never reaches a focused field, and neither does the text the +/// key types (the digit, or Option's character on macOS), which winit sends right after its press. +/// Other typing in the same frame is kept. Ctrl+Alt is left alone: on Windows it's AltGr, which +/// types characters on many layouts. fn resample_shortcut(ui: &egui::Ui, f: &mut Map) { + let resampling = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic") != "none"; let picked = ui.ctx().input_mut(|i| { let mut picked = None; + let mut swallow_text = false; i.events.retain(|e| { + if let egui::Event::Text(_) = e { + return !std::mem::take(&mut swallow_text); + } + swallow_text = false; let egui::Event::Key { key, pressed, modifiers, .. } = e else { return true }; if !modifiers.alt || modifiers.ctrl || modifiers.command || modifiers.mac_cmd { return true; } let Some(&(_, method)) = RESAMPLE_KEYS.iter().find(|(k, _)| k == key) else { return true }; if *pressed { - picked = Some(method); + swallow_text = true; + if let Some(method) = method.filter(|m| resampling && resample_offered(f, m)) { + picked = Some(method); + } } false }); - if picked.is_some() { - i.events.retain(|e| !matches!(e, egui::Event::Text(_))); - } picked }); - let Some(Some(method)) = picked else { return }; - let resampling = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic") != "none"; - if resampling && resample_offered(f, method) { + if let Some(method) = picked { f.insert("resample".into(), json!(method)); } }