Skip to content

Commit a6c9411

Browse files
feat(core): expose user_agent to window config (#5317)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 321f3fe commit a6c9411

File tree

10 files changed

+54
-0
lines changed

10 files changed

+54
-0
lines changed

.changes/user-agent-config.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": minor
3+
---
4+
5+
Added the `user_agent` option to the window configuration.

.changes/user-agent.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"api": minor
3+
"tauri": minor
4+
"tauri-runtime-wry": minor
5+
"tauri-runtime": minor
6+
---
7+
8+
Added the `user_agent` option when creating a window.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,6 +2945,9 @@ fn create_webview<T: UserEvent>(
29452945
webview_builder = webview_builder
29462946
.with_file_drop_handler(create_file_drop_handler(window_event_listeners.clone()));
29472947
}
2948+
if let Some(user_agent) = webview_attributes.user_agent {
2949+
webview_builder = webview_builder.with_user_agent(&user_agent);
2950+
}
29482951
if let Some(handler) = ipc_handler {
29492952
webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
29502953
context,

core/tauri-runtime/src/webview.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::{fmt, path::PathBuf};
2222
#[derive(Debug, Clone)]
2323
pub struct WebviewAttributes {
2424
pub url: WindowUrl,
25+
pub user_agent: Option<String>,
2526
pub initialization_scripts: Vec<String>,
2627
pub data_directory: Option<PathBuf>,
2728
pub file_drop_handler_enabled: bool,
@@ -33,13 +34,21 @@ impl WebviewAttributes {
3334
pub fn new(url: WindowUrl) -> Self {
3435
Self {
3536
url,
37+
user_agent: None,
3638
initialization_scripts: Vec::new(),
3739
data_directory: None,
3840
file_drop_handler_enabled: true,
3941
clipboard: false,
4042
}
4143
}
4244

45+
/// Sets the user agent
46+
#[must_use]
47+
pub fn user_agent(mut self, user_agent: &str) -> Self {
48+
self.user_agent = Some(user_agent.to_string());
49+
self
50+
}
51+
4352
/// Sets the init script.
4453
#[must_use]
4554
pub fn initialization_script(mut self, script: &str) -> Self {

core/tauri-utils/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,9 @@ pub struct WindowConfig {
796796
/// The window webview URL.
797797
#[serde(default)]
798798
pub url: WindowUrl,
799+
/// The user agent for the webview
800+
#[serde(alias = "user-agent")]
801+
pub user_agent: Option<String>,
799802
/// Whether the file drop is enabled or not on the webview. By default it is enabled.
800803
///
801804
/// Disabling it is required to use drag and drop on the frontend on Windows.
@@ -874,6 +877,7 @@ impl Default for WindowConfig {
874877
Self {
875878
label: default_window_label(),
876879
url: WindowUrl::default(),
880+
user_agent: None,
877881
file_drop_enabled: default_file_drop_enabled(),
878882
center: false,
879883
x: None,
@@ -2960,6 +2964,7 @@ mod build {
29602964
fn to_tokens(&self, tokens: &mut TokenStream) {
29612965
let label = str_lit(&self.label);
29622966
let url = &self.url;
2967+
let user_agent = opt_str_lit(self.user_agent.as_ref());
29632968
let file_drop_enabled = self.file_drop_enabled;
29642969
let center = self.center;
29652970
let x = opt_lit(self.x.as_ref());
@@ -2989,6 +2994,7 @@ mod build {
29892994
WindowConfig,
29902995
label,
29912996
url,
2997+
user_agent,
29922998
file_drop_enabled,
29932999
center,
29943000
x,

core/tauri/src/app.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,8 +1463,12 @@ impl<R: Runtime> Builder<R> {
14631463
let url = config.url.clone();
14641464
let label = config.label.clone();
14651465
let file_drop_enabled = config.file_drop_enabled;
1466+
let user_agent = config.user_agent.clone();
14661467

14671468
let mut webview_attributes = WebviewAttributes::new(url);
1469+
if let Some(ua) = user_agent {
1470+
webview_attributes = webview_attributes.user_agent(&ua.to_string());
1471+
}
14681472
if !file_drop_enabled {
14691473
webview_attributes = webview_attributes.disable_file_drop_handler();
14701474
}

core/tauri/src/window.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,13 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
491491
self
492492
}
493493

494+
/// Set the user agent for the webview
495+
#[must_use]
496+
pub fn user_agent(mut self, user_agent: &str) -> Self {
497+
self.webview_attributes.user_agent = Some(user_agent.to_string());
498+
self
499+
}
500+
494501
/// Data directory for the webview.
495502
#[must_use]
496503
pub fn data_directory(mut self, data_directory: PathBuf) -> Self {

examples/api/src-tauri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ impl AppBuilder {
6262

6363
#[allow(unused_mut)]
6464
let mut window_builder = WindowBuilder::new(app, "main", WindowUrl::default())
65+
.user_agent("Tauri API")
6566
.title("Tauri API Validation")
6667
.inner_size(1000., 800.)
6768
.min_inner_size(600., 400.);

tooling/api/src/window.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,10 @@ interface WindowOptions {
20422042
* If `true`, sets the window title to be hidden on macOS.
20432043
*/
20442044
hiddenTitle?: boolean
2045+
/**
2046+
* The user agent for the webview.
2047+
*/
2048+
userAgent?: string
20452049
}
20462050

20472051
function mapMonitor(m: Monitor | null): Monitor | null {

tooling/cli/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,13 @@
509509
}
510510
]
511511
},
512+
"userAgent": {
513+
"description": "The user agent for the webview",
514+
"type": [
515+
"string",
516+
"null"
517+
]
518+
},
512519
"fileDropEnabled": {
513520
"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.",
514521
"default": true,

0 commit comments

Comments
 (0)