#![forbid(unsafe_code)]1#![cfg_attr(docsrs, feature(doc_auto_cfg))]2#![doc(3html_logo_url = "https://bevy.org/assets/icon.png",4html_favicon_url = "https://bevy.org/assets/icon.png"5)]6#![no_std]78//! Reusable accessibility primitives9//!10//! This crate provides accessibility integration for the engine. It exposes the11//! [`AccessibilityPlugin`]. This plugin integrates `AccessKit`, a Rust crate12//! providing OS-agnostic accessibility primitives, with Bevy's ECS.13//!14//! ## Some notes on utility15//!16//! While this crate defines useful types for accessibility, it does not17//! actually power accessibility features in Bevy.18//!19//! Instead, it helps other interfaces coordinate their approach to20//! accessibility. Binary authors should add the [`AccessibilityPlugin`], while21//! library maintainers may use the [`AccessibilityRequested`] and22//! [`ManageAccessibilityUpdates`] resources.23//!24//! The [`AccessibilityNode`] component is useful in both cases. It helps25//! describe an entity in terms of its accessibility factors through an26//! `AccessKit` "node".27//!28//! Typical UI concepts, like buttons, checkboxes, and textboxes, are easily29//! described by this component, though, technically, it can represent any kind30//! of Bevy [`Entity`].31//!32//! ## This crate no longer re-exports `AccessKit`33//!34//! As of Bevy version 0.15, [the `accesskit` crate][accesskit_crate] is no35//! longer re-exported from this crate.[^accesskit_node_confusion] If you need36//! to use `AccessKit` yourself, you'll have to add it as a separate dependency37//! in your project's `Cargo.toml`.38//!39//! Make sure to use the same version of the `accesskit` crate as Bevy.40//! Otherwise, you may experience errors similar to: "Perhaps two different41//! versions of crate `accesskit` are being used?"42//!43//! [accesskit_crate]: https://crates.io/crates/accesskit44//! [`Entity`]: bevy_ecs::entity::Entity45//!46//! <!--47//! note: multi-line footnotes need to be indented like this!48//!49//! please do not remove the indentation, or the second paragraph will display50//! at the end of the module docs, **before** the footnotes...51//! -->52//!53//! [^accesskit_node_confusion]: Some users were confused about `AccessKit`'s54//! `Node` type, sometimes thinking it was Bevy UI's primary way to define55//! nodes!56//!57//! For this reason, its re-export was removed by default. Users who need58//! its types can instead manually depend on the `accesskit` crate.5960#[cfg(feature = "std")]61extern crate std;6263extern crate alloc;6465use alloc::sync::Arc;66use core::sync::atomic::{AtomicBool, Ordering};6768use accesskit::Node;69use bevy_app::Plugin;70use bevy_derive::{Deref, DerefMut};71use bevy_ecs::{component::Component, message::Message, resource::Resource, schedule::SystemSet};7273#[cfg(feature = "bevy_reflect")]74use {75bevy_ecs::reflect::ReflectResource, bevy_reflect::std_traits::ReflectDefault,76bevy_reflect::Reflect,77};7879#[cfg(feature = "serialize")]80use serde::{Deserialize, Serialize};8182#[cfg(all(feature = "bevy_reflect", feature = "serialize"))]83use bevy_reflect::{ReflectDeserialize, ReflectSerialize};8485/// Wrapper struct for [`accesskit::ActionRequest`].86///87/// This newtype is required to use `ActionRequest` as a Bevy `Event`.88#[derive(Message, Deref, DerefMut)]89#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]90pub struct ActionRequest(pub accesskit::ActionRequest);9192/// Tracks whether an assistive technology has requested accessibility93/// information.94///95/// This type is a [`Resource`] initialized by the96/// [`AccessibilityPlugin`]. It may be useful if a third-party plugin needs to97/// conditionally integrate with `AccessKit`.98///99/// In other words, this resource represents whether accessibility providers100/// are "turned on" or "turned off" across an entire Bevy `App`.101///102/// By default, it is set to `false`, indicating that nothing has requested103/// accessibility information yet.104///105/// [`Resource`]: bevy_ecs::resource::Resource106#[derive(Resource, Default, Clone, Debug, Deref, DerefMut)]107#[cfg_attr(108feature = "bevy_reflect",109derive(Reflect),110reflect(Default, Clone, Resource)111)]112pub struct AccessibilityRequested(Arc<AtomicBool>);113114impl AccessibilityRequested {115/// Checks if any assistive technology has requested accessibility116/// information.117///118/// If so, this method returns `true`, indicating that accessibility tree119/// updates should be sent.120pub fn get(&self) -> bool {121self.load(Ordering::SeqCst)122}123124/// Sets the app's preference for sending accessibility updates.125///126/// If the `value` argument is `true`, this method requests that the app,127/// including both Bevy and third-party interfaces, provides updates to128/// accessibility information.129///130/// Setting with `false` requests that the entire app stops providing these131/// updates.132pub fn set(&self, value: bool) {133self.store(value, Ordering::SeqCst);134}135}136137/// Determines whether Bevy's ECS updates the accessibility tree.138///139/// This [`Resource`] tells Bevy internals whether it should be handling140/// `AccessKit` updates (`true`), or if something else is doing that (`false`).141///142/// It defaults to `true`. So, by default, Bevy is configured to maintain the143/// `AccessKit` tree.144///145/// Set to `false` in cases where an external GUI library is sending146/// accessibility updates instead. When this option is set inconsistently with147/// that requirement, the external library and ECS will generate conflicting148/// updates.149///150/// [`Resource`]: bevy_ecs::resource::Resource151#[derive(Resource, Clone, Debug, Deref, DerefMut)]152#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]153#[cfg_attr(154feature = "bevy_reflect",155derive(Reflect),156reflect(Resource, Clone, Default)157)]158#[cfg_attr(159all(feature = "bevy_reflect", feature = "serialize"),160reflect(Serialize, Deserialize)161)]162pub struct ManageAccessibilityUpdates(bool);163164impl Default for ManageAccessibilityUpdates {165fn default() -> Self {166Self(true)167}168}169170impl ManageAccessibilityUpdates {171/// Returns `true` if Bevy's ECS should update the accessibility tree.172pub fn get(&self) -> bool {173self.0174}175176/// Sets whether Bevy's ECS should update the accessibility tree.177pub fn set(&mut self, value: bool) {178self.0 = value;179}180}181182/// Represents an entity to `AccessKit` through an [`accesskit::Node`].183///184/// Platform-specific accessibility APIs utilize `AccessKit` nodes in their185/// accessibility frameworks. So, this component acts as a translation between186/// "Bevy entity" and "platform-agnostic accessibility element".187///188/// ## Organization in the `AccessKit` Accessibility Tree189///190/// `AccessKit` allows users to form a "tree of nodes" providing accessibility191/// information. That tree is **not** Bevy's ECS!192///193/// To explain, let's say this component is added to an entity, `E`.194///195/// ### Parent and Child196///197/// If `E` has a parent, `P`, and `P` also has this `AccessibilityNode`198/// component, then `E`'s `AccessKit` node will be a child of `P`'s `AccessKit`199/// node.200///201/// Resulting `AccessKit` tree:202/// - P203/// - E204///205/// In other words, parent-child relationships are maintained, but only if both206/// have this component.207///208/// ### On the Window209///210/// If `E` doesn't have a parent, or if the immediate parent doesn't have an211/// `AccessibilityNode`, its `AccessKit` node will be an immediate child of the212/// primary window.213///214/// Resulting `AccessKit` tree:215/// - Primary window216/// - E217///218/// When there's no `AccessKit`-compatible parent, the child lacks hierarchical219/// information in `AccessKit`. As such, it is placed directly under the220/// primary window on the `AccessKit` tree.221///222/// This behavior may or may not be intended, so please utilize223/// `AccessibilityNode`s with care.224#[derive(Component, Clone, Deref, DerefMut)]225#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]226pub struct AccessibilityNode(227/// A representation of this component's entity to `AccessKit`.228///229/// Note that, with its parent struct acting as just a newtype, users are230/// intended to directly update this field.231pub Node,232);233234impl From<Node> for AccessibilityNode {235/// Converts an [`accesskit::Node`] into the Bevy Engine236/// [`AccessibilityNode`] newtype.237///238/// Doing so allows it to be inserted onto Bevy entities, representing Bevy239/// entities in the `AccessKit` tree.240fn from(node: Node) -> Self {241Self(node)242}243}244245/// A system set relating to accessibility.246///247/// Helps run accessibility updates all at once.248#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]249#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]250#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]251#[cfg_attr(252all(feature = "bevy_reflect", feature = "serialize"),253reflect(Serialize, Deserialize, Clone)254)]255pub enum AccessibilitySystems {256/// Update the accessibility tree.257Update,258}259260/// Deprecated alias for [`AccessibilitySystems`].261#[deprecated(since = "0.17.0", note = "Renamed to `AccessibilitySystems`.")]262pub type AccessibilitySystem = AccessibilitySystems;263264/// Plugin managing integration with accessibility APIs.265///266/// Note that it doesn't handle GUI aspects of this integration, instead267/// providing helpful resources for other interfaces to utilize.268///269/// ## Behavior270///271/// This plugin's main role is to initialize the [`AccessibilityRequested`] and272/// [`ManageAccessibilityUpdates`] resources to their default values, meaning:273///274/// - no assistive technologies have requested accessibility information yet,275/// and276/// - Bevy's ECS will manage updates to the accessibility tree.277#[derive(Default)]278pub struct AccessibilityPlugin;279280impl Plugin for AccessibilityPlugin {281fn build(&self, app: &mut bevy_app::App) {282app.init_resource::<AccessibilityRequested>()283.init_resource::<ManageAccessibilityUpdates>()284.allow_ambiguous_component::<AccessibilityNode>();285}286}287288289