From 6ff1370035f1cd1ef85ce3ce500183a84bb446ae Mon Sep 17 00:00:00 2001 From: Osspial Date: Thu, 16 May 2019 00:00:30 -0400 Subject: [PATCH] Fix crash caused by WM_PAINT getting invoked at inopportune times. (#866) --- examples/timer.rs | 1 + src/platform_impl/windows/event_loop.rs | 23 ++++++----------------- src/platform_impl/windows/window.rs | 2 +- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/examples/timer.rs b/examples/timer.rs index 3692d327..97b61b84 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -21,6 +21,7 @@ fn main() { Event::NewEvents(StartCause::ResumeTimeReached{..}) => { *control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::new(1, 0)); println!("\nTimer\n"); + _window.set_inner_size(winit::dpi::LogicalSize::new(300.0, 300.0)); }, Event::WindowEvent { event: WindowEvent::CloseRequested, diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index ff5be34a..fc8f7edb 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -839,6 +839,10 @@ unsafe extern "system" fn public_window_callback( use event::WindowEvent::RedrawRequested; let mut runner = subclass_input.event_loop_runner.runner.borrow_mut(); if let Some(ref mut runner) = *runner { + // This check makes sure that calls to `request_redraw()` during `EventsCleared` + // handling dispatch `RedrawRequested` immediately after `EventsCleared`, without + // spinning up a new event loop iteration. We do this because that's what the API + // says to do. match runner.runner_state { RunnerState::Idle(..) | RunnerState::DeferredNewEvents(..) => runner.call_event_handler(Event::WindowEvent { @@ -852,25 +856,10 @@ unsafe extern "system" fn public_window_callback( }, winuser::WM_PAINT => { use event::WindowEvent::RedrawRequested; - let event = || Event::WindowEvent { + subclass_input.send_event(Event::WindowEvent { window_id: RootWindowId(WindowId(window)), event: RedrawRequested, - }; - - let mut send_event = false; - { - let mut runner = subclass_input.event_loop_runner.runner.borrow_mut(); - if let Some(ref mut runner) = *runner { - match runner.runner_state { - RunnerState::Idle(..) | - RunnerState::DeferredNewEvents(..) => runner.call_event_handler(event()), - _ => send_event = true - } - } - } - if send_event { - subclass_input.send_event(event()); - } + }); commctrl::DefSubclassProc(window, msg, wparam, lparam) }, diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 7fe74c32..0f72bf91 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -730,7 +730,7 @@ unsafe fn init( window_flags.set(WindowFlags::MAXIMIZED, attributes.maximized); let window_state = { - let mut window_state = WindowState::new( + let window_state = WindowState::new( &attributes, window_icon, taskbar_icon,