Skip to content

Commit

Permalink
feat: Support custom role descriptions (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
DataTriny authored Jan 3, 2024
1 parent 936fa2c commit c8d1a56
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
8 changes: 8 additions & 0 deletions consumer/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ impl NodeState {
self.data().role()
}

pub fn role_description(&self) -> Option<String> {
self.data().role_description().map(String::from)
}

pub fn has_role_description(&self) -> bool {
self.data().role_description().is_some()
}

pub fn is_hidden(&self) -> bool {
self.data().is_hidden()
}
Expand Down
14 changes: 13 additions & 1 deletion platforms/macos/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use objc2::{
foundation::{
NSArray, NSCopying, NSInteger, NSNumber, NSObject, NSPoint, NSRange, NSRect, NSString,
},
msg_send_id, ns_string,
msg_send, msg_send_id, ns_string,
rc::{Id, Owned, Shared},
runtime::Sel,
sel, ClassType,
Expand Down Expand Up @@ -400,6 +400,18 @@ declare_class!(
Id::autorelease_return(role.copy())
}

#[sel(accessibilityRoleDescription)]
fn role_description(&self) -> *mut NSString {
self.resolve(|node| {
if let Some(role_description) = node.role_description() {
Id::autorelease_return(NSString::from_str(&role_description))
} else {
unsafe { msg_send![super(self), accessibilityRoleDescription] }
}
})
.unwrap_or_else(null_mut)
}

#[sel(accessibilityTitle)]
fn title(&self) -> *mut NSString {
let result = self
Expand Down
4 changes: 4 additions & 0 deletions platforms/unix/src/atspi/interfaces/accessible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl AccessibleInterface<PlatformNode> {
self.node.role()
}

fn get_localized_role_name(&self) -> fdo::Result<String> {
self.node.localized_role_name()
}

fn get_state(&self) -> fdo::Result<StateSet> {
self.node.state()
}
Expand Down
8 changes: 8 additions & 0 deletions platforms/unix/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ impl<'a> NodeWrapper<'a> {
}

pub fn role(&self) -> AtspiRole {
if self.node_state().has_role_description() {
return AtspiRole::Extended;
}

match self.node_state().role() {
Role::Alert => AtspiRole::Notification,
Role::AlertDialog => AtspiRole::Alert,
Expand Down Expand Up @@ -813,6 +817,10 @@ impl PlatformNode {
})
}

pub(crate) fn localized_role_name(&self) -> fdo::Result<String> {
self.resolve(|node| Ok(node.state().role_description().unwrap_or_default()))
}

pub fn state(&self) -> fdo::Result<StateSet> {
self.resolve_with_context(|node, context| {
let wrapper = self.node_wrapper(&node);
Expand Down
5 changes: 5 additions & 0 deletions platforms/windows/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ impl<'a> NodeWrapper<'a> {
}
}

fn localized_control_type(&self) -> Option<String> {
self.node_state().role_description()
}

fn name(&self) -> Option<String> {
match self {
Self::Node(node) => node.name(),
Expand Down Expand Up @@ -841,6 +845,7 @@ macro_rules! patterns {

properties! {
(ControlType, control_type),
(LocalizedControlType, localized_control_type),
(Name, name),
(IsContentElement, is_content_element),
(IsControlElement, is_content_element),
Expand Down

0 comments on commit c8d1a56

Please sign in to comment.