Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/macOS_allow_link_preview.md
Original file line number Diff line number Diff line change
@@ -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)`
28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dpi::Position>,
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"))]
Expand All @@ -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<P: Into<dpi::Position>>(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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use with_link_preview to align with other builder method's naming style

}

#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand All @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
{
Expand Down