From bdbf905e5d802b58693d2bd27582ce4269faf79c Mon Sep 17 00:00:00 2001 From: Julien Kauffmann <90217528+jkauffmann-legion@users.noreply.github.com> Date: Mon, 27 Sep 2021 15:27:37 -0400 Subject: [PATCH] Transformed event-loop callback to FnMut to allow mutable values (#2667) --- .changes/mutable-callbacks.md | 7 +++++++ core/tauri-runtime-wry/src/lib.rs | 14 +++++++------- core/tauri-runtime/src/lib.rs | 2 +- core/tauri/src/app.rs | 12 ++++++------ 4 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 .changes/mutable-callbacks.md diff --git a/.changes/mutable-callbacks.md b/.changes/mutable-callbacks.md new file mode 100644 index 000000000..979bd92c5 --- /dev/null +++ b/.changes/mutable-callbacks.md @@ -0,0 +1,7 @@ +--- +"tauri-runtime-wry": minor +"tauri-runtime": minor +"tauri": minor +--- + +Change event loop callbacks definition to allow callers to move in mutable values. \ No newline at end of file diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 25e1a6756..fda21e2ce 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -1770,7 +1770,7 @@ impl Runtime for Wry { } #[cfg(any(target_os = "windows", target_os = "macos"))] - fn run_iteration(&mut self, callback: F) -> RunIteration { + fn run_iteration(&mut self, mut callback: F) -> RunIteration { use wry::application::platform::run_return::EventLoopExtRunReturn; let windows = self.windows.clone(); let web_context = &self.web_context; @@ -1795,7 +1795,7 @@ impl Runtime for Wry { event_loop, control_flow, EventLoopIterationContext { - callback: &callback, + callback: &mut callback, windows: windows.clone(), window_event_listeners: &window_event_listeners, global_shortcut_manager: global_shortcut_manager.clone(), @@ -1812,7 +1812,7 @@ impl Runtime for Wry { iteration } - fn run(self, callback: F) { + fn run(self, mut callback: F) { let windows = self.windows.clone(); let web_context = self.web_context; let window_event_listeners = self.window_event_listeners.clone(); @@ -1829,7 +1829,7 @@ impl Runtime for Wry { event_loop, control_flow, EventLoopIterationContext { - callback: &callback, + callback: &mut callback, windows: windows.clone(), window_event_listeners: &window_event_listeners, global_shortcut_manager: global_shortcut_manager.clone(), @@ -1846,7 +1846,7 @@ impl Runtime for Wry { } struct EventLoopIterationContext<'a> { - callback: &'a (dyn Fn(RunEvent) + 'static), + callback: &'a mut (dyn FnMut(RunEvent) + 'static), windows: Arc>>, window_event_listeners: &'a WindowEventListeners, global_shortcut_manager: Arc>, @@ -1858,7 +1858,7 @@ struct EventLoopIterationContext<'a> { } struct UserMessageContext<'a> { - callback: Option<&'a (dyn Fn(RunEvent) + 'static)>, + callback: Option<&'a mut (dyn FnMut(RunEvent) + 'static)>, window_event_listeners: &'a WindowEventListeners, global_shortcut_manager: Arc>, clipboard_manager: Arc>, @@ -2388,7 +2388,7 @@ fn handle_event_loop( } fn on_window_close<'a>( - callback: &'a (dyn Fn(RunEvent) + 'static), + callback: &'a mut (dyn FnMut(RunEvent) + 'static), window_id: WindowId, mut windows: MutexGuard<'a, HashMap>, control_flow: &mut ControlFlow, diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index 7b63843da..e673b2103 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -336,7 +336,7 @@ pub trait Runtime: Sized + 'static { fn run_iteration(&mut self, callback: F) -> RunIteration; /// Run the webview runtime. - fn run(self, callback: F); + fn run(self, callback: F); } /// Webview dispatcher. A thread-safe handle to the webview API. diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 018422107..e0221993e 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -432,16 +432,16 @@ impl App { } /// Runs the application. - pub fn run, Event) + 'static>(mut self, callback: F) { + pub fn run, Event) + 'static>(mut self, mut callback: F) { let app_handle = self.handle(); let manager = self.manager.clone(); self.runtime.take().unwrap().run(move |event| match event { RunEvent::Exit => { app_handle.cleanup_before_exit(); - on_event_loop_event(&app_handle, RunEvent::Exit, &manager, Some(&callback)); + on_event_loop_event(&app_handle, RunEvent::Exit, &manager, Some(&mut callback)); } _ => { - on_event_loop_event(&app_handle, event, &manager, Some(&callback)); + on_event_loop_event(&app_handle, event, &manager, Some(&mut callback)); } }); } @@ -475,7 +475,7 @@ impl App { &app_handle, event, &manager, - Option::<&Box, Event)>>::None, + Option::<&mut Box, Event)>>::None, ) }) } @@ -1034,11 +1034,11 @@ impl Builder { } } -fn on_event_loop_event, Event) + 'static>( +fn on_event_loop_event, Event) + 'static>( app_handle: &AppHandle, event: RunEvent, manager: &WindowManager, - callback: Option<&F>, + callback: Option<&mut F>, ) { if let RunEvent::WindowClose(label) = &event { manager.on_window_close(label);