diff --git a/.changes/fix-macos-key-equivalent.md b/.changes/fix-macos-key-equivalent.md new file mode 100644 index 000000000..59a100f15 --- /dev/null +++ b/.changes/fix-macos-key-equivalent.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +On macOS, fixed an issue of not being able to listen to the cmd+key event in javascript in single WebView. diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs index 31a434728..78cffa1e0 100644 --- a/src/wkwebview/mod.rs +++ b/src/wkwebview/mod.rs @@ -398,15 +398,6 @@ impl InnerWebView { sel!(acceptsFirstMouse:), accept_first_mouse as extern "C" fn(&Object, Sel, id) -> BOOL, ); - decl.add_method( - sel!(performKeyEquivalent:), - key_equivalent as extern "C" fn(&mut Object, Sel, id) -> BOOL, - ); - - extern "C" fn key_equivalent(_this: &mut Object, _sel: Sel, _event: id) -> BOOL { - NO - } - extern "C" fn accept_first_mouse(this: &Object, _sel: Sel, _event: id) -> BOOL { unsafe { let accept: bool = *this.get_ivar(ACCEPT_FIRST_MOUSE); @@ -417,6 +408,21 @@ impl InnerWebView { } } } + + // This is a temporary workaround for https://github.com/tauri-apps/tauri/issues/9426 + // FIXME: When the webview is a child webview, performKeyEquivalent always return YES + // and stop propagating the event to the window, hence the menu shortcut won't be + // triggered. However, overriding this method also means the cmd+key event won't be + // handled in webview, which means the key cannot be listened by JavaScript. + if is_child { + decl.add_method( + sel!(performKeyEquivalent:), + key_equivalent as extern "C" fn(&mut Object, Sel, id) -> BOOL, + ); + extern "C" fn key_equivalent(_this: &mut Object, _sel: Sel, _event: id) -> BOOL { + NO + } + } } decl.register() }