Skip to content

Commit

Permalink
feat(core): add WindowBuilder type (#3598)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 5, 2022
1 parent da88243 commit 141133a
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changes/window-builder.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Added a `WindowBuilder` type.
6 changes: 3 additions & 3 deletions core/tauri-runtime/Cargo.toml
Expand Up @@ -35,9 +35,9 @@ infer = "0.4"
[target."cfg(windows)".dependencies]
webview2-com = "0.13.0"

[target."cfg(windows)".dependencies.windows]
version = "0.30.0"
features = [ "Win32_Foundation" ]
[target."cfg(windows)".dependencies.windows]
version = "0.30.0"
features = [ "Win32_Foundation" ]

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
gtk = { version = "0.15", features = [ "v3_20" ] }
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-runtime/src/lib.rs
Expand Up @@ -361,7 +361,7 @@ pub trait Dispatch: Debug + Clone + Send + Sync + Sized + 'static {
type Runtime: Runtime;

/// The winoow builder type.
type WindowBuilder: WindowBuilder + Clone;
type WindowBuilder: WindowBuilder;

/// Run a task on the main thread.
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()>;
Expand Down
6 changes: 3 additions & 3 deletions core/tauri-runtime/src/webview.rs
Expand Up @@ -14,7 +14,7 @@ use windows::Win32::Foundation::HWND;
use std::{fmt, path::PathBuf};

/// The attributes used to create an webview.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct WebviewAttributes {
pub url: WindowUrl,
pub initialization_scripts: Vec<String>,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl WebviewAttributes {
/// Do **NOT** implement this trait except for use in a custom [`Runtime`](crate::Runtime).
///
/// This trait is separate from [`WindowBuilder`] to prevent "accidental" implementation.
pub trait WindowBuilderBase: fmt::Debug + Sized {}
pub trait WindowBuilderBase: fmt::Debug + Clone + Sized {}

/// A builder for all attributes related to a single webview.
///
Expand All @@ -97,7 +97,7 @@ pub trait WindowBuilder: WindowBuilderBase {

/// Window size.
#[must_use]
fn inner_size(self, min_width: f64, min_height: f64) -> Self;
fn inner_size(self, width: f64, height: f64) -> Self;

/// Window min inner size.
#[must_use]
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/Cargo.toml
Expand Up @@ -97,7 +97,7 @@ glib = "0.15"
[target."cfg(target_os = \"macos\")".dependencies]
embed_plist = "1.2"

[target."cfg(windows)".dev-dependencies.windows]
[target."cfg(windows)".dependencies.windows]
version = "0.30.0"
features = [ "Win32_Foundation" ]

Expand Down
8 changes: 7 additions & 1 deletion core/tauri/src/app.rs
Expand Up @@ -15,7 +15,7 @@ use crate::{
plugin::{Plugin, PluginStore},
runtime::{
http::{Request as HttpRequest, Response as HttpResponse},
webview::{WebviewAttributes, WindowBuilder},
webview::{WebviewAttributes, WindowBuilder as _},
window::{PendingWindow, WindowEvent},
Dispatch, ExitRequestedEventAction, RunEvent as RuntimeRunEvent, Runtime,
},
Expand Down Expand Up @@ -359,6 +359,12 @@ macro_rules! shared_app_impl {
/// Creates a new webview window.
///
/// Data URLs are only supported with the `window-data-url` feature flag.
///
/// See [`Self::window_builder`] for an API with extended functionality.
#[deprecated(
since = "1.0.0-rc.4",
note = "The `window_builder` function offers an easier API with extended functionality"
)]
pub fn create_window<F>(
&self,
label: impl Into<String>,
Expand Down
12 changes: 3 additions & 9 deletions core/tauri/src/endpoints/window.rs
Expand Up @@ -152,18 +152,12 @@ impl Cmd {
context: InvokeContext<R>,
options: Box<WindowConfig>,
) -> super::Result<()> {
let mut window = context.window;
let label = options.label.clone();
let url = options.url.clone();

window
.create_window(label, url, |_, webview_attributes| {
(
<<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(*options),
webview_attributes,
)
})
.map_err(crate::error::into_anyhow)?;
let mut builder = context.window.builder(label, url);
builder.window_builder = <<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(*options);
builder.build().map_err(crate::error::into_anyhow)?;

Ok(())
}
Expand Down

0 comments on commit 141133a

Please sign in to comment.