Skip to content

Commit c1cd0a2

Browse files
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>
1 parent 073dbc3 commit c1cd0a2

12 files changed

Lines changed: 104 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@tauri-apps/api": patch:feat
3+
---
4+
5+
macOS/iOS: add option to disable or enable link previews when building a webview (the webkit api has it enabled by default)
6+
- `WindowOptions::allowLinkPreview`
7+
- `WebviewOptions::allowLinkPreview`
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"tauri": patch:feat
3+
"tauri-runtime": patch:feat
4+
"tauri-runtime-wry": patch:feat
5+
---
6+
7+
macOS/iOS: add option to disable or enable link previews when building a webview (the webkit api has it enabled by default)
8+
- `WebViewBuilder.allow_link_preview(allow_link_preview: bool)`
9+
- `WebviewWindowBuilder.allow_link_preview(allow_link_preview: bool)`

crates/tauri-cli/config.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@
545545
"description": "Whether we should disable JavaScript code execution on the webview or not.",
546546
"default": false,
547547
"type": "boolean"
548+
},
549+
"allowLinkPreview": {
550+
"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",
551+
"default": true,
552+
"type": "boolean"
548553
}
549554
},
550555
"additionalProperties": false

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,6 +4536,9 @@ fn create_webview<T: UserEvent>(
45364536
if let Some(data_store_identifier) = &webview_attributes.data_store_identifier {
45374537
webview_builder = webview_builder.with_data_store_identifier(*data_store_identifier);
45384538
}
4539+
4540+
webview_builder =
4541+
webview_builder.with_allow_link_preview(webview_attributes.allow_link_preview);
45394542
}
45404543

45414544
#[cfg(target_os = "macos")]

crates/tauri-runtime/src/webview.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ pub struct WebviewAttributes {
233233
pub traffic_light_position: Option<dpi::Position>,
234234
pub background_throttling: Option<BackgroundThrottlingPolicy>,
235235
pub javascript_disabled: bool,
236+
/// on macOS and iOS there is a link preview on long pressing links, this is enabled by default.
237+
/// see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
238+
pub allow_link_preview: bool,
236239
}
237240

238241
impl From<&WindowConfig> for WebviewAttributes {
@@ -277,6 +280,7 @@ impl From<&WindowConfig> for WebviewAttributes {
277280
builder = builder.background_color(color);
278281
}
279282
builder.javascript_disabled = config.javascript_disabled;
283+
builder.allow_link_preview = config.allow_link_preview;
280284
builder
281285
}
282286
}
@@ -310,6 +314,7 @@ impl WebviewAttributes {
310314
traffic_light_position: None,
311315
background_throttling: None,
312316
javascript_disabled: false,
317+
allow_link_preview: true,
313318
}
314319
}
315320

@@ -532,6 +537,21 @@ impl WebviewAttributes {
532537
self
533538
}
534539

540+
/// Whether to show a link preview when long pressing on links. Available on macOS and iOS only.
541+
///
542+
/// Default is true.
543+
///
544+
/// See https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
545+
///
546+
/// ## Platform-specific
547+
///
548+
/// - **Linux / Windows / Android:** Unsupported.
549+
#[must_use]
550+
pub fn allow_link_preview(mut self, allow_link_preview: bool) -> Self {
551+
self.allow_link_preview = allow_link_preview;
552+
self
553+
}
554+
535555
/// Change the default background throttling behavior.
536556
///
537557
/// By default, browsers use a suspend policy that will throttle timers and even unload

crates/tauri-schema-generator/schemas/config.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@
545545
"description": "Whether we should disable JavaScript code execution on the webview or not.",
546546
"default": false,
547547
"type": "boolean"
548+
},
549+
"allowLinkPreview": {
550+
"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",
551+
"default": true,
552+
"type": "boolean"
548553
}
549554
},
550555
"additionalProperties": false

crates/tauri-utils/src/config.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,10 @@ pub struct WindowConfig {
17361736
/// Whether we should disable JavaScript code execution on the webview or not.
17371737
#[serde(default, alias = "javascript-disabled")]
17381738
pub javascript_disabled: bool,
1739+
/// on macOS and iOS there is a link preview on long pressing links, this is enabled by default.
1740+
/// see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
1741+
#[serde(default = "default_true", alias = "allow-link-preview")]
1742+
pub allow_link_preview: bool,
17391743
}
17401744

17411745
impl Default for WindowConfig {
@@ -1791,6 +1795,7 @@ impl Default for WindowConfig {
17911795
background_color: None,
17921796
background_throttling: None,
17931797
javascript_disabled: false,
1798+
allow_link_preview: true,
17941799
}
17951800
}
17961801
}
@@ -3074,6 +3079,7 @@ mod build {
30743079
let background_color = opt_lit(self.background_color.as_ref());
30753080
let background_throttling = opt_lit(self.background_throttling.as_ref());
30763081
let javascript_disabled = self.javascript_disabled;
3082+
let allow_link_preview = self.allow_link_preview;
30773083

30783084
literal_struct!(
30793085
tokens,
@@ -3127,7 +3133,8 @@ mod build {
31273133
devtools,
31283134
background_color,
31293135
background_throttling,
3130-
javascript_disabled
3136+
javascript_disabled,
3137+
allow_link_preview
31313138
);
31323139
}
31333140
}

crates/tauri/src/webview/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,24 @@ fn main() {
974974
self.webview_attributes.javascript_disabled = true;
975975
self
976976
}
977+
978+
/// Whether to show a link preview when long pressing on links. Available on macOS and iOS only.
979+
///
980+
/// Default is true.
981+
///
982+
/// See https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
983+
///
984+
/// ## Platform-specific
985+
///
986+
/// - **Linux / Windows / Android:** Unsupported.
987+
#[cfg(target_os = "macos")]
988+
#[must_use]
989+
pub fn allow_link_preview(mut self, allow_link_preview: bool) -> Self {
990+
self.webview_attributes = self
991+
.webview_attributes
992+
.allow_link_preview(allow_link_preview);
993+
self
994+
}
977995
}
978996

979997
/// Webview.

crates/tauri/src/webview/plugin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ mod desktop_commands {
5252
background_throttling: Option<BackgroundThrottlingPolicy>,
5353
#[serde(default)]
5454
javascript_disabled: bool,
55+
#[serde(default = "default_true")]
56+
allow_link_preview: bool,
5557
}
5658

5759
#[cfg(feature = "unstable")]
@@ -69,6 +71,7 @@ mod desktop_commands {
6971
builder.webview_attributes.zoom_hotkeys_enabled = config.zoom_hotkeys_enabled;
7072
builder.webview_attributes.background_throttling = config.background_throttling;
7173
builder.webview_attributes.javascript_disabled = config.javascript_disabled;
74+
builder.webview_attributes.allow_link_preview = config.allow_link_preview;
7275
builder
7376
}
7477
}

crates/tauri/src/webview/webview_window.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,22 @@ impl<'a, R: Runtime, M: Manager<R>> WebviewWindowBuilder<'a, R, M> {
718718
self
719719
}
720720

721+
/// Whether to show a link preview when long pressing on links. Available on macOS and iOS only.
722+
///
723+
/// Default is true.
724+
///
725+
/// See https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
726+
///
727+
/// ## Platform-specific
728+
///
729+
/// - **Linux / Windows / Android:** Unsupported.
730+
#[cfg(target_os = "macos")]
731+
#[must_use]
732+
pub fn allow_link_preview(mut self, allow_link_preview: bool) -> Self {
733+
self.webview_builder = self.webview_builder.allow_link_preview(allow_link_preview);
734+
self
735+
}
736+
721737
/// Hide the window title.
722738
#[cfg(target_os = "macos")]
723739
#[must_use]

0 commit comments

Comments
 (0)