Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/user-agent-config.md
@@ -0,0 +1,5 @@
---
"tauri-utils": minor
---

Added the `user_agent` option to the window configuration.
8 changes: 8 additions & 0 deletions .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 changes: 3 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions core/tauri-runtime/src/webview.rs
Expand Up @@ -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,
Expand All @@ -33,13 +34,21 @@ 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,
clipboard: false,
}
}

/// 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 {
Expand Down
6 changes: 6 additions & 0 deletions core/tauri-utils/src/config.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -2989,6 +2994,7 @@ mod build {
WindowConfig,
label,
url,
user_agent,
file_drop_enabled,
center,
x,
Expand Down
4 changes: 4 additions & 0 deletions core/tauri/src/app.rs
Expand Up @@ -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();
}
Expand Down
7 changes: 7 additions & 0 deletions core/tauri/src/window.rs
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions examples/api/src-tauri/src/lib.rs
Expand Up @@ -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.);
Expand Down
4 changes: 4 additions & 0 deletions tooling/api/src/window.ts
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Expand Up @@ -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,
Expand Down