|
| 1 | +use accesskit::Role; |
| 2 | +use bevy_a11y::AccessibilityNode; |
| 3 | +use bevy_app::{App, Plugin}; |
| 4 | +use bevy_ecs::query::Has; |
| 5 | +use bevy_ecs::system::ResMut; |
| 6 | +use bevy_ecs::{ |
| 7 | + component::Component, |
| 8 | + entity::Entity, |
| 9 | + observer::Trigger, |
| 10 | + query::With, |
| 11 | + system::{Commands, Query, SystemId}, |
| 12 | +}; |
| 13 | +use bevy_input::keyboard::{KeyCode, KeyboardInput}; |
| 14 | +use bevy_input_focus::{FocusedInput, InputFocus, InputFocusVisible}; |
| 15 | +use bevy_picking::events::{Cancel, Click, DragEnd, Pointer, Pressed, Released}; |
| 16 | +use bevy_ui::{Depressed, InteractionDisabled}; |
| 17 | + |
| 18 | +/// Headless button widget. This widget maintains a "pressed" state, which is used to |
| 19 | +/// indicate whether the button is currently being pressed by the user. It emits a `ButtonClicked` |
| 20 | +/// event when the button is un-pressed. |
| 21 | +#[derive(Component, Debug)] |
| 22 | +#[require(AccessibilityNode(accesskit::Node::new(Role::Button)))] |
| 23 | +pub struct CoreButton { |
| 24 | + /// Optional system to run when the button is clicked, or when the Enter or Space key |
| 25 | + /// is pressed while the button is focused. If this field is `None`, the button will |
| 26 | + /// emit a `ButtonClicked` event when clicked. |
| 27 | + pub on_click: Option<SystemId>, |
| 28 | +} |
| 29 | + |
| 30 | +fn button_on_key_event( |
| 31 | + mut trigger: Trigger<FocusedInput<KeyboardInput>>, |
| 32 | + q_state: Query<(&CoreButton, Has<InteractionDisabled>)>, |
| 33 | + mut commands: Commands, |
| 34 | +) { |
| 35 | + if let Ok((bstate, disabled)) = q_state.get(trigger.target().unwrap()) { |
| 36 | + if !disabled { |
| 37 | + let event = &trigger.event().input; |
| 38 | + if !event.repeat |
| 39 | + && (event.key_code == KeyCode::Enter || event.key_code == KeyCode::Space) |
| 40 | + { |
| 41 | + if let Some(on_click) = bstate.on_click { |
| 42 | + trigger.propagate(false); |
| 43 | + commands.run_system(on_click); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +fn button_on_pointer_click( |
| 51 | + mut trigger: Trigger<Pointer<Click>>, |
| 52 | + mut q_state: Query<(&CoreButton, Has<Depressed>, Has<InteractionDisabled>)>, |
| 53 | + mut commands: Commands, |
| 54 | +) { |
| 55 | + if let Ok((bstate, pressed, disabled)) = q_state.get_mut(trigger.target().unwrap()) { |
| 56 | + trigger.propagate(false); |
| 57 | + if pressed && !disabled { |
| 58 | + if let Some(on_click) = bstate.on_click { |
| 59 | + commands.run_system(on_click); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn button_on_pointer_down( |
| 66 | + mut trigger: Trigger<Pointer<Pressed>>, |
| 67 | + mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Depressed>), With<CoreButton>>, |
| 68 | + focus: Option<ResMut<InputFocus>>, |
| 69 | + focus_visible: Option<ResMut<InputFocusVisible>>, |
| 70 | + mut commands: Commands, |
| 71 | +) { |
| 72 | + if let Ok((button, disabled, depressed)) = q_state.get_mut(trigger.target().unwrap()) { |
| 73 | + trigger.propagate(false); |
| 74 | + if !disabled { |
| 75 | + if !depressed { |
| 76 | + commands.entity(button).insert(Depressed); |
| 77 | + } |
| 78 | + // Clicking on a button makes it the focused input, |
| 79 | + // and hides the focus ring if it was visible. |
| 80 | + if let Some(mut focus) = focus { |
| 81 | + focus.0 = trigger.target(); |
| 82 | + } |
| 83 | + if let Some(mut focus_visible) = focus_visible { |
| 84 | + focus_visible.0 = false; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +fn button_on_pointer_up( |
| 91 | + mut trigger: Trigger<Pointer<Released>>, |
| 92 | + mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Depressed>), With<CoreButton>>, |
| 93 | + mut commands: Commands, |
| 94 | +) { |
| 95 | + if let Ok((button, disabled, depressed)) = q_state.get_mut(trigger.target().unwrap()) { |
| 96 | + trigger.propagate(false); |
| 97 | + if !disabled && depressed { |
| 98 | + commands.entity(button).remove::<Depressed>(); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn button_on_pointer_drag_end( |
| 104 | + mut trigger: Trigger<Pointer<DragEnd>>, |
| 105 | + mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Depressed>), With<CoreButton>>, |
| 106 | + mut commands: Commands, |
| 107 | +) { |
| 108 | + if let Ok((button, disabled, depressed)) = q_state.get_mut(trigger.target().unwrap()) { |
| 109 | + trigger.propagate(false); |
| 110 | + if !disabled && depressed { |
| 111 | + commands.entity(button).remove::<Depressed>(); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +fn button_on_pointer_cancel( |
| 117 | + mut trigger: Trigger<Pointer<Cancel>>, |
| 118 | + mut q_state: Query<(Entity, Has<InteractionDisabled>, Has<Depressed>), With<CoreButton>>, |
| 119 | + mut commands: Commands, |
| 120 | +) { |
| 121 | + if let Ok((button, disabled, depressed)) = q_state.get_mut(trigger.target().unwrap()) { |
| 122 | + trigger.propagate(false); |
| 123 | + if !disabled && depressed { |
| 124 | + commands.entity(button).remove::<Depressed>(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/// Plugin that adds the observers for the [`CoreButton`] widget. |
| 130 | +pub struct CoreButtonPlugin; |
| 131 | + |
| 132 | +impl Plugin for CoreButtonPlugin { |
| 133 | + fn build(&self, app: &mut App) { |
| 134 | + app.add_observer(button_on_key_event) |
| 135 | + .add_observer(button_on_pointer_down) |
| 136 | + .add_observer(button_on_pointer_up) |
| 137 | + .add_observer(button_on_pointer_click) |
| 138 | + .add_observer(button_on_pointer_drag_end) |
| 139 | + .add_observer(button_on_pointer_cancel); |
| 140 | + } |
| 141 | +} |
0 commit comments