@@ -42,11 +42,13 @@ type ShortcutMap = HashMap<String, Box<dyn Fn() + Send + 'static>>;
4242enum Message {
4343 Task ( Box < dyn FnOnce ( ) + Send > ) ,
4444 CloseWindow ( WindowId ) ,
45+ DestroyWindow ( WindowId ) ,
4546}
4647
4748struct Webview ;
4849
4950struct Window {
51+ label : String ,
5052 webviews : Vec < Webview > ,
5153}
5254
@@ -79,7 +81,7 @@ impl RuntimeContext {
7981 } else {
8082 match message {
8183 Message :: Task ( task) => task ( ) ,
82- Message :: CloseWindow ( id) => {
84+ Message :: CloseWindow ( id) | Message :: DestroyWindow ( id ) => {
8385 self . windows . borrow_mut ( ) . remove ( & id) ;
8486 }
8587 }
@@ -136,11 +138,13 @@ impl<T: UserEvent> RuntimeHandle<T> for MockRuntimeHandle {
136138 ( None , Vec :: new ( ) )
137139 } ;
138140
139- self
140- . context
141- . windows
142- . borrow_mut ( )
143- . insert ( id, Window { webviews } ) ;
141+ self . context . windows . borrow_mut ( ) . insert (
142+ id,
143+ Window {
144+ label : pending. label . clone ( ) ,
145+ webviews,
146+ } ,
147+ ) ;
144148
145149 let webview = webview_id. map ( |id| DetachedWebview {
146150 label : pending. label . clone ( ) ,
@@ -666,11 +670,13 @@ impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
666670 ( None , Vec :: new ( ) )
667671 } ;
668672
669- self
670- . context
671- . windows
672- . borrow_mut ( )
673- . insert ( id, Window { webviews } ) ;
673+ self . context . windows . borrow_mut ( ) . insert (
674+ id,
675+ Window {
676+ label : pending. label . clone ( ) ,
677+ webviews,
678+ } ,
679+ ) ;
674680
675681 let webview = webview_id. map ( |id| DetachedWebview {
676682 label : pending. label . clone ( ) ,
@@ -763,6 +769,11 @@ impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
763769 Ok ( ( ) )
764770 }
765771
772+ fn destroy ( & self ) -> Result < ( ) > {
773+ self . context . send_message ( Message :: DestroyWindow ( self . id ) ) ?;
774+ Ok ( ( ) )
775+ }
776+
766777 fn set_decorations ( & self , decorations : bool ) -> Result < ( ) > {
767778 Ok ( ( ) )
768779 }
@@ -927,11 +938,13 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
927938 ( None , Vec :: new ( ) )
928939 } ;
929940
930- self
931- . context
932- . windows
933- . borrow_mut ( )
934- . insert ( id, Window { webviews } ) ;
941+ self . context . windows . borrow_mut ( ) . insert (
942+ id,
943+ Window {
944+ label : pending. label . clone ( ) ,
945+ webviews,
946+ } ,
947+ ) ;
935948
936949 let webview = webview_id. map ( |id| DetachedWebview {
937950 label : pending. label . clone ( ) ,
@@ -1018,6 +1031,39 @@ impl<T: UserEvent> Runtime<T> for MockRuntime {
10181031 match m {
10191032 Message :: Task ( p) => p ( ) ,
10201033 Message :: CloseWindow ( id) => {
1034+ let label = self
1035+ . context
1036+ . windows
1037+ . borrow ( )
1038+ . get ( & id)
1039+ . map ( |w| w. label . clone ( ) ) ;
1040+ if let Some ( label) = label {
1041+ let ( tx, rx) = channel ( ) ;
1042+ callback ( RunEvent :: WindowEvent {
1043+ label,
1044+ event : WindowEvent :: CloseRequested { signal_tx : tx } ,
1045+ } ) ;
1046+
1047+ let should_prevent = matches ! ( rx. try_recv( ) , Ok ( true ) ) ;
1048+ if !should_prevent {
1049+ self . context . windows . borrow_mut ( ) . remove ( & id) ;
1050+
1051+ let is_empty = self . context . windows . borrow ( ) . is_empty ( ) ;
1052+ if is_empty {
1053+ let ( tx, rx) = channel ( ) ;
1054+ callback ( RunEvent :: ExitRequested { code : None , tx } ) ;
1055+
1056+ let recv = rx. try_recv ( ) ;
1057+ let should_prevent = matches ! ( recv, Ok ( ExitRequestedEventAction :: Prevent ) ) ;
1058+
1059+ if !should_prevent {
1060+ break ;
1061+ }
1062+ }
1063+ }
1064+ }
1065+ }
1066+ Message :: DestroyWindow ( id) => {
10211067 let removed = self . context . windows . borrow_mut ( ) . remove ( & id) . is_some ( ) ;
10221068 if removed {
10231069 let is_empty = self . context . windows . borrow ( ) . is_empty ( ) ;
0 commit comments