Skip to content

Commit 49dff27

Browse files
feat(core): create WindowBuilder from WindowConfig (#6073)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent b08ae63 commit 49dff27

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

.changes/window-from-config.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'minor'
3+
---
4+
5+
Add a method to the `WindowBuilder` struct to recreate windows from tauri.conf.json configurations.

core/tauri/src/endpoints/window.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,9 @@ impl Cmd {
215215
context: InvokeContext<R>,
216216
options: Box<WindowConfig>,
217217
) -> super::Result<()> {
218-
let label = options.label.clone();
219-
let url = options.url.clone();
220-
let file_drop_enabled = options.file_drop_enabled;
221-
222-
let mut builder = crate::window::Window::builder(&context.window, label, url);
223-
if !file_drop_enabled {
224-
builder = builder.disable_file_drop_handler();
225-
}
226-
227-
builder.window_builder =
228-
<<R::Dispatcher as Dispatch<crate::EventLoopMessage>>::WindowBuilder>::with_config(*options);
229-
builder.build().map_err(crate::error::into_anyhow)?;
230-
218+
crate::window::WindowBuilder::from_config(&context.window, *options)
219+
.build()
220+
.map_err(crate::error::into_anyhow)?;
231221
Ok(())
232222
}
233223

core/tauri/src/window.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
},
3131
sealed::ManagerBase,
3232
sealed::RuntimeOrDispatch,
33-
utils::config::WindowUrl,
33+
utils::config::{WindowConfig, WindowUrl},
3434
CursorIcon, EventLoopMessage, Icon, Invoke, InvokeError, InvokeMessage, InvokeResolver, Manager,
3535
PageLoadPayload, Runtime, Theme, WindowEvent,
3636
};
@@ -188,6 +188,54 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
188188
}
189189
}
190190

191+
/// Initializes a webview window builder from a window config from tauri.conf.json.
192+
/// Keep in mind that you can't create 2 windows with the same `label` so make sure
193+
/// that the initial window was closed or change the label of the new `WindowBuilder`.
194+
///
195+
/// # Known issues
196+
///
197+
/// On Windows, this function deadlocks when used in a synchronous command, see [the Webview2 issue].
198+
/// You should use `async` commands when creating windows.
199+
///
200+
/// # Examples
201+
///
202+
/// - Create a window in a command:
203+
///
204+
/// ```
205+
/// #[tauri::command]
206+
/// async fn reopen_window(app: tauri::AppHandle) {
207+
/// let window = tauri::WindowBuilder::from_config(&app, app.config().tauri.windows.get(0).unwrap())
208+
/// .build()
209+
/// .unwrap();
210+
/// }
211+
/// ```
212+
///
213+
/// [the Webview2 issue]: https://github.com/tauri-apps/wry/issues/583
214+
pub fn from_config<M: Manager<R>>(manager: &'a M, config: WindowConfig) -> Self {
215+
let runtime = manager.runtime();
216+
let app_handle = manager.app_handle();
217+
let url = config.url.clone();
218+
let file_drop_enabled = config.file_drop_enabled;
219+
let mut builder = Self {
220+
manager: manager.manager().clone(),
221+
runtime,
222+
app_handle,
223+
label: config.label.clone(),
224+
window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder::with_config(
225+
config,
226+
),
227+
webview_attributes: WebviewAttributes::new(url),
228+
web_resource_request_handler: None,
229+
navigation_handler: None,
230+
};
231+
232+
if !file_drop_enabled {
233+
builder = builder.disable_file_drop_handler();
234+
}
235+
236+
builder
237+
}
238+
191239
/// Defines a closure to be executed when the webview makes an HTTP request for a web resource, allowing you to modify the response.
192240
///
193241
/// Currently only implemented for the `tauri` URI protocol.

0 commit comments

Comments
 (0)