From 869b09ee4c369e4bb24c95880e71c57e59936d61 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 10 Feb 2025 16:48:16 +1100 Subject: [PATCH 01/30] Introduce `run_return` --- crates/tauri-runtime-wry/src/lib.rs | 57 +++++++++++++++++------------ crates/tauri-runtime/src/lib.rs | 4 ++ crates/tauri/src/app.rs | 37 +++++++++++++++++++ 3 files changed, 74 insertions(+), 24 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 7e8013d6a743..bc062d4c6536 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2830,7 +2830,9 @@ impl Runtime for Wry { }); } - fn run) + 'static>(self, mut callback: F) { + fn run_return) + 'static>(&mut self, callback: F) -> i32 { + use tao::platform::run_return::EventLoopExtRunReturn; + let windows = self.context.main_thread.windows.clone(); let window_id_map = self.context.window_id_map.clone(); let web_context = self.context.main_thread.web_context; @@ -2840,12 +2842,31 @@ impl Runtime for Wry { let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); let proxy = self.event_loop.create_proxy(); - self.event_loop.run(move |event, event_loop, control_flow| { - for p in plugins.lock().unwrap().iter_mut() { - let prevent_default = p.on_event( - &event, + self + .event_loop + .run_return(move |event, event_loop, control_flow| { + for p in plugins.lock().unwrap().iter_mut() { + let prevent_default = p.on_event( + &event, + event_loop, + &proxy, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: window_id_map.clone(), + windows: windows.clone(), + #[cfg(feature = "tracing")] + active_tracing_spans: active_tracing_spans.clone(), + }, + &web_context, + ); + if prevent_default { + return; + } + } + handle_event_loop( + event, event_loop, - &proxy, control_flow, EventLoopIterationContext { callback: &mut callback, @@ -2854,25 +2875,13 @@ impl Runtime for Wry { #[cfg(feature = "tracing")] active_tracing_spans: active_tracing_spans.clone(), }, - &web_context, ); - if prevent_default { - return; - } - } - handle_event_loop( - event, - event_loop, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), - #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), - }, - ); - }) + }) + } + + fn run) + 'static>(mut self, mut callback: F) { + let exit_code = self.run_return(callback); + std::process::exit(exit_code); } } diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index 8c93a19a0907..cf3a68c8c1a2 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -437,6 +437,10 @@ pub trait Runtime: Debug + Sized + 'static { #[cfg(desktop)] fn run_iteration) + 'static>(&mut self, callback: F); + /// Equivalent to [`Runtime::run`] but returns the exit code instead of exiting the process. + #[cfg(desktop)] + fn run_return) + 'static>(&mut self, callback: F) -> i32; + /// Run the webview runtime. fn run) + 'static>(self, callback: F); } diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 57fc7a00ac56..13cb24833166 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -1092,6 +1092,43 @@ impl App { }); } + /// Runs the application, returning the exit code to use. + /// + /// # Examples + /// ```,no_run + /// let app = tauri::Builder::default() + /// // on an actual app, remove the string argument + /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json")) + /// .expect("error while building tauri application"); + /// let exit_code = app.run_return(|_app_handle, event| match event { + /// tauri::RunEvent::ExitRequested { api, .. } => { + /// api.prevent_exit(); + /// } + /// _ => {} + /// }); + /// + /// std::process::exit(exit_code); + /// ``` + pub fn run_return, RunEvent) + 'static>(mut self, mut callback: F) -> i32 { + let manager = self.manager.clone(); + let app_handle = self.handle().clone(); + + if !self.ran_setup { + if let Err(e) = setup(&mut self) { + panic!("Failed to setup app: {e}"); + } + } + + let exit_code = self.runtime.as_mut().unwrap().run_return(move |event| { + let event = on_event_loop_event(&app_handle, event, &manager); + callback(&app_handle, event); + }); + + self.cleanup_before_exit(); + + exit_code + } + /// Runs an iteration of the runtime event loop and immediately return. /// /// Note that when using this API, app cleanup is not automatically done. From 6ef503d8825b1d9bce9a4645b24da97214f85a02 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 10 Feb 2025 16:58:11 +1100 Subject: [PATCH 02/30] Fix compile error --- crates/tauri-runtime-wry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index bc062d4c6536..059c2e4de907 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2830,7 +2830,7 @@ impl Runtime for Wry { }); } - fn run_return) + 'static>(&mut self, callback: F) -> i32 { + fn run_return) + 'static>(&mut self, mut callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; let windows = self.context.main_thread.windows.clone(); From 930e45fd0a3ccf5f2dafb00558a5592d8e22a8ed Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 10 Feb 2025 16:59:07 +1100 Subject: [PATCH 03/30] Clone web_context --- crates/tauri-runtime-wry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 059c2e4de907..562cb89cca20 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2835,7 +2835,7 @@ impl Runtime for Wry { let windows = self.context.main_thread.windows.clone(); let window_id_map = self.context.window_id_map.clone(); - let web_context = self.context.main_thread.web_context; + let web_context = self.context.main_thread.web_context.clone(); let plugins = self.context.plugins.clone(); #[cfg(feature = "tracing")] From a495daf52306712aaa0f1a65c4c99a6d97072aed Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:26:13 +1100 Subject: [PATCH 04/30] Refactor to Result API --- crates/tauri/src/app.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 13cb24833166..3d524883b823 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -1100,23 +1100,23 @@ impl App { /// // on an actual app, remove the string argument /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json")) /// .expect("error while building tauri application"); - /// let exit_code = app.run_return(|_app_handle, event| match event { - /// tauri::RunEvent::ExitRequested { api, .. } => { - /// api.prevent_exit(); - /// } - /// _ => {} - /// }); + /// let exit_code = app + /// .run_return(|_app_handle, event| match event { + /// tauri::RunEvent::ExitRequested { api, .. } => { + /// api.prevent_exit(); + /// } + /// _ => {} + /// }) + /// .unwrap(); /// /// std::process::exit(exit_code); /// ``` - pub fn run_return, RunEvent) + 'static>(mut self, mut callback: F) -> i32 { + pub fn run_return, RunEvent) + 'static>(mut self, mut callback: F) -> std::result::Result { let manager = self.manager.clone(); let app_handle = self.handle().clone(); if !self.ran_setup { - if let Err(e) = setup(&mut self) { - panic!("Failed to setup app: {e}"); - } + setup(&mut self)?; } let exit_code = self.runtime.as_mut().unwrap().run_return(move |event| { @@ -1126,7 +1126,7 @@ impl App { self.cleanup_before_exit(); - exit_code + Ok(exit_code) } /// Runs an iteration of the runtime event loop and immediately return. From 94ae3ef849e7d7f91d6db657358d37b04dfea0a9 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:26:35 +1100 Subject: [PATCH 05/30] Fix clippy --- crates/tauri-runtime-wry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 562cb89cca20..35be03fc4cd2 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2879,7 +2879,7 @@ impl Runtime for Wry { }) } - fn run) + 'static>(mut self, mut callback: F) { + fn run) + 'static>(mut self, callback: F) { let exit_code = self.run_return(callback); std::process::exit(exit_code); } From 3b3dd3a743eb6cb988a0d65b64af13b8e0da1c95 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:28:52 +1100 Subject: [PATCH 06/30] Impl mock runtime --- crates/tauri/src/test/mock_runtime.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/tauri/src/test/mock_runtime.rs b/crates/tauri/src/test/mock_runtime.rs index 2d45794762ca..388c9e7fad15 100644 --- a/crates/tauri/src/test/mock_runtime.rs +++ b/crates/tauri/src/test/mock_runtime.rs @@ -1188,6 +1188,12 @@ impl Runtime for MockRuntime { ))] fn run_iteration)>(&mut self, callback: F) {} + fn run_return) + 'static>(&mut self, mut callback: F) -> i32 { + self.run(callback); + + 0 + } + fn run) + 'static>(self, mut callback: F) { self.is_running.store(true, Ordering::Relaxed); callback(RunEvent::Ready); From 5f4f2f40174b3acb4745d21ae1fad1c7a7e5d059 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:29:32 +1100 Subject: [PATCH 07/30] Make it desktop-only --- crates/tauri/src/app.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 3d524883b823..5ad5c61ec4a3 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -1111,6 +1111,7 @@ impl App { /// /// std::process::exit(exit_code); /// ``` + #[cfg(desktop)] pub fn run_return, RunEvent) + 'static>(mut self, mut callback: F) -> std::result::Result { let manager = self.manager.clone(); let app_handle = self.handle().clone(); From 9be493e2cf56e951e5ba1e5956ba46a18854bf7f Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:33:03 +1100 Subject: [PATCH 08/30] Add changelog entry --- .changes/introduce-run-return.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/introduce-run-return.md diff --git a/.changes/introduce-run-return.md b/.changes/introduce-run-return.md new file mode 100644 index 000000000000..96121f4d9c94 --- /dev/null +++ b/.changes/introduce-run-return.md @@ -0,0 +1,5 @@ +--- +tauri: 'minor:feat' +--- + +Add `App::run_return` function. Contrary to `App::run`, this will **not** exit the thread but instead return the requested exit-code. This allows the host app to perform further cleanup after Tauri has exited. From 0325274187879f503979b1011a929e175f28561b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:38:12 +1100 Subject: [PATCH 09/30] Fix compile error --- crates/tauri/src/app.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 5ad5c61ec4a3..937d0b7359ef 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -1112,7 +1112,10 @@ impl App { /// std::process::exit(exit_code); /// ``` #[cfg(desktop)] - pub fn run_return, RunEvent) + 'static>(mut self, mut callback: F) -> std::result::Result { + pub fn run_return, RunEvent) + 'static>( + mut self, + mut callback: F, + ) -> std::result::Result> { let manager = self.manager.clone(); let app_handle = self.handle().clone(); From 9953452e718c7f617d7e3f2b125f32cebacdecbb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:40:26 +1100 Subject: [PATCH 10/30] Make it semver compatible --- crates/tauri-runtime/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index cf3a68c8c1a2..b1c06f397721 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -438,8 +438,12 @@ pub trait Runtime: Debug + Sized + 'static { fn run_iteration) + 'static>(&mut self, callback: F); /// Equivalent to [`Runtime::run`] but returns the exit code instead of exiting the process. + /// + /// Note: This function only has a default impl to avoid a semver-incompatible change. #[cfg(desktop)] - fn run_return) + 'static>(&mut self, callback: F) -> i32; + fn run_return) + 'static>(&mut self, callback: F) -> i32 { + unimplemented!("Runtime::run_return is not implemented") + } /// Run the webview runtime. fn run) + 'static>(self, callback: F); From 918a1d50c3cd22ef1743850370708a37f10facf0 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:40:51 +1100 Subject: [PATCH 11/30] Extend changelog entry --- .changes/introduce-run-return.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.changes/introduce-run-return.md b/.changes/introduce-run-return.md index 96121f4d9c94..80d8604f141a 100644 --- a/.changes/introduce-run-return.md +++ b/.changes/introduce-run-return.md @@ -1,5 +1,7 @@ --- tauri: 'minor:feat' +tauri-runtime: 'minor:feat' +tauri-runtime-wry: 'minor:feat' --- Add `App::run_return` function. Contrary to `App::run`, this will **not** exit the thread but instead return the requested exit-code. This allows the host app to perform further cleanup after Tauri has exited. From f0580df595d51a254cfa838f1f0bdf2594dba185 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:52:12 +1100 Subject: [PATCH 12/30] Undo semver-hack --- crates/tauri-runtime/src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index b1c06f397721..cf3a68c8c1a2 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -438,12 +438,8 @@ pub trait Runtime: Debug + Sized + 'static { fn run_iteration) + 'static>(&mut self, callback: F); /// Equivalent to [`Runtime::run`] but returns the exit code instead of exiting the process. - /// - /// Note: This function only has a default impl to avoid a semver-incompatible change. #[cfg(desktop)] - fn run_return) + 'static>(&mut self, callback: F) -> i32 { - unimplemented!("Runtime::run_return is not implemented") - } + fn run_return) + 'static>(&mut self, callback: F) -> i32; /// Run the webview runtime. fn run) + 'static>(self, callback: F); From 075d43616c71eff878641f0a84f21348a508d7da Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:54:44 +1100 Subject: [PATCH 13/30] Reduce diff --- crates/tauri-runtime-wry/src/lib.rs | 51 ++++++++++++++--------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 35be03fc4cd2..218db71013a1 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2837,36 +2837,18 @@ impl Runtime for Wry { let window_id_map = self.context.window_id_map.clone(); let web_context = self.context.main_thread.web_context.clone(); let plugins = self.context.plugins.clone(); + let event_loop = &mut self.event_loop; #[cfg(feature = "tracing")] let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); - let proxy = self.event_loop.create_proxy(); + let proxy = event_loop.create_proxy(); - self - .event_loop - .run_return(move |event, event_loop, control_flow| { - for p in plugins.lock().unwrap().iter_mut() { - let prevent_default = p.on_event( - &event, - event_loop, - &proxy, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), - #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), - }, - &web_context, - ); - if prevent_default { - return; - } - } - handle_event_loop( - event, + event_loop.run_return(move |event, event_loop, control_flow| { + for p in plugins.lock().unwrap().iter_mut() { + let prevent_default = p.on_event( + &event, event_loop, + &proxy, control_flow, EventLoopIterationContext { callback: &mut callback, @@ -2875,8 +2857,25 @@ impl Runtime for Wry { #[cfg(feature = "tracing")] active_tracing_spans: active_tracing_spans.clone(), }, + &web_context, ); - }) + if prevent_default { + return; + } + } + handle_event_loop( + event, + event_loop, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: window_id_map.clone(), + windows: windows.clone(), + #[cfg(feature = "tracing")] + active_tracing_spans: active_tracing_spans.clone(), + }, + ); + }) } fn run) + 'static>(mut self, callback: F) { From 3d8821189dd89d58c1c73601b6fd9a59c819413c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 17:57:24 +1100 Subject: [PATCH 14/30] Remove unnecessary mut --- crates/tauri-runtime-wry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 218db71013a1..62e50933dadc 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2878,7 +2878,7 @@ impl Runtime for Wry { }) } - fn run) + 'static>(mut self, callback: F) { + fn run) + 'static>(self, callback: F) { let exit_code = self.run_return(callback); std::process::exit(exit_code); } From ac2711baca74f0b8101143368ef3b349e79c87bd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 18:00:39 +1100 Subject: [PATCH 15/30] Make it take `self` by value --- crates/tauri-runtime-wry/src/lib.rs | 4 ++-- crates/tauri-runtime/src/lib.rs | 2 +- crates/tauri/src/app.rs | 2 +- crates/tauri/src/test/mock_runtime.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 62e50933dadc..d226e293da00 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2830,12 +2830,12 @@ impl Runtime for Wry { }); } - fn run_return) + 'static>(&mut self, mut callback: F) -> i32 { + fn run_return) + 'static>(mut self, mut callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; let windows = self.context.main_thread.windows.clone(); let window_id_map = self.context.window_id_map.clone(); - let web_context = self.context.main_thread.web_context.clone(); + let web_context = self.context.main_thread.web_context; let plugins = self.context.plugins.clone(); let event_loop = &mut self.event_loop; diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index cf3a68c8c1a2..a6f0d7a0bbf1 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -439,7 +439,7 @@ pub trait Runtime: Debug + Sized + 'static { /// Equivalent to [`Runtime::run`] but returns the exit code instead of exiting the process. #[cfg(desktop)] - fn run_return) + 'static>(&mut self, callback: F) -> i32; + fn run_return) + 'static>(self, callback: F) -> i32; /// Run the webview runtime. fn run) + 'static>(self, callback: F); diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 937d0b7359ef..042bd1caebb6 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -1123,7 +1123,7 @@ impl App { setup(&mut self)?; } - let exit_code = self.runtime.as_mut().unwrap().run_return(move |event| { + let exit_code = self.runtime.take().unwrap().run_return(move |event| { let event = on_event_loop_event(&app_handle, event, &manager); callback(&app_handle, event); }); diff --git a/crates/tauri/src/test/mock_runtime.rs b/crates/tauri/src/test/mock_runtime.rs index 388c9e7fad15..2ab415e66df9 100644 --- a/crates/tauri/src/test/mock_runtime.rs +++ b/crates/tauri/src/test/mock_runtime.rs @@ -1188,7 +1188,7 @@ impl Runtime for MockRuntime { ))] fn run_iteration)>(&mut self, callback: F) {} - fn run_return) + 'static>(&mut self, mut callback: F) -> i32 { + fn run_return) + 'static>(self, mut callback: F) -> i32 { self.run(callback); 0 From fcda38400639686ecae3e5c57a9aab57ba18199d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 18:01:05 +1100 Subject: [PATCH 16/30] Reduce diff --- crates/tauri-runtime-wry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index d226e293da00..5314a1589e65 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2841,7 +2841,7 @@ impl Runtime for Wry { #[cfg(feature = "tracing")] let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); - let proxy = event_loop.create_proxy(); + let proxy = self.event_loop.create_proxy(); event_loop.run_return(move |event, event_loop, control_flow| { for p in plugins.lock().unwrap().iter_mut() { From e04ff0953a75693d95db9876f70e5b5119ecf604 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 18:01:22 +1100 Subject: [PATCH 17/30] Undo diff hack --- crates/tauri-runtime-wry/src/lib.rs | 49 +++++++++++++++-------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 5314a1589e65..d9ff67d527d7 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2837,18 +2837,36 @@ impl Runtime for Wry { let window_id_map = self.context.window_id_map.clone(); let web_context = self.context.main_thread.web_context; let plugins = self.context.plugins.clone(); - let event_loop = &mut self.event_loop; #[cfg(feature = "tracing")] let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); let proxy = self.event_loop.create_proxy(); - event_loop.run_return(move |event, event_loop, control_flow| { - for p in plugins.lock().unwrap().iter_mut() { - let prevent_default = p.on_event( - &event, + self + .event_loop + .run_return(move |event, event_loop, control_flow| { + for p in plugins.lock().unwrap().iter_mut() { + let prevent_default = p.on_event( + &event, + event_loop, + &proxy, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: window_id_map.clone(), + windows: windows.clone(), + #[cfg(feature = "tracing")] + active_tracing_spans: active_tracing_spans.clone(), + }, + &web_context, + ); + if prevent_default { + return; + } + } + handle_event_loop( + event, event_loop, - &proxy, control_flow, EventLoopIterationContext { callback: &mut callback, @@ -2857,25 +2875,8 @@ impl Runtime for Wry { #[cfg(feature = "tracing")] active_tracing_spans: active_tracing_spans.clone(), }, - &web_context, ); - if prevent_default { - return; - } - } - handle_event_loop( - event, - event_loop, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), - #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), - }, - ); - }) + }) } fn run) + 'static>(self, callback: F) { From e48e8c3f3ec0cb5daa0bc6a4b55e4a4695ab2fe1 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 18:02:34 +1100 Subject: [PATCH 18/30] Make everything cfg(desktop) --- crates/tauri-runtime-wry/src/lib.rs | 1 + crates/tauri/src/test/mock_runtime.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index d9ff67d527d7..c7db66b87ca9 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2830,6 +2830,7 @@ impl Runtime for Wry { }); } + #[cfg(desktop)] fn run_return) + 'static>(mut self, mut callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; diff --git a/crates/tauri/src/test/mock_runtime.rs b/crates/tauri/src/test/mock_runtime.rs index 2ab415e66df9..83c0ebbc29ce 100644 --- a/crates/tauri/src/test/mock_runtime.rs +++ b/crates/tauri/src/test/mock_runtime.rs @@ -1188,6 +1188,7 @@ impl Runtime for MockRuntime { ))] fn run_iteration)>(&mut self, callback: F) {} + #[cfg(desktop)] fn run_return) + 'static>(self, mut callback: F) -> i32 { self.run(callback); From 71c51ea2d157ec39deb99928d494d9985758b498 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 11 Feb 2025 18:03:48 +1100 Subject: [PATCH 19/30] Rename vars to reduce diff --- crates/tauri-runtime-wry/src/lib.rs | 50 ++++++++++++++--------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index c7db66b87ca9..81e931e8dfe0 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2843,32 +2843,13 @@ impl Runtime for Wry { let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); let proxy = self.event_loop.create_proxy(); - self - .event_loop - .run_return(move |event, event_loop, control_flow| { - for p in plugins.lock().unwrap().iter_mut() { - let prevent_default = p.on_event( - &event, - event_loop, - &proxy, - control_flow, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), - #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), - }, - &web_context, - ); - if prevent_default { - return; - } - } - handle_event_loop( - event, + self.event_loop.run_return(move |e, event_loop, cf| { + for p in plugins.lock().unwrap().iter_mut() { + let prevent_default = p.on_event( + &e, event_loop, - control_flow, + &proxy, + cf, EventLoopIterationContext { callback: &mut callback, window_id_map: window_id_map.clone(), @@ -2876,8 +2857,25 @@ impl Runtime for Wry { #[cfg(feature = "tracing")] active_tracing_spans: active_tracing_spans.clone(), }, + &web_context, ); - }) + if prevent_default { + return; + } + } + handle_event_loop( + e, + event_loop, + cf, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: window_id_map.clone(), + windows: windows.clone(), + #[cfg(feature = "tracing")] + active_tracing_spans: active_tracing_spans.clone(), + }, + ); + }) } fn run) + 'static>(self, callback: F) { From 5818937c23ac2fb76ccb71e30c28bbfcb72d66cd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 13 Feb 2025 11:30:08 +1100 Subject: [PATCH 20/30] Fix clippy --- crates/tauri/src/test/mock_runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri/src/test/mock_runtime.rs b/crates/tauri/src/test/mock_runtime.rs index 83c0ebbc29ce..e5f0fa9a9e1f 100644 --- a/crates/tauri/src/test/mock_runtime.rs +++ b/crates/tauri/src/test/mock_runtime.rs @@ -1189,7 +1189,7 @@ impl Runtime for MockRuntime { fn run_iteration)>(&mut self, callback: F) {} #[cfg(desktop)] - fn run_return) + 'static>(self, mut callback: F) -> i32 { + fn run_return) + 'static>(self, callback: F) -> i32 { self.run(callback); 0 From 97a18a67140d90b2d9b1456f751626c9cec9d84b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 13 Feb 2025 11:37:40 +1100 Subject: [PATCH 21/30] Extract make_event_handler --- crates/tauri-runtime-wry/src/lib.rs | 86 ++++++++++++++++------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 81e931e8dfe0..e13eba884697 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2831,42 +2831,43 @@ impl Runtime for Wry { } #[cfg(desktop)] - fn run_return) + 'static>(mut self, mut callback: F) -> i32 { + fn run_return) + 'static>(mut self, callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; - let windows = self.context.main_thread.windows.clone(); - let window_id_map = self.context.window_id_map.clone(); - let web_context = self.context.main_thread.web_context; - let plugins = self.context.plugins.clone(); + self + .event_loop + .run_return(make_event_handler(&self, callback)) + } - #[cfg(feature = "tracing")] - let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); - let proxy = self.event_loop.create_proxy(); + fn run) + 'static>(self, callback: F) { + self.event_loop.run(make_event_handler(&self, callback)) + } +} - self.event_loop.run_return(move |e, event_loop, cf| { - for p in plugins.lock().unwrap().iter_mut() { - let prevent_default = p.on_event( - &e, - event_loop, - &proxy, - cf, - EventLoopIterationContext { - callback: &mut callback, - window_id_map: window_id_map.clone(), - windows: windows.clone(), - #[cfg(feature = "tracing")] - active_tracing_spans: active_tracing_spans.clone(), - }, - &web_context, - ); - if prevent_default { - return; - } - } - handle_event_loop( - e, +fn make_event_handler( + runtime: Wry, + mut callback: F, +) -> impl FnMut(Event<'_, T>, &EventLoopWindowTarget, &mut ControlFlow) +where + T: UserEvent, + F: FnMut(RunEvent) + 'static, +{ + let windows = self.context.main_thread.windows.clone(); + let window_id_map = self.context.window_id_map.clone(); + let web_context = self.context.main_thread.web_context.clone(); + let plugins = self.context.plugins.clone(); + + #[cfg(feature = "tracing")] + let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); + let proxy = self.event_loop.create_proxy(); + + move |event, event_loop, control_flow| { + for p in plugins.lock().unwrap().iter_mut() { + let prevent_default = p.on_event( + &event, event_loop, - cf, + &proxy, + control_flow, EventLoopIterationContext { callback: &mut callback, window_id_map: window_id_map.clone(), @@ -2874,13 +2875,24 @@ impl Runtime for Wry { #[cfg(feature = "tracing")] active_tracing_spans: active_tracing_spans.clone(), }, + &web_context, ); - }) - } - - fn run) + 'static>(self, callback: F) { - let exit_code = self.run_return(callback); - std::process::exit(exit_code); + if prevent_default { + return; + } + } + handle_event_loop( + event, + event_loop, + control_flow, + EventLoopIterationContext { + callback: &mut callback, + window_id_map: window_id_map.clone(), + windows: windows.clone(), + #[cfg(feature = "tracing")] + active_tracing_spans: active_tracing_spans.clone(), + }, + ); } } From 419b08429dd68d866999fc662e6c399683a472f6 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 13 Feb 2025 11:38:11 +1100 Subject: [PATCH 22/30] Reduce diff --- crates/tauri-runtime-wry/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index e13eba884697..5208d77737fb 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2830,6 +2830,10 @@ impl Runtime for Wry { }); } + fn run) + 'static>(self, callback: F) { + self.event_loop.run(make_event_handler(&self, callback)) + } + #[cfg(desktop)] fn run_return) + 'static>(mut self, callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; @@ -2838,10 +2842,6 @@ impl Runtime for Wry { .event_loop .run_return(make_event_handler(&self, callback)) } - - fn run) + 'static>(self, callback: F) { - self.event_loop.run(make_event_handler(&self, callback)) - } } fn make_event_handler( From 4d7b761c4bb4653a2c2ed6a73f27638b6e141f70 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 13 Feb 2025 11:42:02 +1100 Subject: [PATCH 23/30] Deprecate `App::run_return` --- crates/tauri/src/app.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 042bd1caebb6..0c9f9e67275b 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -1156,6 +1156,9 @@ impl App { /// } /// ``` #[cfg(desktop)] + #[deprecated( + note = "When called in a loop (as suggested by the name), this function will busy-loop. To re-gain control of control flow after the app has exited, use `App::run_return` instead." + )] pub fn run_iteration, RunEvent) + 'static>(&mut self, mut callback: F) { let manager = self.manager.clone(); let app_handle = self.handle().clone(); From dad72213c2a79a062e3036c638eddabac9d72774 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 13 Feb 2025 11:43:04 +1100 Subject: [PATCH 24/30] Update changelog --- .changes/introduce-run-return.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changes/introduce-run-return.md b/.changes/introduce-run-return.md index 80d8604f141a..23ec9e36d12a 100644 --- a/.changes/introduce-run-return.md +++ b/.changes/introduce-run-return.md @@ -5,3 +5,4 @@ tauri-runtime-wry: 'minor:feat' --- Add `App::run_return` function. Contrary to `App::run`, this will **not** exit the thread but instead return the requested exit-code. This allows the host app to perform further cleanup after Tauri has exited. +The `App::run_iteration` function is deprecated as part of this because calling it in a loop - as suggested by the name - will cause a busy-loop. From 793e2d56526e31448479fd94062a68245895ca02 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 17 Feb 2025 21:12:18 +1100 Subject: [PATCH 25/30] Fix compile errors --- crates/tauri-runtime-wry/src/lib.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 5208d77737fb..b5e42804d88d 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2838,28 +2838,28 @@ impl Runtime for Wry { fn run_return) + 'static>(mut self, callback: F) -> i32 { use tao::platform::run_return::EventLoopExtRunReturn; - self - .event_loop - .run_return(make_event_handler(&self, callback)) + let event_handler = make_event_handler(&self, callback); + + self.event_loop.run_return(event_handler) } } fn make_event_handler( runtime: Wry, mut callback: F, -) -> impl FnMut(Event<'_, T>, &EventLoopWindowTarget, &mut ControlFlow) +) -> impl FnMut(Event<'_, Message>, &EventLoopWindowTarget>, &mut ControlFlow) where T: UserEvent, F: FnMut(RunEvent) + 'static, { - let windows = self.context.main_thread.windows.clone(); - let window_id_map = self.context.window_id_map.clone(); - let web_context = self.context.main_thread.web_context.clone(); - let plugins = self.context.plugins.clone(); + let windows = runtime.context.main_thread.windows.clone(); + let window_id_map = runtime.context.window_id_map.clone(); + let web_context = runtime.context.main_thread.web_context.clone(); + let plugins = runtime.context.plugins.clone(); #[cfg(feature = "tracing")] - let active_tracing_spans = self.context.main_thread.active_tracing_spans.clone(); - let proxy = self.event_loop.create_proxy(); + let active_tracing_spans = runtime.context.main_thread.active_tracing_spans.clone(); + let proxy = runtime.event_loop.create_proxy(); move |event, event_loop, control_flow| { for p in plugins.lock().unwrap().iter_mut() { From c5aadac6117fd9d2064dc21cd53cd3d340b01274 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 17 Feb 2025 21:15:37 +1100 Subject: [PATCH 26/30] Accept reference --- crates/tauri-runtime-wry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index b5e42804d88d..5349ddc2aef2 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2845,7 +2845,7 @@ impl Runtime for Wry { } fn make_event_handler( - runtime: Wry, + runtime: &Wry, mut callback: F, ) -> impl FnMut(Event<'_, Message>, &EventLoopWindowTarget>, &mut ControlFlow) where From 3eacfa505d503ca7126cb75de1101ce664718bab Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 17 Feb 2025 21:22:32 +1100 Subject: [PATCH 27/30] Create event handler first --- crates/tauri-runtime-wry/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 5349ddc2aef2..e5e260d70a6a 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -2831,7 +2831,9 @@ impl Runtime for Wry { } fn run) + 'static>(self, callback: F) { - self.event_loop.run(make_event_handler(&self, callback)) + let event_handler = make_event_handler(&self, callback); + + self.event_loop.run(event_handler) } #[cfg(desktop)] From 226adbb523b5d73dd38ee1a290c371144638c9b6 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 17 Feb 2025 21:24:59 +1100 Subject: [PATCH 28/30] Update example --- .../{run-iteration => run-return}/README.md | 0 .../{run-iteration => run-return}/index.html | 0 examples/{run-iteration => run-return}/main.rs | 17 +++++++---------- .../tauri.conf.json | 2 +- 4 files changed, 8 insertions(+), 11 deletions(-) rename examples/{run-iteration => run-return}/README.md (100%) rename examples/{run-iteration => run-return}/index.html (100%) rename examples/{run-iteration => run-return}/main.rs (61%) rename examples/{run-iteration => run-return}/tauri.conf.json (95%) diff --git a/examples/run-iteration/README.md b/examples/run-return/README.md similarity index 100% rename from examples/run-iteration/README.md rename to examples/run-return/README.md diff --git a/examples/run-iteration/index.html b/examples/run-return/index.html similarity index 100% rename from examples/run-iteration/index.html rename to examples/run-return/index.html diff --git a/examples/run-iteration/main.rs b/examples/run-return/main.rs similarity index 61% rename from examples/run-iteration/main.rs rename to examples/run-return/main.rs index 666de49fe5c9..512e368fa529 100644 --- a/examples/run-iteration/main.rs +++ b/examples/run-return/main.rs @@ -9,18 +9,15 @@ use tauri::Manager; fn main() { let mut app = tauri::Builder::default() .build(tauri::generate_context!( - "../../examples/run-iteration/tauri.conf.json" + "../../examples/run-return/tauri.conf.json" )) .expect("error while building tauri application"); - loop { - app.run_iteration(|_app, _event| { - //println!("{:?}", _event); - }); + let exit_code = app.run_return(|_app, _event| { + //println!("{:?}", _event); + }); - if app.webview_windows().is_empty() { - app.cleanup_before_exit(); - break; - } - } + println!("I run after exit"); + + std::process::exit(exit_code); } diff --git a/examples/run-iteration/tauri.conf.json b/examples/run-return/tauri.conf.json similarity index 95% rename from examples/run-iteration/tauri.conf.json rename to examples/run-return/tauri.conf.json index 80851c7aeeaf..fb3eaf94d7c0 100644 --- a/examples/run-iteration/tauri.conf.json +++ b/examples/run-return/tauri.conf.json @@ -1,6 +1,6 @@ { "$schema": "../../crates/tauri-schema-generator/schemas/config.schema.json", - "productName": "RunIteration", + "productName": "RunReturn", "version": "0.1.0", "identifier": "com.tauri.dev", "build": { From 80591e02934e8ef26c3d30883349a2a5376e1199 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 17 Feb 2025 21:27:39 +1100 Subject: [PATCH 29/30] Update manifest --- crates/tauri/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/tauri/Cargo.toml b/crates/tauri/Cargo.toml index 412d12ba2819..5440e3f341f7 100644 --- a/crates/tauri/Cargo.toml +++ b/crates/tauri/Cargo.toml @@ -225,8 +225,8 @@ name = "multiwindow" path = "../../examples/multiwindow/main.rs" [[example]] -name = "run-iteration" -path = "../../examples/run-iteration/main.rs" +name = "run-return" +path = "../../examples/run-return/main.rs" [[example]] name = "splashscreen" From 93f566ca4f46b390ac0833c75ca8c39338a1c446 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 17 Feb 2025 21:32:27 +1100 Subject: [PATCH 30/30] Fix example --- examples/run-return/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/run-return/main.rs b/examples/run-return/main.rs index 512e368fa529..df3d1fd0781c 100644 --- a/examples/run-return/main.rs +++ b/examples/run-return/main.rs @@ -4,18 +4,18 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use tauri::Manager; - fn main() { - let mut app = tauri::Builder::default() + let app = tauri::Builder::default() .build(tauri::generate_context!( "../../examples/run-return/tauri.conf.json" )) .expect("error while building tauri application"); - let exit_code = app.run_return(|_app, _event| { - //println!("{:?}", _event); - }); + let exit_code = app + .run_return(|_app, _event| { + //println!("{:?}", _event); + }) + .expect("unreachable, we haven't provided a setup fn"); println!("I run after exit");