Browse Source

refactor(core): `create_window` API signature on the Window struct (#1746)

Lucas Fernandes Nogueira 4 năm trước cách đây
mục cha
commit
dbd9b078aa

+ 5 - 0
.changes/window-create-refactor.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+The `Window#create_window` API now has the same signature as `App#create_window`.

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

@@ -3,7 +3,8 @@
 // SPDX-License-Identifier: MIT
 
 #[cfg(window_create)]
-use crate::Manager;
+use crate::sealed::ManagerBase;
+
 use crate::{
   api::config::WindowConfig,
   endpoints::InvokeResponse,
@@ -110,7 +111,7 @@ impl Cmd {
             crate::runtime::webview::WebviewAttributes::new(url),
             label.clone(),
           );
-          window.create_window(pending)?.emit_others(
+          window.create_new_window(pending)?.emit_others(
             &crate::runtime::manager::tauri_event::<P::Event>("tauri://window-created"),
             Some(WindowCreatedEvent {
               label: label.to_string(),

+ 16 - 14
core/tauri/src/lib.rs

@@ -43,7 +43,7 @@ use crate::{
   runtime::{
     tag::{Tag, TagRef},
     window::PendingWindow,
-    Dispatch, Runtime,
+    Runtime,
   },
 };
 use serde::Serialize;
@@ -184,19 +184,6 @@ pub trait Manager<P: Params>: sealed::ManagerBase<P> {
       .emit_filter(event, payload, |w| label == w.label())
   }
 
-  /// Creates a new [`Window`] on the [`Runtime`] and attaches it to the [`Manager`].
-  fn create_window(&mut self, pending: PendingWindow<P>) -> Result<Window<P>> {
-    use sealed::RuntimeOrDispatch::*;
-
-    let labels = self.manager().labels().into_iter().collect::<Vec<_>>();
-    let pending = self.manager().prepare_window(pending, &labels)?;
-    match self.runtime() {
-      Runtime(runtime) => runtime.create_window(pending),
-      Dispatch(mut dispatcher) => dispatcher.create_window(pending),
-    }
-    .map(|window| self.manager().attach_window(window))
-  }
-
   /// Listen to a global event.
   fn listen_global<E: Into<P::Event>, F>(&self, event: E, handler: F) -> EventHandler
   where
@@ -283,6 +270,21 @@ pub(crate) mod sealed {
 
     /// The runtime or runtime dispatcher of the [`Managed`] item.
     fn runtime(&mut self) -> RuntimeOrDispatch<'_, P>;
+
+    /// Creates a new [`Window`] on the [`Runtime`] and attaches it to the [`Manager`].
+    fn create_new_window(
+      &mut self,
+      pending: crate::PendingWindow<P>,
+    ) -> crate::Result<crate::Window<P>> {
+      use crate::runtime::Dispatch;
+      let labels = self.manager().labels().into_iter().collect::<Vec<_>>();
+      let pending = self.manager().prepare_window(pending, &labels)?;
+      match self.runtime() {
+        RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
+        RuntimeOrDispatch::Dispatch(mut dispatcher) => dispatcher.create_window(pending),
+      }
+      .map(|window| self.manager().attach_window(window))
+    }
   }
 }
 

+ 26 - 1
core/tauri/src/runtime/app.rs

@@ -42,6 +42,31 @@ impl<P: Params> ManagerBase<P> for App<P> {
   }
 }
 
+impl<P: Params> App<P> {
+  /// Creates a new webview window.
+  pub fn create_window<F>(&mut self, label: P::Label, url: WindowUrl, setup: F) -> crate::Result<()>
+  where
+    F: FnOnce(
+      <<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
+      WebviewAttributes,
+    ) -> (
+      <<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
+      WebviewAttributes,
+    ),
+  {
+    let (window_attributes, webview_attributes) = setup(
+      <<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder::new(),
+      WebviewAttributes::new(url),
+    );
+    self.create_new_window(PendingWindow::new(
+      window_attributes,
+      webview_attributes,
+      label,
+    ))?;
+    Ok(())
+  }
+}
+
 #[cfg(feature = "updater")]
 impl<M: Params> App<M> {
   /// Runs the updater hook with built-in dialog.
@@ -238,7 +263,7 @@ where
     self
   }
 
-  /// Creates a new webview.
+  /// Creates a new webview window.
   pub fn create_window<F>(mut self, label: L, url: WindowUrl, setup: F) -> Self
   where
     F: FnOnce(

+ 29 - 1
core/tauri/src/runtime/window.rs

@@ -5,7 +5,7 @@
 //! A layer between raw [`Runtime`] webview windows and Tauri.
 
 use crate::{
-  api::config::WindowConfig,
+  api::config::{WindowConfig, WindowUrl},
   event::{Event, EventHandler},
   hooks::{InvokeMessage, InvokeResolver, PageLoadPayload},
   runtime::{
@@ -218,6 +218,34 @@ pub(crate) mod export {
       Self { window, manager }
     }
 
+    /// Creates a new webview window.
+    pub fn create_window<F>(
+      &mut self,
+      label: P::Label,
+      url: WindowUrl,
+      setup: F,
+    ) -> crate::Result<()>
+    where
+      F: FnOnce(
+        <<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
+        WebviewAttributes,
+      ) -> (
+        <<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
+        WebviewAttributes,
+      ),
+    {
+      let (window_attributes, webview_attributes) = setup(
+        <<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder::new(),
+        WebviewAttributes::new(url),
+      );
+      self.create_new_window(PendingWindow::new(
+        window_attributes,
+        webview_attributes,
+        label,
+      ))?;
+      Ok(())
+    }
+
     /// The current window's dispatcher.
     pub(crate) fn dispatcher(&self) -> <P::Runtime as Runtime>::Dispatcher {
       self.window.dispatcher.clone()