feat: macOS/iOS: add option to disable or enable link previews when building a webview (#13090)

* 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)`
  -  `WebViewBuilder.allow_link_preview(allow_link_preview: bool)`
  -  `WebviewWindowBuilder.allow_link_preview(allow_link_preview: bool)`

* also call on iOS

* add api

* fix tests

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Simon Laux 2025-04-11 12:00:34 +00:00 committed by GitHub
parent 073dbc3953
commit c1cd0a2ddb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 104 additions and 1 deletions

View File

@ -0,0 +1,7 @@
---
"@tauri-apps/api": patch:feat
---
macOS/iOS: add option to disable or enable link previews when building a webview (the webkit api has it enabled by default)
- `WindowOptions::allowLinkPreview`
- `WebviewOptions::allowLinkPreview`

View File

@ -0,0 +1,9 @@
---
"tauri": patch:feat
"tauri-runtime": patch:feat
"tauri-runtime-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)
- `WebViewBuilder.allow_link_preview(allow_link_preview: bool)`
- `WebviewWindowBuilder.allow_link_preview(allow_link_preview: bool)`

View File

@ -545,6 +545,11 @@
"description": "Whether we should disable JavaScript code execution on the webview or not.",
"default": false,
"type": "boolean"
},
"allowLinkPreview": {
"description": "on macOS and iOS there is a link preview on long pressing links, this is enabled by default.\n see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview",
"default": true,
"type": "boolean"
}
},
"additionalProperties": false

View File

@ -4536,6 +4536,9 @@ fn create_webview<T: UserEvent>(
if let Some(data_store_identifier) = &webview_attributes.data_store_identifier {
webview_builder = webview_builder.with_data_store_identifier(*data_store_identifier);
}
webview_builder =
webview_builder.with_allow_link_preview(webview_attributes.allow_link_preview);
}
#[cfg(target_os = "macos")]

View File

@ -233,6 +233,9 @@ pub struct WebviewAttributes {
pub traffic_light_position: Option<dpi::Position>,
pub background_throttling: Option<BackgroundThrottlingPolicy>,
pub javascript_disabled: bool,
/// on macOS and iOS there is a link preview on long pressing links, this is enabled by default.
/// see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
pub allow_link_preview: bool,
}
impl From<&WindowConfig> for WebviewAttributes {
@ -277,6 +280,7 @@ impl From<&WindowConfig> for WebviewAttributes {
builder = builder.background_color(color);
}
builder.javascript_disabled = config.javascript_disabled;
builder.allow_link_preview = config.allow_link_preview;
builder
}
}
@ -310,6 +314,7 @@ impl WebviewAttributes {
traffic_light_position: None,
background_throttling: None,
javascript_disabled: false,
allow_link_preview: true,
}
}
@ -532,6 +537,21 @@ impl WebviewAttributes {
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
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android:** Unsupported.
#[must_use]
pub fn allow_link_preview(mut self, allow_link_preview: bool) -> Self {
self.allow_link_preview = allow_link_preview;
self
}
/// Change the default background throttling behavior.
///
/// By default, browsers use a suspend policy that will throttle timers and even unload

View File

@ -545,6 +545,11 @@
"description": "Whether we should disable JavaScript code execution on the webview or not.",
"default": false,
"type": "boolean"
},
"allowLinkPreview": {
"description": "on macOS and iOS there is a link preview on long pressing links, this is enabled by default.\n see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview",
"default": true,
"type": "boolean"
}
},
"additionalProperties": false

View File

@ -1736,6 +1736,10 @@ pub struct WindowConfig {
/// Whether we should disable JavaScript code execution on the webview or not.
#[serde(default, alias = "javascript-disabled")]
pub javascript_disabled: bool,
/// on macOS and iOS there is a link preview on long pressing links, this is enabled by default.
/// see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
#[serde(default = "default_true", alias = "allow-link-preview")]
pub allow_link_preview: bool,
}
impl Default for WindowConfig {
@ -1791,6 +1795,7 @@ impl Default for WindowConfig {
background_color: None,
background_throttling: None,
javascript_disabled: false,
allow_link_preview: true,
}
}
}
@ -3074,6 +3079,7 @@ mod build {
let background_color = opt_lit(self.background_color.as_ref());
let background_throttling = opt_lit(self.background_throttling.as_ref());
let javascript_disabled = self.javascript_disabled;
let allow_link_preview = self.allow_link_preview;
literal_struct!(
tokens,
@ -3127,7 +3133,8 @@ mod build {
devtools,
background_color,
background_throttling,
javascript_disabled
javascript_disabled,
allow_link_preview
);
}
}

View File

@ -974,6 +974,24 @@ fn main() {
self.webview_attributes.javascript_disabled = true;
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
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android:** Unsupported.
#[cfg(target_os = "macos")]
#[must_use]
pub fn allow_link_preview(mut self, allow_link_preview: bool) -> Self {
self.webview_attributes = self
.webview_attributes
.allow_link_preview(allow_link_preview);
self
}
}
/// Webview.

View File

@ -52,6 +52,8 @@ mod desktop_commands {
background_throttling: Option<BackgroundThrottlingPolicy>,
#[serde(default)]
javascript_disabled: bool,
#[serde(default = "default_true")]
allow_link_preview: bool,
}
#[cfg(feature = "unstable")]
@ -69,6 +71,7 @@ mod desktop_commands {
builder.webview_attributes.zoom_hotkeys_enabled = config.zoom_hotkeys_enabled;
builder.webview_attributes.background_throttling = config.background_throttling;
builder.webview_attributes.javascript_disabled = config.javascript_disabled;
builder.webview_attributes.allow_link_preview = config.allow_link_preview;
builder
}
}

View File

@ -718,6 +718,22 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
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
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android:** Unsupported.
#[cfg(target_os = "macos")]
#[must_use]
pub fn allow_link_preview(mut self, allow_link_preview: bool) -> Self {
self.webview_builder = self.webview_builder.allow_link_preview(allow_link_preview);
self
}
/// Hide the window title.
#[cfg(target_os = "macos")]
#[must_use]

View File

@ -795,6 +795,11 @@ interface WebviewOptions {
* Whether we should disable JavaScript code execution on the webview or not.
*/
javascriptDisabled?: boolean
/**
* on macOS and iOS there is a link preview on long pressing links, this is enabled by default.
* see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
*/
allowLinkPreview?: boolean
}
export { Webview, getCurrentWebview, getAllWebviews }

View File

@ -2383,6 +2383,11 @@ interface WindowOptions {
* Whether we should disable JavaScript code execution on the webview or not.
*/
javascriptDisabled?: boolean
/**
* on macOS and iOS there is a link preview on long pressing links, this is enabled by default.
* see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
*/
allowLinkPreview?: boolean
}
function mapMonitor(m: Monitor | null): Monitor | null {