Skip to content

Commit

Permalink
use the From trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 24, 2023
1 parent 8dd116d commit 878666b
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 97 deletions.
5 changes: 0 additions & 5 deletions .changes/tauri-runtime.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changes/webview-attributes-from-window-config-impl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-runtime': 'patch'
---

impl `From<&WindowConfig>` for `WebviewAttributes`.
34 changes: 17 additions & 17 deletions core/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ pub struct WebviewAttributes {
pub additional_browser_args: Option<String>,
}

impl From<&WindowConfig> for WebviewAttributes {
fn from(config: &WindowConfig) -> Self {
let mut builder = Self::new(config.url.clone());
builder = builder.accept_first_mouse(config.accept_first_mouse);
if !config.file_drop_enabled {
builder = builder.disable_file_drop_handler();
}
if let Some(user_agent) = &config.user_agent {
builder = builder.user_agent(user_agent);
}
if let Some(additional_browser_args) = &config.additional_browser_args {
builder = builder.additional_browser_args(additional_browser_args);
}
builder
}
}

impl WebviewAttributes {
/// Initializes the default attributes for a webview.
pub fn new(url: WindowUrl) -> Self {
Expand All @@ -46,22 +63,6 @@ impl WebviewAttributes {
}
}

/// Initializes the attributes for a webview from [`WindowConfig`].
pub fn from_config(config: &WindowConfig) -> Self {
let mut builder = Self::new(config.url.clone());
builder = builder.accept_first_mouse(config.accept_first_mouse);
if !config.file_drop_enabled {
builder = builder.disable_file_drop_handler();
}
if let Some(user_agent) = &config.user_agent {
builder = builder.user_agent(user_agent);
}
if let Some(additional_browser_args) = &config.additional_browser_args {
builder = builder.additional_browser_args(additional_browser_args);
}
builder
}

/// Sets the user agent
#[must_use]
pub fn user_agent(mut self, user_agent: &str) -> Self {
Expand Down Expand Up @@ -128,7 +129,6 @@ pub trait WindowBuilder: WindowBuilderBase {
/// Initializes a new window attributes builder.
fn new() -> Self;

// TODO: rename to `from_config` in v2 for consistency
/// Initializes a new webview builder from a [`WindowConfig`]
fn with_config(config: WindowConfig) -> Self;

Expand Down
1 change: 0 additions & 1 deletion core/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ impl<T: UserEvent, R: Runtime<T>> PendingWindow<T, R> {
}
}

// TODO: rename to `from_config` in v2 for consistency
/// Create a new [`PendingWindow`] from a [`WindowConfig`] with a label and starting url.
pub fn with_config(
window_config: WindowConfig,
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ impl<R: Runtime> Builder<R> {
// set up all the windows defined in the config
for config in manager.config().tauri.windows.clone() {
let label = config.label.clone();
let webview_attributes = WebviewAttributes::from_config(&config);
let webview_attributes = WebviewAttributes::from(&config);

self.pending_windows.push(PendingWindow::with_config(
config,
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
runtime: manager.runtime(),
app_handle: manager.app_handle(),
label: config.label.clone(),
webview_attributes: WebviewAttributes::from_config(&config),
webview_attributes: WebviewAttributes::from(&config),
window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder::with_config(
config,
),
Expand Down
Loading

0 comments on commit 878666b

Please sign in to comment.