Skip to content

feat: Let parents declare actions supported on their children #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 97 additions & 23 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ struct Properties {
pub struct Node {
role: Role,
actions: u32,
child_actions: u32,
flags: u32,
properties: Properties,
}
Expand Down Expand Up @@ -1600,6 +1601,31 @@ impl Node {
pub fn clear_actions(&mut self) {
self.actions = 0;
}

/// Return whether the specified action is in the set supported on this node's
/// direct children in the filtered tree.
#[inline]
pub fn child_supports_action(&self, action: Action) -> bool {
(self.child_actions & action.mask()) != 0
}
/// Add the specified action to the set supported on this node's direct
/// children in the filtered tree.
#[inline]
pub fn add_child_action(&mut self, action: Action) {
self.child_actions |= action.mask();
}
/// Remove the specified action from the set supported on this node's direct
/// children in the filtered tree.
#[inline]
pub fn remove_child_action(&mut self, action: Action) {
self.child_actions &= !(action.mask());
}
/// Clear the set of actions supported on this node's direct children in the
/// filtered tree.
#[inline]
pub fn clear_child_actions(&mut self) {
self.child_actions = 0;
}
}

flag_methods! {
Expand Down Expand Up @@ -2141,6 +2167,11 @@ impl fmt::Debug for Node {
fmt.field("actions", &supported_actions);
}

let child_supported_actions = action_mask_to_action_vec(self.child_actions);
if !child_supported_actions.is_empty() {
fmt.field("child_actions", &child_supported_actions);
}

self.debug_flag_properties(&mut fmt);
self.debug_node_id_vec_properties(&mut fmt);
self.debug_node_id_properties(&mut fmt);
Expand Down Expand Up @@ -2820,31 +2851,38 @@ mod tests {
assert_eq!(node.role(), Role::CheckBox);
}

macro_rules! assert_absent_action {
($node:ident, $action:ident) => {
assert!(!$node.supports_action(Action::$action));
assert!(!$node.child_supports_action(Action::$action));
};
}

#[test]
fn new_node_should_not_support_anyaction() {
let node = Node::new(Role::Unknown);
assert!(!node.supports_action(Action::Click));
assert!(!node.supports_action(Action::Focus));
assert!(!node.supports_action(Action::Blur));
assert!(!node.supports_action(Action::Collapse));
assert!(!node.supports_action(Action::Expand));
assert!(!node.supports_action(Action::CustomAction));
assert!(!node.supports_action(Action::Decrement));
assert!(!node.supports_action(Action::Increment));
assert!(!node.supports_action(Action::HideTooltip));
assert!(!node.supports_action(Action::ShowTooltip));
assert!(!node.supports_action(Action::ReplaceSelectedText));
assert!(!node.supports_action(Action::ScrollDown));
assert!(!node.supports_action(Action::ScrollLeft));
assert!(!node.supports_action(Action::ScrollRight));
assert!(!node.supports_action(Action::ScrollUp));
assert!(!node.supports_action(Action::ScrollIntoView));
assert!(!node.supports_action(Action::ScrollToPoint));
assert!(!node.supports_action(Action::SetScrollOffset));
assert!(!node.supports_action(Action::SetTextSelection));
assert!(!node.supports_action(Action::SetSequentialFocusNavigationStartingPoint));
assert!(!node.supports_action(Action::SetValue));
assert!(!node.supports_action(Action::ShowContextMenu));
assert_absent_action!(node, Click);
assert_absent_action!(node, Focus);
assert_absent_action!(node, Blur);
assert_absent_action!(node, Collapse);
assert_absent_action!(node, Expand);
assert_absent_action!(node, CustomAction);
assert_absent_action!(node, Decrement);
assert_absent_action!(node, Increment);
assert_absent_action!(node, HideTooltip);
assert_absent_action!(node, ShowTooltip);
assert_absent_action!(node, ReplaceSelectedText);
assert_absent_action!(node, ScrollDown);
assert_absent_action!(node, ScrollLeft);
assert_absent_action!(node, ScrollRight);
assert_absent_action!(node, ScrollUp);
assert_absent_action!(node, ScrollIntoView);
assert_absent_action!(node, ScrollToPoint);
assert_absent_action!(node, SetScrollOffset);
assert_absent_action!(node, SetTextSelection);
assert_absent_action!(node, SetSequentialFocusNavigationStartingPoint);
assert_absent_action!(node, SetValue);
assert_absent_action!(node, ShowContextMenu);
}

#[test]
Expand All @@ -2856,6 +2894,15 @@ mod tests {
assert!(node.supports_action(Action::Blur));
}

#[test]
fn node_add_child_action_should_add_the_action() {
let mut node = Node::new(Role::Unknown);
node.add_child_action(Action::Focus);
assert!(node.child_supports_action(Action::Focus));
node.add_child_action(Action::Blur);
assert!(node.child_supports_action(Action::Blur));
}

#[test]
fn node_add_action_should_do_nothing_if_the_action_is_already_supported() {
let mut node = Node::new(Role::Unknown);
Expand All @@ -2864,6 +2911,14 @@ mod tests {
assert!(node.supports_action(Action::Focus));
}

#[test]
fn node_add_child_action_should_do_nothing_if_the_action_is_already_supported() {
let mut node = Node::new(Role::Unknown);
node.add_child_action(Action::Focus);
node.add_child_action(Action::Focus);
assert!(node.child_supports_action(Action::Focus));
}

#[test]
fn node_remove_action_should_remove_the_action() {
let mut node = Node::new(Role::Unknown);
Expand All @@ -2872,6 +2927,14 @@ mod tests {
assert!(!node.supports_action(Action::Blur));
}

#[test]
fn node_remove_child_action_should_remove_the_action() {
let mut node = Node::new(Role::Unknown);
node.add_child_action(Action::Blur);
node.remove_child_action(Action::Blur);
assert!(!node.child_supports_action(Action::Blur));
}

#[test]
fn node_clear_actions_should_remove_all_actions() {
let mut node = Node::new(Role::Unknown);
Expand All @@ -2882,11 +2945,22 @@ mod tests {
assert!(!node.supports_action(Action::Blur));
}

#[test]
fn node_clear_child_actions_should_remove_all_actions() {
let mut node = Node::new(Role::Unknown);
node.add_child_action(Action::Focus);
node.add_child_action(Action::Blur);
node.clear_child_actions();
assert!(!node.child_supports_action(Action::Focus));
assert!(!node.child_supports_action(Action::Blur));
}

#[test]
fn node_should_have_debug_repr() {
let mut node = Node::new(Role::Unknown);
node.add_action(Action::Click);
node.add_action(Action::Focus);
node.add_child_action(Action::ScrollIntoView);
node.set_hidden();
node.set_multiselectable();
node.set_children([NodeId(0), NodeId(1)]);
Expand All @@ -2898,7 +2972,7 @@ mod tests {

assert_eq!(
&format!("{node:?}"),
r#"Node { role: Unknown, actions: [Click, Focus], is_hidden: true, is_multiselectable: true, children: [#0, #1], active_descendant: #2, custom_actions: [CustomAction { id: 0, description: "test action" }] }"#
r#"Node { role: Unknown, actions: [Click, Focus], child_actions: [ScrollIntoView], is_hidden: true, is_multiselectable: true, children: [#0, #1], active_descendant: #2, custom_actions: [CustomAction { id: 0, description: "test action" }] }"#
);
}

Expand Down
34 changes: 22 additions & 12 deletions consumer/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl<'a> Node<'a> {
self.tree_state.focus == self.id()
}

pub fn is_focusable(&self) -> bool {
self.supports_action(Action::Focus) || self.is_focused_in_tree()
pub fn is_focusable(&self, parent_filter: &impl Fn(&Node) -> FilterResult) -> bool {
self.supports_action(Action::Focus, parent_filter) || self.is_focused_in_tree()
}

pub fn is_root(&self) -> bool {
Expand Down Expand Up @@ -452,8 +452,8 @@ impl<'a> Node<'a> {
// and we assume that it will based on the role, the attempted action
// does nothing. This stance is a departure from Chromium.

pub fn is_clickable(&self) -> bool {
self.supports_action(Action::Click)
pub fn is_clickable(&self, parent_filter: &impl Fn(&Node) -> FilterResult) -> bool {
self.supports_action(Action::Click, parent_filter)
}

pub fn is_selectable(&self) -> bool {
Expand Down Expand Up @@ -491,7 +491,7 @@ impl<'a> Node<'a> {
self.data().is_expanded().is_some()
}

pub fn is_invocable(&self) -> bool {
pub fn is_invocable(&self, parent_filter: &impl Fn(&Node) -> FilterResult) -> bool {
// A control is "invocable" if it initiates an action when activated but
// does not maintain any state. A control that maintains state
// when activated would be considered a toggle or expand-collapse
Expand All @@ -500,24 +500,34 @@ impl<'a> Node<'a> {
// such as when clicking a text input, the control is not considered
// "invocable", as the "invoke" action would be a redundant synonym
// for the "set focus" action. The same logic applies to selection.
self.is_clickable()
self.is_clickable(parent_filter)
&& !self.is_text_input()
&& !matches!(self.role(), Role::Document | Role::Terminal)
&& !self.supports_toggle()
&& !self.supports_expand_collapse()
&& self.is_selected().is_none()
}

pub fn supports_action(&self, action: Action) -> bool {
self.data().supports_action(action)
pub fn supports_action(
&self,
action: Action,
parent_filter: &impl Fn(&Node) -> FilterResult,
) -> bool {
if self.data().supports_action(action) {
return true;
}
if let Some(parent) = self.filtered_parent(parent_filter) {
return parent.data().child_supports_action(action);
}
false
}

pub fn supports_increment(&self) -> bool {
self.supports_action(Action::Increment)
pub fn supports_increment(&self, parent_filter: &impl Fn(&Node) -> FilterResult) -> bool {
self.supports_action(Action::Increment, parent_filter)
}

pub fn supports_decrement(&self) -> bool {
self.supports_action(Action::Decrement)
pub fn supports_decrement(&self, parent_filter: &impl Fn(&Node) -> FilterResult) -> bool {
self.supports_action(Action::Decrement, parent_filter)
}
}

Expand Down
11 changes: 7 additions & 4 deletions platforms/android/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ impl Adapter {
ACTION_CLICK => ActionRequest {
action: {
let node = tree_state.node_by_id(target).unwrap();
if node.is_focusable() && !node.is_focused() && !node.is_clickable() {
if node.is_focusable(&filter)
&& !node.is_focused()
&& !node.is_clickable(&filter)
{
Action::Focus
} else {
Action::Click
Expand Down Expand Up @@ -422,12 +425,12 @@ impl Adapter {
}
}
} else if action == ACTION_SCROLL_BACKWARD {
if node.supports_action(Action::ScrollUp) {
if node.supports_action(Action::ScrollUp, &filter) {
Action::ScrollUp
} else {
Action::ScrollLeft
}
} else if node.supports_action(Action::ScrollDown) {
} else if node.supports_action(Action::ScrollDown, &filter) {
Action::ScrollDown
} else {
Action::ScrollRight
Expand Down Expand Up @@ -492,7 +495,7 @@ impl Adapter {
// TalkBack expects the text selection change to take effect
// immediately, so we optimistically update the node.
// But don't be *too* optimistic.
if !node.supports_action(Action::SetTextSelection) {
if !node.supports_action(Action::SetTextSelection, &filter) {
return None;
}
let (anchor, focus, extra) = selection_factory(&node)?;
Expand Down
19 changes: 11 additions & 8 deletions platforms/android/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl NodeWrapper<'_> {
}

fn is_focusable(&self) -> bool {
self.0.is_focusable() && self.0.role() != Role::ScrollView
self.0.is_focusable(&filter) && self.0.role() != Role::ScrollView
}

fn is_focused(&self) -> bool {
Expand All @@ -60,10 +60,10 @@ impl NodeWrapper<'_> {
}

fn is_scrollable(&self) -> bool {
self.0.supports_action(Action::ScrollDown)
|| self.0.supports_action(Action::ScrollLeft)
|| self.0.supports_action(Action::ScrollRight)
|| self.0.supports_action(Action::ScrollUp)
self.0.supports_action(Action::ScrollDown, &filter)
|| self.0.supports_action(Action::ScrollLeft, &filter)
|| self.0.supports_action(Action::ScrollRight, &filter)
|| self.0.supports_action(Action::ScrollUp, &filter)
}

fn is_selected(&self) -> bool {
Expand Down Expand Up @@ -331,7 +331,7 @@ impl NodeWrapper<'_> {
.unwrap();

let can_focus = self.is_focusable() && !self.0.is_focused();
if self.0.is_clickable() || can_focus {
if self.0.is_clickable(&filter) || can_focus {
add_action(env, node_info, ACTION_CLICK);
}
if can_focus {
Expand All @@ -353,10 +353,13 @@ impl NodeWrapper<'_> {
)
.unwrap();
}
if self.0.supports_action(Action::ScrollLeft) || self.0.supports_action(Action::ScrollUp) {
if self.0.supports_action(Action::ScrollLeft, &filter)
|| self.0.supports_action(Action::ScrollUp, &filter)
{
add_action(env, node_info, ACTION_SCROLL_BACKWARD);
}
if self.0.supports_action(Action::ScrollRight) || self.0.supports_action(Action::ScrollDown)
if self.0.supports_action(Action::ScrollRight, &filter)
|| self.0.supports_action(Action::ScrollDown, &filter)
{
add_action(env, node_info, ACTION_SCROLL_FORWARD);
}
Expand Down
Loading