Sfoglia il codice sorgente

feat(core): expose user_agent to window config (#5317)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Eric Hagman 2 anni fa
parent
commit
a6c94119d8

+ 5 - 0
.changes/user-agent-config.md

@@ -0,0 +1,5 @@
+---
+"tauri-utils": minor
+---
+
+Added the `user_agent` option to the window configuration.

+ 8 - 0
.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.

+ 3 - 0
core/tauri-runtime-wry/src/lib.rs

@@ -2945,6 +2945,9 @@ fn create_webview<T: UserEvent>(
     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,

+ 9 - 0
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<String>,
   pub initialization_scripts: Vec<String>,
   pub data_directory: Option<PathBuf>,
   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 {

+ 6 - 0
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<String>,
   /// 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,

+ 4 - 0
core/tauri/src/app.rs

@@ -1463,8 +1463,12 @@ impl<R: Runtime> Builder<R> {
       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();
       }

+ 7 - 0
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 {

+ 1 - 0
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.);

+ 4 - 0
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 {

+ 7 - 0
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,