Skip to content

Commit

Permalink
fix(macos): do not trigger unsupported key feedback sound on keypress (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Oct 30, 2022
1 parent cd08410 commit 51c7f12
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/macos-key-feedback.md
@@ -0,0 +1,5 @@
---
"wry": patch
---

Keypress on non-input element no longer triggers unsupported key feedback sound.
2 changes: 1 addition & 1 deletion examples/custom_protocol.rs
Expand Up @@ -31,7 +31,7 @@ fn main() -> wry::Result<()> {
// Read the file content from file path
let content = read(canonicalize(PathBuf::from("examples").join(
if path == &"/" {
"index.html"
"custom_protocol_page1.html"
} else {
// remove leading slash
&path[1..]
Expand Down
18 changes: 17 additions & 1 deletion src/webview/wkwebview/mod.rs
Expand Up @@ -663,10 +663,26 @@ r#"Object.defineProperty(window, 'ipc', {
// Inject the web view into the window as main content
#[cfg(target_os = "macos")]
{
let parent_view_cls = match ClassDecl::new("WryWebViewParent", class!(NSView)) {
Some(mut decl) => {
decl.add_method(
sel!(keyDown:),
key_down as extern "C" fn(&mut Object, Sel, id),
);

extern "C" fn key_down(_this: &mut Object, _sel: Sel, _event: id) {
// do nothing; this prevents macOS from playing an unsupported key feedback sound
}

decl.register()
}
None => class!(NSView),
};

// Create a view to contain the webview, without it devtools will try to
// inject a subview into the frame causing an obnoxious warning.
// See https://github.com/tauri-apps/wry/issues/273
let parent_view: id = msg_send![class!(NSView), alloc];
let parent_view: id = msg_send![parent_view_cls, alloc];
let _: () = msg_send![parent_view, init];
parent_view.setAutoresizingMask_(NSViewHeightSizable | NSViewWidthSizable);
let _: () = msg_send![parent_view, addSubview: webview];
Expand Down

0 comments on commit 51c7f12

Please sign in to comment.