Skip to content

Commit

Permalink
On macOS, fix keyinput missing by calling super class methods (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
wusyong committed Nov 21, 2022
1 parent afb75e4 commit e40e55a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changes/fix-key.md
@@ -0,0 +1,6 @@
---
"wry": patch
---

On macOS, fix keyinput missing by calling superclass methods.

18 changes: 13 additions & 5 deletions src/webview/wkwebview/mod.rs
Expand Up @@ -277,8 +277,8 @@ impl InnerWebView {
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),
sel!(performKeyEquivalent:),
perform_key_equivalent as extern "C" fn(&mut Object, Sel, id),
);
decl.add_method(
sel!(keyDown:),
Expand All @@ -296,19 +296,27 @@ impl InnerWebView {
}
}

extern "C" fn perform_key_equivalent(this: &mut Object, _: Sel, event: id) {
unsafe {
let superclass: *const Class = msg_send![this, superclass];
let () = msg_send![super(this, &*superclass), performKeyEquivalent: event];
}
}

// 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) {
extern "C" fn key_down(this: &mut Object, _: Sel, event: id) {
unsafe {
let superclass: *const Class = msg_send![this, superclass];
let () = msg_send![super(this, &*superclass), keyDown: event];

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 e40e55a

Please sign in to comment.