From dbd9b078aaa53663f61318153ba3d50c7e554ad8 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 8 May 2021 09:37:06 -0300 Subject: [PATCH] refactor(core): `create_window` API signature on the Window struct (#1746) --- .changes/window-create-refactor.md | 5 +++++ core/tauri/src/endpoints/window.rs | 5 +++-- core/tauri/src/lib.rs | 30 ++++++++++++++++-------------- core/tauri/src/runtime/app.rs | 27 ++++++++++++++++++++++++++- core/tauri/src/runtime/window.rs | 30 +++++++++++++++++++++++++++++- 5 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 .changes/window-create-refactor.md diff --git a/.changes/window-create-refactor.md b/.changes/window-create-refactor.md new file mode 100644 index 000000000..cb31e8f40 --- /dev/null +++ b/.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`. diff --git a/core/tauri/src/endpoints/window.rs b/core/tauri/src/endpoints/window.rs index 68ef98c91..4cbf75814 100644 --- a/core/tauri/src/endpoints/window.rs +++ b/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::("tauri://window-created"), Some(WindowCreatedEvent { label: label.to_string(), diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index fefee3d0c..2a73e837f 100644 --- a/core/tauri/src/lib.rs +++ b/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: sealed::ManagerBase

{ .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

) -> Result> { - use sealed::RuntimeOrDispatch::*; - - let labels = self.manager().labels().into_iter().collect::>(); - 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, 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

, + ) -> crate::Result> { + use crate::runtime::Dispatch; + let labels = self.manager().labels().into_iter().collect::>(); + 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)) + } } } diff --git a/core/tauri/src/runtime/app.rs b/core/tauri/src/runtime/app.rs index 0f25188b1..e2e6d6efd 100644 --- a/core/tauri/src/runtime/app.rs +++ b/core/tauri/src/runtime/app.rs @@ -42,6 +42,31 @@ impl ManagerBase

for App

{ } } +impl App

{ + /// Creates a new webview window. + pub fn create_window(&mut self, label: P::Label, url: WindowUrl, setup: F) -> crate::Result<()> + where + F: FnOnce( + <::Dispatcher as Dispatch>::WindowBuilder, + WebviewAttributes, + ) -> ( + <::Dispatcher as Dispatch>::WindowBuilder, + WebviewAttributes, + ), + { + let (window_attributes, webview_attributes) = setup( + <::Dispatcher as Dispatch>::WindowBuilder::new(), + WebviewAttributes::new(url), + ); + self.create_new_window(PendingWindow::new( + window_attributes, + webview_attributes, + label, + ))?; + Ok(()) + } +} + #[cfg(feature = "updater")] impl App { /// 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(mut self, label: L, url: WindowUrl, setup: F) -> Self where F: FnOnce( diff --git a/core/tauri/src/runtime/window.rs b/core/tauri/src/runtime/window.rs index de78d6cc0..49f83fec5 100644 --- a/core/tauri/src/runtime/window.rs +++ b/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( + &mut self, + label: P::Label, + url: WindowUrl, + setup: F, + ) -> crate::Result<()> + where + F: FnOnce( + <::Dispatcher as Dispatch>::WindowBuilder, + WebviewAttributes, + ) -> ( + <::Dispatcher as Dispatch>::WindowBuilder, + WebviewAttributes, + ), + { + let (window_attributes, webview_attributes) = setup( + <::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) -> ::Dispatcher { self.window.dispatcher.clone()