diff --git a/crates/bevy_core_widgets/src/callback.rs b/crates/bevy_core_widgets/src/callback.rs index f6dcc5ae5e..64606ac0c4 100644 --- a/crates/bevy_core_widgets/src/callback.rs +++ b/crates/bevy_core_widgets/src/callback.rs @@ -1,8 +1,8 @@ -use bevy_ecs::component::Component; -use bevy_ecs::lifecycle::HookContext; use bevy_ecs::system::{Commands, EntityCommands, IntoSystem, SystemId, SystemInput}; use bevy_ecs::world::{DeferredWorld, EntityWorldMut, World}; +use crate::owner::OwnedBy; + /// A callback defines how we want to be notified when a widget changes state. Unlike an event /// or observer, callbacks are intended for "point-to-point" communication that cuts across the /// hierarchy of entities. Callbacks can be created in advance of the entity they are attached @@ -114,24 +114,6 @@ impl Notify for DeferredWorld<'_> { } } -/// A component that hangs on to a registered one-shot system, and unregisters it when the -/// component is despawned. -#[derive(Component)] -#[component(on_remove = on_despawn_callback_owner::, storage = "SparseSet")] -pub struct OwnedCallbackSystem(SystemId); - -fn on_despawn_callback_owner( - mut world: DeferredWorld, - context: HookContext, -) { - let system_id = world - .entity(context.entity) - .get::>() - .unwrap() - .0; - world.commands().unregister_system(system_id); -} - /// Methods for registering scoped callbacks. pub trait RegisterOwnedCallback { /// Registers a scoped one-shot system, with no input, that will be removed when the parent @@ -161,7 +143,8 @@ impl RegisterOwnedCallback for EntityCommands<'_> { let system_id = self.commands().register_system(callback); let owner = self.id(); self.commands() - .spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner))); + .entity(owner) + .add_one_related::(system_id.entity()); Callback::System(system_id) } @@ -176,7 +159,8 @@ impl RegisterOwnedCallback for EntityCommands<'_> { let owner = self.id(); let system_id = self.commands().register_system(callback); self.commands() - .spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner))); + .entity(owner) + .add_one_related::(system_id.entity()); Callback::System(system_id) } } @@ -189,7 +173,9 @@ impl RegisterOwnedCallback for EntityWorldMut<'_> { let owner = self.id(); let system_id = self.world_scope(|world| world.register_system(callback)); self.world_scope(|world| { - world.spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner))); + world + .entity_mut(owner) + .add_one_related::(system_id.entity()); }); Callback::System(system_id) } @@ -205,7 +191,9 @@ impl RegisterOwnedCallback for EntityWorldMut<'_> { let owner = self.id(); let system_id = self.world_scope(|world| world.register_system(callback)); self.world_scope(|world| { - world.spawn((OwnedCallbackSystem(system_id), crate::owner::OwnedBy(owner))); + world + .entity_mut(owner) + .add_one_related::(system_id.entity()); }); Callback::System(system_id) }