diff --git a/native-windows-gui/src/lib.rs b/native-windows-gui/src/lib.rs index 6d6ba45a..9d2e6db0 100644 --- a/native-windows-gui/src/lib.rs +++ b/native-windows-gui/src/lib.rs @@ -24,7 +24,9 @@ pub use common_types::*; pub(crate) mod win32; pub use win32::{ - dispatch_thread_events, dispatch_thread_events_with_callback, stop_thread_dispatch, enable_visual_styles, init_common_controls, + dispatch_thread_events, dispatch_thread_events_with_callback, + dispatch_thread_events_blocking_with_callback, + stop_thread_dispatch, enable_visual_styles, init_common_controls, window::{ EventHandler, RawEventHandler, full_bind_event_handler, bind_event_handler, unbind_event_handler, diff --git a/native-windows-gui/src/win32/mod.rs b/native-windows-gui/src/win32/mod.rs index b9bba7c5..37cab118 100644 --- a/native-windows-gui/src/win32/mod.rs +++ b/native-windows-gui/src/win32/mod.rs @@ -81,6 +81,27 @@ pub fn dispatch_thread_events_with_callback(mut cb: F) } } +/** + Dispatch system events in the current thread AND execute a callback after each message received. +*/ +pub fn dispatch_thread_events_blocking_with_callback(mut cb: F) + where F: FnMut() -> () + 'static +{ + use winapi::um::winuser::GetMessageW; + use winapi::um::winuser::MSG; + + unsafe { + let mut msg: MSG = mem::zeroed(); + while GetMessageW(&mut msg, ptr::null_mut(), 0, 0) != 0 { + if IsDialogMessageW(GetAncestor(msg.hwnd, GA_ROOT), &mut msg) == 0 { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + cb(); + } + } +} + /** Break the events loop running on the current thread */