Skip to content

Commit ecbced2

Browse files
feat: macOS/iOS: add option to disable or enable link previews when building a webview (#1534)
* 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)` * cleanup --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent e5ac513 commit ecbced2

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"wry": patch:feat
3+
---
4+
5+
macOS/iOS: add option to disable link previews when building a webview (the webkit API has it enabled by default)
6+
- `WebViewBuilderExtDarwin.with_allow_link_preview(bool)`

src/lib.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,10 +1394,23 @@ impl<'a> WebViewBuilder<'a> {
13941394
}
13951395

13961396
#[cfg(any(target_os = "macos", target_os = "ios"))]
1397-
#[derive(Clone, Default)]
1397+
#[derive(Clone)]
13981398
pub(crate) struct PlatformSpecificWebViewAttributes {
13991399
data_store_identifier: Option<[u8; 16]>,
14001400
traffic_light_inset: Option<dpi::Position>,
1401+
allow_link_preview: bool,
1402+
}
1403+
1404+
#[cfg(any(target_os = "macos", target_os = "ios"))]
1405+
impl Default for PlatformSpecificWebViewAttributes {
1406+
fn default() -> Self {
1407+
Self {
1408+
data_store_identifier: None,
1409+
traffic_light_inset: None,
1410+
// platform default for this is true
1411+
allow_link_preview: true,
1412+
}
1413+
}
14011414
}
14021415

14031416
#[cfg(any(target_os = "macos", target_os = "ios"))]
@@ -1415,6 +1428,12 @@ pub trait WebViewBuilderExtDarwin {
14151428
/// Warning: Do not use this if your chosen window library does not support traffic light insets.
14161429
/// Warning: Only use this in **decorated** windows with a **hidden titlebar**!
14171430
fn with_traffic_light_inset<P: Into<dpi::Position>>(self, position: P) -> Self;
1431+
/// Whether to show a link preview when long pressing on links. Available on macOS and iOS only.
1432+
///
1433+
/// Default is true.
1434+
///
1435+
/// See https://developer.apple.com/documentation/webkit/wkwebview/allowslinkpreview
1436+
fn with_allow_link_preview(self, allow_link_preview: bool) -> Self;
14181437
}
14191438

14201439
#[cfg(any(target_os = "macos", target_os = "ios"))]
@@ -1432,6 +1451,13 @@ impl WebViewBuilderExtDarwin for WebViewBuilder<'_> {
14321451
Ok(b)
14331452
})
14341453
}
1454+
1455+
fn with_allow_link_preview(self, allow_link_preview: bool) -> Self {
1456+
self.and_then(|mut b| {
1457+
b.platform_specific.allow_link_preview = allow_link_preview;
1458+
Ok(b)
1459+
})
1460+
}
14351461
}
14361462

14371463
#[cfg(windows)]

src/wkwebview/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ r#"Object.defineProperty(window, 'ipc', {
538538
w.navigate_to_string(&html);
539539
}
540540

541+
// Allow Link Preview
542+
w.webview.setAllowsLinkPreview(pl_attrs.allow_link_preview);
543+
541544
// Inject the web view into the window as main content
542545
#[cfg(target_os = "macos")]
543546
{

0 commit comments

Comments
 (0)