From 2e5e1381789c332654a5ffee47d578042a9be87b Mon Sep 17 00:00:00 2001 From: "Ngo Iok Ui (Wu Yu Wei)" Date: Mon, 21 Nov 2022 16:11:57 +0800 Subject: [PATCH] On macOS, pass key event to menu on key press (#760) --- .changes/key-eq.md | 6 ++++++ src/webview/wkwebview/mod.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 .changes/key-eq.md diff --git a/.changes/key-eq.md b/.changes/key-eq.md new file mode 100644 index 000000000..57ded3379 --- /dev/null +++ b/.changes/key-eq.md @@ -0,0 +1,6 @@ +--- +"wry": patch +--- + +On macOS, pass key event to menu if we have one on key press. + diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 161b0595d..2995ac08d 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -276,6 +276,14 @@ impl InnerWebView { sel!(acceptsFirstMouse:), accept_first_mouse as extern "C" fn(&Object, Sel, id) -> BOOL, ); + decl.add_method( + sel!(doCommandBySelector:), + do_command_by_selector as extern "C" fn(&Object, Sel, Sel), + ); + decl.add_method( + sel!(keyDown:), + key_down as extern "C" fn(&mut Object, Sel, id), + ); extern "C" fn accept_first_mouse(this: &Object, _sel: Sel, _event: id) -> BOOL { unsafe { @@ -287,6 +295,20 @@ impl InnerWebView { } } } + + // Key event chain is consumed by window and cannot pass to menu. + // So we pass the event to menu if we have one + extern "C" fn key_down(_: &mut Object, _: Sel, event: id) { + unsafe { + let app = cocoa::appkit::NSApp(); + let menu: id = msg_send![app, mainMenu]; + if !menu.is_null() { + let () = msg_send![menu, performKeyEquivalent: event]; + } + } + } + // We need to declare this method to get Command+ key equivalent. + extern "C" fn do_command_by_selector(_: &Object, _: Sel, _: Sel) {} } decl.register() }