@@ -586,13 +586,19 @@ impl TryFrom<WindowIcon> for WryIcon {
586586struct WindowEventWrapper ( Option < WindowEvent > ) ;
587587
588588impl WindowEventWrapper {
589- fn parse ( webview : & WindowHandle , event : & WryWindowEvent < ' _ > ) -> Self {
589+ fn parse ( webview : & Option < WindowHandle > , event : & WryWindowEvent < ' _ > ) -> Self {
590590 match event {
591591 // resized event from tao doesn't include a reliable size on macOS
592592 // because wry replaces the NSView
593- WryWindowEvent :: Resized ( _) => Self ( Some ( WindowEvent :: Resized (
594- PhysicalSizeWrapper ( webview. inner_size ( ) ) . into ( ) ,
595- ) ) ) ,
593+ WryWindowEvent :: Resized ( _) => {
594+ if let Some ( webview) = webview {
595+ Self ( Some ( WindowEvent :: Resized (
596+ PhysicalSizeWrapper ( webview. inner_size ( ) ) . into ( ) ,
597+ ) ) )
598+ } else {
599+ Self ( None )
600+ }
601+ }
596602 e => e. into ( ) ,
597603 }
598604 }
@@ -1536,7 +1542,7 @@ impl WindowHandle {
15361542#[ derive( Debug ) ]
15371543pub struct WindowWrapper {
15381544 label : String ,
1539- inner : WindowHandle ,
1545+ inner : Option < WindowHandle > ,
15401546 menu_items : Option < HashMap < u16 , WryCustomMenuItem > > ,
15411547}
15421548
@@ -2008,28 +2014,29 @@ fn handle_user_message<T: UserEvent>(
20082014 match message {
20092015 Message :: Task ( task) => task ( ) ,
20102016 Message :: Window ( id, window_message) => {
2011- if let Some ( webview ) = windows
2017+ if let Some ( ( Some ( window_handle ) , menu_items ) ) = windows
20122018 . lock ( )
20132019 . expect ( "poisoned webview collection" )
20142020 . get_mut ( & id)
2021+ . map ( |w| ( w. inner . as_ref ( ) , & mut w. menu_items ) )
20152022 {
2016- let window = webview . inner . window ( ) ;
2023+ let window = window_handle . window ( ) ;
20172024 match window_message {
20182025 #[ cfg( any( debug_assertions, feature = "devtools" ) ) ]
20192026 WindowMessage :: OpenDevTools => {
2020- if let WindowHandle :: Webview ( w) = & webview . inner {
2027+ if let WindowHandle :: Webview ( w) = & window_handle {
20212028 w. open_devtools ( ) ;
20222029 }
20232030 }
20242031 #[ cfg( any( debug_assertions, feature = "devtools" ) ) ]
20252032 WindowMessage :: CloseDevTools => {
2026- if let WindowHandle :: Webview ( w) = & webview . inner {
2033+ if let WindowHandle :: Webview ( w) = & window_handle {
20272034 w. close_devtools ( ) ;
20282035 }
20292036 }
20302037 #[ cfg( any( debug_assertions, feature = "devtools" ) ) ]
20312038 WindowMessage :: IsDevToolsOpen ( tx) => {
2032- if let WindowHandle :: Webview ( w) = & webview . inner {
2039+ if let WindowHandle :: Webview ( w) = & window_handle {
20332040 tx. send ( w. is_devtools_open ( ) ) . unwrap ( ) ;
20342041 } else {
20352042 tx. send ( false ) . unwrap ( ) ;
@@ -2054,7 +2061,7 @@ fn handle_user_message<T: UserEvent>(
20542061 )
20552062 . unwrap ( ) ,
20562063 WindowMessage :: InnerSize ( tx) => tx
2057- . send ( PhysicalSizeWrapper ( webview . inner . inner_size ( ) ) . into ( ) )
2064+ . send ( PhysicalSizeWrapper ( window_handle . inner_size ( ) ) . into ( ) )
20582065 . unwrap ( ) ,
20592066 WindowMessage :: OuterSize ( tx) => tx
20602067 . send ( PhysicalSizeWrapper ( window. outer_size ( ) ) . into ( ) )
@@ -2084,7 +2091,7 @@ fn handle_user_message<T: UserEvent>(
20842091 WindowMessage :: GtkWindow ( tx) => tx. send ( GtkWindow ( window. gtk_window ( ) . clone ( ) ) ) . unwrap ( ) ,
20852092 // Setters
20862093 WindowMessage :: Center ( tx) => {
2087- tx. send ( center_window ( window, webview . inner . inner_size ( ) ) )
2094+ tx. send ( center_window ( window, window_handle . inner_size ( ) ) )
20882095 . unwrap ( ) ;
20892096 }
20902097 WindowMessage :: RequestUserAttention ( request_type) => {
@@ -2136,7 +2143,7 @@ fn handle_user_message<T: UserEvent>(
21362143 let _ = window. drag_window ( ) ;
21372144 }
21382145 WindowMessage :: UpdateMenuItem ( id, update) => {
2139- if let Some ( menu_items) = webview . menu_items . as_mut ( ) {
2146+ if let Some ( menu_items) = menu_items. as_mut ( ) {
21402147 let item = menu_items. get_mut ( & id) . expect ( "menu item not found" ) ;
21412148 match update {
21422149 MenuUpdate :: SetEnabled ( enabled) => item. set_enabled ( enabled) ,
@@ -2161,7 +2168,7 @@ fn handle_user_message<T: UserEvent>(
21612168 . lock ( )
21622169 . expect ( "poisoned webview collection" )
21632170 . get ( & id)
2164- . map ( |w| & w. inner )
2171+ . and_then ( |w| w. inner . as_ref ( ) )
21652172 {
21662173 if let Err ( e) = webview. evaluate_script ( & script) {
21672174 #[ cfg( debug_assertions) ]
@@ -2174,7 +2181,7 @@ fn handle_user_message<T: UserEvent>(
21742181 . lock ( )
21752182 . expect ( "poisoned webview collection" )
21762183 . get ( & id)
2177- . map ( |w| & w. inner )
2184+ . and_then ( |w| w. inner . as_ref ( ) )
21782185 {
21792186 let _ = webview. print ( ) ;
21802187 }
@@ -2226,7 +2233,7 @@ fn handle_user_message<T: UserEvent>(
22262233 window_id,
22272234 WindowWrapper {
22282235 label,
2229- inner : WindowHandle :: Window ( w. clone ( ) ) ,
2236+ inner : Some ( WindowHandle :: Window ( w. clone ( ) ) ) ,
22302237 menu_items : Default :: default ( ) ,
22312238 } ,
22322239 ) ;
@@ -2445,7 +2452,7 @@ fn handle_event_loop<T: UserEvent>(
24452452 . lock ( )
24462453 . expect ( "poisoned webview collection" )
24472454 . get ( & window_id)
2448- . map ( |w| & w. inner )
2455+ . and_then ( |w| w. inner . as_ref ( ) )
24492456 {
24502457 // only focus the webview if the window is visible
24512458 // somehow tao is sending a Focused(true) event even when the window is invisible,
@@ -2494,16 +2501,18 @@ fn handle_event_loop<T: UserEvent>(
24942501 ) ;
24952502 }
24962503 WryWindowEvent :: Destroyed => {
2497- let is_empty = windows. lock ( ) . unwrap ( ) . is_empty ( ) ;
2498- if is_empty {
2499- let ( tx, rx) = channel ( ) ;
2500- callback ( RunEvent :: ExitRequested { tx } ) ;
2504+ if windows. lock ( ) . unwrap ( ) . remove ( & window_id) . is_some ( ) {
2505+ let is_empty = windows. lock ( ) . unwrap ( ) . is_empty ( ) ;
2506+ if is_empty {
2507+ let ( tx, rx) = channel ( ) ;
2508+ callback ( RunEvent :: ExitRequested { tx } ) ;
25012509
2502- let recv = rx. try_recv ( ) ;
2503- let should_prevent = matches ! ( recv, Ok ( ExitRequestedEventAction :: Prevent ) ) ;
2510+ let recv = rx. try_recv ( ) ;
2511+ let should_prevent = matches ! ( recv, Ok ( ExitRequestedEventAction :: Prevent ) ) ;
25042512
2505- if !should_prevent {
2506- * control_flow = ControlFlow :: Exit ;
2513+ if !should_prevent {
2514+ * control_flow = ControlFlow :: Exit ;
2515+ }
25072516 }
25082517 }
25092518 }
@@ -2512,7 +2521,7 @@ fn handle_event_loop<T: UserEvent>(
25122521 . lock ( )
25132522 . expect ( "poisoned webview collection" )
25142523 . get ( & window_id)
2515- . map ( |w| & w. inner )
2524+ . and_then ( |w| w. inner . as_ref ( ) )
25162525 {
25172526 if let Err ( e) = webview. resize ( ) {
25182527 #[ cfg( debug_assertions) ]
@@ -2564,7 +2573,7 @@ fn on_close_requested<'a, T: UserEvent>(
25642573 windows : Arc < Mutex < HashMap < WebviewId , WindowWrapper > > > ,
25652574 window_event_listeners : & WindowEventListeners ,
25662575 menu_event_listeners : MenuEventListeners ,
2567- ) -> Option < WindowWrapper > {
2576+ ) {
25682577 let ( tx, rx) = channel ( ) ;
25692578 let windows_guard = windows. lock ( ) . expect ( "poisoned webview collection" ) ;
25702579 if let Some ( w) = windows_guard. get ( & window_id) {
@@ -2588,32 +2597,25 @@ fn on_close_requested<'a, T: UserEvent>(
25882597 event : WindowEvent :: CloseRequested { signal_tx : tx } ,
25892598 } ) ;
25902599 if let Ok ( true ) = rx. try_recv ( ) {
2591- None
25922600 } else {
25932601 on_window_close (
25942602 window_id,
25952603 windows. lock ( ) . expect ( "poisoned webview collection" ) ,
25962604 menu_event_listeners,
2597- )
2605+ ) ;
25982606 }
2599- } else {
2600- None
26012607 }
26022608}
26032609
26042610fn on_window_close (
26052611 window_id : WebviewId ,
26062612 mut windows : MutexGuard < ' _ , HashMap < WebviewId , WindowWrapper > > ,
26072613 menu_event_listeners : MenuEventListeners ,
2608- ) -> Option < WindowWrapper > {
2609- # [ allow ( unused_mut ) ]
2610- let w = if let Some ( mut webview ) = windows . remove ( & window_id ) {
2614+ ) {
2615+ if let Some ( mut window_wrapper ) = windows . get_mut ( & window_id ) {
2616+ window_wrapper . inner = None ;
26112617 menu_event_listeners. lock ( ) . unwrap ( ) . remove ( & window_id) ;
2612- Some ( webview)
2613- } else {
2614- None
2615- } ;
2616- w
2618+ }
26172619}
26182620
26192621fn center_window ( window : & Window , window_size : WryPhysicalSize < u32 > ) -> Result < ( ) > {
@@ -2803,7 +2805,7 @@ fn create_webview<T: UserEvent>(
28032805
28042806 Ok ( WindowWrapper {
28052807 label,
2806- inner : WindowHandle :: Webview ( webview) ,
2808+ inner : Some ( WindowHandle :: Webview ( webview) ) ,
28072809 menu_items,
28082810 } )
28092811}
0 commit comments