2023-03-01 22:45:04 +00:00
|
|
|
//! Accessibility for Bevy
|
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
Suppress the `clippy::type_complexity` lint (#8313)
# Objective
The clippy lint `type_complexity` is known not to play well with bevy.
It frequently triggers when writing complex queries, and taking the
lint's advice of using a type alias almost always just obfuscates the
code with no benefit. Because of this, this lint is currently ignored in
CI, but unfortunately it still shows up when viewing bevy code in an
IDE.
As someone who's made a fair amount of pull requests to this repo, I
will say that this issue has been a consistent thorn in my side. Since
bevy code is filled with spurious, ignorable warnings, it can be very
difficult to spot the *real* warnings that must be fixed -- most of the
time I just ignore all warnings, only to later find out that one of them
was real after I'm done when CI runs.
## Solution
Suppress this lint in all bevy crates. This was previously attempted in
#7050, but the review process ended up making it more complicated than
it needs to be and landed on a subpar solution.
The discussion in https://github.com/rust-lang/rust-clippy/pull/10571
explores some better long-term solutions to this problem. Since there is
no timeline on when these solutions may land, we should resolve this
issue in the meantime by locally suppressing these lints.
### Unresolved issues
Currently, these lints are not suppressed in our examples, since that
would require suppressing the lint in every single source file. They are
still ignored in CI.
2023-04-06 21:27:36 +00:00
|
|
|
#![allow(clippy::type_complexity)]
|
2023-03-01 22:45:04 +00:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
num::NonZeroU128,
|
|
|
|
sync::{atomic::AtomicBool, Arc},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub use accesskit;
|
|
|
|
use accesskit::{NodeBuilder, NodeId};
|
|
|
|
use bevy_app::Plugin;
|
|
|
|
use bevy_derive::{Deref, DerefMut};
|
|
|
|
use bevy_ecs::{
|
|
|
|
prelude::{Component, Entity},
|
|
|
|
system::Resource,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// 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>);
|
|
|
|
|
|
|
|
/// 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)]
|
|
|
|
pub struct AccessibilityNode(pub NodeBuilder);
|
|
|
|
|
|
|
|
impl From<NodeBuilder> for AccessibilityNode {
|
|
|
|
fn from(node: NodeBuilder) -> Self {
|
|
|
|
Self(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extensions to ease integrating entities with [`AccessKit`](https://accesskit.dev).
|
|
|
|
pub trait AccessKitEntityExt {
|
|
|
|
/// Convert an entity to a stable [`NodeId`].
|
|
|
|
fn to_node_id(&self) -> NodeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccessKitEntityExt for Entity {
|
|
|
|
fn to_node_id(&self) -> NodeId {
|
|
|
|
let id = NonZeroU128::new(self.to_bits() as u128 + 1);
|
|
|
|
NodeId(id.unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resource representing which entity has keyboard focus, if any.
|
|
|
|
#[derive(Resource, Default, Deref, DerefMut)]
|
|
|
|
pub struct Focus(Option<Entity>);
|
|
|
|
|
|
|
|
/// Plugin managing non-GUI aspects of integrating with accessibility APIs.
|
|
|
|
pub struct AccessibilityPlugin;
|
|
|
|
|
|
|
|
impl Plugin for AccessibilityPlugin {
|
|
|
|
fn build(&self, app: &mut bevy_app::App) {
|
|
|
|
app.init_resource::<AccessibilityRequested>()
|
|
|
|
.init_resource::<Focus>();
|
|
|
|
}
|
|
|
|
}
|