PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): record Shift in Keyboard Shortcuts capture (storytold/photocraft#7190)
nrq6993-a11yfc298ebf12c6
diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rsindex 6c910865b..b86c459f7 100644--- a/crates/ui-egui/src/prefs_ui.rs+++ b/crates/ui-egui/src/prefs_ui.rs@@ -859,6 +859,67 @@ pub fn is_modifier_key(key: egui::Key) -> bool {matches!(key, ShiftLeft | ShiftRight | ControlLeft | ControlRight | AltLeft | AltRight | SuperLeft | SuperRight)}+/// Modifier bits a physical modifier-key press contributes when egui leaves them out of+/// [`Modifiers`] on the key event (#7190).+fn modifier_key_modifiers(key: egui::Key) -> egui::Modifiers {+ use egui::Key::*;+ match key {+ ShiftLeft | ShiftRight => egui::Modifiers::SHIFT,+ ControlLeft | ControlRight => egui::Modifiers::CTRL,+ AltLeft | AltRight => egui::Modifiers::ALT,+ SuperLeft | SuperRight => egui::Modifiers::COMMAND,+ _ => egui::Modifiers::NONE,+ }+}++fn capture_mods_field(v: Option<&Value>) -> egui::Modifiers {+ let o = v.and_then(Value::as_object);+ egui::Modifiers {+ shift: o.and_then(|o| o.get("shift")).and_then(Value::as_bool).unwrap_or(false),+ ctrl: o.and_then(|o| o.get("ctrl")).and_then(Value::as_bool).unwrap_or(false),+ alt: o.and_then(|o| o.get("alt")).and_then(Value::as_bool).unwrap_or(false),+ command: o.and_then(|o| o.get("command")).and_then(Value::as_bool).unwrap_or(false),+ mac_cmd: o.and_then(|o| o.get("mac_cmd")).and_then(Value::as_bool).unwrap_or(false),+ }+}++fn capture_mods_json(m: egui::Modifiers) -> Value {+ json!({+ "shift": m.shift,+ "ctrl": m.ctrl,+ "alt": m.alt,+ "command": m.command,+ "mac_cmd": m.mac_cmd,+ })+}++/// 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)>) {+ let mut mods = capture_mods | i.modifiers;+ let mut pressed = None;+ for e in &i.events {+ if let egui::Event::ModifiersChanged(m) = e {+ mods = *m;+ }+ if let egui::Event::Key { key, pressed: true, modifiers, .. } = e {+ if is_modifier_key(*key) {+ mods = *modifiers | modifier_key_modifiers(*key);+ continue;+ }+ pressed = Some((*key, mods | *modifiers));+ break;+ }+ if let egui::Event::Key { key, pressed: false, modifiers, .. } = e {+ if is_modifier_key(*key) {+ mods = *modifiers;+ }+ }+ }+ (mods, pressed)+}+// ------------------------------------------------------------------ menu routing/// Shell front ends for Edit-menu commands invoked without parameters (dialogs, pickers).@@ -1767,14 +1828,11 @@ fn shortcuts_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<String}p.alternates(id, main)};+ let mut capture_mods = capture_mods_field(f.get("captureMods"));// Key capture for the selected command: its main key, or (Add Shortcut) one more key.if capture && tab == 0 && !selected.is_empty() {- let pressed = ui.input(|i| {- i.events.iter().find_map(|e| match e {- egui::Event::Key { key, pressed: true, modifiers, .. } if !is_modifier_key(*key) => Some((*key, *modifiers)),- _ => None,- })- });+ let (next_mods, pressed) = ui.input(|i| capture_key_press(capture_mods, i));+ capture_mods = next_mods;if let Some((key, m)) = pressed {ui.input_mut(|i| {i.consume_key(m, key);@@ -2054,6 +2112,11 @@ fn shortcuts_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Stringf.insert("colors".into(), json!(colors));f.insert("selected".into(), json!(selected));f.insert("capture".into(), json!(capture));+ if capture {+ f.insert("captureMods".into(), capture_mods_json(capture_mods));+ } else {+ f.remove("captureMods");+ }f.insert("message".into(), json!(message));f.insert("messageWarning".into(), json!(message_warning));}diff --git a/crates/ui-egui/src/shortcut_capture_tests.rs b/crates/ui-egui/src/shortcut_capture_tests.rsindex f5db00170..f0dea8a78 100644--- a/crates/ui-egui/src/shortcut_capture_tests.rs+++ b/crates/ui-egui/src/shortcut_capture_tests.rs@@ -134,6 +134,21 @@ fn capture(h: &mut Harness<'_, PhotocraftApp>, button: &str, modifier: Key, heldh.run_steps(2);}+#[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.+ let (mut h, id) = dialog(1.0, "edit.search", "search…");+ h.get_by_label(&crate::shortcuts::pretty("Cmd+F")).click();+ h.run_steps(2);+ key(&mut h, Key::ShiftLeft, true, Modifiers::NONE);+ h.run_steps(1);+ key(&mut h, Key::F6, true, Modifiers::NONE);+ h.run_steps(2);+ assert_eq!(field(&h, id, "overrides")["edit.search"], json!("Shift+F6"));+ assert_eq!(field(&h, id, "capture"), json!(false));+}+#[test]fn ctrl_k_is_recorded_as_ctrl_k_not_the_control_key() {for ppp in [1.0, 2.0] {