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

This commit is contained in:
Julien Kauffmann 2021-09-27 15:27:37 -04:00 committed by GitHub
parent 2792531347
commit bdbf905e5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 14 deletions

View File

@ -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.

View File

@ -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,

View File

@ -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.

View File

@ -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);