Path: blob/main/crates/bevy_core_widgets/src/callback.rs
6595 views
use bevy_ecs::system::{Commands, SystemId, SystemInput};1use bevy_ecs::world::{DeferredWorld, World};2use bevy_reflect::{prelude::ReflectDefault, Reflect};34/// A callback defines how we want to be notified when a widget changes state. Unlike an event5/// or observer, callbacks are intended for "point-to-point" communication that cuts across the6/// hierarchy of entities. Callbacks can be created in advance of the entity they are attached7/// to, and can be passed around as parameters.8///9/// Example:10/// ```11/// use bevy_app::App;12/// use bevy_core_widgets::{Callback, Notify};13/// use bevy_ecs::system::{Commands, IntoSystem};14///15/// let mut app = App::new();16///17/// // Register a one-shot system18/// fn my_callback_system() {19/// println!("Callback executed!");20/// }21///22/// let system_id = app.world_mut().register_system(my_callback_system);23///24/// // Wrap system in a callback25/// let callback = Callback::System(system_id);26///27/// // Later, when we want to execute the callback:28/// app.world_mut().commands().notify(&callback);29/// ```30#[derive(Default, Debug, Reflect)]31#[reflect(Default)]32pub enum Callback<I: SystemInput = ()> {33/// Invoke a one-shot system34System(SystemId<I>),35/// Ignore this notification36#[default]37Ignore,38}3940/// Trait used to invoke a [`Callback`], unifying the API across callers.41pub trait Notify {42/// Invoke the callback with no arguments.43fn notify(&mut self, callback: &Callback<()>);4445/// Invoke the callback with one argument.46fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)47where48I: SystemInput<Inner<'static>: Send> + 'static;49}5051impl<'w, 's> Notify for Commands<'w, 's> {52fn notify(&mut self, callback: &Callback<()>) {53match callback {54Callback::System(system_id) => self.run_system(*system_id),55Callback::Ignore => (),56}57}5859fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)60where61I: SystemInput<Inner<'static>: Send> + 'static,62{63match callback {64Callback::System(system_id) => self.run_system_with(*system_id, input),65Callback::Ignore => (),66}67}68}6970impl Notify for World {71fn notify(&mut self, callback: &Callback<()>) {72match callback {73Callback::System(system_id) => {74let _ = self.run_system(*system_id);75}76Callback::Ignore => (),77}78}7980fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)81where82I: SystemInput<Inner<'static>: Send> + 'static,83{84match callback {85Callback::System(system_id) => {86let _ = self.run_system_with(*system_id, input);87}88Callback::Ignore => (),89}90}91}9293impl Notify for DeferredWorld<'_> {94fn notify(&mut self, callback: &Callback<()>) {95match callback {96Callback::System(system_id) => {97self.commands().run_system(*system_id);98}99Callback::Ignore => (),100}101}102103fn notify_with<I>(&mut self, callback: &Callback<I>, input: I::Inner<'static>)104where105I: SystemInput<Inner<'static>: Send> + 'static,106{107match callback {108Callback::System(system_id) => {109self.commands().run_system_with(*system_id, input);110}111Callback::Ignore => (),112}113}114}115116117