From 6a9e6055c18b386899f8c0020558ebdd5ffbd291 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 1 Mar 2022 17:58:58 +0200 Subject: [PATCH 1/3] feat: expose webview native handles, closes #495 --- .changes/native-handles.md | 5 +++++ src/webview/mod.rs | 46 +++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 .changes/native-handles.md diff --git a/.changes/native-handles.md b/.changes/native-handles.md new file mode 100644 index 000000000..e9d854411 --- /dev/null +++ b/.changes/native-handles.md @@ -0,0 +1,5 @@ +--- +"wry": "minor" +--- + +Expose methods to access the underlying native handles of the webview. diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 322289e5a..4c4562f87 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -435,13 +435,53 @@ pub fn webview_version() -> Result { #[cfg(target_os = "windows")] pub trait WebviewExtWindows { /// Returns WebView2 Controller - fn controller(&self) -> Option; + fn controller(&self) -> ICoreWebView2Controller; } #[cfg(target_os = "windows")] impl WebviewExtWindows for WebView { - fn controller(&self) -> Option { - 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; +} + +#[cfg(target_os = "linux")] +impl WebviewExtUnix for WebView { + fn webview(&self) -> Rc { + 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() } } From 30b5e838e9a14bb50d8623371c369ed149b8c750 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 1 Mar 2022 18:07:41 +0200 Subject: [PATCH 2/3] fix linux --- src/webview/webkitgtk/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index 86e34816e..95d715fdd 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -33,7 +33,7 @@ mod file_drop; mod web_context; pub struct InnerWebView { - webview: Rc, + pub(crate) webview: Rc, } impl InnerWebView { From 5e25e80163dd7c476f7892e496af10fe3ee4119f Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Tue, 1 Mar 2022 18:16:47 +0200 Subject: [PATCH 3/3] [skip ci] update changefile --- .changes/native-handles.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.changes/native-handles.md b/.changes/native-handles.md index e9d854411..d2cc487da 100644 --- a/.changes/native-handles.md +++ b/.changes/native-handles.md @@ -2,4 +2,5 @@ "wry": "minor" --- -Expose methods to access the underlying native handles of the webview. +* 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`