|
1 | 1 | use crate::{FocusPolicy, Interaction, Node}; |
| 2 | +use accesskit::Role; |
| 3 | +use bevy_a11y::AccessibilityNode; |
2 | 4 | use bevy_ecs::{component::Component, reflect::ReflectComponent}; |
3 | 5 | use bevy_reflect::{std_traits::ReflectDefault, Reflect}; |
4 | 6 |
|
5 | | -/// Marker struct for buttons |
| 7 | +/// Unified button component that combines UI layout, interaction handling, focus management, |
| 8 | +/// and accessibility support. |
| 9 | +/// |
| 10 | +/// This component automatically includes all necessary components for a functional button |
| 11 | +/// through the `#[require]` attribute: |
| 12 | +/// - [`Node`]: For UI layout |
| 13 | +/// - [`FocusPolicy::Block`]: For focus management |
| 14 | +/// - [`Interaction`]: For tracking interaction state |
| 15 | +/// - [`AccessibilityNode`]: For screen reader support |
| 16 | +/// |
| 17 | +/// # Interactive Behavior |
| 18 | +/// |
| 19 | +/// To enable full interactive behavior (pressed state, activation events), you need to: |
| 20 | +/// 1. Add the [`bevy_ui_widgets::ButtonPlugin`] to your app |
| 21 | +/// 2. Listen for [`bevy_ui_widgets::Activate`] events |
| 22 | +/// |
| 23 | +/// # Example |
| 24 | +/// |
| 25 | +/// ```rust,ignore |
| 26 | +/// use bevy::prelude::*; |
| 27 | +/// use bevy::ui::widget::Button; |
| 28 | +/// use bevy::ui_widgets::{Activate, ButtonPlugin}; |
| 29 | +/// |
| 30 | +/// App::new() |
| 31 | +/// .add_plugins(DefaultPlugins) |
| 32 | +/// .add_plugins(ButtonPlugin) |
| 33 | +/// .add_systems(Startup, setup) |
| 34 | +/// .add_observer(on_button_activate) |
| 35 | +/// .run(); |
| 36 | +/// |
| 37 | +/// fn setup(mut commands: Commands) { |
| 38 | +/// commands.spawn(Button); |
| 39 | +/// } |
| 40 | +/// |
| 41 | +/// fn on_button_activate(trigger: Trigger<Activate>) { |
| 42 | +/// println!("Button {:?} was activated!", trigger.entity()); |
| 43 | +/// } |
| 44 | +/// ``` |
6 | 45 | #[derive(Component, Debug, Default, Clone, Copy, PartialEq, Eq, Reflect)] |
7 | 46 | #[reflect(Component, Default, Debug, PartialEq, Clone)] |
8 | | -#[require(Node, FocusPolicy::Block, Interaction)] |
| 47 | +#[require( |
| 48 | + Node, |
| 49 | + FocusPolicy::Block, |
| 50 | + Interaction, |
| 51 | + AccessibilityNode(accesskit::Node::new(Role::Button)) |
| 52 | +)] |
9 | 53 | pub struct Button; |
0 commit comments