@@ -4,14 +4,15 @@ use std::{io, mem, ptr};
44use std:: cell:: Cell ;
55use std:: ffi:: OsStr ;
66use std:: os:: windows:: ffi:: OsStrExt ;
7- use std:: sync:: { Arc , Mutex } ;
7+ use std:: sync:: Arc ;
88use std:: sync:: mpsc:: channel;
9+ use parking_lot:: { Mutex , MutexGuard } ;
910
1011use winapi:: ctypes:: c_int;
1112use winapi:: shared:: minwindef:: { BOOL , DWORD , FALSE , LPARAM , TRUE , UINT , WORD , WPARAM } ;
1213use winapi:: shared:: windef:: { HWND , LPPOINT , POINT , RECT } ;
1314use winapi:: um:: { combaseapi, dwmapi, libloaderapi, winuser, ole2} ;
14- use winapi:: um:: objbase:: COINIT_MULTITHREADED ;
15+ use winapi:: um:: objbase:: COINIT_APARTMENTTHREADED ;
1516use winapi:: um:: shobjidl_core:: { CLSID_TaskbarList , ITaskbarList2 } ;
1617use winapi:: um:: wingdi:: { CreateRectRgn , DeleteObject } ;
1718use winapi:: um:: oleidl:: LPDROPTARGET ;
@@ -237,7 +238,7 @@ impl Window {
237238 }
238239
239240 pub ( crate ) fn set_min_dimensions_physical ( & self , dimensions : Option < ( u32 , u32 ) > ) {
240- self . window_state . lock ( ) . unwrap ( ) . min_size = dimensions. map ( Into :: into) ;
241+ self . window_state . lock ( ) . min_size = dimensions. map ( Into :: into) ;
241242 // Make windows re-check the window size bounds.
242243 self . get_inner_size_physical ( )
243244 . map ( |( width, height) | self . set_inner_size_physical ( width, height) ) ;
@@ -253,7 +254,7 @@ impl Window {
253254 }
254255
255256 pub fn set_max_dimensions_physical ( & self , dimensions : Option < ( u32 , u32 ) > ) {
256- self . window_state . lock ( ) . unwrap ( ) . max_size = dimensions. map ( Into :: into) ;
257+ self . window_state . lock ( ) . max_size = dimensions. map ( Into :: into) ;
257258 // Make windows re-check the window size bounds.
258259 self . get_inner_size_physical ( )
259260 . map ( |( width, height) | self . set_inner_size_physical ( width, height) ) ;
@@ -270,7 +271,7 @@ impl Window {
270271
271272 #[ inline]
272273 pub fn set_resizable ( & self , resizable : bool ) {
273- let mut window_state = self . window_state . lock ( ) . unwrap ( ) ;
274+ let mut window_state = self . window_state . lock ( ) ;
274275 if mem:: replace ( & mut window_state. resizable , resizable) != resizable {
275276 // If we're in fullscreen, update stored configuration but don't apply anything.
276277 if window_state. fullscreen . is_none ( ) {
@@ -320,7 +321,7 @@ impl Window {
320321 MouseCursor :: Help => winuser:: IDC_HELP ,
321322 _ => winuser:: IDC_ARROW , // use arrow for the missing cases.
322323 } ) ;
323- self . window_state . lock ( ) . unwrap ( ) . cursor = cursor_id;
324+ self . window_state . lock ( ) . cursor = cursor_id;
324325 self . events_loop_proxy . execute_in_thread ( move || unsafe {
325326 let cursor = winuser:: LoadCursorW (
326327 ptr:: null_mut ( ) ,
@@ -376,7 +377,7 @@ impl Window {
376377 #[ inline]
377378 pub fn grab_cursor ( & self , grab : bool ) -> Result < ( ) , String > {
378379 let currently_grabbed = unsafe { self . cursor_is_grabbed ( ) } ?;
379- let window_state_lock = self . window_state . lock ( ) . unwrap ( ) ;
380+ let window_state_lock = self . window_state . lock ( ) ;
380381 if currently_grabbed == grab && grab == window_state_lock. cursor_grabbed {
381382 return Ok ( ( ) ) ;
382383 }
@@ -386,7 +387,7 @@ impl Window {
386387 self . events_loop_proxy . execute_in_thread ( move || {
387388 let result = unsafe { Self :: grab_cursor_inner ( & window, grab) } ;
388389 if result. is_ok ( ) {
389- window_state. lock ( ) . unwrap ( ) . cursor_grabbed = grab;
390+ window_state. lock ( ) . cursor_grabbed = grab;
390391 }
391392 let _ = tx. send ( result) ;
392393 } ) ;
@@ -404,14 +405,14 @@ impl Window {
404405
405406 #[ inline]
406407 pub fn hide_cursor ( & self , hide : bool ) {
407- let window_state_lock = self . window_state . lock ( ) . unwrap ( ) ;
408+ let window_state_lock = self . window_state . lock ( ) ;
408409 // We don't want to increment/decrement the display count more than once!
409410 if hide == window_state_lock. cursor_hidden { return ; }
410411 let ( tx, rx) = channel ( ) ;
411412 let window_state = Arc :: clone ( & self . window_state ) ;
412413 self . events_loop_proxy . execute_in_thread ( move || {
413414 unsafe { Self :: hide_cursor_inner ( hide) } ;
414- window_state. lock ( ) . unwrap ( ) . cursor_hidden = hide;
415+ window_state. lock ( ) . cursor_hidden = hide;
415416 let _ = tx. send ( ( ) ) ;
416417 } ) ;
417418 drop ( window_state_lock) ;
@@ -420,7 +421,7 @@ impl Window {
420421
421422 #[ inline]
422423 pub fn get_hidpi_factor ( & self ) -> f64 {
423- self . window_state . lock ( ) . unwrap ( ) . dpi_factor
424+ self . window_state . lock ( ) . dpi_factor
424425 }
425426
426427 fn set_cursor_position_physical ( & self , x : i32 , y : i32 ) -> Result < ( ) , String > {
@@ -450,7 +451,7 @@ impl Window {
450451
451452 #[ inline]
452453 pub fn set_maximized ( & self , maximized : bool ) {
453- let mut window_state = self . window_state . lock ( ) . unwrap ( ) ;
454+ let mut window_state = self . window_state . lock ( ) ;
454455 if mem:: replace ( & mut window_state. maximized , maximized) != maximized {
455456 // We only maximize if we're not in fullscreen.
456457 if window_state. fullscreen . is_none ( ) {
@@ -495,7 +496,7 @@ impl Window {
495496 ( saved_window_info. style , saved_window_info. ex_style )
496497 }
497498
498- unsafe fn restore_saved_window ( & self , window_state_lock : & mut WindowState ) {
499+ unsafe fn restore_saved_window ( & self , mut window_state_lock : MutexGuard < WindowState > ) {
499500 let ( rect, mut style, ex_style) = {
500501 // 'saved_window_info' can be None if the window has never been
501502 // in fullscreen mode before this method gets called.
@@ -520,6 +521,7 @@ impl Window {
520521 let resizable = window_state_lock. resizable ;
521522 let maximized = window_state_lock. maximized ;
522523
524+ drop ( window_state_lock) ;
523525 // We're restoring the window to its size and position from before being fullscreened.
524526 // `ShowWindow` resizes the window, so it must be called from the main thread.
525527 self . events_loop_proxy . execute_in_thread ( move || {
@@ -558,23 +560,31 @@ impl Window {
558560
559561 mark_fullscreen ( window. 0 , false ) ;
560562
561- let window_state_lock = window_state. lock ( ) . unwrap ( ) ;
563+ let window_state_lock = window_state. lock ( ) ;
562564 let _ = Self :: grab_cursor_inner ( & window, window_state_lock. cursor_grabbed ) ;
563565 } ) ;
564566 }
565567
566568 #[ inline]
567569 pub fn set_fullscreen ( & self , monitor : Option < RootMonitorId > ) {
568- let mut window_state_lock = self . window_state . lock ( ) . unwrap ( ) ;
570+ let mut window_state_lock = self . window_state . lock ( ) ;
569571 unsafe {
570- match & monitor {
571- & Some ( RootMonitorId { ref inner } ) => {
572+ let monitor_rect = monitor . as_ref ( )
573+ . map ( | RootMonitorId { ref inner } | {
572574 let ( x, y) : ( i32 , i32 ) = inner. get_position ( ) . into ( ) ;
573575 let ( width, height) : ( u32 , u32 ) = inner. get_dimensions ( ) . into ( ) ;
576+ ( x, y, width, height)
577+ } ) ;
578+
579+ match monitor_rect {
580+ Some ( ( x, y, width, height) ) => {
574581 let window = self . window . clone ( ) ;
575582 let window_state = Arc :: clone ( & self . window_state ) ;
576583
577584 let ( style, ex_style) = self . set_fullscreen_style ( & mut window_state_lock) ;
585+ window_state_lock. fullscreen = monitor;
586+ drop ( window_state_lock) ;
587+
578588 self . events_loop_proxy . execute_in_thread ( move || {
579589 let _ = Self :: grab_cursor_inner ( & window, false ) ;
580590
@@ -609,25 +619,24 @@ impl Window {
609619
610620 mark_fullscreen ( window. 0 , true ) ;
611621
612- let window_state_lock = window_state. lock ( ) . unwrap ( ) ;
622+ let window_state_lock = window_state. lock ( ) ;
613623 let _ = Self :: grab_cursor_inner ( & window, window_state_lock. cursor_grabbed ) ;
614624 } ) ;
615- }
616- & None => {
617- self . restore_saved_window ( & mut window_state_lock) ;
625+ } ,
626+ None => {
627+ window_state_lock. fullscreen = None ;
628+ self . restore_saved_window ( window_state_lock)
618629 }
619630 }
620631 }
621-
622- window_state_lock. fullscreen = monitor;
623632 }
624633
625634 #[ inline]
626635 pub fn set_decorations ( & self , decorations : bool ) {
627- let mut window_state = self . window_state . lock ( ) . unwrap ( ) ;
636+ let mut window_state = self . window_state . lock ( ) ;
628637 if mem:: replace ( & mut window_state. decorations , decorations) != decorations {
629- let style_flags = ( winuser:: WS_CAPTION | winuser:: WS_THICKFRAME ) as LONG ;
630- let ex_style_flags = ( winuser:: WS_EX_WINDOWEDGE ) as LONG ;
638+ let style_flags = ( winuser:: WS_CAPTION | winuser:: WS_THICKFRAME ) as LONG ;
639+ let ex_style_flags = ( winuser:: WS_EX_WINDOWEDGE ) as LONG ;
631640
632641 // if we are in fullscreen mode, we only change the saved window info
633642 if window_state. fullscreen . is_some ( ) {
@@ -697,7 +706,7 @@ impl Window {
697706
698707 #[ inline]
699708 pub fn set_always_on_top ( & self , always_on_top : bool ) {
700- let mut window_state = self . window_state . lock ( ) . unwrap ( ) ;
709+ let mut window_state = self . window_state . lock ( ) ;
701710 if mem:: replace ( & mut window_state. always_on_top , always_on_top) != always_on_top {
702711 let window = self . window . clone ( ) ;
703712 self . events_loop_proxy . execute_in_thread ( move || {
@@ -739,7 +748,7 @@ impl Window {
739748 } else {
740749 icon:: unset_for_window ( self . window . 0 , IconType :: Small ) ;
741750 }
742- self . window_state . lock ( ) . unwrap ( ) . window_icon = window_icon;
751+ self . window_state . lock ( ) . window_icon = window_icon;
743752 }
744753
745754 #[ inline]
@@ -752,7 +761,7 @@ impl Window {
752761 } else {
753762 icon:: unset_for_window ( self . window . 0 , IconType :: Big ) ;
754763 }
755- self . window_state . lock ( ) . unwrap ( ) . taskbar_icon = taskbar_icon;
764+ self . window_state . lock ( ) . taskbar_icon = taskbar_icon;
756765 }
757766
758767 #[ inline]
@@ -1057,6 +1066,7 @@ unsafe fn init(
10571066
10581067 let file_drop_handler = {
10591068 use winapi:: shared:: winerror:: { OLE_E_WRONGCOMPOBJ , RPC_E_CHANGED_MODE , S_OK } ;
1069+
10601070 let ole_init_result = ole2:: OleInitialize ( ptr:: null_mut ( ) ) ;
10611071 // It is ok if the initialize result is `S_FALSE` because it might happen that
10621072 // multiple windows are created on the same thread.
@@ -1136,7 +1146,7 @@ impl Drop for ComInitialized {
11361146thread_local ! {
11371147 static COM_INITIALIZED : ComInitialized = {
11381148 unsafe {
1139- combaseapi:: CoInitializeEx ( ptr:: null_mut( ) , COINIT_MULTITHREADED ) ;
1149+ combaseapi:: CoInitializeEx ( ptr:: null_mut( ) , COINIT_APARTMENTTHREADED ) ;
11401150 ComInitialized ( ptr:: null_mut( ) )
11411151 }
11421152 } ;
0 commit comments