From 7dd7ed141dcdde037b129398e0c1559d36e855cc Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Wed, 10 Apr 2024 09:43:41 +0200 Subject: [PATCH] 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 --- .../platforms/cocoa/qcocoaaccessibility.mm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/plugins/platforms/cocoa/qcocoaaccessibility.mm b/src/plugins/platforms/cocoa/qcocoaaccessibility.mm index c5e40a40877..40c1e905119 100644 --- a/src/plugins/platforms/cocoa/qcocoaaccessibility.mm +++ b/src/plugins/platforms/cocoa/qcocoaaccessibility.mm @@ -36,6 +36,23 @@ void QCocoaAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event) } switch (event->type()) { + case QAccessible::Announcement: { + auto *announcementEvent = static_cast(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;