bevy/crates/bevy_input_focus/src/autofocus.rs
Antony 3578f9e4d0
Reflect bevy_input_focus (#17212)
# Objective

Fixes #17099.

## Solution

Derive, register, and feature flag.

## Testing

Ran CI.
2025-01-07 18:16:46 +00:00

30 lines
971 B
Rust

//! Contains the [`AutoFocus`] component and related machinery.
use bevy_ecs::{component::ComponentId, prelude::*, world::DeferredWorld};
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::{prelude::*, Reflect};
use crate::InputFocus;
/// Indicates that this widget should automatically receive [`InputFocus`].
///
/// This can be useful for things like dialog boxes, the first text input in a form,
/// or the first button in a game menu.
///
/// The focus is swapped when this component is added
/// or an entity with this component is spawned.
#[derive(Debug, Default, Component, Copy, Clone)]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Debug, Default, Component)
)]
#[component(on_add = on_auto_focus_added)]
pub struct AutoFocus;
fn on_auto_focus_added(mut world: DeferredWorld, entity: Entity, _: ComponentId) {
if let Some(mut input_focus) = world.get_resource_mut::<InputFocus>() {
input_focus.set(entity);
}
}