Skip to content

Commit

Permalink
Reduce WM_PAINT messages of thread target window
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Sep 3, 2024
1 parent 38b73e7 commit b97b9f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
15 changes: 9 additions & 6 deletions rio-window/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ impl ActiveEventLoop {
&self,
custom_cursor: CustomCursorSource,
) -> CustomCursor {
let _span = tracing::debug_span!("rio_window::ActiveEventLoop::create_custom_cursor",)
.entered();
let _span =
tracing::debug_span!("rio_window::ActiveEventLoop::create_custom_cursor",)
.entered();

self.p.create_custom_cursor(custom_cursor)
}
Expand All @@ -428,7 +429,8 @@ impl ActiveEventLoop {
#[inline]
pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
let _span =
tracing::debug_span!("rio_window::ActiveEventLoop::available_monitors",).entered();
tracing::debug_span!("rio_window::ActiveEventLoop::available_monitors",)
.entered();

#[allow(clippy::useless_conversion)] // false positive on some platforms
self.p
Expand All @@ -446,8 +448,8 @@ impl ActiveEventLoop {
/// **Wayland / Web:** Always returns `None`.
#[inline]
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
let _span =
tracing::debug_span!("rio_window::ActiveEventLoop::primary_monitor",).entered();
let _span = tracing::debug_span!("rio_window::ActiveEventLoop::primary_monitor",)
.entered();

self.p
.primary_monitor()
Expand Down Expand Up @@ -584,7 +586,8 @@ impl<T: 'static> EventLoopProxy<T> {
///
/// [`UserEvent(event)`]: Event::UserEvent
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
let _span = tracing::debug_span!("rio_window::EventLoopProxy::send_event",).entered();
let _span =
tracing::debug_span!("rio_window::EventLoopProxy::send_event",).entered();

self.event_loop_proxy.send_event(event)
}
Expand Down
9 changes: 5 additions & 4 deletions rio-window/src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2526,17 +2526,14 @@ unsafe extern "system" fn thread_event_target_callback(
}
let userdata = unsafe { Box::from_raw(userdata_ptr) };

if msg != WM_PAINT {
unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) };
}

let mut userdata_removed = false;

// I decided to bind the closure to `callback` and pass it to catch_unwind rather than passing
// the closure to catch_unwind directly so that the match body indentation wouldn't change and
// the git blame and history would be preserved.
let callback = || match msg {
WM_NCDESTROY => {
unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) };
unsafe { super::set_window_long(window, GWL_USERDATA, 0) };
userdata_removed = true;
0
Expand All @@ -2549,11 +2546,13 @@ unsafe extern "system" fn thread_event_target_callback(
},

WM_INPUT_DEVICE_CHANGE => {
unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) };
let event = match wparam as u32 {
GIDC_ARRIVAL => DeviceEvent::Added,
GIDC_REMOVAL => DeviceEvent::Removed,
_ => unreachable!(),
};
unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) };

userdata.send_event(Event::DeviceEvent {
device_id: wrap_device_id(lparam as u32),
Expand All @@ -2572,6 +2571,7 @@ unsafe extern "system" fn thread_event_target_callback(
}

_ if msg == USER_EVENT_MSG_ID.get() => {
unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) };
// synthesis a placeholder UserEvent, so that if the callback is
// re-entered it can be buffered for later delivery. the real
// user event is still in the mpsc channel and will be pulled
Expand All @@ -2581,6 +2581,7 @@ unsafe extern "system" fn thread_event_target_callback(
0
}
_ if msg == EXEC_MSG_ID.get() => {
unsafe { RedrawWindow(window, ptr::null(), 0, RDW_INTERNALPAINT) };
let mut function: ThreadExecFn = unsafe { Box::from_raw(wparam as *mut _) };
function();
0
Expand Down

0 comments on commit b97b9f8

Please sign in to comment.