Skip to content

Commit

Permalink
On macOS, pass key event to menu on key press (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
wusyong authored Nov 21, 2022
1 parent 487dff0 commit 2e5e138
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changes/key-eq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": patch
---

On macOS, pass key event to menu if we have one on key press.

22 changes: 22 additions & 0 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down

0 comments on commit 2e5e138

Please sign in to comment.