diff --git a/.changes/window-from-config.md b/.changes/window-from-config.md new file mode 100644 index 00000000000..c485c3e40ff --- /dev/null +++ b/.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. diff --git a/core/tauri/src/endpoints/window.rs b/core/tauri/src/endpoints/window.rs index f4e69a3206c..e08b743ed2d 100644 --- a/core/tauri/src/endpoints/window.rs +++ b/core/tauri/src/endpoints/window.rs @@ -215,19 +215,9 @@ impl Cmd { context: InvokeContext, options: Box, ) -> 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 = - <>::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(()) } diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 1f513263794..12b531bf340 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -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, }; @@ -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>(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: >::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.