@@ -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