From dfac3b9bfd0b343cd20e15e1f3adaf4e8a31efbd Mon Sep 17 00:00:00 2001 From: jiang heng Date: Tue, 28 Jan 2025 13:37:23 +0800 Subject: [PATCH] Fix window close in example cause panic (#17533) # Objective Fixes #17532 ## Solution - check window valide --- examples/2d/2d_viewport_to_world.rs | 6 +++++- examples/3d/3d_viewport_to_world.rs | 6 +++++- examples/ecs/observers.rs | 6 +++++- examples/ecs/parallel_query.rs | 5 ++++- examples/games/contributors.rs | 6 +++++- examples/mobile/src/lib.rs | 6 +++++- examples/stress_tests/bevymark.rs | 12 ++++++++++-- 7 files changed, 39 insertions(+), 8 deletions(-) diff --git a/examples/2d/2d_viewport_to_world.rs b/examples/2d/2d_viewport_to_world.rs index 74cb9bb7f3..6647437be3 100644 --- a/examples/2d/2d_viewport_to_world.rs +++ b/examples/2d/2d_viewport_to_world.rs @@ -12,9 +12,13 @@ fn main() { fn draw_cursor( camera_query: Single<(&Camera, &GlobalTransform)>, - window: Single<&Window>, + window: Query<&Window>, mut gizmos: Gizmos, ) { + let Ok(window) = window.get_single() else { + return; + }; + let (camera, camera_transform) = *camera_query; let Some(cursor_position) = window.cursor_position() else { diff --git a/examples/3d/3d_viewport_to_world.rs b/examples/3d/3d_viewport_to_world.rs index 18a454cbdc..ef6da961d7 100644 --- a/examples/3d/3d_viewport_to_world.rs +++ b/examples/3d/3d_viewport_to_world.rs @@ -13,9 +13,13 @@ fn main() { fn draw_cursor( camera_query: Single<(&Camera, &GlobalTransform)>, ground: Single<&GlobalTransform, With>, - windows: Single<&Window>, + windows: Query<&Window>, mut gizmos: Gizmos, ) { + let Ok(windows) = windows.get_single() else { + return; + }; + let (camera, camera_transform) = *camera_query; let Some(cursor_position) = windows.cursor_position() else { diff --git a/examples/ecs/observers.rs b/examples/ecs/observers.rs index 105d0305d4..6d8027f01d 100644 --- a/examples/ecs/observers.rs +++ b/examples/ecs/observers.rs @@ -172,9 +172,13 @@ fn draw_shapes(mut gizmos: Gizmos, mines: Query<&Mine>) { fn handle_click( mouse_button_input: Res>, camera: Single<(&Camera, &GlobalTransform)>, - windows: Single<&Window>, + windows: Query<&Window>, mut commands: Commands, ) { + let Ok(windows) = windows.get_single() else { + return; + }; + let (camera, camera_transform) = *camera; if let Some(pos) = windows .cursor_position() diff --git a/examples/ecs/parallel_query.rs b/examples/ecs/parallel_query.rs index 0e1c473783..18bc3fc183 100644 --- a/examples/ecs/parallel_query.rs +++ b/examples/ecs/parallel_query.rs @@ -42,7 +42,10 @@ fn move_system(mut sprites: Query<(&mut Transform, &Velocity)>) { } // Bounce sprites outside the window -fn bounce_system(window: Single<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) { +fn bounce_system(window: Query<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) { + let Ok(window) = window.get_single() else { + return; + }; let width = window.width(); let height = window.height(); let left = width / -2.0; diff --git a/examples/games/contributors.rs b/examples/games/contributors.rs index 8d977fd21d..59f03eba6e 100644 --- a/examples/games/contributors.rs +++ b/examples/games/contributors.rs @@ -252,10 +252,14 @@ fn gravity(time: Res