@@ -127,13 +127,12 @@ unsafe extern "system" fn wnd_proc(
127127
128128 let window_state_ptr = GetWindowLongPtrW ( hwnd, GWLP_USERDATA ) as * mut RefCell < WindowState > ;
129129 if !window_state_ptr. is_null ( ) {
130- let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
131-
132- let mut window = window_state. create_window ( hwnd) ;
133- let mut window = crate :: Window :: new ( & mut window) ;
134-
135130 match msg {
136131 WM_MOUSEMOVE => {
132+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
133+ let mut window = window_state. create_window ( hwnd) ;
134+ let mut window = crate :: Window :: new ( & mut window) ;
135+
137136 let x = ( lparam & 0xFFFF ) as i16 as i32 ;
138137 let y = ( ( lparam >> 16 ) & 0xFFFF ) as i16 as i32 ;
139138
@@ -148,6 +147,10 @@ unsafe extern "system" fn wnd_proc(
148147 return 0 ;
149148 }
150149 WM_MOUSEWHEEL => {
150+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
151+ let mut window = window_state. create_window ( hwnd) ;
152+ let mut window = crate :: Window :: new ( & mut window) ;
153+
151154 let value = ( wparam >> 16 ) as i16 ;
152155 let value = value as i32 ;
153156 let value = value as f32 / WHEEL_DELTA as f32 ;
@@ -163,7 +166,11 @@ unsafe extern "system" fn wnd_proc(
163166 }
164167 WM_LBUTTONDOWN | WM_LBUTTONUP | WM_MBUTTONDOWN | WM_MBUTTONUP | WM_RBUTTONDOWN
165168 | WM_RBUTTONUP | WM_XBUTTONDOWN | WM_XBUTTONUP => {
166- let mut mouse_button_counter = ( * window_state_ptr) . borrow ( ) . mouse_button_counter ;
169+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
170+ let mut window = window_state. create_window ( hwnd) ;
171+ let mut window = crate :: Window :: new ( & mut window) ;
172+
173+ let mut mouse_button_counter = window_state. mouse_button_counter ;
167174
168175 let button = match msg {
169176 WM_LBUTTONDOWN | WM_LBUTTONUP => Some ( MouseButton :: Left ) ,
@@ -205,19 +212,37 @@ unsafe extern "system" fn wnd_proc(
205212 }
206213 }
207214 WM_TIMER => {
215+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
216+ let mut window = window_state. create_window ( hwnd) ;
217+ let mut window = crate :: Window :: new ( & mut window) ;
218+
208219 if wparam == WIN_FRAME_TIMER {
209220 window_state. handler . on_frame ( & mut window) ;
210221 }
211222 return 0 ;
212223 }
213224 WM_CLOSE => {
214- window_state. handler . on_event ( & mut window, Event :: Window ( WindowEvent :: WillClose ) ) ;
225+ // Make sure to release the borrow before the DefWindowProc call
226+ {
227+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
228+ let mut window = window_state. create_window ( hwnd) ;
229+ let mut window = crate :: Window :: new ( & mut window) ;
230+
231+ window_state
232+ . handler
233+ . on_event ( & mut window, Event :: Window ( WindowEvent :: WillClose ) ) ;
234+ }
235+
215236 // DestroyWindow(hwnd);
216237 // return 0;
217238 return DefWindowProcW ( hwnd, msg, wparam, lparam) ;
218239 }
219240 WM_CHAR | WM_SYSCHAR | WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP
220241 | WM_INPUTLANGCHANGE => {
242+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
243+ let mut window = window_state. create_window ( hwnd) ;
244+ let mut window = crate :: Window :: new ( & mut window) ;
245+
221246 let opt_event =
222247 window_state. keyboard_state . process_message ( hwnd, msg, wparam, lparam) ;
223248
@@ -230,6 +255,10 @@ unsafe extern "system" fn wnd_proc(
230255 }
231256 }
232257 WM_SIZE => {
258+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
259+ let mut window = window_state. create_window ( hwnd) ;
260+ let mut window = crate :: Window :: new ( & mut window) ;
261+
233262 let width = ( lparam & 0xFFFF ) as u16 as u32 ;
234263 let height = ( ( lparam >> 16 ) & 0xFFFF ) as u16 as u32 ;
235264
@@ -245,6 +274,8 @@ unsafe extern "system" fn wnd_proc(
245274 . on_event ( & mut window, Event :: Window ( WindowEvent :: Resized ( window_info) ) ) ;
246275 }
247276 WM_DPICHANGED => {
277+ let mut window_state = ( * window_state_ptr) . borrow_mut ( ) ;
278+
248279 // To avoid weirdness with the realtime borrow checker.
249280 let new_rect = {
250281 if let WindowScalePolicy :: SystemScaleFactor = window_state. scale_policy {
0 commit comments