Skip to content

Commit

Permalink
fix(core): immediately create window when using tauri::App, closes #4170
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 21, 2022
1 parent 4ac7bb1 commit 52d1775
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-app-create-window.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Immediately create windows when using `tauri::App` as manager.
5 changes: 5 additions & 0 deletions .changes/window-builder-lifetime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

**Breaking change:** The `WindowBuilder` struct now has a lifetime annotation `WindowBuilder<R: Runtime, 'a>`.
70 changes: 17 additions & 53 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,22 @@ impl Monitor {
}
}

/// A runtime handle or dispatch used to create new windows.
#[derive(Debug, Clone)]
pub enum RuntimeHandleOrDispatch<R: Runtime> {
/// Handle to the running [`Runtime`].
RuntimeHandle(R::Handle),

/// A dispatcher to the running [`Runtime`].
Dispatch(R::Dispatcher),
}

/// A builder for a webview window managed by Tauri.
#[default_runtime(crate::Wry, wry)]
pub struct WindowBuilder<R: Runtime> {
pub struct WindowBuilder<'a, R: Runtime> {
manager: WindowManager<R>,
runtime: RuntimeHandleOrDispatch<R>,
runtime: RuntimeOrDispatch<'a, R>,
app_handle: AppHandle<R>,
label: String,
pub(crate) window_builder: <R::Dispatcher as Dispatch<EventLoopMessage>>::WindowBuilder,
pub(crate) webview_attributes: WebviewAttributes,
web_resource_request_handler: Option<Box<dyn Fn(&HttpRequest, &mut HttpResponse) + Send + Sync>>,
}

impl<R: Runtime> fmt::Debug for WindowBuilder<R> {
impl<'a, R: Runtime> fmt::Debug for WindowBuilder<'a, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WindowBuilder")
.field("manager", &self.manager)
.field("runtime", &self.runtime)
.field("app_handle", &self.app_handle)
.field("label", &self.label)
.field("window_builder", &self.window_builder)
Expand All @@ -129,31 +118,10 @@ impl<R: Runtime> fmt::Debug for WindowBuilder<R> {
}
}

impl<R: Runtime> ManagerBase<R> for WindowBuilder<R> {
fn manager(&self) -> &WindowManager<R> {
&self.manager
}

fn runtime(&self) -> RuntimeOrDispatch<'_, R> {
match &self.runtime {
RuntimeHandleOrDispatch::RuntimeHandle(r) => RuntimeOrDispatch::RuntimeHandle(r.clone()),
RuntimeHandleOrDispatch::Dispatch(d) => RuntimeOrDispatch::Dispatch(d.clone()),
}
}

fn managed_app_handle(&self) -> AppHandle<R> {
self.app_handle.clone()
}
}

impl<R: Runtime> WindowBuilder<R> {
impl<'a, R: Runtime> WindowBuilder<'a, R> {
/// Initializes a webview window builder with the given window label and URL to load on the webview.
pub fn new<M: Manager<R>, L: Into<String>>(manager: &M, label: L, url: WindowUrl) -> Self {
let runtime = match manager.runtime() {
RuntimeOrDispatch::Runtime(r) => RuntimeHandleOrDispatch::RuntimeHandle(r.handle()),
RuntimeOrDispatch::RuntimeHandle(h) => RuntimeHandleOrDispatch::RuntimeHandle(h),
RuntimeOrDispatch::Dispatch(d) => RuntimeHandleOrDispatch::Dispatch(d),
};
pub fn new<M: Manager<R>, L: Into<String>>(manager: &'a M, label: L, url: WindowUrl) -> Self {
let runtime = manager.runtime();
let app_handle = manager.app_handle();
Self {
manager: manager.manager().clone(),
Expand Down Expand Up @@ -219,25 +187,21 @@ impl<R: Runtime> WindowBuilder<R> {
self.webview_attributes.clone(),
self.label.clone(),
)?;
let labels = self.manager().labels().into_iter().collect::<Vec<_>>();
let pending = self.manager().prepare_window(
self.managed_app_handle(),
let labels = self.manager.labels().into_iter().collect::<Vec<_>>();
let pending = self.manager.prepare_window(
self.app_handle.clone(),
pending,
&labels,
web_resource_request_handler,
)?;
let window = match self.runtime() {
let window = match &mut self.runtime {
RuntimeOrDispatch::Runtime(runtime) => runtime.create_window(pending),
RuntimeOrDispatch::RuntimeHandle(handle) => handle.create_window(pending),
RuntimeOrDispatch::Dispatch(mut dispatcher) => dispatcher.create_window(pending),
RuntimeOrDispatch::Dispatch(dispatcher) => dispatcher.create_window(pending),
}
.map(|window| {
self
.manager()
.attach_window(self.managed_app_handle(), window)
})?;
.map(|window| self.manager.attach_window(self.app_handle.clone(), window))?;

self.manager().emit_filter(
self.manager.emit_filter(
"tauri://window-created",
None,
Some(WindowCreatedEvent {
Expand Down Expand Up @@ -686,12 +650,12 @@ impl<R: Runtime> Window<R> {
/// Initializes a webview window builder with the given window label and URL to load on the webview.
///
/// Data URLs are only supported with the `window-data-url` feature flag.
pub fn builder<M: Manager<R>, L: Into<String>>(
manager: &M,
pub fn builder<'a, M: Manager<R>, L: Into<String>>(
manager: &'a M,
label: L,
url: WindowUrl,
) -> WindowBuilder<R> {
WindowBuilder::<R>::new(manager, label.into(), url)
) -> WindowBuilder<'a, R> {
WindowBuilder::<'a, R>::new(manager, label.into(), url)
}

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

0 comments on commit 52d1775

Please sign in to comment.