Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blocking dispatch_thread_events function #306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion native-windows-gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions native-windows-gui/src/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ pub fn dispatch_thread_events_with_callback<F>(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<F>(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
*/
Expand Down