Skip to content

Commit 52d1775

Browse files
authored
fix(core): immediately create window when using tauri::App, closes #4170 (#4172)
1 parent 4ac7bb1 commit 52d1775

3 files changed

Lines changed: 27 additions & 53 deletions

File tree

.changes/fix-app-create-window.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+
Immediately create windows when using `tauri::App` as manager.
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+
**Breaking change:** The `WindowBuilder` struct now has a lifetime annotation `WindowBuilder<R: Runtime, 'a>`.

core/tauri/src/window.rs

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,22 @@ impl Monitor {
9494
}
9595
}
9696

97-
/// A runtime handle or dispatch used to create new windows.
98-
#[derive(Debug, Clone)]
99-
pub enum RuntimeHandleOrDispatch<R: Runtime> {
100-
/// Handle to the running [`Runtime`].
101-
RuntimeHandle(R::Handle),
102-
103-
/// A dispatcher to the running [`Runtime`].
104-
Dispatch(R::Dispatcher),
105-
}
106-
10797
/// A builder for a webview window managed by Tauri.
10898
#[default_runtime(crate::Wry, wry)]
109-
pub struct WindowBuilder<R: Runtime> {
99+
pub struct WindowBuilder<'a, R: Runtime> {
110100
manager: WindowManager<R>,
111-
runtime: RuntimeHandleOrDispatch<R>,
101+
runtime: RuntimeOrDispatch<'a, R>,
112102
app_handle: AppHandle<R>,
113103
label: String,
114104
pub(crate) window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
115105
pub(crate) webview_attributes: WebviewAttributes,
116106
web_resource_request_handler: Option<Box<dyn Fn(&HttpRequest, &mut HttpResponse) + Send + Sync>>,
117107
}
118108

119-
impl<R: Runtime> fmt::Debug for WindowBuilder<R> {
109+
impl<'a, R: Runtime> fmt::Debug for WindowBuilder<'a, R> {
120110
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121111
f.debug_struct("WindowBuilder")
122112
.field("manager", &self.manager)
123-
.field("runtime", &self.runtime)
124113
.field("app_handle", &self.app_handle)
125114
.field("label", &self.label)
126115
.field("window_builder", &self.window_builder)
@@ -129,31 +118,10 @@ impl<R: Runtime> fmt::Debug for WindowBuilder<R> {
129118
}
130119
}
131120

132-
impl<R: Runtime> ManagerBase<R> for WindowBuilder<R> {
133-
fn manager(&self) -> &WindowManager<R> {
134-
&self.manager
135-
}
136-
137-
fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
138-
match &self.runtime {
139-
RuntimeHandleOrDispatch::RuntimeHandle(r) => RuntimeOrDispatch::RuntimeHandle(r.clone()),
140-
RuntimeHandleOrDispatch::Dispatch(d) => RuntimeOrDispatch::Dispatch(d.clone()),
141-
}
142-
}
143-
144-
fn managed_app_handle(&self) -> AppHandle<R> {
145-
self.app_handle.clone()
146-
}
147-
}
148-
149-
impl<R: Runtime> WindowBuilder<R> {
121+
impl<'a, R: Runtime> WindowBuilder<'a, R> {
150122
/// Initializes a webview window builder with the given window label and URL to load on the webview.
151-
pub fn new<M: Manager<R>, L: Into<String>>(manager: &M, label: L, url: WindowUrl) -> Self {
152-
let runtime = match manager.runtime() {
153-
RuntimeOrDispatch::Runtime(r) => RuntimeHandleOrDispatch::RuntimeHandle(r.handle()),
154-
RuntimeOrDispatch::RuntimeHandle(h) => RuntimeHandleOrDispatch::RuntimeHandle(h),
155-
RuntimeOrDispatch::Dispatch(d) => RuntimeHandleOrDispatch::Dispatch(d),
156-
};
123+
pub fn new<M: Manager<R>, L: Into<String>>(manager: &'a M, label: L, url: WindowUrl) -> Self {
124+
let runtime = manager.runtime();
157125
let app_handle = manager.app_handle();
158126
Self {
159127
manager: manager.manager().clone(),
@@ -219,25 +187,21 @@ impl<R: Runtime> WindowBuilder<R> {
219187
self.webview_attributes.clone(),
220188
self.label.clone(),
221189
)?;
222-
let labels = self.manager().labels().into_iter().collect::<Vec<_>>();
223-
let pending = self.manager().prepare_window(
224-
self.managed_app_handle(),
190+
let labels = self.manager.labels().into_iter().collect::<Vec<_>>();
191+
let pending = self.manager.prepare_window(
192+
self.app_handle.clone(),
225193
pending,
226194
&labels,
227195
web_resource_request_handler,
228196
)?;
229-
let window = match self.runtime() {
197+
let window = match &mut self.runtime {
230198
RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
231199
RuntimeOrDispatch::RuntimeHandle(handle) => handle.create_window(pending),
232-
RuntimeOrDispatch::Dispatch(mut dispatcher) => dispatcher.create_window(pending),
200+
RuntimeOrDispatch::Dispatch(dispatcher) => dispatcher.create_window(pending),
233201
}
234-
.map(|window| {
235-
self
236-
.manager()
237-
.attach_window(self.managed_app_handle(), window)
238-
})?;
202+
.map(|window| self.manager.attach_window(self.app_handle.clone(), window))?;
239203

240-
self.manager().emit_filter(
204+
self.manager.emit_filter(
241205
"tauri://window-created",
242206
None,
243207
Some(WindowCreatedEvent {
@@ -686,12 +650,12 @@ impl<R: Runtime> Window<R> {
686650
/// Initializes a webview window builder with the given window label and URL to load on the webview.
687651
///
688652
/// Data URLs are only supported with the `window-data-url` feature flag.
689-
pub fn builder<M: Manager<R>, L: Into<String>>(
690-
manager: &M,
653+
pub fn builder<'a, M: Manager<R>, L: Into<String>>(
654+
manager: &'a M,
691655
label: L,
692656
url: WindowUrl,
693-
) -> WindowBuilder<R> {
694-
WindowBuilder::<R>::new(manager, label.into(), url)
657+
) -> WindowBuilder<'a, R> {
658+
WindowBuilder::<'a, R>::new(manager, label.into(), url)
695659
}
696660

697661
pub(crate) fn invoke_responder(&self) -> Arc<InvokeResponder<R>> {

0 commit comments

Comments
 (0)