Skip to content

Commit bdbf905

Browse files
author
Julien Kauffmann
authored
Transformed event-loop callback to FnMut to allow mutable values (#2667)
1 parent 2792531 commit bdbf905

4 files changed

Lines changed: 21 additions & 14 deletions

File tree

.changes/mutable-callbacks.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri-runtime-wry": minor
3+
"tauri-runtime": minor
4+
"tauri": minor
5+
---
6+
7+
Change event loop callbacks definition to allow callers to move in mutable values.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ impl Runtime for Wry {
17701770
}
17711771

17721772
#[cfg(any(target_os = "windows", target_os = "macos"))]
1773-
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration {
1773+
fn run_iteration<F: FnMut(RunEvent) + 'static>(&mut self, mut callback: F) -> RunIteration {
17741774
use wry::application::platform::run_return::EventLoopExtRunReturn;
17751775
let windows = self.windows.clone();
17761776
let web_context = &self.web_context;
@@ -1795,7 +1795,7 @@ impl Runtime for Wry {
17951795
event_loop,
17961796
control_flow,
17971797
EventLoopIterationContext {
1798-
callback: &callback,
1798+
callback: &mut callback,
17991799
windows: windows.clone(),
18001800
window_event_listeners: &window_event_listeners,
18011801
global_shortcut_manager: global_shortcut_manager.clone(),
@@ -1812,7 +1812,7 @@ impl Runtime for Wry {
18121812
iteration
18131813
}
18141814

1815-
fn run<F: Fn(RunEvent) + 'static>(self, callback: F) {
1815+
fn run<F: FnMut(RunEvent) + 'static>(self, mut callback: F) {
18161816
let windows = self.windows.clone();
18171817
let web_context = self.web_context;
18181818
let window_event_listeners = self.window_event_listeners.clone();
@@ -1829,7 +1829,7 @@ impl Runtime for Wry {
18291829
event_loop,
18301830
control_flow,
18311831
EventLoopIterationContext {
1832-
callback: &callback,
1832+
callback: &mut callback,
18331833
windows: windows.clone(),
18341834
window_event_listeners: &window_event_listeners,
18351835
global_shortcut_manager: global_shortcut_manager.clone(),
@@ -1846,7 +1846,7 @@ impl Runtime for Wry {
18461846
}
18471847

18481848
struct EventLoopIterationContext<'a> {
1849-
callback: &'a (dyn Fn(RunEvent) + 'static),
1849+
callback: &'a mut (dyn FnMut(RunEvent) + 'static),
18501850
windows: Arc<Mutex<HashMap<WindowId, WindowWrapper>>>,
18511851
window_event_listeners: &'a WindowEventListeners,
18521852
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
@@ -1858,7 +1858,7 @@ struct EventLoopIterationContext<'a> {
18581858
}
18591859

18601860
struct UserMessageContext<'a> {
1861-
callback: Option<&'a (dyn Fn(RunEvent) + 'static)>,
1861+
callback: Option<&'a mut (dyn FnMut(RunEvent) + 'static)>,
18621862
window_event_listeners: &'a WindowEventListeners,
18631863
global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
18641864
clipboard_manager: Arc<Mutex<Clipboard>>,
@@ -2388,7 +2388,7 @@ fn handle_event_loop(
23882388
}
23892389

23902390
fn on_window_close<'a>(
2391-
callback: &'a (dyn Fn(RunEvent) + 'static),
2391+
callback: &'a mut (dyn FnMut(RunEvent) + 'static),
23922392
window_id: WindowId,
23932393
mut windows: MutexGuard<'a, HashMap<WindowId, WindowWrapper>>,
23942394
control_flow: &mut ControlFlow,

core/tauri-runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pub trait Runtime: Sized + 'static {
336336
fn run_iteration<F: Fn(RunEvent) + 'static>(&mut self, callback: F) -> RunIteration;
337337

338338
/// Run the webview runtime.
339-
fn run<F: Fn(RunEvent) + 'static>(self, callback: F);
339+
fn run<F: FnMut(RunEvent) + 'static>(self, callback: F);
340340
}
341341

342342
/// Webview dispatcher. A thread-safe handle to the webview API.

core/tauri/src/app.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,16 @@ impl<R: Runtime> App<R> {
432432
}
433433

434434
/// Runs the application.
435-
pub fn run<F: Fn(&AppHandle<R>, Event) + 'static>(mut self, callback: F) {
435+
pub fn run<F: FnMut(&AppHandle<R>, Event) + 'static>(mut self, mut callback: F) {
436436
let app_handle = self.handle();
437437
let manager = self.manager.clone();
438438
self.runtime.take().unwrap().run(move |event| match event {
439439
RunEvent::Exit => {
440440
app_handle.cleanup_before_exit();
441-
on_event_loop_event(&app_handle, RunEvent::Exit, &manager, Some(&callback));
441+
on_event_loop_event(&app_handle, RunEvent::Exit, &manager, Some(&mut callback));
442442
}
443443
_ => {
444-
on_event_loop_event(&app_handle, event, &manager, Some(&callback));
444+
on_event_loop_event(&app_handle, event, &manager, Some(&mut callback));
445445
}
446446
});
447447
}
@@ -475,7 +475,7 @@ impl<R: Runtime> App<R> {
475475
&app_handle,
476476
event,
477477
&manager,
478-
Option::<&Box<dyn Fn(&AppHandle<R>, Event)>>::None,
478+
Option::<&mut Box<dyn FnMut(&AppHandle<R>, Event)>>::None,
479479
)
480480
})
481481
}
@@ -1034,11 +1034,11 @@ impl<R: Runtime> Builder<R> {
10341034
}
10351035
}
10361036

1037-
fn on_event_loop_event<R: Runtime, F: Fn(&AppHandle<R>, Event) + 'static>(
1037+
fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, Event) + 'static>(
10381038
app_handle: &AppHandle<R>,
10391039
event: RunEvent,
10401040
manager: &WindowManager<R>,
1041-
callback: Option<&F>,
1041+
callback: Option<&mut F>,
10421042
) {
10431043
if let RunEvent::WindowClose(label) = &event {
10441044
manager.on_window_close(label);

0 commit comments

Comments
 (0)