From d401330c4ee9cdb6e04ccfe073778eae7f6ca292 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Fri, 28 Mar 2025 18:51:41 +0100 Subject: [PATCH 1/2] macOS/iOS: add option to disable or enable link previews when building a webview (the webkit api has it enabled by default) - `WebViewBuilderExtDarwin.allow_link_preview(allow_link_preview: bool)` --- .changes/macOS_allow_link_preview.md | 6 ++++++ src/lib.rs | 28 +++++++++++++++++++++++++++- src/wkwebview/mod.rs | 6 ++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 .changes/macOS_allow_link_preview.md diff --git a/.changes/macOS_allow_link_preview.md b/.changes/macOS_allow_link_preview.md new file mode 100644 index 000000000..80dd2bb37 --- /dev/null +++ b/.changes/macOS_allow_link_preview.md @@ -0,0 +1,6 @@ +--- +"wry": patch:feat +--- + +macOS/iOS: add option to disable or enable link previews when building a webview (the webkit api has it enabled by default) + - `WebViewBuilderExtDarwin.allow_link_preview(allow_link_preview: bool)` diff --git a/src/lib.rs b/src/lib.rs index 020f8df94..6a16adbba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1394,10 +1394,23 @@ impl<'a> WebViewBuilder<'a> { } #[cfg(any(target_os = "macos", target_os = "ios"))] -#[derive(Clone, Default)] +#[derive(Clone)] pub(crate) struct PlatformSpecificWebViewAttributes { data_store_identifier: Option<[u8; 16]>, traffic_light_inset: Option, + allow_link_preview: bool, +} + +#[cfg(any(target_os = "macos", target_os = "ios"))] +impl Default for PlatformSpecificWebViewAttributes { + fn default() -> Self { + Self { + data_store_identifier: None, + traffic_light_inset: None, + // platform default for this is true + allow_link_preview: true, + } + } } #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -1415,6 +1428,12 @@ pub trait WebViewBuilderExtDarwin { /// Warning: Do not use this if your chosen window library does not support traffic light insets. /// Warning: Only use this in **decorated** windows with a **hidden titlebar**! fn with_traffic_light_inset>(self, position: P) -> Self; + /// Whether to show a link preview when long pressing on links. Available on macOS and iOS only. + /// + /// Default is true. + /// + /// See https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview + fn allow_link_preview(self, allow_link_preview: bool) -> Self; } #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -1432,6 +1451,13 @@ impl WebViewBuilderExtDarwin for WebViewBuilder<'_> { Ok(b) }) } + + fn allow_link_preview(self, allow_link_preview: bool) -> Self { + self.and_then(|mut b| { + b.platform_specific.allow_link_preview = allow_link_preview; + Ok(b) + }) + } } #[cfg(windows)] diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs index 392556275..0ac396a6b 100644 --- a/src/wkwebview/mod.rs +++ b/src/wkwebview/mod.rs @@ -544,6 +544,12 @@ r#"Object.defineProperty(window, 'ipc', { w.navigate_to_string(&html); } + // Allow Link Preview + #[cfg(target_vendor = "apple")] + { + w.webview.setAllowsLinkPreview(pl_attrs.allow_link_preview); + } + // Inject the web view into the window as main content #[cfg(target_os = "macos")] { From 1d5504c6d02dce9cadd4fe0fac2ee46b7623b4d4 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sun, 6 Apr 2025 08:53:30 -0300 Subject: [PATCH 2/2] cleanup --- .changes/macOS_allow_link_preview.md | 4 ++-- src/lib.rs | 6 +++--- src/wkwebview/mod.rs | 5 +---- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.changes/macOS_allow_link_preview.md b/.changes/macOS_allow_link_preview.md index 80dd2bb37..c1292a08d 100644 --- a/.changes/macOS_allow_link_preview.md +++ b/.changes/macOS_allow_link_preview.md @@ -2,5 +2,5 @@ "wry": patch:feat --- -macOS/iOS: add option to disable or enable link previews when building a webview (the webkit api has it enabled by default) - - `WebViewBuilderExtDarwin.allow_link_preview(allow_link_preview: bool)` +macOS/iOS: add option to disable link previews when building a webview (the webkit API has it enabled by default) + - `WebViewBuilderExtDarwin.with_allow_link_preview(bool)` diff --git a/src/lib.rs b/src/lib.rs index 6a16adbba..bcaf378c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1432,8 +1432,8 @@ pub trait WebViewBuilderExtDarwin { /// /// Default is true. /// - /// See https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview - fn allow_link_preview(self, allow_link_preview: bool) -> Self; + /// See https://developer.apple.com/documentation/webkit/wkwebview/allowslinkpreview + fn with_allow_link_preview(self, allow_link_preview: bool) -> Self; } #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -1452,7 +1452,7 @@ impl WebViewBuilderExtDarwin for WebViewBuilder<'_> { }) } - fn allow_link_preview(self, allow_link_preview: bool) -> Self { + fn with_allow_link_preview(self, allow_link_preview: bool) -> Self { self.and_then(|mut b| { b.platform_specific.allow_link_preview = allow_link_preview; Ok(b) diff --git a/src/wkwebview/mod.rs b/src/wkwebview/mod.rs index 0ac396a6b..5775fa60c 100644 --- a/src/wkwebview/mod.rs +++ b/src/wkwebview/mod.rs @@ -545,10 +545,7 @@ r#"Object.defineProperty(window, 'ipc', { } // Allow Link Preview - #[cfg(target_vendor = "apple")] - { - w.webview.setAllowsLinkPreview(pl_attrs.allow_link_preview); - } + w.webview.setAllowsLinkPreview(pl_attrs.allow_link_preview); // Inject the web view into the window as main content #[cfg(target_os = "macos")]