diff --git a/crates/bevy_render/src/view/window/cursor.rs b/crates/bevy_render/src/view/window/cursor.rs index 3bed2236316fd..dbc5ebda3510d 100644 --- a/crates/bevy_render/src/view/window/cursor.rs +++ b/crates/bevy_render/src/view/window/cursor.rs @@ -4,10 +4,11 @@ use bevy_ecs::{ change_detection::DetectChanges, component::Component, entity::Entity, + observer::Trigger, query::With, reflect::ReflectComponent, system::{Commands, Local, Query, Res}, - world::Ref, + world::{OnRemove, Ref}, }; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_utils::{tracing::warn, HashSet}; @@ -27,6 +28,8 @@ impl Plugin for CursorPlugin { app.register_type::() .init_resource::() .add_systems(Last, update_cursors); + + app.observe(on_remove_cursor_icon); } } @@ -84,12 +87,12 @@ pub enum CustomCursor { pub fn update_cursors( mut commands: Commands, - mut windows: Query<(Entity, Ref), With>, + windows: Query<(Entity, Ref), With>, cursor_cache: Res, images: Res>, mut queue: Local>, ) { - for (entity, cursor) in windows.iter_mut() { + for (entity, cursor) in windows.iter() { if !(queue.remove(&entity) || cursor.is_changed()) { continue; } @@ -161,6 +164,15 @@ pub fn update_cursors( } } +/// Resets the cursor to the default icon when `CursorIcon` is removed. +pub fn on_remove_cursor_icon(trigger: Trigger, mut commands: Commands) { + commands + .entity(trigger.entity()) + .insert(PendingCursor(Some(CursorSource::System( + convert_system_cursor_icon(SystemCursorIcon::Default), + )))); +} + /// Returns the image data as a `Vec`. /// Only supports rgba8 and rgba32float formats. fn image_to_rgba_pixels(image: &Image) -> Option> { diff --git a/examples/window/screenshot.rs b/examples/window/screenshot.rs index 1bf800d18c16c..806478bd0804a 100644 --- a/examples/window/screenshot.rs +++ b/examples/window/screenshot.rs @@ -32,7 +32,9 @@ fn screenshot_saving( screenshot_saving: Query>, windows: Query>, ) { - let window = windows.single(); + let Ok(window) = windows.get_single() else { + return; + }; match screenshot_saving.iter().count() { 0 => { commands.entity(window).remove::();