2023-03-01 22:45:04 +00:00
|
|
|
#![forbid(unsafe_code)]
|
2024-03-23 02:22:52 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
2024-03-25 18:52:50 +00:00
|
|
|
#![doc(
|
|
|
|
html_logo_url = "https://bevyengine.org/assets/icon.png",
|
|
|
|
html_favicon_url = "https://bevyengine.org/assets/icon.png"
|
|
|
|
)]
|
2024-03-23 02:22:52 +00:00
|
|
|
|
|
|
|
//! Accessibility for Bevy
|
2024-11-08 21:01:16 +00:00
|
|
|
//!
|
|
|
|
//! As of Bevy version 0.15 `accesskit` is no longer re-exported from this crate.
|
|
|
|
//!
|
|
|
|
//! If you need to use `accesskit`, you will need to add it as a separate dependency in your `Cargo.toml`.
|
|
|
|
//!
|
|
|
|
//! Make sure to use the same version of `accesskit` as Bevy.
|
2023-03-01 22:45:04 +00:00
|
|
|
|
2024-09-27 00:59:59 +00:00
|
|
|
extern crate alloc;
|
|
|
|
|
|
|
|
use alloc::sync::Arc;
|
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
2023-03-01 22:45:04 +00:00
|
|
|
|
2024-11-04 20:07:38 +00:00
|
|
|
use accesskit::Node;
|
2023-03-01 22:45:04 +00:00
|
|
|
use bevy_app::Plugin;
|
|
|
|
use bevy_derive::{Deref, DerefMut};
|
|
|
|
use bevy_ecs::{
|
2024-08-15 17:33:20 +00:00
|
|
|
prelude::{Component, Entity, Event, ReflectResource},
|
2023-10-02 21:22:52 +00:00
|
|
|
schedule::SystemSet,
|
2023-03-01 22:45:04 +00:00
|
|
|
system::Resource,
|
|
|
|
};
|
2024-09-24 11:42:59 +00:00
|
|
|
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
2023-03-01 22:45:04 +00:00
|
|
|
|
2023-06-06 14:44:32 +00:00
|
|
|
/// Wrapper struct for [`accesskit::ActionRequest`]. Required to allow it to be used as an `Event`.
|
|
|
|
#[derive(Event, Deref, DerefMut)]
|
|
|
|
pub struct ActionRequest(pub accesskit::ActionRequest);
|
|
|
|
|
2023-03-01 22:45:04 +00:00
|
|
|
/// Resource that tracks whether an assistive technology has requested
|
|
|
|
/// accessibility information.
|
|
|
|
///
|
|
|
|
/// Useful if a third-party plugin needs to conditionally integrate with
|
|
|
|
/// `AccessKit`
|
|
|
|
#[derive(Resource, Default, Clone, Debug, Deref, DerefMut)]
|
|
|
|
pub struct AccessibilityRequested(Arc<AtomicBool>);
|
|
|
|
|
2023-10-02 21:22:52 +00:00
|
|
|
impl AccessibilityRequested {
|
|
|
|
/// Returns `true` if an access technology is active and accessibility tree
|
|
|
|
/// updates should be sent.
|
|
|
|
pub fn get(&self) -> bool {
|
|
|
|
self.load(Ordering::SeqCst)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets whether accessibility updates were requested by an access technology.
|
|
|
|
pub fn set(&self, value: bool) {
|
|
|
|
self.store(value, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resource whose value determines whether the accessibility tree is updated
|
|
|
|
/// via the ECS.
|
|
|
|
///
|
|
|
|
/// Set to `false` in cases where an external GUI library is sending
|
|
|
|
/// accessibility updates instead. Without this, the external library and ECS
|
|
|
|
/// will generate conflicting updates.
|
|
|
|
#[derive(Resource, Clone, Debug, Deref, DerefMut)]
|
|
|
|
pub struct ManageAccessibilityUpdates(bool);
|
|
|
|
|
|
|
|
impl Default for ManageAccessibilityUpdates {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ManageAccessibilityUpdates {
|
|
|
|
/// Returns `true` if the ECS should update the accessibility tree.
|
|
|
|
pub fn get(&self) -> bool {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets whether the ECS should update the accessibility tree.
|
|
|
|
pub fn set(&mut self, value: bool) {
|
|
|
|
self.0 = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 22:45:04 +00:00
|
|
|
/// Component to wrap a [`accesskit::Node`], representing this entity to the platform's
|
|
|
|
/// accessibility API.
|
|
|
|
///
|
|
|
|
/// If an entity has a parent, and that parent also has an `AccessibilityNode`,
|
|
|
|
/// the entity's node will be a child of the parent's node.
|
|
|
|
///
|
|
|
|
/// If the entity doesn't have a parent, or if the immediate parent doesn't have
|
|
|
|
/// an `AccessibilityNode`, its node will be an immediate child of the primary window.
|
|
|
|
#[derive(Component, Clone, Deref, DerefMut)]
|
2024-11-04 20:07:38 +00:00
|
|
|
pub struct AccessibilityNode(pub Node);
|
2023-03-01 22:45:04 +00:00
|
|
|
|
2024-11-04 20:07:38 +00:00
|
|
|
impl From<Node> for AccessibilityNode {
|
|
|
|
fn from(node: Node) -> Self {
|
2023-03-01 22:45:04 +00:00
|
|
|
Self(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resource representing which entity has keyboard focus, if any.
|
2024-08-15 17:33:20 +00:00
|
|
|
#[derive(Resource, Default, Deref, DerefMut, Reflect)]
|
2024-09-14 01:43:16 +00:00
|
|
|
#[reflect(Resource, Default)]
|
2023-10-02 21:22:52 +00:00
|
|
|
pub struct Focus(pub Option<Entity>);
|
|
|
|
|
|
|
|
/// Set enum for the systems relating to accessibility
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
|
|
|
|
pub enum AccessibilitySystem {
|
|
|
|
/// Update the accessibility tree
|
|
|
|
Update,
|
|
|
|
}
|
2023-03-01 22:45:04 +00:00
|
|
|
|
|
|
|
/// Plugin managing non-GUI aspects of integrating with accessibility APIs.
|
2024-07-16 01:14:33 +00:00
|
|
|
#[derive(Default)]
|
2023-03-01 22:45:04 +00:00
|
|
|
pub struct AccessibilityPlugin;
|
|
|
|
|
|
|
|
impl Plugin for AccessibilityPlugin {
|
|
|
|
fn build(&self, app: &mut bevy_app::App) {
|
2024-08-15 17:33:20 +00:00
|
|
|
app.register_type::<Focus>();
|
|
|
|
|
2023-03-01 22:45:04 +00:00
|
|
|
app.init_resource::<AccessibilityRequested>()
|
2023-10-02 21:22:52 +00:00
|
|
|
.init_resource::<ManageAccessibilityUpdates>()
|
2024-01-09 19:08:15 +00:00
|
|
|
.init_resource::<Focus>()
|
|
|
|
.allow_ambiguous_component::<AccessibilityNode>();
|
2023-03-01 22:45:04 +00:00
|
|
|
}
|
|
|
|
}
|