diff --git a/.changes/user-agent-config.md b/.changes/user-agent-config.md new file mode 100644 index 00000000000..f0e168b7a4b --- /dev/null +++ b/.changes/user-agent-config.md @@ -0,0 +1,5 @@ +--- +"tauri-utils": minor +--- + +Added the `user_agent` option to the window configuration. diff --git a/.changes/user-agent.md b/.changes/user-agent.md new file mode 100644 index 00000000000..08fa1c4231b --- /dev/null +++ b/.changes/user-agent.md @@ -0,0 +1,8 @@ +--- +"api": minor +"tauri": minor +"tauri-runtime-wry": minor +"tauri-runtime": minor +--- + +Added the `user_agent` option when creating a window. diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 6eb8194f1ea..9519f7eef7f 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -2945,6 +2945,9 @@ fn create_webview( webview_builder = webview_builder .with_file_drop_handler(create_file_drop_handler(window_event_listeners.clone())); } + if let Some(user_agent) = webview_attributes.user_agent { + webview_builder = webview_builder.with_user_agent(&user_agent); + } if let Some(handler) = ipc_handler { webview_builder = webview_builder.with_ipc_handler(create_ipc_handler( context, diff --git a/core/tauri-runtime/src/webview.rs b/core/tauri-runtime/src/webview.rs index 7db22146692..939d74ab053 100644 --- a/core/tauri-runtime/src/webview.rs +++ b/core/tauri-runtime/src/webview.rs @@ -22,6 +22,7 @@ use std::{fmt, path::PathBuf}; #[derive(Debug, Clone)] pub struct WebviewAttributes { pub url: WindowUrl, + pub user_agent: Option, pub initialization_scripts: Vec, pub data_directory: Option, pub file_drop_handler_enabled: bool, @@ -33,6 +34,7 @@ impl WebviewAttributes { pub fn new(url: WindowUrl) -> Self { Self { url, + user_agent: None, initialization_scripts: Vec::new(), data_directory: None, file_drop_handler_enabled: true, @@ -40,6 +42,13 @@ impl WebviewAttributes { } } + /// Sets the user agent + #[must_use] + pub fn user_agent(mut self, user_agent: &str) -> Self { + self.user_agent = Some(user_agent.to_string()); + self + } + /// Sets the init script. #[must_use] pub fn initialization_script(mut self, script: &str) -> Self { diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 993481b74c6..3d7bcbc1039 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -796,6 +796,9 @@ pub struct WindowConfig { /// The window webview URL. #[serde(default)] pub url: WindowUrl, + /// The user agent for the webview + #[serde(alias = "user-agent")] + pub user_agent: Option, /// Whether the file drop is enabled or not on the webview. By default it is enabled. /// /// Disabling it is required to use drag and drop on the frontend on Windows. @@ -874,6 +877,7 @@ impl Default for WindowConfig { Self { label: default_window_label(), url: WindowUrl::default(), + user_agent: None, file_drop_enabled: default_file_drop_enabled(), center: false, x: None, @@ -2960,6 +2964,7 @@ mod build { fn to_tokens(&self, tokens: &mut TokenStream) { let label = str_lit(&self.label); let url = &self.url; + let user_agent = opt_str_lit(self.user_agent.as_ref()); let file_drop_enabled = self.file_drop_enabled; let center = self.center; let x = opt_lit(self.x.as_ref()); @@ -2989,6 +2994,7 @@ mod build { WindowConfig, label, url, + user_agent, file_drop_enabled, center, x, diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index c19a6b50549..f0620c9b63a 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -1463,8 +1463,12 @@ impl Builder { let url = config.url.clone(); let label = config.label.clone(); let file_drop_enabled = config.file_drop_enabled; + let user_agent = config.user_agent.clone(); let mut webview_attributes = WebviewAttributes::new(url); + if let Some(ua) = user_agent { + webview_attributes = webview_attributes.user_agent(&ua.to_string()); + } if !file_drop_enabled { webview_attributes = webview_attributes.disable_file_drop_handler(); } diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index e4b60f14d4f..8e863e72065 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -491,6 +491,13 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> { self } + /// Set the user agent for the webview + #[must_use] + pub fn user_agent(mut self, user_agent: &str) -> Self { + self.webview_attributes.user_agent = Some(user_agent.to_string()); + self + } + /// Data directory for the webview. #[must_use] pub fn data_directory(mut self, data_directory: PathBuf) -> Self { diff --git a/examples/api/src-tauri/src/lib.rs b/examples/api/src-tauri/src/lib.rs index a695505b3bb..2e51010a10d 100644 --- a/examples/api/src-tauri/src/lib.rs +++ b/examples/api/src-tauri/src/lib.rs @@ -62,6 +62,7 @@ impl AppBuilder { #[allow(unused_mut)] let mut window_builder = WindowBuilder::new(app, "main", WindowUrl::default()) + .user_agent("Tauri API") .title("Tauri API Validation") .inner_size(1000., 800.) .min_inner_size(600., 400.); diff --git a/tooling/api/src/window.ts b/tooling/api/src/window.ts index 06743721600..f562c1b6c5c 100644 --- a/tooling/api/src/window.ts +++ b/tooling/api/src/window.ts @@ -2042,6 +2042,10 @@ interface WindowOptions { * If `true`, sets the window title to be hidden on macOS. */ hiddenTitle?: boolean + /** + * The user agent for the webview. + */ + userAgent?: string } function mapMonitor(m: Monitor | null): Monitor | null { diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 3e21db80590..468a9983881 100644 --- a/tooling/cli/schema.json +++ b/tooling/cli/schema.json @@ -509,6 +509,13 @@ } ] }, + "userAgent": { + "description": "The user agent for the webview", + "type": [ + "string", + "null" + ] + }, "fileDropEnabled": { "description": "Whether the file drop is enabled or not on the webview. By default it is enabled.\n\nDisabling it is required to use drag and drop on the frontend on Windows.", "default": true,