Merge pull request #15054 from RickiNano/feat/14789-image-size-resample-shortcuts

Image Size: Alt+1 to Alt+8 pick the Resample method (#14789)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-15 16:02:04 +01:00
commit d3249c4ad9
2 files changed
+273 -4

No files matched your search

@@ -0,0 +1,208 @@
//! #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<String, Value> {
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<u8> = 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");
}
+65 -4
View File
@@ -90,6 +90,65 @@ 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<String, Value>, 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 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<String, Value>) {
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 {
swallow_text = true;
if let Some(method) = method.filter(|m| resampling && resample_offered(f, m)) {
picked = Some(method);
}
}
false
});
picked
});
if let Some(method) = picked {
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 {
@@ -353,6 +412,7 @@ fn set_unit(f: &mut Map<String, Value>, key: &str, unit: &str) {
fn image_size(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
let t = Tokens::get(ui.ctx());
resample_shortcut(ui, f);
let (ow, oh) = (num(f, "__origW"), num(f, "__origH"));
let resample = f.get("resample").and_then(Value::as_str).unwrap_or("bicubic").to_string();
let resampling = resample != "none";
@@ -500,10 +560,7 @@ fn image_size(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
("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));
}
@@ -633,3 +690,7 @@ fn anchor_grid(ui: &mut egui::Ui, current: &str, dw: f64, dh: f64) -> Option<&'s
mod image_size_readout_tests;
#[cfg(test)]
mod tests;
#[cfg(test)]
#[path = "image_size_resample_shortcuts_tests.rs"]
mod image_size_resample_shortcuts_tests;