Skip to content

Commit

Permalink
feat: expose webview native handles, closes #495
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Mar 1, 2022
1 parent d3d03dc commit 6a9e605
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/native-handles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

Expose methods to access the underlying native handles of the webview.
46 changes: 43 additions & 3 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,53 @@ pub fn webview_version() -> Result<String> {
#[cfg(target_os = "windows")]
pub trait WebviewExtWindows {
/// Returns WebView2 Controller
fn controller(&self) -> Option<ICoreWebView2Controller>;
fn controller(&self) -> ICoreWebView2Controller;
}

#[cfg(target_os = "windows")]
impl WebviewExtWindows for WebView {
fn controller(&self) -> Option<ICoreWebView2Controller> {
Some(self.webview.controller.clone())
fn controller(&self) -> ICoreWebView2Controller {
self.webview.controller.clone()
}
}

/// Additional methods on `WebView` that are specific to Linux.
#[cfg(target_os = "linux")]
pub trait WebviewExtUnix {
/// Returns Webkit2gtk Webview handle
fn webview(&self) -> Rc<webkit2gtk::WebView>;
}

#[cfg(target_os = "linux")]
impl WebviewExtUnix for WebView {
fn webview(&self) -> Rc<webkit2gtk::WebView> {
self.webview.webview.clone()
}
}

/// Additional methods on `WebView` that are specific to macOS.
#[cfg(target_os = "macOS")]
pub trait WebviewExtMacOS {
/// Returns WKWebView handle
fn webview(&self) -> cocoa::base::id;
/// Returns WKWebView manager [(userContentController)](https://developer.apple.com/documentation/webkit/wkscriptmessagehandler/1396222-usercontentcontroller) handle
fn manager(&self) -> cocoa::base::id;
/// Returns NSWindow associated with the WKWebView webview
fn ns_window(&self) -> cocoa::base::id;
}

#[cfg(target_os = "macOS")]
impl WebviewExtMacOS for WebView {
fn webview(&self) -> cocoa::base::id {
self.webview.webview.clone()
}

fn manager(&self) -> cocoa::base::id {
self.webview.manager.clone()
}

fn ns_window(&self) -> cocoa::base::id {
self.webview.ns_window.clone()
}
}

Expand Down

0 comments on commit 6a9e605

Please sign in to comment.