Skip to content

Commit

Permalink
feat: expose webview native handles, closes #495 (#513)
Browse files Browse the repository at this point in the history
* feat: expose webview native handles, closes #495

* fix linux

* [skip ci] update changefile
  • Loading branch information
amrbashir committed Mar 3, 2022
1 parent 742a8cb commit e54afec
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/native-handles.md
@@ -0,0 +1,6 @@
---
"wry": "minor"
---

* Expose methods to access the underlying native handles of the webview.
* **Breaking change**: `WebviewExtWindows::controller` now returns the controller directley and not wrapped in an `Option`
46 changes: 43 additions & 3 deletions src/webview/mod.rs
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
2 changes: 1 addition & 1 deletion src/webview/webkitgtk/mod.rs
Expand Up @@ -33,7 +33,7 @@ mod file_drop;
mod web_context;

pub struct InnerWebView {
webview: Rc<WebView>,
pub(crate) webview: Rc<WebView>,
}

impl InnerWebView {
Expand Down

0 comments on commit e54afec

Please sign in to comment.