Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Keyboard Shortcuts: the recorder waits for the key pressed with the modifiers

aionggbaa0fa349478
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 34832db14..cf3988b17 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -144,6 +144,11 @@ fn user_dictionary(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {
});
}
+fn is_modifier_key(key: egui::Key) -> bool {
+ use egui::Key::*;
+ matches!(key, ShiftLeft | ShiftRight | ControlLeft | ControlRight | AltLeft | AltRight | SuperLeft | SuperRight)
+}
+
fn keyboard_shortcuts(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {
let mut q = d.s("query");
ui.add(
@@ -153,11 +158,12 @@ fn keyboard_shortcuts(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {
);
d.fields.insert("query".into(), json!(q));
let recording = d.s("recording");
- // Capture the next key press for the command being recorded.
+ // Capture the next key press for the command being recorded. Pressing a modifier
+ // reports the modifier itself as a key; skip it and wait for the key it modifies.
if !recording.is_empty() {
let pressed = ui.input(|i| {
i.events.iter().find_map(|e| match e {
- egui::Event::Key { key, pressed: true, modifiers, .. } => Some((*key, *modifiers)),
+ egui::Event::Key { key, pressed: true, modifiers, .. } if !is_modifier_key(*key) => Some((*key, *modifiers)),
_ => None,
})
});
@@ -2681,4 +2687,19 @@ mod tests {
let _ = command_fields(c.params);
}
}
+
+ #[test]
+ fn shortcut_recorder_waits_for_the_key_after_its_modifiers() {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let mut d = Dialog::new("keyboardShortcuts", json!({"query": "", "recording": "app.palette"}));
+ let ctx = egui::Context::default();
+ let key = |key, modifiers| egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers };
+ // Holding Ctrl reports Ctrl itself as a key press, then Ctrl+K arrives.
+ for event in [key(egui::Key::ControlLeft, egui::Modifiers::COMMAND), key(egui::Key::K, egui::Modifiers::COMMAND)] {
+ let input = egui::RawInput { events: vec![event], ..Default::default() };
+ ctx.run_ui(input, |ui| keyboard_shortcuts(&mut app, ui, &mut d)).textures_delta.clear();
+ }
+ assert_eq!(crate::menus::shortcut_of(&app, "app.palette").as_deref(), Some("Cmd+K"));
+ assert_eq!(d.s("recording"), "", "recording ends with the recorded shortcut");
+ }
}