Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose webview native handles, closes #495 #513

Merged
merged 3 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/native-handles.md
Original file line number Diff line number Diff line change
@@ -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
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
2 changes: 1 addition & 1 deletion src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
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