Skip to content

Commit f4b5467

Browse files
committed
Merge button components
1 parent c392d28 commit f4b5467

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed
Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
use crate::{FocusPolicy, Interaction, Node};
2+
use accesskit::Role;
3+
use bevy_a11y::AccessibilityNode;
24
use bevy_ecs::{component::Component, reflect::ReflectComponent};
35
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
46

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+
/// ```
645
#[derive(Component, Debug, Default, Clone, Copy, PartialEq, Eq, Reflect)]
746
#[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+
)]
953
pub struct Button;

crates/bevy_ui_widgets/src/button.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use accesskit::Role;
2-
use bevy_a11y::AccessibilityNode;
31
use bevy_app::{App, Plugin};
42
use bevy_ecs::query::Has;
53
use bevy_ecs::{
6-
component::Component,
74
entity::Entity,
85
observer::On,
96
query::With,
@@ -17,12 +14,8 @@ use bevy_ui::{InteractionDisabled, Pressed};
1714

1815
use crate::Activate;
1916

20-
/// Headless button widget. This widget maintains a "pressed" state, which is used to
21-
/// indicate whether the button is currently being pressed by the user. It emits an [`Activate`]
22-
/// event when the button is un-pressed.
23-
#[derive(Component, Default, Debug)]
24-
#[require(AccessibilityNode(accesskit::Node::new(Role::Button)))]
25-
pub struct Button;
17+
// Re-export the Button component from bevy_ui
18+
pub use bevy_ui::widget::Button;
2619

2720
fn button_on_key_event(
2821
mut event: On<FocusedInput<KeyboardInput>>,

crates/bevy_ui_widgets/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
//! We are actively seeking feedback on the design and implementation of this crate, so please
1111
//! file issues or create PRs if you have any comments or suggestions.
1212
//!
13+
//! ## Button Component
14+
//!
15+
//! The [`Button`] component is re-exported from [`bevy_ui::widget::Button`] to provide a unified
16+
//! API. The component itself is defined in `bevy_ui`, while the interactive behavior (plugins and
17+
//! observers) is provided by this crate through [`ButtonPlugin`].
18+
//!
1319
//! ## State Management
1420
//!
1521
//! Most of the widgets use external state management: this means that the widgets do not

0 commit comments

Comments
 (0)