a11y macOS: Report selection/Bridge QAccessibleSelectionInterface

On macOS, support NSAccessibilityProtocol's accessibilitySelectedChildren
method [1] by retrieving the selected children via the
QAccessibleSelectionInterface introduced in commit
9d16d5e224.

This e.g. makes a "selected children" attribute show up
in "UI Browser" [2] for the item views in the "interview" example
(examples/widgets/itemviews/interview/interview.app/Contents/MacOS/interview)
and that one has an array containing the selected items as value.

Sample scenario:

1) run the interview example (examples/widgets/itemviews/interview/interview.app)
2) select "Item 1:0", "Item 2:0" and "Item 3:0" by left-clicking
  on "Item 1:0" in the left view, then shift+clicking on "Item 3:0".
3) start UI Browser and navigate to the "group (group 1)" element
   and check that the value of its "selected children" attribute is an
   array containing the three selected items

[1] https://developer.apple.com/documentation/appkit/nsaccessibilityprotocol/1524672-accessibilityselectedchildren
[2] https://latenightsw.com/freeware/ui-browser/

Change-Id: Ic62f7fa82f2d01341a1dbc5ade2bd02c3ff99d9f
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Michael Weghorn 2023-01-03 12:08:13 +01:00
parent 86ea40b478
commit 98e4e992fe

View File

@ -434,6 +434,30 @@ static void convertLineOffset(QAccessibleTextInterface *text, int *line, int *of
return QCocoaAccessible::unignoredChildren(iface);
}
- (NSArray *) accessibilitySelectedChildren {
QAccessibleInterface *iface = QAccessible::accessibleInterface(axid);
if (!iface || !iface->isValid())
return nil;
QAccessibleSelectionInterface *selection = iface->selectionInterface();
if (!selection)
return nil;
const QList<QAccessibleInterface *> selectedList = selection->selectedItems();
const qsizetype numSelected = selectedList.size();
NSMutableArray<QMacAccessibilityElement *> *selectedChildren =
[NSMutableArray<QMacAccessibilityElement *> arrayWithCapacity:numSelected];
for (QAccessibleInterface *selectedChild : selectedList) {
if (selectedChild && selectedChild->isValid()) {
QAccessible::Id id = QAccessible::uniqueId(selectedChild);
QMacAccessibilityElement *element = [QMacAccessibilityElement elementWithId:id];
if (element)
[selectedChildren addObject:element];
}
}
return NSAccessibilityUnignoredChildren(selectedChildren);
}
- (id) accessibilityWindow {
// We're in the same window as our parent.
return [self.accessibilityParent accessibilityWindow];