Browse Source

feat(core): create WindowBuilder from WindowConfig (#6073)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Fabian-Lars 2 years ago
parent
commit
49dff27ef1
3 changed files with 57 additions and 14 deletions
  1. 5 0
      .changes/window-from-config.md
  2. 3 13
      core/tauri/src/endpoints/window.rs
  3. 49 1
      core/tauri/src/window.rs

+ 5 - 0
.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.

+ 3 - 13
core/tauri/src/endpoints/window.rs

@@ -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(())
   }
 

+ 49 - 1
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<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.