Skip to content

Commit dfac3b9

Browse files
authored
Fix window close in example cause panic (#17533)
# Objective Fixes #17532 ## Solution - check window valide
1 parent 51bb4f0 commit dfac3b9

File tree

7 files changed

+39
-8
lines changed

7 files changed

+39
-8
lines changed

examples/2d/2d_viewport_to_world.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ fn main() {
1212

1313
fn draw_cursor(
1414
camera_query: Single<(&Camera, &GlobalTransform)>,
15-
window: Single<&Window>,
15+
window: Query<&Window>,
1616
mut gizmos: Gizmos,
1717
) {
18+
let Ok(window) = window.get_single() else {
19+
return;
20+
};
21+
1822
let (camera, camera_transform) = *camera_query;
1923

2024
let Some(cursor_position) = window.cursor_position() else {

examples/3d/3d_viewport_to_world.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ fn main() {
1313
fn draw_cursor(
1414
camera_query: Single<(&Camera, &GlobalTransform)>,
1515
ground: Single<&GlobalTransform, With<Ground>>,
16-
windows: Single<&Window>,
16+
windows: Query<&Window>,
1717
mut gizmos: Gizmos,
1818
) {
19+
let Ok(windows) = windows.get_single() else {
20+
return;
21+
};
22+
1923
let (camera, camera_transform) = *camera_query;
2024

2125
let Some(cursor_position) = windows.cursor_position() else {

examples/ecs/observers.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,13 @@ fn draw_shapes(mut gizmos: Gizmos, mines: Query<&Mine>) {
172172
fn handle_click(
173173
mouse_button_input: Res<ButtonInput<MouseButton>>,
174174
camera: Single<(&Camera, &GlobalTransform)>,
175-
windows: Single<&Window>,
175+
windows: Query<&Window>,
176176
mut commands: Commands,
177177
) {
178+
let Ok(windows) = windows.get_single() else {
179+
return;
180+
};
181+
178182
let (camera, camera_transform) = *camera;
179183
if let Some(pos) = windows
180184
.cursor_position()

examples/ecs/parallel_query.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ fn move_system(mut sprites: Query<(&mut Transform, &Velocity)>) {
4242
}
4343

4444
// Bounce sprites outside the window
45-
fn bounce_system(window: Single<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) {
45+
fn bounce_system(window: Query<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) {
46+
let Ok(window) = window.get_single() else {
47+
return;
48+
};
4649
let width = window.width();
4750
let height = window.height();
4851
let left = width / -2.0;

examples/games/contributors.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,14 @@ fn gravity(time: Res<Time>, mut velocity_query: Query<&mut Velocity>) {
252252
/// velocity. On collision with the ground it applies an upwards
253253
/// force.
254254
fn collisions(
255-
window: Single<&Window>,
255+
window: Query<&Window>,
256256
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
257257
mut rng: ResMut<SharedRng>,
258258
) {
259+
let Ok(window) = window.get_single() else {
260+
return;
261+
};
262+
259263
let window_size = window.size();
260264

261265
let collision_area = Aabb2d::new(Vec2::ZERO, (window_size - SPRITE_SIZE) / 2.);

examples/mobile/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ fn main() {
4646
}
4747

4848
fn touch_camera(
49-
window: Single<&Window>,
49+
window: Query<&Window>,
5050
mut touches: EventReader<TouchInput>,
5151
mut camera_transform: Single<&mut Transform, With<Camera3d>>,
5252
mut last_position: Local<Option<Vec2>>,
5353
mut rotations: EventReader<RotationGesture>,
5454
) {
55+
let Ok(window) = window.get_single() else {
56+
return;
57+
};
58+
5559
for touch in touches.read() {
5660
if touch.phase == TouchPhase::Started {
5761
*last_position = None;

examples/stress_tests/bevymark.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,16 @@ fn mouse_handler(
327327
args: Res<Args>,
328328
time: Res<Time>,
329329
mouse_button_input: Res<ButtonInput<MouseButton>>,
330-
window: Single<&Window>,
330+
window: Query<&Window>,
331331
bird_resources: ResMut<BirdResources>,
332332
mut counter: ResMut<BevyCounter>,
333333
mut rng: Local<Option<ChaCha8Rng>>,
334334
mut wave: Local<usize>,
335335
) {
336+
let Ok(window) = window.get_single() else {
337+
return;
338+
};
339+
336340
if rng.is_none() {
337341
// We're seeding the PRNG here to make this example deterministic for testing purposes.
338342
// This isn't strictly required in practical use unless you need your app to be deterministic.
@@ -529,7 +533,11 @@ fn handle_collision(half_extents: Vec2, translation: &Vec3, velocity: &mut Vec3)
529533
velocity.y = 0.0;
530534
}
531535
}
532-
fn collision_system(window: Single<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) {
536+
fn collision_system(window: Query<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) {
537+
let Ok(window) = window.get_single() else {
538+
return;
539+
};
540+
533541
let half_extents = 0.5 * window.size();
534542

535543
for (mut bird, transform) in &mut bird_query {

0 commit comments

Comments
 (0)