diff --git a/.changes/mac-view.md b/.changes/mac-view.md new file mode 100644 index 000000000..0a57a2086 --- /dev/null +++ b/.changes/mac-view.md @@ -0,0 +1,6 @@ +--- +"wry": patch +--- + +On macOS, add WKWebview as subview of existing NSView directly. + diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 5114e55e5..5637a062b 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -25,7 +25,7 @@ use std::{ slice, str, }; -use core_graphics::geometry::{CGPoint, CGRect, CGSize}; +use core_graphics::geometry::CGRect; use objc::{ declare::ClassDecl, runtime::{Class, Object, Sel, BOOL}, @@ -317,9 +317,9 @@ impl InnerWebView { #[cfg(target_os = "macos")] { - // Initialize webview with zero point - let zero = CGRect::new(&CGPoint::new(0., 0.), &CGSize::new(0., 0.)); - let _: () = msg_send![webview, initWithFrame:zero configuration:config]; + let ns_view = window.ns_view() as id; + let frame: CGRect = msg_send![ns_view, frame]; + let _: () = msg_send![webview, initWithFrame:frame configuration:config]; // Auto-resize on macOS webview.setAutoresizingMask_(NSViewHeightSizable | NSViewWidthSizable); } @@ -663,38 +663,13 @@ 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![parent_view_cls, alloc]; - let _: () = msg_send![parent_view, init]; - parent_view.setAutoresizingMask_(NSViewHeightSizable | NSViewWidthSizable); - let _: () = msg_send![parent_view, addSubview: webview]; + let ns_view = window.ns_view() as id; + let _: () = msg_send![ns_view, addSubview: webview]; // Tell the webview we use layers (macOS only) let _: () = msg_send![webview, setWantsLayer: YES]; // inject the webview into the window let ns_window = window.ns_window() as id; - // NOTE: We ignore the fact that we may get back the existing view as we - // expect tao::Window to hold a handle to it and release it for us - // eventually. - let _: () = msg_send![ns_window, setContentView: parent_view]; // Tell the webview receive keyboard events in the window. // See https://github.com/tauri-apps/wry/issues/739 let _: () = msg_send![ns_window, makeFirstResponder: webview];