Skip to content

Commit

Permalink
On macOS, add WKWebview as subview of existing NSView directly (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
wusyong authored Nov 7, 2022
1 parent 61b7fec commit 008eca8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .changes/mac-view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": patch
---

On macOS, add WKWebview as subview of existing NSView directly.

37 changes: 6 additions & 31 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 008eca8

Please sign in to comment.