Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Document #7190 shortcut-capture root cause and unit-test capture_key_press.

nrq6993-a11ya99277109c4c
diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rs
index a2e426db0..d43a0e689 100644
--- a/crates/ui-egui/src/prefs_ui.rs
+++ b/crates/ui-egui/src/prefs_ui.rs
@@ -893,10 +893,18 @@ fn capture_mods_json(m: egui::Modifiers) -> Value {
})
}
-/// While capture is active: refresh the held modifiers (including across frames) and, if a
-/// non-modifier key was pressed this frame, return it with the modifiers that held with it.
-/// Modifier-key presses count even when egui reports no modifier bits on them (#7190).
-fn capture_key_press(capture_mods: egui::Modifiers, i: &egui::InputState) -> (egui::Modifiers, Option<(egui::Key, egui::Modifiers)>) {
+/// While capture is active: refresh held modifiers (including across frames via dialog
+/// `captureMods`) and, if a non-modifier key was pressed this frame, return it with the
+/// modifiers held with it.
+///
+/// #7190 root cause: capture used to [`find_map`](Iterator::find_map) the first non-modifier
+/// [`Event::Key`] and pass only that event's `modifiers` into [`shortcut_text`]. On Linux and
+/// Windows, winit/egui often reports `ShiftLeft`/`ShiftRight` (and sometimes the letter key) with
+/// [`Modifiers::NONE`], and the modifier down and the letter down often land in separate UI
+/// frames. Shift was then missing from the stored binding (`F6` instead of `Shift+F6`). Like #292
+/// for `ControlLeft`, we walk the frame's events, OR in [`modifier_key_modifiers`] for modifier
+/// keys, and persist accumulated modifiers in `captureMods` until capture ends.
+pub(crate) fn capture_key_press(capture_mods: egui::Modifiers, i: &egui::InputState) -> (egui::Modifiers, Option<(egui::Key, egui::Modifiers)>) {
let mut mods = capture_mods | i.modifiers;
let mut pressed = None;
for e in &i.events {
diff --git a/crates/ui-egui/src/shortcut_capture_tests.rs b/crates/ui-egui/src/shortcut_capture_tests.rs
index 769343e78..ad35fcc7c 100644
--- a/crates/ui-egui/src/shortcut_capture_tests.rs
+++ b/crates/ui-egui/src/shortcut_capture_tests.rs
@@ -13,6 +13,16 @@ fn key(h: &mut Harness<'_, PhotocraftApp>, key: Key, pressed: bool, modifiers: M
h.event(Event::Key { key, physical_key: None, pressed, repeat: false, modifiers });
}
+fn input_state(events: Vec<Event>, modifiers: Modifiers) -> egui::InputState {
+ let ctx = egui::Context::default();
+ ctx.begin_pass(egui::RawInput { events, ..Default::default() });
+ let state = ctx.input(|i| i.clone());
+ ctx.end_pass().textures_delta.clear();
+ let mut state = state;
+ state.modifiers = modifiers;
+ state
+}
+
/// The Keyboard Shortcuts dialog with `command` selected, filtered to it.
fn dialog(ppp: f32, command: &str, filter: &str) -> (Harness<'static, PhotocraftApp>, u64) {
let mut h = Harness::builder().with_size(egui::vec2(1100.0, 760.0)).with_pixels_per_point(ppp).with_max_steps(64).build_eframe(|cc| {
@@ -134,10 +144,32 @@ fn capture(h: &mut Harness<'_, PhotocraftApp>, button: &str, modifier: Key, held
h.run_steps(2);
}
+#[test]
+fn capture_key_press_records_shift_when_key_events_carry_no_modifier_bits() {
+ // Same event pattern as Linux/Windows egui: modifier and letter Key events with empty bits.
+ // Legacy capture → (F6, NONE) → shortcut_text → "F6"; capture_key_press → "Shift+F6".
+ let key_event = |key: Key| Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE };
+ let (mods, pressed) = crate::prefs_ui::capture_key_press(
+ Modifiers::NONE,
+ &input_state(vec![key_event(Key::ShiftLeft), key_event(Key::F6)], Modifiers::NONE),
+ );
+ assert!(mods.shift);
+ let (key, m) = pressed.expect("F6 press");
+ assert_eq!(key, Key::F6);
+ assert!(m.shift);
+ assert_eq!(crate::prefs_ui::shortcut_text(key, m).as_deref(), Some("Shift+F6"));
+ let (mods, pressed) = crate::prefs_ui::capture_key_press(Modifiers::NONE, &input_state(vec![key_event(Key::ShiftLeft)], Modifiers::NONE));
+ assert!(mods.shift);
+ assert!(pressed.is_none(), "modifier alone does not finish capture");
+ let (_, pressed) = crate::prefs_ui::capture_key_press(mods, &input_state(vec![key_event(Key::F6)], Modifiers::NONE));
+ let (_, m) = pressed.expect("letter in a later frame");
+ assert!(m.shift, "captureMods carries Shift across frames");
+ assert_eq!(crate::prefs_ui::shortcut_text(Key::F6, m).as_deref(), Some("Shift+F6"));
+}
+
#[test]
fn shift_is_recorded_when_modifier_key_events_carry_no_bits() {
- // #7190: some platforms report ShiftLeft/ShiftRight with Modifiers::NONE; the real key can
- // too, so capture must walk the frame's events instead of trusting one Key event.
+ // End-to-end: dialog capture → overrides JSON (shortcut_text → prefs::normalize_shortcut).
let (mut h, id) = dialog(1.0, "edit.search", "search…");
h.get_by_label(&crate::shortcuts::pretty("Cmd+F")).click();
h.run_steps(2);