a11y macOS: Forward new QAccessibleAnnouncementEvent on macOS

Bridge the newly added QAccessibleAnnouncementEvent on macOS
by calling NSAccessibilityPostNotificationWithUserInfo with
param NSAccessibilityAnnouncementRequestedNotification [1] and
a dict containing the message and priority as data.

Post the notification on the application element, as the
comment for NSAccessibilityAnnouncementRequestedNotification
in the NSAccessibilityConstants.h header says:

"This notification allows an application to request that an announcement be made
to the user by an assistive application such as VoiceOver.  The notification
requires a user info dictionary with the key NSAccessibilityAnnouncementKey and
the announcement as a localized string.  In addition, the key
NSAccessibilityAnnouncementPriorityKey should also be used to help an assistive
application determine the importance of this announcement.  This notification
should be posted for the application element."

This makes the announcement from the example app attached
to QTBUG-75003 work when using the VoiceOver screen reader
on macOS.

[1] https://developer.apple.com/documentation/appkit/nsaccessibilityannouncementrequestednotification

Task-number: QTBUG-75003
Change-Id: Iabd9acf8267b0fef00706004f17d48ebc3ecc161
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Michael Weghorn 2024-04-10 09:43:41 +02:00
parent 0377ad2f83
commit 7dd7ed141d

View File

@ -36,6 +36,23 @@ void QCocoaAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
}
switch (event->type()) {
case QAccessible::Announcement: {
auto *announcementEvent = static_cast<QAccessibleAnnouncementEvent *>(event);
auto priorityLevel = (announcementEvent->priority() == QAccessible::AnnouncementPriority::Assertive)
? NSAccessibilityPriorityHigh
: NSAccessibilityPriorityMedium;
NSDictionary *announcementInfo = @{
NSAccessibilityPriorityKey: [NSNumber numberWithInt:priorityLevel],
NSAccessibilityAnnouncementKey: announcementEvent->message().toNSString()
};
// post event for application element, as the comment for
// NSAccessibilityAnnouncementRequestedNotification in the
// NSAccessibilityConstants.h header says
NSAccessibilityPostNotificationWithUserInfo(NSApp,
NSAccessibilityAnnouncementRequestedNotification,
announcementInfo);
break;
}
case QAccessible::Focus: {
NSAccessibilityPostNotification(element, NSAccessibilityFocusedUIElementChangedNotification);
break;