Skip to content

Commit dbd9b07

Browse files
authored
refactor(core): create_window API signature on the Window struct (#1746)
1 parent d208039 commit dbd9b07

File tree

5 files changed

+79
-18
lines changed

5 files changed

+79
-18
lines changed

.changes/window-create-refactor.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
The `Window#create_window` API now has the same signature as `App#create_window`.

core/tauri/src/endpoints/window.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// SPDX-License-Identifier: MIT
44

55
#[cfg(window_create)]
6-
use crate::Manager;
6+
use crate::sealed::ManagerBase;
7+
78
use crate::{
89
api::config::WindowConfig,
910
endpoints::InvokeResponse,
@@ -110,7 +111,7 @@ impl Cmd {
110111
crate::runtime::webview::WebviewAttributes::new(url),
111112
label.clone(),
112113
);
113-
window.create_window(pending)?.emit_others(
114+
window.create_new_window(pending)?.emit_others(
114115
&crate::runtime::manager::tauri_event::<P::Event>("tauri://window-created"),
115116
Some(WindowCreatedEvent {
116117
label: label.to_string(),

core/tauri/src/lib.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::{
4343
runtime::{
4444
tag::{Tag, TagRef},
4545
window::PendingWindow,
46-
Dispatch, Runtime,
46+
Runtime,
4747
},
4848
};
4949
use serde::Serialize;
@@ -184,19 +184,6 @@ pub trait Manager<P: Params>: sealed::ManagerBase<P> {
184184
.emit_filter(event, payload, |w| label == w.label())
185185
}
186186

187-
/// Creates a new [`Window`] on the [`Runtime`] and attaches it to the [`Manager`].
188-
fn create_window(&mut self, pending: PendingWindow<P>) -> Result<Window<P>> {
189-
use sealed::RuntimeOrDispatch::*;
190-
191-
let labels = self.manager().labels().into_iter().collect::<Vec<_>>();
192-
let pending = self.manager().prepare_window(pending, &labels)?;
193-
match self.runtime() {
194-
Runtime(runtime) => runtime.create_window(pending),
195-
Dispatch(mut dispatcher) => dispatcher.create_window(pending),
196-
}
197-
.map(|window| self.manager().attach_window(window))
198-
}
199-
200187
/// Listen to a global event.
201188
fn listen_global<E: Into<P::Event>, F>(&self, event: E, handler: F) -> EventHandler
202189
where
@@ -283,6 +270,21 @@ pub(crate) mod sealed {
283270

284271
/// The runtime or runtime dispatcher of the [`Managed`] item.
285272
fn runtime(&mut self) -> RuntimeOrDispatch<'_, P>;
273+
274+
/// Creates a new [`Window`] on the [`Runtime`] and attaches it to the [`Manager`].
275+
fn create_new_window(
276+
&mut self,
277+
pending: crate::PendingWindow<P>,
278+
) -> crate::Result<crate::Window<P>> {
279+
use crate::runtime::Dispatch;
280+
let labels = self.manager().labels().into_iter().collect::<Vec<_>>();
281+
let pending = self.manager().prepare_window(pending, &labels)?;
282+
match self.runtime() {
283+
RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
284+
RuntimeOrDispatch::Dispatch(mut dispatcher) => dispatcher.create_window(pending),
285+
}
286+
.map(|window| self.manager().attach_window(window))
287+
}
286288
}
287289
}
288290

core/tauri/src/runtime/app.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ impl<P: Params> ManagerBase<P> for App<P> {
4242
}
4343
}
4444

45+
impl<P: Params> App<P> {
46+
/// Creates a new webview window.
47+
pub fn create_window<F>(&mut self, label: P::Label, url: WindowUrl, setup: F) -> crate::Result<()>
48+
where
49+
F: FnOnce(
50+
<<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
51+
WebviewAttributes,
52+
) -> (
53+
<<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
54+
WebviewAttributes,
55+
),
56+
{
57+
let (window_attributes, webview_attributes) = setup(
58+
<<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder::new(),
59+
WebviewAttributes::new(url),
60+
);
61+
self.create_new_window(PendingWindow::new(
62+
window_attributes,
63+
webview_attributes,
64+
label,
65+
))?;
66+
Ok(())
67+
}
68+
}
69+
4570
#[cfg(feature = "updater")]
4671
impl<M: Params> App<M> {
4772
/// Runs the updater hook with built-in dialog.
@@ -238,7 +263,7 @@ where
238263
self
239264
}
240265

241-
/// Creates a new webview.
266+
/// Creates a new webview window.
242267
pub fn create_window<F>(mut self, label: L, url: WindowUrl, setup: F) -> Self
243268
where
244269
F: FnOnce(

core/tauri/src/runtime/window.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! A layer between raw [`Runtime`] webview windows and Tauri.
66
77
use crate::{
8-
api::config::WindowConfig,
8+
api::config::{WindowConfig, WindowUrl},
99
event::{Event, EventHandler},
1010
hooks::{InvokeMessage, InvokeResolver, PageLoadPayload},
1111
runtime::{
@@ -218,6 +218,34 @@ pub(crate) mod export {
218218
Self { window, manager }
219219
}
220220

221+
/// Creates a new webview window.
222+
pub fn create_window<F>(
223+
&mut self,
224+
label: P::Label,
225+
url: WindowUrl,
226+
setup: F,
227+
) -> crate::Result<()>
228+
where
229+
F: FnOnce(
230+
<<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
231+
WebviewAttributes,
232+
) -> (
233+
<<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder,
234+
WebviewAttributes,
235+
),
236+
{
237+
let (window_attributes, webview_attributes) = setup(
238+
<<P::Runtime as Runtime>::Dispatcher as Dispatch>::WindowBuilder::new(),
239+
WebviewAttributes::new(url),
240+
);
241+
self.create_new_window(PendingWindow::new(
242+
window_attributes,
243+
webview_attributes,
244+
label,
245+
))?;
246+
Ok(())
247+
}
248+
221249
/// The current window's dispatcher.
222250
pub(crate) fn dispatcher(&self) -> <P::Runtime as Runtime>::Dispatcher {
223251
self.window.dispatcher.clone()

0 commit comments

Comments
 (0)