@@ -13,11 +13,11 @@ pub use winit_windows::*;
13
13
use bevy_app:: { App , AppExit , CoreStage , Events , ManualEventReader , Plugin } ;
14
14
use bevy_ecs:: { system:: IntoExclusiveSystem , world:: World } ;
15
15
use bevy_math:: { ivec2, DVec2 , Vec2 } ;
16
- use bevy_utils:: tracing:: { error, trace, warn} ;
16
+ use bevy_utils:: tracing:: { error, info , trace, warn} ;
17
17
use bevy_window:: {
18
18
CreateWindow , CursorEntered , CursorLeft , CursorMoved , FileDragAndDrop , ReceivedCharacter ,
19
- WindowBackendScaleFactorChanged , WindowCloseRequested , WindowCreated , WindowFocused ,
20
- WindowMoved , WindowResized , WindowScaleFactorChanged , Windows ,
19
+ WindowBackendScaleFactorChanged , WindowCloseRequested , WindowClosed , WindowCreated ,
20
+ WindowFocused , WindowMoved , WindowResized , WindowScaleFactorChanged , Windows ,
21
21
} ;
22
22
use winit:: {
23
23
dpi:: PhysicalPosition ,
@@ -43,8 +43,9 @@ impl Plugin for WinitPlugin {
43
43
44
44
fn change_window ( world : & mut World ) {
45
45
let world = world. cell ( ) ;
46
- let winit_windows = world. get_resource :: < WinitWindows > ( ) . unwrap ( ) ;
46
+ let mut winit_windows = world. get_resource_mut :: < WinitWindows > ( ) . unwrap ( ) ;
47
47
let mut windows = world. get_resource_mut :: < Windows > ( ) . unwrap ( ) ;
48
+ let mut removed_windows = Vec :: new ( ) ;
48
49
49
50
for bevy_window in windows. iter_mut ( ) {
50
51
let id = bevy_window. id ( ) ;
@@ -160,9 +161,26 @@ fn change_window(world: &mut World) {
160
161
window. set_max_inner_size ( Some ( max_inner_size) ) ;
161
162
}
162
163
}
164
+ bevy_window:: WindowCommand :: Close => {
165
+ let window = winit_windows. remove_window ( id) ;
166
+ // Close the window
167
+ drop ( window) ;
168
+ // Since we borrow `windows` here to iterate through them, we can't mutate it here.
169
+ // Add it to the queue to solve this
170
+ removed_windows. push ( id) ;
171
+ // No need to run any further commands - this drops the rest of the commands, although the `bevy_window::Window` will be dropped later anyway
172
+ break ;
173
+ }
163
174
}
164
175
}
165
176
}
177
+ if !removed_windows. is_empty ( ) {
178
+ let mut events = world. get_resource_mut :: < Events < WindowClosed > > ( ) . unwrap ( ) ;
179
+ for id in removed_windows {
180
+ windows. remove ( id) ;
181
+ events. send ( WindowClosed { id } ) ;
182
+ }
183
+ }
166
184
}
167
185
168
186
fn run < F > ( event_loop : EventLoop < ( ) > , event_handler : F ) -> !
@@ -280,7 +298,8 @@ pub fn winit_runner_with(mut app: App) {
280
298
let window = if let Some ( window) = windows. get_mut ( window_id) {
281
299
window
282
300
} else {
283
- warn ! ( "Skipped event for unknown Window Id {:?}" , winit_window_id) ;
301
+ // If we're here, this window was previously opened
302
+ info ! ( "Skipped event for closed window: {:?}" , window_id) ;
284
303
return ;
285
304
} ;
286
305
0 commit comments