@@ -12,6 +12,7 @@ pub use winit_windows::*;
12
12
use bevy_app:: { prelude:: * , AppExit } ;
13
13
use bevy_ecs:: { Resources , World } ;
14
14
use bevy_math:: Vec2 ;
15
+ use bevy_log:: info;
15
16
use bevy_utils:: tracing:: { error, trace} ;
16
17
use bevy_window:: {
17
18
CreateWindow , CursorEntered , CursorLeft , CursorMoved , ReceivedCharacter , WindowCloseRequested ,
@@ -166,10 +167,15 @@ pub fn winit_runner(mut app: App) {
166
167
167
168
trace ! ( "Entering winit event loop" ) ;
168
169
169
- let should_return_from_run = app
170
+ let WinitConfig {
171
+ return_from_run : should_return_from_run,
172
+ ignore_unknown_window_id : should_ignore_unknown_window_id,
173
+ } = app
170
174
. resources
171
175
. get :: < WinitConfig > ( )
172
- . map_or ( false , |config| config. return_from_run ) ;
176
+ . as_deref ( )
177
+ . cloned ( )
178
+ . unwrap_or_default ( ) ;
173
179
174
180
let event_handler = move |event : Event < ( ) > ,
175
181
event_loop : & EventLoopWindowTarget < ( ) > ,
@@ -187,163 +193,150 @@ pub fn winit_runner(mut app: App) {
187
193
event,
188
194
window_id : winit_window_id,
189
195
..
190
- } => match event {
191
- WindowEvent :: Resized ( size) => {
192
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
193
- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
194
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
195
- let window = windows. get_mut ( window_id) . unwrap ( ) ;
196
- window. update_actual_size_from_backend ( size. width , size. height ) ;
197
- let mut resize_events =
198
- app. resources . get_mut :: < Events < WindowResized > > ( ) . unwrap ( ) ;
199
- resize_events. send ( WindowResized {
200
- id : window_id,
196
+ } => {
197
+ let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
198
+ let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
199
+ let window_id_opt = winit_windows. get_window_id ( winit_window_id) ;
200
+ let window_opt = window_id_opt
201
+ . as_ref ( )
202
+ . cloned ( )
203
+ . and_then ( |id| windows. get_mut ( id) ) ;
204
+ if should_ignore_unknown_window_id && window_opt. is_none ( ) {
205
+ info ! ( "Skipped event for unknown Window Id {:?}" , winit_window_id) ;
206
+ return ;
207
+ }
208
+ let window = window_opt. unwrap ( ) ;
209
+ let window_id = window_id_opt. unwrap ( ) ;
210
+ match event {
211
+ WindowEvent :: Resized ( size) => {
212
+ window. update_actual_size_from_backend ( size. width , size. height ) ;
213
+ let mut resize_events =
214
+ app. resources . get_mut :: < Events < WindowResized > > ( ) . unwrap ( ) ;
215
+ resize_events. send ( WindowResized {
216
+ id : window_id,
201
217
width : window. width ( ) ,
202
218
height : window. height ( ) ,
203
- } ) ;
204
- }
205
- WindowEvent :: CloseRequested => {
206
- let mut window_close_requested_events = app
207
- . resources
208
- . get_mut :: < Events < WindowCloseRequested > > ( )
209
- . unwrap ( ) ;
210
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
211
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
212
- window_close_requested_events. send ( WindowCloseRequested { id : window_id } ) ;
213
- }
214
- WindowEvent :: KeyboardInput { ref input, .. } => {
215
- let mut keyboard_input_events =
216
- app. resources . get_mut :: < Events < KeyboardInput > > ( ) . unwrap ( ) ;
217
- keyboard_input_events. send ( converters:: convert_keyboard_input ( input) ) ;
218
- }
219
- WindowEvent :: CursorMoved { position, .. } => {
220
- let mut cursor_moved_events =
221
- app. resources . get_mut :: < Events < CursorMoved > > ( ) . unwrap ( ) ;
222
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
223
- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
224
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
225
- let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
226
- let window = windows. get_mut ( window_id) . unwrap ( ) ;
227
- let position = position. to_logical ( winit_window. scale_factor ( ) ) ;
228
- let inner_size = winit_window
229
- . inner_size ( )
230
- . to_logical :: < f32 > ( winit_window. scale_factor ( ) ) ;
219
+ } ) ;
220
+ }
221
+ WindowEvent :: CloseRequested => {
222
+ let mut window_close_requested_events = app
223
+ . resources
224
+ . get_mut :: < Events < WindowCloseRequested > > ( )
225
+ . unwrap ( ) ;
226
+ window_close_requested_events. send ( WindowCloseRequested { id : window_id } ) ;
227
+ }
228
+ WindowEvent :: KeyboardInput { ref input, .. } => {
229
+ let mut keyboard_input_events =
230
+ app. resources . get_mut :: < Events < KeyboardInput > > ( ) . unwrap ( ) ;
231
+ keyboard_input_events. send ( converters:: convert_keyboard_input ( input) ) ;
232
+ }
233
+ WindowEvent :: CursorMoved { position, .. } => {
234
+ let mut cursor_moved_events =
235
+ app. resources . get_mut :: < Events < CursorMoved > > ( ) . unwrap ( ) ;
236
+ let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
237
+ let position = position. to_logical ( winit_window. scale_factor ( ) ) ;
238
+ let inner_size = winit_window
239
+ . inner_size ( )
240
+ . to_logical :: < f32 > ( winit_window. scale_factor ( ) ) ;
231
241
232
- // move origin to bottom left
233
- let y_position = inner_size. height - position. y ;
242
+ // move origin to bottom left
243
+ let y_position = inner_size. height - position. y ;
234
244
235
- let position = Vec2 :: new ( position. x , y_position) ;
236
- window. update_cursor_position_from_backend ( Some ( position) ) ;
245
+ let position = Vec2 :: new ( position. x , y_position) ;
246
+ window. update_cursor_position_from_backend ( Some ( position) ) ;
237
247
238
- cursor_moved_events. send ( CursorMoved {
239
- id : window_id,
240
- position,
241
- } ) ;
242
- }
243
- WindowEvent :: CursorEntered { .. } => {
244
- let mut cursor_entered_events =
245
- app. resources . get_mut :: < Events < CursorEntered > > ( ) . unwrap ( ) ;
246
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
247
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
248
- cursor_entered_events. send ( CursorEntered { id : window_id } ) ;
249
- }
250
- WindowEvent :: CursorLeft { .. } => {
251
- let mut cursor_left_events =
252
- app. resources . get_mut :: < Events < CursorLeft > > ( ) . unwrap ( ) ;
253
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
254
- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
255
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
256
- let window = windows. get_mut ( window_id) . unwrap ( ) ;
257
- window. update_cursor_position_from_backend ( None ) ;
258
- cursor_left_events. send ( CursorLeft { id : window_id } ) ;
259
- }
260
- WindowEvent :: MouseInput { state, button, .. } => {
261
- let mut mouse_button_input_events =
262
- app. resources . get_mut :: < Events < MouseButtonInput > > ( ) . unwrap ( ) ;
263
- mouse_button_input_events. send ( MouseButtonInput {
264
- button : converters:: convert_mouse_button ( button) ,
265
- state : converters:: convert_element_state ( state) ,
266
- } ) ;
267
- }
268
- WindowEvent :: MouseWheel { delta, .. } => match delta {
269
- event:: MouseScrollDelta :: LineDelta ( x, y) => {
270
- let mut mouse_wheel_input_events =
271
- app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
272
- mouse_wheel_input_events. send ( MouseWheel {
273
- unit : MouseScrollUnit :: Line ,
274
- x,
275
- y,
248
+ cursor_moved_events. send ( CursorMoved {
249
+ id : window_id,
250
+ position,
276
251
} ) ;
277
252
}
278
- event:: MouseScrollDelta :: PixelDelta ( p) => {
279
- let mut mouse_wheel_input_events =
280
- app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
281
- mouse_wheel_input_events. send ( MouseWheel {
282
- unit : MouseScrollUnit :: Pixel ,
283
- x : p. x as f32 ,
284
- y : p. y as f32 ,
253
+ WindowEvent :: CursorEntered { .. } => {
254
+ let mut cursor_entered_events =
255
+ app. resources . get_mut :: < Events < CursorEntered > > ( ) . unwrap ( ) ;
256
+ cursor_entered_events. send ( CursorEntered { id : window_id } ) ;
257
+ }
258
+ WindowEvent :: CursorLeft { .. } => {
259
+ let mut cursor_left_events =
260
+ app. resources . get_mut :: < Events < CursorLeft > > ( ) . unwrap ( ) ;
261
+ window. update_cursor_position_from_backend ( None ) ;
262
+ cursor_left_events. send ( CursorLeft { id : window_id } ) ;
263
+ }
264
+ WindowEvent :: MouseInput { state, button, .. } => {
265
+ let mut mouse_button_input_events =
266
+ app. resources . get_mut :: < Events < MouseButtonInput > > ( ) . unwrap ( ) ;
267
+ mouse_button_input_events. send ( MouseButtonInput {
268
+ button : converters:: convert_mouse_button ( button) ,
269
+ state : converters:: convert_element_state ( state) ,
285
270
} ) ;
286
271
}
287
- } ,
288
- WindowEvent :: Touch ( touch) => {
289
- let mut touch_input_events =
290
- app. resources . get_mut :: < Events < TouchInput > > ( ) . unwrap ( ) ;
272
+ WindowEvent :: MouseWheel { delta, .. } => match delta {
273
+ event:: MouseScrollDelta :: LineDelta ( x, y) => {
274
+ let mut mouse_wheel_input_events =
275
+ app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
276
+ mouse_wheel_input_events. send ( MouseWheel {
277
+ unit : MouseScrollUnit :: Line ,
278
+ x,
279
+ y,
280
+ } ) ;
281
+ }
282
+ event:: MouseScrollDelta :: PixelDelta ( p) => {
283
+ let mut mouse_wheel_input_events =
284
+ app. resources . get_mut :: < Events < MouseWheel > > ( ) . unwrap ( ) ;
285
+ mouse_wheel_input_events. send ( MouseWheel {
286
+ unit : MouseScrollUnit :: Pixel ,
287
+ x : p. x as f32 ,
288
+ y : p. y as f32 ,
289
+ } ) ;
290
+ }
291
+ } ,
292
+ WindowEvent :: Touch ( touch) => {
293
+ let mut touch_input_events =
294
+ app. resources . get_mut :: < Events < TouchInput > > ( ) . unwrap ( ) ;
291
295
292
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
293
- let windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
294
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
295
- let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
296
- let mut location = touch. location . to_logical ( winit_window. scale_factor ( ) ) ;
296
+ let winit_window = winit_windows. get_window ( window_id) . unwrap ( ) ;
297
+ let mut location = touch. location . to_logical ( winit_window. scale_factor ( ) ) ;
297
298
298
- // FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
299
- if cfg ! ( target_os = "android" ) {
299
+ // FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
300
+ if cfg ! ( target_os = "android" ) {
300
301
let window_height = windows. get_primary ( ) . unwrap ( ) . height ( ) ;
301
- location. y = window_height - location. y ;
302
+ location. y = window_height - location. y ;
303
+ }
304
+ touch_input_events. send ( converters:: convert_touch_input ( touch, location) ) ;
302
305
}
303
- touch_input_events. send ( converters:: convert_touch_input ( touch, location) ) ;
304
- }
305
- WindowEvent :: ReceivedCharacter ( c) => {
306
- let mut char_input_events = app
307
- . resources
308
- . get_mut :: < Events < ReceivedCharacter > > ( )
309
- . unwrap ( ) ;
310
-
311
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
312
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
306
+ WindowEvent :: ReceivedCharacter ( c) => {
307
+ let mut char_input_events = app
308
+ . resources
309
+ . get_mut :: < Events < ReceivedCharacter > > ( )
310
+ . unwrap ( ) ;
313
311
314
- char_input_events. send ( ReceivedCharacter {
315
- id : window_id,
316
- char : c,
317
- } )
318
- }
319
- WindowEvent :: ScaleFactorChanged {
320
- scale_factor,
321
- new_inner_size,
322
- } => {
323
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
324
- let mut windows = app. resources . get_mut :: < Windows > ( ) . unwrap ( ) ;
325
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
326
- let window = windows. get_mut ( window_id) . unwrap ( ) ;
312
+ char_input_events. send ( ReceivedCharacter {
313
+ id : window_id,
314
+ char : c,
315
+ } )
316
+ }
317
+ WindowEvent :: ScaleFactorChanged {
318
+ scale_factor,
319
+ new_inner_size,
320
+ } => {
327
321
window. update_actual_size_from_backend (
328
- new_inner_size. width ,
329
- new_inner_size. height ,
330
- ) ;
331
- window. update_scale_factor_from_backend ( scale_factor) ;
322
+ new_inner_size. width ,
323
+ new_inner_size. height ,
324
+ ) ;
325
+ window. update_scale_factor_from_backend ( scale_factor) ;
332
326
// should we send a resize event to indicate the change in
333
327
// logical size?
328
+ }
329
+ WindowEvent :: Focused ( focused) => {
330
+ let mut focused_events =
331
+ app. resources . get_mut :: < Events < WindowFocused > > ( ) . unwrap ( ) ;
332
+ focused_events. send ( WindowFocused {
333
+ id : window_id,
334
+ focused,
335
+ } ) ;
336
+ }
337
+ _ => { }
334
338
}
335
- WindowEvent :: Focused ( focused) => {
336
- let mut focused_events =
337
- app. resources . get_mut :: < Events < WindowFocused > > ( ) . unwrap ( ) ;
338
- let winit_windows = app. resources . get_mut :: < WinitWindows > ( ) . unwrap ( ) ;
339
- let window_id = winit_windows. get_window_id ( winit_window_id) . unwrap ( ) ;
340
- focused_events. send ( WindowFocused {
341
- id : window_id,
342
- focused,
343
- } ) ;
344
- }
345
- _ => { }
346
- } ,
339
+ }
347
340
event:: Event :: DeviceEvent {
348
341
event : DeviceEvent :: MouseMotion { delta } ,
349
342
..
0 commit comments