bevy/examples/3d
Joona Aalto e5dc177b4b
Rename Trigger to On (#19596)
# Objective

Currently, the observer API looks like this:

```rust
app.add_observer(|trigger: Trigger<Explode>| {
    info!("Entity {} exploded!", trigger.target());
});
```

Future plans for observers also include "multi-event observers" with a
trigger that looks like this (see [Cart's
example](https://github.com/bevyengine/bevy/issues/14649#issuecomment-2960402508)):

```rust
trigger: Trigger<(
    OnAdd<Pressed>,
    OnRemove<Pressed>,
    OnAdd<InteractionDisabled>,
    OnRemove<InteractionDisabled>,
    OnInsert<Hovered>,
)>,
```

In scenarios like this, there is a lot of repetition of `On`. These are
expected to be very high-traffic APIs especially in UI contexts, so
ergonomics and readability are critical.

By renaming `Trigger` to `On`, we can make these APIs read more cleanly
and get rid of the repetition:

```rust
app.add_observer(|trigger: On<Explode>| {
    info!("Entity {} exploded!", trigger.target());
});
```

```rust
trigger: On<(
    Add<Pressed>,
    Remove<Pressed>,
    Add<InteractionDisabled>,
    Remove<InteractionDisabled>,
    Insert<Hovered>,
)>,
```

Names like `On<Add<Pressed>>` emphasize the actual event listener nature
more than `Trigger<OnAdd<Pressed>>`, and look cleaner. This *also* frees
up the `Trigger` name if we want to use it for the observer event type,
splitting them out from buffered events (bikeshedding this is out of
scope for this PR though).

For prior art:
[`bevy_eventlistener`](https://github.com/aevyrie/bevy_eventlistener)
used
[`On`](https://docs.rs/bevy_eventlistener/latest/bevy_eventlistener/event_listener/struct.On.html)
for its event listener type. Though in our case, the observer is the
event listener, and `On` is just a type containing information about the
triggered event.

## Solution

Steal from `bevy_event_listener` by @aevyrie and use `On`.

- Rename `Trigger` to `On`
- Rename `OnAdd` to `Add`
- Rename `OnInsert` to `Insert`
- Rename `OnReplace` to `Replace`
- Rename `OnRemove` to `Remove`
- Rename `OnDespawn` to `Despawn`

## Discussion

### Naming Conflicts??

Using a name like `Add` might initially feel like a very bad idea, since
it risks conflict with `core::ops::Add`. However, I don't expect this to
be a big problem in practice.

- You rarely need to actually implement the `Add` trait, especially in
modules that would use the Bevy ECS.
- In the rare cases where you *do* get a conflict, it is very easy to
fix by just disambiguating, for example using `ops::Add`.
- The `Add` event is a struct while the `Add` trait is a trait (duh), so
the compiler error should be very obvious.

For the record, renaming `OnAdd` to `Add`, I got exactly *zero* errors
or conflicts within Bevy itself. But this is of course not entirely
representative of actual projects *using* Bevy.

You might then wonder, why not use `Added`? This would conflict with the
`Added` query filter, so it wouldn't work. Additionally, the current
naming convention for observer events does not use past tense.

### Documentation

This does make documentation slightly more awkward when referring to
`On` or its methods. Previous docs often referred to `Trigger::target`
or "sends a `Trigger`" (which is... a bit strange anyway), which would
now be `On::target` and "sends an observer `Event`".

You can see the diff in this PR to see some of the effects. I think it
should be fine though, we may just need to reword more documentation to
read better.
2025-06-12 18:22:33 +00:00
..
3d_scene.rs
3d_shapes.rs Add binned 2d/3d Wireframe render phase (#18587) 2025-04-09 21:34:53 +00:00
3d_viewport_to_world.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
animated_material.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
anisotropy.rs Add a Sphere to anisotropy example (#17676) 2025-02-05 20:23:43 +00:00
anti_aliasing.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
atmosphere.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
atmospheric_fog.rs Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
auto_exposure.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
blend_modes.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
bloom_3d.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
camera_sub_view.rs
clearcoat.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
clustered_decals.rs Disable clustered decals on Metal. (#17554) 2025-01-27 05:39:07 +00:00
color_grading.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
decal.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
deferred_rendering.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
depth_of_field.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
edit_material_on_gltf.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
fog_volumes.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
fog.rs Fix mismatched FogFalloff (#19174) 2025-05-26 17:52:27 +00:00
generate_custom_mesh.rs Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
irradiance_volumes.rs Revert "Replace Ambient Lights with Environment Map Lights (#17482)" (#18167) 2025-03-05 23:08:46 +00:00
lighting.rs Revert "Replace Ambient Lights with Environment Map Lights (#17482)" (#18167) 2025-03-05 23:08:46 +00:00
lightmaps.rs Add GltfMeshName component and Deref implementations (#19331) 2025-05-23 20:56:48 +00:00
lines.rs
load_gltf_extras.rs Merge Style properties into Node. Use ComputedNode for computed properties. (#15975) 2024-10-18 22:25:33 +00:00
load_gltf.rs
mesh_ray_cast.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
meshlet.rs Add external assets to .gitignore (#17388) 2025-01-17 01:20:14 +00:00
mixed_lighting.rs Rename Trigger to On (#19596) 2025-06-12 18:22:33 +00:00
motion_blur.rs Remove WebGL padding from MotionBlur (#18727) 2025-04-06 20:00:59 +00:00
occlusion_culling.rs Adopt consistent FooSystems naming convention for system sets (#18900) 2025-05-06 15:18:03 +00:00
order_independent_transparency.rs Relationships (non-fragmenting, one-to-many) (#17398) 2025-01-18 22:20:30 +00:00
orthographic.rs Improve API for scaling orthographic cameras (#15969) 2024-10-17 17:50:06 +00:00
parallax_mapping.rs
parenting.rs
pbr.rs Fix pbr example text rotation (#19571) 2025-06-10 16:57:57 +00:00
pcss.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
post_processing.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
query_gltf_primitives.rs Cosmetic tweaks to query_gltf_primitives (#16102) 2024-10-27 19:06:19 +00:00
reflection_probes.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
render_to_texture.rs aligning public apis of Time,Timer and Stopwatch (#15962) 2024-10-16 21:09:32 +00:00
rotate_environment_map.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
scrolling_fog.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
shadow_biases.rs Fix shadow_biases example (#18303) 2025-03-14 19:50:49 +00:00
shadow_caster_receiver.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
skybox.rs Revert "Replace Ambient Lights with Environment Map Lights (#17482)" (#18167) 2025-03-05 23:08:46 +00:00
specular_tint.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
spherical_area_lights.rs Revert "Replace Ambient Lights with Environment Map Lights (#17482)" (#18167) 2025-03-05 23:08:46 +00:00
split_screen.rs separate border colors (#18682) 2025-05-26 16:57:13 +00:00
spotlight.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
ssao.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
ssr.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
texture.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
tonemapping.rs Rename JustifyText to Justify (#19522) 2025-06-09 19:59:48 +00:00
transmission.rs Make TAA non-experimental, fixes (#18349) 2025-06-02 16:04:08 +00:00
transparency_3d.rs Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
two_passes.rs
update_gltf_scene.rs
vertex_colors.rs
visibility_range.rs Switch ChildOf back to tuple struct (#18672) 2025-04-02 00:10:10 +00:00
volumetric_fog.rs Split Camera.hdr out into a new component (#18873) 2025-05-26 19:24:45 +00:00
wireframe.rs Add binned 2d/3d Wireframe render phase (#18587) 2025-04-09 21:34:53 +00:00