Skip to content

Commit

Permalink
fix: Use common filters across platform adapters (AccessKit#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcampbell authored and lunixbochs committed Aug 31, 2023
1 parent fdc2624 commit b3a1093
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 110 deletions.
49 changes: 49 additions & 0 deletions consumer/src/filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 The AccessKit Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (found in
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

use accesskit::Role;

use crate::node::{DetachedNode, Node, NodeState};

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum FilterResult {
Include,
ExcludeNode,
ExcludeSubtree,
}

fn common_filter_base(node: &NodeState) -> FilterResult {
if node.is_hidden() {
return FilterResult::ExcludeSubtree;
}

let role = node.role();
if role == Role::Presentation || role == Role::GenericContainer || role == Role::InlineTextBox {
return FilterResult::ExcludeNode;
}

FilterResult::Include
}

pub fn common_filter(node: &Node) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}
common_filter_base(node.state())
}

pub fn common_filter_detached(node: &DetachedNode) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}
common_filter_base(node.state())
}

pub fn common_filter_with_root_exception(node: &Node) -> FilterResult {
if node.is_root() {
return FilterResult::Include;
}
common_filter(node)
}
9 changes: 1 addition & 8 deletions consumer/src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::iter::FusedIterator;

use accesskit::NodeId;

use crate::{node::Node, tree::State as TreeState};
use crate::{filters::FilterResult, node::Node, tree::State as TreeState};

/// An iterator that yields following siblings of a node.
///
Expand Down Expand Up @@ -178,13 +178,6 @@ impl<'a> ExactSizeIterator for PrecedingSiblings<'a> {}

impl<'a> FusedIterator for PrecedingSiblings<'a> {}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum FilterResult {
Include,
ExcludeNode,
ExcludeSubtree,
}

fn next_filtered_sibling<'a>(
node: Option<Node<'a>>,
filter: &impl Fn(&Node) -> FilterResult,
Expand Down
6 changes: 5 additions & 1 deletion consumer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ pub use tree::{ChangeHandler as TreeChangeHandler, State as TreeState, Tree};
pub(crate) mod node;
pub use node::{DetachedNode, Node, NodeState};

pub(crate) mod filters;
pub use filters::{
common_filter, common_filter_detached, common_filter_with_root_exception, FilterResult,
};

pub(crate) mod iterators;
pub use iterators::FilterResult;

pub(crate) mod text;
pub use text::{
Expand Down
3 changes: 2 additions & 1 deletion consumer/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use accesskit::{
Role, TextSelection,
};

use crate::filters::FilterResult;
use crate::iterators::{
FilterResult, FilteredChildren, FollowingFilteredSiblings, FollowingSiblings, LabelledBy,
FilteredChildren, FollowingFilteredSiblings, FollowingSiblings, LabelledBy,
PrecedingFilteredSiblings, PrecedingSiblings,
};
use crate::tree::State as TreeState;
Expand Down
3 changes: 2 additions & 1 deletion platforms/macos/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use crate::{
appkit::NSView,
context::Context,
event::{EventGenerator, QueuedEvents},
node::{can_be_focused, filter},
filters::filter,
node::can_be_focused,
util::*,
};

Expand Down
3 changes: 2 additions & 1 deletion platforms/macos/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use std::{collections::HashSet, rc::Rc};
use crate::{
appkit::*,
context::Context,
node::{filter, filter_detached, NodeWrapper},
filters::{filter, filter_detached},
node::NodeWrapper,
};

// Workaround for https://github.com/madsmtm/objc2/issues/306
Expand Down
8 changes: 8 additions & 0 deletions platforms/macos/src/filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2023 The AccessKit Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (found in
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

pub(crate) use accesskit_consumer::{
common_filter as filter, common_filter_detached as filter_detached,
};
1 change: 1 addition & 0 deletions platforms/macos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

mod appkit;
mod context;
mod filters;
mod node;
mod util;

Expand Down
31 changes: 1 addition & 30 deletions platforms/macos/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::{
rc::{Rc, Weak},
};

use crate::{appkit::*, context::Context, util::*};
use crate::{appkit::*, context::Context, filters::filter, util::*};

fn ns_role(node_state: &NodeState) -> &'static NSString {
let role = node_state.role();
Expand Down Expand Up @@ -232,35 +232,6 @@ fn ns_role(node_state: &NodeState) -> &'static NSString {
}
}

fn filter_common(node_state: &NodeState) -> FilterResult {
let ns_role = ns_role(node_state);
if ns_role == unsafe { NSAccessibilityUnknownRole } {
return FilterResult::ExcludeNode;
}

if node_state.is_hidden() {
return FilterResult::ExcludeSubtree;
}

FilterResult::Include
}

pub(crate) fn filter(node: &Node) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}

filter_common(node.state())
}

pub(crate) fn filter_detached(node: &DetachedNode) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}

filter_common(node.state())
}

pub(crate) fn can_be_focused(node: &Node) -> bool {
filter(node) == FilterResult::Include && node.role() != Role::Window
}
Expand Down
3 changes: 2 additions & 1 deletion platforms/unix/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::{
Bus, ObjectId, ACCESSIBLE_PATH_PREFIX,
},
context::Context,
node::{filter, filter_detached, NodeWrapper, PlatformNode},
filters::{filter, filter_detached},
node::{NodeWrapper, PlatformNode},
util::{block_on, AppContext},
};
use accesskit::{ActionHandler, NodeId, Rect, Role, TreeUpdate};
Expand Down
8 changes: 8 additions & 0 deletions platforms/unix/src/filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2023 The AccessKit Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (found in
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

pub(crate) use accesskit_consumer::{
common_filter as filter, common_filter_detached as filter_detached,
};
1 change: 1 addition & 0 deletions platforms/unix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate zbus;
mod adapter;
mod atspi;
mod context;
mod filters;
mod node;
mod util;

Expand Down
30 changes: 1 addition & 29 deletions platforms/unix/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
ObjectId, ObjectRef, Rect as AtspiRect, ACCESSIBLE_PATH_PREFIX,
},
context::Context,
filters::{filter, filter_detached},
util::WindowBounds,
};
use accesskit::{
Expand All @@ -32,35 +33,6 @@ use std::{
};
use zbus::fdo;

fn filter_common(node: &NodeState) -> FilterResult {
if node.is_hidden() {
return FilterResult::ExcludeSubtree;
}

let role = node.role();
if role == Role::Presentation || role == Role::GenericContainer || role == Role::InlineTextBox {
return FilterResult::ExcludeNode;
}

FilterResult::Include
}

pub(crate) fn filter(node: &Node) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}

filter_common(node.state())
}

pub(crate) fn filter_detached(node: &DetachedNode) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}

filter_common(node.state())
}

pub(crate) enum NodeWrapper<'a> {
Node(&'a Node<'a>),
DetachedNode(&'a DetachedNode),
Expand Down
3 changes: 2 additions & 1 deletion platforms/windows/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use windows::Win32::{

use crate::{
context::Context,
filters::{filter, filter_detached},
init::UiaInitMarker,
node::{filter, filter_detached, NodeWrapper, PlatformNode},
node::{NodeWrapper, PlatformNode},
util::QueuedEvent,
};

Expand Down
9 changes: 9 additions & 0 deletions platforms/windows/src/filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2023 The AccessKit Authors. All rights reserved.
// Licensed under the Apache License, Version 2.0 (found in
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

pub(crate) use accesskit_consumer::{
common_filter as filter, common_filter_detached as filter_detached,
common_filter_with_root_exception as filter_with_root_exception,
};
1 change: 1 addition & 0 deletions platforms/windows/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// the LICENSE-MIT file), at your option.

mod context;
mod filters;
mod node;
mod text;
mod util;
Expand Down
43 changes: 6 additions & 37 deletions platforms/windows/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ use windows::{
Win32::{Foundation::*, System::Com::*, UI::Accessibility::*},
};

use crate::{context::Context, text::PlatformRange as PlatformTextRange, util::*};
use crate::{
context::Context,
filters::{filter, filter_detached, filter_with_root_exception},
text::PlatformRange as PlatformTextRange,
util::*,
};

const RUNTIME_ID_SIZE: usize = 3;

Expand All @@ -35,42 +40,6 @@ fn runtime_id_from_node_id(id: NodeId) -> [i32; RUNTIME_ID_SIZE] {
]
}

fn filter_common(node: &NodeState) -> FilterResult {
if node.is_hidden() {
return FilterResult::ExcludeSubtree;
}

let role = node.role();
if role == Role::Presentation || role == Role::GenericContainer || role == Role::InlineTextBox {
return FilterResult::ExcludeNode;
}

FilterResult::Include
}

pub(crate) fn filter(node: &Node) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}

filter_common(node.state())
}

pub(crate) fn filter_detached(node: &DetachedNode) -> FilterResult {
if node.is_focused() {
return FilterResult::Include;
}

filter_common(node.state())
}

fn filter_with_root_exception(node: &Node) -> FilterResult {
if node.is_root() {
return FilterResult::Include;
}
filter(node)
}

pub(crate) enum NodeWrapper<'a> {
Node(&'a Node<'a>),
DetachedNode(&'a DetachedNode),
Expand Down

0 comments on commit b3a1093

Please sign in to comment.