From c1cd0a2ddb5bc3e99451cbe399b5fc9f0035f571 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Fri, 11 Apr 2025 12:00:34 +0000 Subject: [PATCH] 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 --- .changes/macOS_allow_link_preview-api.md | 7 +++++++ .changes/macOS_allow_link_preview.md | 9 +++++++++ crates/tauri-cli/config.schema.json | 5 +++++ crates/tauri-runtime-wry/src/lib.rs | 3 +++ crates/tauri-runtime/src/webview.rs | 20 +++++++++++++++++++ .../schemas/config.schema.json | 5 +++++ crates/tauri-utils/src/config.rs | 9 ++++++++- crates/tauri/src/webview/mod.rs | 18 +++++++++++++++++ crates/tauri/src/webview/plugin.rs | 3 +++ crates/tauri/src/webview/webview_window.rs | 16 +++++++++++++++ packages/api/src/webview.ts | 5 +++++ packages/api/src/window.ts | 5 +++++ 12 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 .changes/macOS_allow_link_preview-api.md create mode 100644 .changes/macOS_allow_link_preview.md diff --git a/.changes/macOS_allow_link_preview-api.md b/.changes/macOS_allow_link_preview-api.md new file mode 100644 index 000000000..597d3227e --- /dev/null +++ b/.changes/macOS_allow_link_preview-api.md @@ -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` diff --git a/.changes/macOS_allow_link_preview.md b/.changes/macOS_allow_link_preview.md new file mode 100644 index 000000000..40490f5e6 --- /dev/null +++ b/.changes/macOS_allow_link_preview.md @@ -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)` diff --git a/crates/tauri-cli/config.schema.json b/crates/tauri-cli/config.schema.json index 62e163112..5ca75b858 100644 --- a/crates/tauri-cli/config.schema.json +++ b/crates/tauri-cli/config.schema.json @@ -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 diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 4db6c5fe0..300e03184 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -4536,6 +4536,9 @@ fn create_webview( 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")] diff --git a/crates/tauri-runtime/src/webview.rs b/crates/tauri-runtime/src/webview.rs index fa96b3fdf..ca834bcaf 100644 --- a/crates/tauri-runtime/src/webview.rs +++ b/crates/tauri-runtime/src/webview.rs @@ -233,6 +233,9 @@ pub struct WebviewAttributes { pub traffic_light_position: Option, pub background_throttling: Option, 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 diff --git a/crates/tauri-schema-generator/schemas/config.schema.json b/crates/tauri-schema-generator/schemas/config.schema.json index 62e163112..5ca75b858 100644 --- a/crates/tauri-schema-generator/schemas/config.schema.json +++ b/crates/tauri-schema-generator/schemas/config.schema.json @@ -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 diff --git a/crates/tauri-utils/src/config.rs b/crates/tauri-utils/src/config.rs index 8bc4d2be3..fd2e59a45 100644 --- a/crates/tauri-utils/src/config.rs +++ b/crates/tauri-utils/src/config.rs @@ -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 ); } } diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index 435c7ec8a..6479742e5 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -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. diff --git a/crates/tauri/src/webview/plugin.rs b/crates/tauri/src/webview/plugin.rs index faae0d446..c8c0de09c 100644 --- a/crates/tauri/src/webview/plugin.rs +++ b/crates/tauri/src/webview/plugin.rs @@ -52,6 +52,8 @@ mod desktop_commands { background_throttling: Option, #[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 } } diff --git a/crates/tauri/src/webview/webview_window.rs b/crates/tauri/src/webview/webview_window.rs index 6fbcb8b0e..e796aacd8 100644 --- a/crates/tauri/src/webview/webview_window.rs +++ b/crates/tauri/src/webview/webview_window.rs @@ -718,6 +718,22 @@ impl<'a, R: Runtime, M: Manager> 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] diff --git a/packages/api/src/webview.ts b/packages/api/src/webview.ts index 531ab00f7..c1d3ea426 100644 --- a/packages/api/src/webview.ts +++ b/packages/api/src/webview.ts @@ -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 } diff --git a/packages/api/src/window.ts b/packages/api/src/window.ts index bd9ebbbdc..ac0d09f2f 100644 --- a/packages/api/src/window.ts +++ b/packages/api/src/window.ts @@ -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 {