Revision control

Copy as Markdown

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default
#include "qiosplatformaccessibility.h"
#if QT_CONFIG(accessibility)
#include <QtGui/QtGui>
#include "qioswindow.h"
#include "quiaccessibilityelement.h"
QT_NAMESPACE_ALIAS_OBJC_CLASS(QMacAccessibilityElement);
QIOSPlatformAccessibility::QIOSPlatformAccessibility()
{
m_focusObserver = QMacNotificationObserver(
nil, UIAccessibilityElementFocusedNotification, [&](NSNotification *notification) {
id element = notification.userInfo[UIAccessibilityFocusedElementKey];
m_focusElement = static_cast<QMacAccessibilityElement *>(element);
});
}
QIOSPlatformAccessibility::~QIOSPlatformAccessibility()
{}
void invalidateCache(QAccessibleInterface *iface)
{
if (!iface || !iface->isValid()) {
qWarning() << "invalid accessible interface: " << iface;
return;
}
// This will invalidate everything regardless of what window the
// interface belonged to. We might want to revisit this strategy later.
// (Therefore this function still takes the interface as argument)
//
// Clear every window, not just the top levels, as each view vends the
// elements of its own window only, so a child window's cache is the one
// holding the elements that a change below a native view boundary affects.
const auto windows = QGuiApplication::allWindows();
for (QWindow *win : windows) {
if (win && win->handle()) {
auto *window = static_cast<QIOSWindow *>(win->handle());
window->clearAccessibleCache();
}
}
}
void QIOSPlatformAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
{
auto *accessibleInterface = event->accessibleInterface();
if (!isActive() || !accessibleInterface)
return;
switch (event->type()) {
case QAccessible::Announcement: {
auto *announcementEvent = static_cast<QAccessibleAnnouncementEvent *>(event);
const bool queueAnnouncement =
(announcementEvent->politeness() == QAccessible::AnnouncementPoliteness::Polite);
NSAttributedString *message = [[NSAttributedString alloc]
initWithString:announcementEvent->message().toNSString()
attributes:@{UIAccessibilitySpeechAttributeQueueAnnouncement: @(queueAnnouncement)}];
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, [message autorelease]);
break;
}
case QAccessible::Focus: {
auto *element = [QMacAccessibilityElement elementWithId:event->uniqueId()];
Q_ASSERT(element);
// There's no NSAccessibilityFocusedUIElementChangedNotification, like we have on
// macOS. Instead, the documentation for UIAccessibilityLayoutChangedNotification
// specifies that the optional argument to UIAccessibilityPostNotification is the
// accessibility element for VoiceOver to move to after processing the notification.
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, element);
break;
}
case QAccessible::DescriptionChanged:
case QAccessible::NameChanged: {
auto *element = [QMacAccessibilityElement elementWithId:event->uniqueId()];
if (element == m_focusElement)
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, element);
break;
}
case QAccessible::StateChanged:
case QAccessible::ValueChanged: {
auto *element = [QMacAccessibilityElement elementWithId:event->uniqueId()];
if (element)
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, element);
break;
}
case QAccessible::ObjectCreated:
case QAccessible::ObjectShow:
case QAccessible::ObjectHide:
invalidateCache(accessibleInterface);
switch (accessibleInterface->role()) {
case QAccessible::Window:
case QAccessible::Dialog:
// Bigger changes to the UI require a full reset of VoiceOver
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);
break;
default:
// While smaller changes can be handled by re-reading the layout
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);
}
break;
case QAccessible::ObjectDestroyed:
invalidateCache(accessibleInterface);
// It would be nice if we could read the role from an
// destroyed a11y object, to check whether the object was
// a window or dialog before it was destroyed, but we can't
// trust that doing so won't try to read the object itself.
// Instead of unconditionally posting a screen change here,
// we assume that anything that is destroyed is hidden first.
break;
default:
break;
}
}
#endif