Browse Source

Transformed event-loop callback to FnMut to allow mutable values (#2667)

Julien Kauffmann 3 năm trước cách đây
mục cha
commit
bdbf905e5d

+ 7 - 0
.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.

+ 7 - 7
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<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration {
+  fn run_iteration<F: FnMut(RunEvent) + 'static>(&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<F: Fn(RunEvent) + 'static>(self, callback: F) {
+  fn run<F: FnMut(RunEvent) + 'static>(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<Mutex<HashMap<WindowId, WindowWrapper>>>,
   window_event_listeners: &'a WindowEventListeners,
   global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
@@ -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<Mutex<WryShortcutManager>>,
   clipboard_manager: Arc<Mutex<Clipboard>>,
@@ -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<WindowId, WindowWrapper>>,
   control_flow: &mut ControlFlow,

+ 1 - 1
core/tauri-runtime/src/lib.rs

@@ -336,7 +336,7 @@ pub trait Runtime: Sized + 'static {
   fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration;
 
   /// Run the webview runtime.
-  fn run<F: Fn(RunEvent) + 'static>(self, callback: F);
+  fn run<F: FnMut(RunEvent) + 'static>(self, callback: F);
 }
 
 /// Webview dispatcher. A thread-safe handle to the webview API.

+ 6 - 6
core/tauri/src/app.rs

@@ -432,16 +432,16 @@ impl<R: Runtime> App<R> {
   }
 
   /// Runs the application.
-  pub fn run<F: Fn(&AppHandle<R>, Event) + 'static>(mut self, callback: F) {
+  pub fn run<F: FnMut(&AppHandle<R>, 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<R: Runtime> App<R> {
         &app_handle,
         event,
         &manager,
-        Option::<&Box<dyn Fn(&AppHandle<R>, Event)>>::None,
+        Option::<&mut Box<dyn FnMut(&AppHandle<R>, Event)>>::None,
       )
     })
   }
@@ -1034,11 +1034,11 @@ impl<R: Runtime> Builder<R> {
   }
 }
 
-fn on_event_loop_event<R: Runtime, F: Fn(&AppHandle<R>, Event) + 'static>(
+fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, Event) + 'static>(
   app_handle: &AppHandle<R>,
   event: RunEvent,
   manager: &WindowManager<R>,
-  callback: Option<&F>,
+  callback: Option<&mut F>,
 ) {
   if let RunEvent::WindowClose(label) = &event {
     manager.on_window_close(label);