From 2baca48a3dccb2a218c286ccdd3ecf79a99910ed Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 30 Nov 2026 16:38:56 +0100 Subject: [PATCH] Collect macOS menu key equivalents before consuming native key-up --- crates/ui-egui/src/app.rs | 7 ++++++- crates/ui-egui/src/menus/native.rs | 1 - crates/ui-egui/src/tests/nativemenu.rs | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 228a01b2f6..246341ef71 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -380,7 +380,12 @@ impl VectorcraftApp { } /// A window's raw input, before egui and the keys read it (the host's `raw_input_hook`). - pub fn raw_input(&self, ctx: &egui::Context, raw: &mut egui::RawInput) { + pub fn raw_input(&mut self, ctx: &egui::Context, raw: &mut egui::RawInput) { + // AppKit consumes the key-down and winit can already have queued its key-up. + // Poll before the runtime orders the press and release in this frame's input. + if let Some(menu) = self.services.native_menu.as_mut() { + menu.poll(ctx); + } crate::type_editing::keep_layout_text(self, ctx, raw); } diff --git a/crates/ui-egui/src/menus/native.rs b/crates/ui-egui/src/menus/native.rs index 0cd3eeaa87..2450a9b201 100644 --- a/crates/ui-egui/src/menus/native.rs +++ b/crates/ui-egui/src/menus/native.rs @@ -27,7 +27,6 @@ pub(crate) fn run(app: &mut VectorcraftApp, ctx: &egui::Context) { let gate = Gate::default().modal(app.file_dialog_open()).recording_shortcut(recording_shortcut(app)).text_editing(ctx.egui_wants_keyboard_input()); let Some(menu) = app.services.native_menu.as_mut() else { return }; - menu.poll(ctx); menu.set_gate(gate); let clicks = menu.take_clicks(ctx); // A menu opening reads the Scripts folder again. diff --git a/crates/ui-egui/src/tests/nativemenu.rs b/crates/ui-egui/src/tests/nativemenu.rs index 1fad419639..41b5264479 100644 --- a/crates/ui-egui/src/tests/nativemenu.rs +++ b/crates/ui-egui/src/tests/nativemenu.rs @@ -458,3 +458,24 @@ fn a_key_equivalent_paste_pastes_a_picture_from_another_app() { assert_eq!(images(&m), n, "press {n} pastes one picture"); } } + +/// AppKit handles a menu shortcut's key-down before winit delivers the matching key-up. +/// The two must reach the same input batch, even if this frame can have only one pass. +#[test] +fn a_native_undo_and_redo_key_equivalent_each_change_the_canvas_once() { + let mut m = Mac::new(true); + m.ctx.options_mut(|o| o.max_passes = std::num::NonZeroUsize::MIN); + let id = m.app.run("shape.rectangle", json!({"x": 200, "y": 150, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let id = vectorcraft_doc::NodeId(id); + m.frame(vec![]); + for (chord, shift, present) in [("Cmd+Z", false, false), ("Cmd+Shift+Z", true, true)] { + let mut modifiers = if crate::keymap::platform().is_mac() { Modifiers::MAC_CMD } else { Modifiers::CTRL }; + modifiers.command = true; + modifiers.shift = shift; + m.fake.report(MenuEvent::Key(Chord::parse(chord).unwrap())); + m.frame(vec![Event::Key { key: Key::Z, physical_key: Some(Key::Z), pressed: false, repeat: false, modifiers }]); + assert_eq!(m.app.session.active().unwrap().doc.node(id).is_some(), present, "{chord} with the native key-up"); + m.frame(vec![]); + assert_eq!(m.app.session.active().unwrap().doc.node(id).is_some(), present, "{chord} was applied once"); + } +}