Skip to content

Commit

Permalink
feat(core): create WindowBuilder from WindowConfig (#6073)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
FabianLars and lucasfernog committed Jan 17, 2023
1 parent b08ae63 commit 49dff27
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changes/window-from-config.md
@@ -0,0 +1,5 @@
---
'tauri': 'minor'
---

Add a method to the `WindowBuilder` struct to recreate windows from tauri.conf.json configurations.
16 changes: 3 additions & 13 deletions core/tauri/src/endpoints/window.rs
Expand Up @@ -215,19 +215,9 @@ impl Cmd {
context: InvokeContext<R>,
options: Box<WindowConfig>,
) -> super::Result<()> {
let label = options.label.clone();
let url = options.url.clone();
let file_drop_enabled = options.file_drop_enabled;

let mut builder = crate::window::Window::builder(&context.window, label, url);
if !file_drop_enabled {
builder = builder.disable_file_drop_handler();
}

builder.window_builder =
<<R::Dispatcher as Dispatch<crate::EventLoopMessage>>::WindowBuilder>::with_config(*options);
builder.build().map_err(crate::error::into_anyhow)?;

crate::window::WindowBuilder::from_config(&context.window, *options)
.build()
.map_err(crate::error::into_anyhow)?;
Ok(())
}

Expand Down
50 changes: 49 additions & 1 deletion core/tauri/src/window.rs
Expand Up @@ -30,7 +30,7 @@ use crate::{
},
sealed::ManagerBase,
sealed::RuntimeOrDispatch,
utils::config::WindowUrl,
utils::config::{WindowConfig, WindowUrl},
CursorIcon, EventLoopMessage, Icon, Invoke, InvokeError, InvokeMessage, InvokeResolver, Manager,
PageLoadPayload, Runtime, Theme, WindowEvent,
};
Expand Down Expand Up @@ -188,6 +188,54 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
}
}

/// Initializes a webview window builder from a window config from tauri.conf.json.
/// Keep in mind that you can't create 2 windows with the same `label` so make sure
/// that the initial window was closed or change the label of the new `WindowBuilder`.
///
/// # Known issues
///
/// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue].
/// You should use `async` commands when creating windows.
///
/// # Examples
///
/// - Create a window in a command:
///
/// ```
/// #[tauri::command]
/// async fn reopen_window(app: tauri::AppHandle) {
/// let window = tauri::WindowBuilder::from_config(&app, app.config().tauri.windows.get(0).unwrap())
/// .build()
/// .unwrap();
/// }
/// ```
///
/// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583
pub fn from_config<M: Manager<R>>(manager: &'a M, config: WindowConfig) -> Self {
let runtime = manager.runtime();
let app_handle = manager.app_handle();
let url = config.url.clone();
let file_drop_enabled = config.file_drop_enabled;
let mut builder = Self {
manager: manager.manager().clone(),
runtime,
app_handle,
label: config.label.clone(),
window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder::with_config(
config,
),
webview_attributes: WebviewAttributes::new(url),
web_resource_request_handler: None,
navigation_handler: None,
};

if !file_drop_enabled {
builder = builder.disable_file_drop_handler();
}

builder
}

/// Defines a closure to be executed when the webview makes an HTTP request for a web resource, allowing you to modify the response.
///
/// Currently only implemented for the `tauri` URI protocol.
Expand Down

0 comments on commit 49dff27

Please sign in to comment.