9#include <QtCore/private/qwasmsuspendresumecontrol_p.h>
10#include <QtGui/qwindow.h>
14 QWasmAccessibility::enable();
17#if QT_CONFIG(accessibility)
19#include <QtGui/private/qaccessiblebridgeutils_p.h>
21Q_LOGGING_CATEGORY(lcQpaAccessibility,
"qt.qpa.accessibility")
24EM_JS(emscripten::EM_VAL, getActiveElement_js, (emscripten::EM_VAL undefHandle), {
25 var activeEl = document.activeElement;
29 }
else if (activeEl.shadowRoot) {
30 activeEl = activeEl.shadowRoot.activeElement;
32 return Emval.toHandle(activeEl);
48QWasmAccessibility::QWasmAccessibility()
50 if (qEnvironmentVariableIntValue(
"QT_WASM_ENABLE_ACCESSIBILITY") == 1)
51 enableAccessibility();
54 QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get();
56 m_eventHandlerIndex = suspendResume->registerEventHandler([
this](
const emscripten::val event){
57 this->handleEventFromHtmlElement(event);
61QWasmAccessibility::~QWasmAccessibility()
64 QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get();
65 suspendResume->removeEventHandler(m_eventHandlerIndex);
68QWasmAccessibility *QWasmAccessibility::instance()
70 QWasmIntegration *integration = QWasmIntegration::get();
71 return integration ?
static_cast<QWasmAccessibility *>(integration->accessibility()) :
nullptr;
74void QWasmAccessibility::addAccessibilityEnableButton(QWindow *window)
76 if (QWasmAccessibility *accessibility = instance(); accessibility && !accessibility->isActive())
77 accessibility->addEnableButton(window);
80void QWasmAccessibility::onShowWindow(QWindow *window)
82 if (QWasmAccessibility *accessibility = instance(); accessibility && accessibility->isActive())
83 accessibility->populateAccessibilityTree(window->accessibleRoot());
86void QWasmAccessibility::onRemoveWindow(QWindow *window)
88 if (QWasmAccessibility *accessibility = instance())
89 accessibility->removeWindow(window);
92bool QWasmAccessibility::isEnabled()
94 const QWasmAccessibility *accessibility = instance();
95 return accessibility && accessibility->isActive();
98void QWasmAccessibility::enable()
100 if (QWasmAccessibility *accessibility = instance(); accessibility && !accessibility->isActive())
101 accessibility->enableAccessibility();
104void QWasmAccessibility::addEnableButton(QWindow *window)
106 emscripten::val container = getElementContainer(window);
107 if (container.isUndefined())
110 emscripten::val document = getDocument(container);
111 emscripten::val button = document.call<emscripten::val>(
"createElement", std::string(
"button"));
112 setProperty(button,
"innerText",
"Enable Screen Reader");
113 button[
"classList"].call<
void>(
"add", emscripten::val(
"hidden-visually-read-by-screen-reader"));
114 container.call<
void>(
"appendChild", button);
116 auto enableContext = std::make_tuple(button, std::make_unique<qstdweb::EventCallback>
117 (button, std::string(
"click"), [
this](emscripten::val) { enableAccessibility(); }));
118 m_enableButtons.insert(std::make_pair(window, std::move(enableContext)));
121void QWasmAccessibility::removeWindow(QWindow *window)
124 const auto it = m_enableButtons.find(window);
125 if (it != m_enableButtons.end()) {
127 auto [element, callback] = it->second;
129 element[
"parentElement"].call<
void>(
"removeChild", element);
130 m_enableButtons.erase(it);
133 const QWasmWindow *wasmWindow = QWasmWindow::fromWindow(window);
134 if (!isActive() || !wasmWindow)
137 const emscripten::val a11yContainer = wasmWindow->a11yContainer();
140 for (
auto it = m_elements.begin(); it != m_elements.end();) {
141 if (a11yContainer.call<
bool>(
"contains", it.value()))
142 it = m_elements.erase(it);
146 getElementContainer(window).call<
void>(
"replaceChildren");
147 getDescribedByContainer(window).call<
void>(
"replaceChildren");
150void QWasmAccessibility::enableAccessibility()
155 Q_ASSERT(!isActive());
157 for (
const auto& [key, value] : m_enableButtons) {
158 const auto &[element, callback] = value;
160 if (
auto wasmWindow = QWasmWindow::fromWindow(key))
161 wasmWindow->onAccessibilityEnable();
162 populateAccessibilityTree(key->accessibleRoot());
163 element[
"parentElement"].call<
void>(
"removeChild", element);
165 m_enableButtons.clear();
168bool QWasmAccessibility::isWindowNode(QAccessibleInterface *iface)
170 return (iface && !getWindow(iface->parent()) && getWindow(iface));
173emscripten::val QWasmAccessibility::getDescribedByContainer(QWindow *window)
175 const QWasmWindow *wasmWindow = QWasmWindow::fromWindow(window);
176 return wasmWindow ? wasmWindow->a11yDescribedByContainer() : emscripten::val::undefined();
179emscripten::val QWasmAccessibility::getDescribedByContainer(QAccessibleInterface *iface)
181 return getDescribedByContainer(getWindow(iface));
184emscripten::val QWasmAccessibility::getElementContainer(QWindow *window)
186 const QWasmWindow *wasmWindow = QWasmWindow::fromWindow(window);
187 return wasmWindow ? wasmWindow->a11yElementContainer() : emscripten::val::undefined();
190emscripten::val QWasmAccessibility::getElementContainer(QAccessibleInterface *iface)
197 if (!getWindow(iface))
198 return emscripten::val::undefined();
200 if (isWindowNode(iface))
201 return emscripten::val::undefined();
203 if (isWindowNode(iface->parent()))
204 return getElementContainer(getWindow(iface->parent()));
207 return getHtmlElement(iface->parent());
210QWindow *QWasmAccessibility::getWindow(QAccessibleInterface *iface)
215 QWindow *window = iface->window();
217 if (!window && iface->parent())
218 window = iface->parent()->window();
222emscripten::val QWasmAccessibility::getDocument(
const emscripten::val &container)
224 if (container.isUndefined())
225 return emscripten::val::global(
"document");
226 return container[
"ownerDocument"];
229void QWasmAccessibility::setAttribute(emscripten::val element,
const std::string &attr,
230 const std::string &val)
233 element.call<
void>(
"setAttribute", attr, val);
235 element.call<
void>(
"removeAttribute", attr);
238void QWasmAccessibility::setAttribute(emscripten::val element,
const std::string &attr,
241 setAttribute(element, attr, std::string(val));
244void QWasmAccessibility::setAttribute(emscripten::val element,
const std::string &attr,
bool val)
247 element.call<
void>(
"setAttribute", attr, val);
249 element.call<
void>(
"removeAttribute", attr);
252void QWasmAccessibility::setProperty(emscripten::val element,
const std::string &property,
253 const std::string &val)
255 element.set(property, val);
258void QWasmAccessibility::setProperty(emscripten::val element,
const std::string &property,
const char *val)
260 setProperty(element, property, std::string(val));
263void QWasmAccessibility::setProperty(emscripten::val element,
const std::string &property,
bool val)
265 element.set(property, val);
268void QWasmAccessibility::setNamedAttribute(QAccessibleInterface *iface,
const std::string &attribute, QAccessible::Text text)
270 const emscripten::val element = getHtmlElement(iface);
271 setAttribute(element, attribute, iface->text(text).toStdString());
273void QWasmAccessibility::setNamedProperty(QAccessibleInterface *iface,
const std::string &property, QAccessible::Text text)
275 const emscripten::val element = getHtmlElement(iface);
276 setProperty(element, property, iface->text(text).toStdString());
279void QWasmAccessibility::addEventListener(QAccessibleInterface *iface, emscripten::val element,
const char *eventType)
281 element.set(
"qtA11yId", QAccessible::uniqueId(iface));
282 element.call<
void>(
"addEventListener", emscripten::val(eventType),
283 QWasmSuspendResumeControl::get()->jsEventHandlerAt(m_eventHandlerIndex),
287void QWasmAccessibility::sendEvent(QAccessibleInterface *iface, QAccessible::Event eventType)
289 if (iface->object()) {
290 QAccessibleEvent event(iface->object(), eventType);
291 handleUpdateByInterfaceRole(&event);
293 QAccessibleEvent event(iface, eventType);
294 handleUpdateByInterfaceRole(&event);
298emscripten::val QWasmAccessibility::createHtmlElement(QAccessibleInterface *iface)
303 emscripten::val container = getElementContainer(iface);
307 emscripten::val document = getDocument(container);
312 emscripten::val element = [
this, iface, document] {
314 emscripten::val element = emscripten::val::undefined();
316 switch (iface->role()) {
318 case QAccessible::Button: {
319 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
320 addEventListener(iface, element,
"click");
323 case QAccessible::Grouping:
324 case QAccessible::CheckBox: {
330 emscripten::val checkbox = emscripten::val::undefined();
331 if (iface->role() == QAccessible::CheckBox) {
332 checkbox = document.call<emscripten::val>(
"createElement", std::string(
"input"));
333 setAttribute(checkbox,
"type",
"checkbox");
336 setProperty(checkbox,
"checked", iface->state().checked);
337 setProperty(checkbox,
"indeterminate", iface->state().checkStateMixed);
338 addEventListener(iface, checkbox,
"change");
341 if (iface->childCount() > 0 || iface->role() == QAccessible::Grouping) {
342 auto label = document.call<emscripten::val>(
"createElement", std::string(
"span"));
344 const std::string id =
"lbid_" + std::to_string(QAccessible::uniqueId(iface));
345 setAttribute(label,
"id", id);
347 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
348 element.call<
void>(
"appendChild", label);
350 setAttribute(element,
"role",
"group");
351 setAttribute(element,
"aria-labelledby", id);
353 if (!checkbox.isUndefined()) {
354 element.call<
void>(
"appendChild", checkbox);
355 addEventListener(iface, checkbox,
"focus");
362 case QAccessible::Switch: {
363 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
364 setAttribute(element,
"type",
"button");
365 setAttribute(element,
"role",
"switch");
366 if (iface->state().checked)
367 setAttribute(element,
"aria-checked",
"true");
369 setAttribute(element,
"aria-checked",
"false");
370 addEventListener(iface, element,
"click");
373 case QAccessible::RadioButton: {
374 element = document.call<emscripten::val>(
"createElement", std::string(
"input"));
375 setAttribute(element,
"type",
"radio");
376 setProperty(element,
"checked", iface->state().checked);
377 setProperty(element,
"name",
"buttonGroup");
378 addEventListener(iface, element,
"change");
381 case QAccessible::Dial: {
384 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
385 setAttribute(element,
"tabindex",
"0");
386 setAttribute(element,
"role",
"slider");
387 if (QAccessibleValueInterface *valueInterface = iface->valueInterface()) {
388 setAttribute(element,
"aria-valuemin",
389 valueInterface->minimumValue().toString().toStdString());
390 setAttribute(element,
"aria-valuemax",
391 valueInterface->maximumValue().toString().toStdString());
392 setAttribute(element,
"aria-valuenow",
393 valueInterface->currentValue().toString().toStdString());
398 case QAccessible::SpinBox: {
399 element = document.call<emscripten::val>(
"createElement", std::string(
"input"));
400 setAttribute(element,
"type",
"number");
401 if (QAccessibleValueInterface *valueInterface = iface->valueInterface()) {
402 setAttribute(element,
"min", valueInterface->minimumValue().toString().toStdString());
403 setAttribute(element,
"max", valueInterface->maximumValue().toString().toStdString());
404 setAttribute(element,
"step",
405 valueInterface->minimumStepSize().toString().toStdString());
406 setProperty(element,
"value", valueInterface->currentValue().toString().toStdString());
408 addEventListener(iface, element,
"change");
411 case QAccessible::Slider: {
414 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
415 setAttribute(element,
"tabindex",
"0");
416 setAttribute(element,
"role",
"slider");
417 if (QAccessibleValueInterface *valueInterface = iface->valueInterface()) {
418 setAttribute(element,
"aria-valuemin",
419 valueInterface->minimumValue().toString().toStdString());
420 setAttribute(element,
"aria-valuemax",
421 valueInterface->maximumValue().toString().toStdString());
422 setAttribute(element,
"aria-valuenow",
423 valueInterface->currentValue().toString().toStdString());
425 setHtmlElementOrientation(element, iface);
428 case QAccessible::PageTabList:{
429 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
430 setAttribute(element,
"role",
"tablist");
431 setHtmlElementOrientation(element, iface);
433 m_elements[QAccessible::uniqueId(iface)] = element;
435 for (
int i = 0; i < iface->childCount(); ++i)
436 createHtmlElement(iface->child(i));
440 case QAccessible::PageTab:{
441 const QString text = iface->text(QAccessible::Name);
442 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
443 setAttribute(element,
"role",
"tab");
444 setAttribute(element,
"title", text.toStdString());
445 addEventListener(iface, element,
"click");
448 case QAccessible::ScrollBar: {
451 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
452 setAttribute(element,
"tabindex",
"0");
453 setAttribute(element,
"role",
"scrollbar");
454 if (QAccessibleValueInterface *valueInterface = iface->valueInterface()) {
455 setAttribute(element,
"aria-valuemin",
456 valueInterface->minimumValue().toString().toStdString());
457 setAttribute(element,
"aria-valuemax",
458 valueInterface->maximumValue().toString().toStdString());
459 setAttribute(element,
"aria-valuenow",
460 valueInterface->currentValue().toString().toStdString());
462 setHtmlElementOrientation(element, iface);
463 addEventListener(iface, element,
"change");
466 case QAccessible::StaticText: {
467 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
469 case QAccessible::Dialog: {
470 element = document.call<emscripten::val>(
"createElement", std::string(
"dialog"));
472 case QAccessible::ToolBar:{
473 const QString text = iface->text(QAccessible::Name);
474 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
475 setAttribute(element,
"role",
"toolbar");
476 setAttribute(element,
"title", text.toStdString());
477 setHtmlElementOrientation(element, iface);
478 addEventListener(iface, element,
"click");
480 case QAccessible::MenuItem:
481 case QAccessible::ButtonDropDown:
482 case QAccessible::ButtonMenu: {
483 const QString text = iface->text(QAccessible::Name);
484 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
485 setAttribute(element,
"role",
"menuitem");
486 setAttribute(element,
"title", text.toStdString());
487 addEventListener(iface, element,
"click");
489 case QAccessible::MenuBar:
490 case QAccessible::PopupMenu: {
491 const QString text = iface->text(QAccessible::Name);
492 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
493 setAttribute(element,
"role",
494 iface->role() == QAccessible::PopupMenu ?
"menu" :
"menubar");
495 setAttribute(element,
"title", text.toStdString());
496 setHtmlElementOrientation(element, iface);
497 m_elements[QAccessible::uniqueId(iface)] = element;
501 for (
int i = 0; i < iface->childCount(); ++i)
502 createHtmlElement(iface->child(i));
504 case QAccessible::EditableText: {
505 element = document.call<emscripten::val>(
"createElement", std::string(
"input"));
506 setAttribute(element,
"type",
"text");
507 setAttribute(element,
"contenteditable",
"true");
508 setAttribute(element,
"readonly", iface->state().readOnly);
509 setAttribute(element,
"autocorrect",
"off");
510 setProperty(element,
"inputMode",
"text");
512 case QAccessible::List: {
513 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
514 setAttribute(element,
"tabindex",
"0");
515 setAttribute(element,
"role",
"list");
517 case QAccessible::ListItem: {
518 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
519 setAttribute(element,
"tabindex",
"0");
520 setAttribute(element,
"role",
"listitem");
522 case QAccessible::ProgressBar: {
523 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
524 setAttribute(element,
"tabindex",
"0");
525 setAttribute(element,
"role",
"progressbar");
526 setAttribute(element,
"aria-live",
"polite");
527 if (
auto valueInterface = iface->valueInterface()) {
528 const auto name = iface->text(QAccessible::Name).toStdString();
529 const auto min = valueInterface->minimumValue().toInt();
530 const auto max = valueInterface->maximumValue().toInt();
531 const auto cur = valueInterface->currentValue().toInt();
534 const auto totalSteps = qint64(max) - min;
535 const auto progress = (totalSteps == 0) ? 0 :
static_cast<
int>((qint64(cur) - min) * 100.0 / totalSteps);
538 setAttribute(element,
"aria-valuenow",
"0");
540 setAttribute(element,
"aria-valuenow",
"100");
542 setAttribute(element,
"aria-valuenow", std::to_string(progress));
543 setAttribute(element,
"aria-valuemin",
"0");
544 setAttribute(element,
"aria-valuemax",
"100");
545 setAttribute(element,
"aria-label", name);
549 qCDebug(lcQpaAccessibility) <<
"TODO: createHtmlElement() handle" << iface->role();
550 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
553 addEventListener(iface, element,
"focus");
558 m_elements[QAccessible::uniqueId(iface)] = element;
560 setHtmlElementGeometry(iface);
561 setHtmlElementDisabled(iface);
562 setHtmlElementVisibility(iface, !iface->state().invisible);
563 handleIdentifierUpdate(iface);
564 handleDescriptionChanged(iface);
565 sendEvent(iface, QAccessible::NameChanged);
569 for (
int i = 0; i < iface->childCount(); ++i) {
570 if (!getHtmlElement(iface->child(i)).isUndefined())
571 linkToParent(iface->child(i));
577void QWasmAccessibility::destroyHtmlElement(QAccessibleInterface *iface)
580 qCDebug(lcQpaAccessibility) <<
"TODO destroyHtmlElement";
583emscripten::val QWasmAccessibility::getHtmlElement(QAccessibleInterface *iface)
586 return emscripten::val::undefined();
587 return m_elements.value(QAccessible::uniqueId(iface), emscripten::val::undefined());
590void QWasmAccessibility::repairLinks(QAccessibleInterface *iface)
595 for (
int i = 0; i < iface->childCount(); ++i) {
596 const auto elementI = getHtmlElement(iface->child(i));
597 const auto containerI = getElementContainer(iface->child(i));
599 if (!elementI.isUndefined() &&
600 !containerI.isUndefined() &&
601 !elementI[
"parentElement"].isUndefined() &&
602 !elementI[
"parentElement"].isNull() &&
603 elementI[
"parentElement"] != containerI) {
609 for (
int i = 0; i < iface->childCount(); ++i) {
610 const auto elementI = getHtmlElement(iface->child(i));
611 const auto containerI = getElementContainer(iface->child(i));
612 if (!elementI.isUndefined() && !containerI.isUndefined())
613 containerI.call<
void>(
"appendChild", elementI);
618void QWasmAccessibility::linkToParent(QAccessibleInterface *iface)
620 emscripten::val element = getHtmlElement(iface);
621 emscripten::val container = getElementContainer(iface);
623 if (container.isUndefined() || element.isUndefined())
627 const auto activeElementBefore = emscripten::val::take_ownership(
628 getActiveElement_js(emscripten::val::undefined().as_handle()));
631 repairLinks(iface->parent());
633 emscripten::val next = emscripten::val::undefined();
634 const int thisIndex = iface->parent()->indexOfChild(iface);
635 if (thisIndex >= 0) {
636 Q_ASSERT(thisIndex < iface->parent()->childCount());
637 for (
int i = thisIndex + 1; i < iface->parent()->childCount(); ++i) {
638 const auto elementI = getHtmlElement(iface->parent()->child(i));
639 if (!elementI.isUndefined() &&
640 elementI[
"parentElement"] == container) {
645 if (next.isUndefined())
646 container.call<
void>(
"appendChild", element);
648 container.call<
void>(
"insertBefore", element, next);
650 const auto activeElementAfter = emscripten::val::take_ownership(
651 getActiveElement_js(emscripten::val::undefined().as_handle()));
652 if (activeElementBefore != activeElementAfter) {
653 if (!activeElementBefore.isUndefined() && !activeElementBefore.isNull())
654 activeElementBefore.call<
void>(
"focus");
658void QWasmAccessibility::setHtmlElementVisibility(QAccessibleInterface *iface,
bool visible)
660 emscripten::val element = getHtmlElement(iface);
661 setAttribute(element,
"inert", !visible);
664void QWasmAccessibility::setHtmlElementGeometry(QAccessibleInterface *iface)
666 const emscripten::val element = getHtmlElement(iface);
668 QRect windowGeometry = iface->rect();
669 if (iface->parent()) {
673 const QRect parentRect = iface->parent()->rect();
674 const QRect thisRect = iface->rect();
675 const QRect result(thisRect.topLeft() - parentRect.topLeft(), thisRect.size());
676 windowGeometry = result;
680 Q_ASSERT(!getWindow(iface));
682 setHtmlElementGeometry(element, windowGeometry);
685void QWasmAccessibility::setHtmlElementGeometry(emscripten::val element, QRect geometry)
689 emscripten::val style = element[
"style"];
690 style.set(
"position", std::string(
"absolute"));
691 style.set(
"z-index", std::string(
"-1"));
693 style.set(
"left", std::to_string(geometry.x()) +
"px");
694 style.set(
"top", std::to_string(geometry.y()) +
"px");
695 style.set(
"width", std::to_string(geometry.width()) +
"px");
696 style.set(
"height", std::to_string(geometry.height()) +
"px");
699void QWasmAccessibility::setHtmlElementFocus(QAccessibleInterface *iface)
701 const auto element = getHtmlElement(iface);
702 element.call<
void>(
"focus");
705void QWasmAccessibility::setHtmlElementDisabled(QAccessibleInterface *iface)
707 auto element = getHtmlElement(iface);
708 setAttribute(element,
"aria-disabled", iface->state().disabled);
711void QWasmAccessibility::setHtmlElementOrientation(emscripten::val element, QAccessibleInterface *iface)
714 if (QAccessibleAttributesInterface *attributesIface = iface->attributesInterface()) {
715 const QVariant orientationVariant =
716 attributesIface->attributeValue(QAccessible::Attribute::Orientation);
717 if (orientationVariant.isValid()) {
718 Q_ASSERT(orientationVariant.canConvert<Qt::Orientation>());
719 const Qt::Orientation orientation = orientationVariant.value<Qt::Orientation>();
720 const std::string value = orientation == Qt::Horizontal ?
"horizontal" :
"vertical";
721 setAttribute(element,
"aria-orientation", value);
726void QWasmAccessibility::handleListItemUpdate(QAccessibleEvent *event)
728 auto iface = event->accessibleInterface();
730 switch (event->type()) {
731 case QAccessible::NameChanged: {
733 setNamedProperty(iface,
"innerText", QAccessible::Name);
736 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleListItemUpdate for event" << event->type();
741void QWasmAccessibility::handleDialUpdate(QAccessibleEvent *event)
743 switch (event->type()) {
744 case QAccessible::ObjectCreated:
745 case QAccessible::StateChanged: {
747 case QAccessible::Focus:
748 case QAccessible::NameChanged: {
749 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
751 case QAccessible::ValueChanged: {
752 QAccessibleInterface *accessible = event->accessibleInterface();
753 if (QAccessibleValueInterface *valueInterface = accessible->valueInterface()) {
754 setAttribute(getHtmlElement(accessible),
"aria-valuenow",
755 valueInterface->currentValue().toString().toStdString());
759 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleDialUpdate for event" << event->type();
764void QWasmAccessibility::handleStaticTextUpdate(QAccessibleEvent *event)
766 switch (event->type()) {
767 case QAccessible::NameChanged: {
769 setNamedProperty(event->accessibleInterface(),
"innerText", QAccessible::Name);
772 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleStaticTextUpdate for event" << event->type();
777void QWasmAccessibility::handleLineEditUpdate(QAccessibleEvent *event)
779 switch (event->type()) {
780 case QAccessible::StateChanged: {
781 auto iface = event->accessibleInterface();
782 auto element = getHtmlElement(iface);
783 setAttribute(element,
"readonly", iface->state().readOnly);
784 if (iface->state().passwordEdit)
785 setProperty(element,
"type",
"password");
787 setProperty(element,
"type",
"text");
789 case QAccessible::ValueChanged:
790 case QAccessible::NameChanged: {
791 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
793 case QAccessible::ObjectShow:
794 case QAccessible::Focus: {
795 auto iface = event->accessibleInterface();
796 auto element = getHtmlElement(iface);
797 if (!element.isUndefined()) {
798 setAttribute(element,
"readonly", iface->state().readOnly);
799 if (iface->state().passwordEdit)
800 setProperty(element,
"type",
"password");
802 setProperty(element,
"type",
"text");
804 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
806 case QAccessible::TextRemoved:
807 case QAccessible::TextInserted: {
808 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
810 case QAccessible::TextCaretMoved: {
812 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
815 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleLineEditUpdate for event" << event->type();
820void QWasmAccessibility::handleEventFromHtmlElement(
const emscripten::val event)
822 const emscripten::val target = event[
"target"];
823 if (target.isNull() || target.isUndefined() || target[
"qtA11yId"].isUndefined())
827 QAccessibleInterface *iface =
828 QAccessible::accessibleInterface(target[
"qtA11yId"].as<QAccessible::Id>());
829 if (!iface || !iface->isValid())
832 const auto eventType = QString::fromStdString(event[
"type"].as<std::string>());
833 const auto& actionNames = QAccessibleBridgeUtils::effectiveActionNames(iface);
834 bool handled =
false;
836 if (eventType ==
"blur") {
838 }
else if (eventType ==
"keydown") {
840 }
else if (eventType ==
"keyup") {
842 }
else if (eventType ==
"focus") {
843 if (actionNames.contains(QAccessibleActionInterface::setFocusAction()))
844 iface->actionInterface()->doAction(QAccessibleActionInterface::setFocusAction());
845 }
else if (eventType ==
"click") {
847 if (actionNames.contains(QAccessibleActionInterface::pressAction()))
848 iface->actionInterface()->doAction(QAccessibleActionInterface::pressAction());
849 else if (actionNames.contains(QAccessibleActionInterface::showMenuAction()))
850 iface->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
851 }
else if (eventType ==
"change") {
855 if (actionNames.contains(QAccessibleActionInterface::pressAction()))
856 iface->actionInterface()->doAction(QAccessibleActionInterface::pressAction());
857 else if (actionNames.contains(QAccessibleActionInterface::toggleAction()))
858 iface->actionInterface()->doAction(QAccessibleActionInterface::toggleAction());
860 qCDebug(lcQpaAccessibility) <<
"Unknown event" << eventType;
864 event.call<
void>(
"preventDefault");
865 event.call<
void>(
"stopPropagation");
869void QWasmAccessibility::handleButtonUpdate(QAccessibleEvent *event)
871 switch (event->type()) {
872 case QAccessible::Focus:
873 case QAccessible::NameChanged: {
874 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
877 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleCheckBoxUpdate for event" << event->type();
882void QWasmAccessibility::handleCheckBoxUpdate(QAccessibleEvent *event)
884 switch (event->type()) {
885 case QAccessible::Focus:
886 case QAccessible::NameChanged: {
887 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
889 case QAccessible::StateChanged: {
890 QAccessibleInterface *accessible = event->accessibleInterface();
891 const emscripten::val element = getHtmlElement(accessible);
892 setProperty(element,
"checked", accessible->state().checked);
893 setProperty(element,
"indeterminate", accessible->state().checkStateMixed);
896 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleCheckBoxUpdate for event" << event->type();
901void QWasmAccessibility::handleGroupBoxUpdate(QAccessibleEvent *event)
903 QAccessibleInterface *iface = event->accessibleInterface();
905 emscripten::val parent = getHtmlElement(iface);
906 emscripten::val label = parent[
"children"][0];
907 emscripten::val checkbox = emscripten::val::undefined();
908 if (iface->role() == QAccessible::CheckBox)
909 checkbox = parent[
"children"][1];
911 switch (event->type()) {
912 case QAccessible::Focus:
913 case QAccessible::NameChanged: {
914 setProperty(label,
"innerText", iface->text(QAccessible::Name).toStdString());
915 if (!checkbox.isUndefined())
916 setAttribute(checkbox,
"aria-label", iface->text(QAccessible::Name).toStdString());
918 case QAccessible::StateChanged: {
919 if (!checkbox.isUndefined()) {
920 setProperty(checkbox,
"checked", iface->state().checked);
921 setProperty(checkbox,
"indeterminate", iface->state().checkStateMixed);
925 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleCheckBoxUpdate for event" << event->type();
930void QWasmAccessibility::handleSwitchUpdate(QAccessibleEvent *event)
932 switch (event->type()) {
933 case QAccessible::Focus:
934 case QAccessible::NameChanged: {
936 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
938 case QAccessible::StateChanged: {
939 QAccessibleInterface *accessible = event->accessibleInterface();
940 const emscripten::val element = getHtmlElement(accessible);
941 if (accessible->state().checked)
942 setAttribute(element,
"aria-checked",
"true");
944 setAttribute(element,
"aria-checked",
"false");
947 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleSwitchUpdate for event" << event->type();
952void QWasmAccessibility::handleToolUpdate(QAccessibleEvent *event)
954 QAccessibleInterface *iface = event->accessibleInterface();
955 QString text = iface->text(QAccessible::Name);
956 QString desc = iface->text(QAccessible::Description);
957 switch (event->type()) {
958 case QAccessible::NameChanged:
959 case QAccessible::StateChanged:{
960 const emscripten::val element = getHtmlElement(iface);
961 setAttribute(element,
"title", text.toStdString());
964 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleToolUpdate for event" << event->type();
968void QWasmAccessibility::handleMenuUpdate(QAccessibleEvent *event)
970 QAccessibleInterface *iface = event->accessibleInterface();
971 QString text = iface->text(QAccessible::Name);
972 QString desc = iface->text(QAccessible::Description);
973 switch (event->type()) {
974 case QAccessible::Focus:
975 case QAccessible::NameChanged:
976 case QAccessible::MenuStart :
977 case QAccessible::StateChanged:{
978 const emscripten::val element = getHtmlElement(iface);
979 setAttribute(element,
"title", text.toStdString());
981 case QAccessible::PopupMenuStart: {
982 if (iface->childCount() > 0) {
983 const auto childElement = getHtmlElement(iface->child(0));
984 childElement.call<
void>(
"focus");
988 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleMenuUpdate for event" << event->type();
992void QWasmAccessibility::handleDialogUpdate(QAccessibleEvent *event) {
994 switch (event->type()) {
995 case QAccessible::NameChanged:
996 case QAccessible::Focus:
997 case QAccessible::DialogStart:
998 case QAccessible::StateChanged: {
999 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1002 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleLineEditUpdate for event" << event->type();
1007void QWasmAccessibility::populateAccessibilityTree(QAccessibleInterface *iface)
1014 const QWindow *window1 = getWindow(iface);
1015 const QWindow *window0 = (iface->parent()) ? getWindow(iface->parent()) :
nullptr;
1017 if (window1 && window0 == window1) {
1019 bool exists = !getHtmlElement(iface).isUndefined();
1021 exists = !createHtmlElement(iface).isUndefined();
1024 linkToParent(iface);
1025 setHtmlElementVisibility(iface, !iface->state().invisible);
1026 setHtmlElementGeometry(iface);
1027 setHtmlElementDisabled(iface);
1028 handleIdentifierUpdate(iface);
1029 handleDescriptionChanged(iface);
1030 sendEvent(iface, QAccessible::NameChanged);
1033 for (
int i = 0; i < iface->childCount(); ++i)
1034 populateAccessibilityTree(iface->child(i));
1037void QWasmAccessibility::handleRadioButtonUpdate(QAccessibleEvent *event)
1039 switch (event->type()) {
1040 case QAccessible::Focus:
1041 case QAccessible::NameChanged: {
1042 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1044 case QAccessible::StateChanged: {
1045 QAccessibleInterface *accessible = event->accessibleInterface();
1046 const emscripten::val element = getHtmlElement(accessible);
1047 setProperty(element,
"checked", accessible->state().checked);
1050 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleRadioButtonUpdate for event" << event->type();
1055void QWasmAccessibility::handleSpinBoxUpdate(QAccessibleEvent *event)
1057 switch (event->type()) {
1058 case QAccessible::ObjectCreated:
1059 case QAccessible::StateChanged: {
1061 case QAccessible::Focus:
1062 case QAccessible::NameChanged: {
1063 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1065 case QAccessible::ValueChanged: {
1066 QAccessibleInterface *accessible = event->accessibleInterface();
1067 if (QAccessibleValueInterface *valueInterface = accessible->valueInterface()) {
1068 setProperty(getHtmlElement(accessible),
"value",
1069 valueInterface->currentValue().toString().toStdString());
1073 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleSpinBoxUpdate for event" << event->type();
1078void QWasmAccessibility::handleSliderUpdate(QAccessibleEvent *event)
1080 switch (event->type()) {
1081 case QAccessible::ObjectCreated:
1082 case QAccessible::StateChanged: {
1084 case QAccessible::Focus:
1085 case QAccessible::NameChanged: {
1086 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1088 case QAccessible::ValueChanged: {
1089 QAccessibleInterface *accessible = event->accessibleInterface();
1090 if (QAccessibleValueInterface *valueInterface = accessible->valueInterface()) {
1091 setAttribute(getHtmlElement(accessible),
"aria-valuenow",
1092 valueInterface->currentValue().toString().toStdString());
1096 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleSliderUpdate for event" << event->type();
1101void QWasmAccessibility::handleScrollBarUpdate(QAccessibleEvent *event)
1103 switch (event->type()) {
1104 case QAccessible::Focus:
1105 case QAccessible::NameChanged: {
1106 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1108 case QAccessible::ValueChanged: {
1109 QAccessibleInterface *accessible = event->accessibleInterface();
1110 if (QAccessibleValueInterface *valueInterface = accessible->valueInterface()) {
1111 setAttribute(getHtmlElement(accessible),
"aria-valuenow",
1112 valueInterface->currentValue().toString().toStdString());
1116 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleScrollBarUpdate for event" << event->type();
1121void QWasmAccessibility::handlePageTabUpdate(QAccessibleEvent *event)
1123 switch (event->type()) {
1124 case QAccessible::NameChanged: {
1125 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1127 case QAccessible::Focus: {
1128 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1131 qCDebug(lcQpaAccessibility) <<
"TODO: implement handlePageTabUpdate for event" << event->type();
1136void QWasmAccessibility::handlePageTabListUpdate(QAccessibleEvent *event)
1138 switch (event->type()) {
1139 case QAccessible::NameChanged: {
1140 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1142 case QAccessible::Focus: {
1143 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1146 qCDebug(lcQpaAccessibility) <<
"TODO: implement handlePageTabListUpdate for event" << event->type();
1151void QWasmAccessibility::handleIdentifierUpdate(QAccessibleInterface *iface)
1155 const emscripten::val element = getHtmlElement(iface);
1157 QString id = iface->text(QAccessible::Identifier).replace(
" ",
"_");
1159 id = QStringLiteral(
"qt-a11y-") + QString::number(QAccessible::uniqueId(iface));
1160 setAttribute(element,
"id", id.toStdString());
1163void QWasmAccessibility::handleDescriptionChanged(QAccessibleInterface *iface)
1167 const auto desc = iface->text(QAccessible::Description).toStdString();
1168 auto element = getHtmlElement(iface);
1169 auto container = getDescribedByContainer(iface);
1170 if (element.isUndefined() || container.isUndefined())
1173 auto describedBy = element[
"qtA11yDescribedBy"];
1175 if (!describedBy.isUndefined())
1176 describedBy.call<
void>(
"remove");
1177 element.set(
"qtA11yDescribedBy", emscripten::val::undefined());
1178 setAttribute(element,
"aria-describedby",
"");
1180 const std::string id =
"dbid_" + std::to_string(QAccessible::uniqueId(iface));
1181 if (describedBy.isUndefined()) {
1182 describedBy = getDocument(container).call<emscripten::val>(
"createElement",
1184 container.call<
void>(
"appendChild", describedBy);
1185 element.set(
"qtA11yDescribedBy", describedBy);
1187 setAttribute(describedBy,
"id", id);
1188 setAttribute(describedBy,
"inert",
true);
1189 setAttribute(element,
"aria-describedby", id);
1190 setProperty(describedBy,
"innerText", desc);
1194void QWasmAccessibility::handleAnnouncement(QAccessibleInterface *iface,
1195 QAccessibleAnnouncementEvent *announcementEvent)
1197 emscripten::val element = getHtmlElement(iface);
1198 if (element[
"ariaNotify"].isUndefined())
1201 const std::string message = announcementEvent->message().toStdString();
1202 const std::string prio(announcementEvent->politeness()
1203 == QAccessible::AnnouncementPoliteness::Assertive
1206 emscripten::val options = emscripten::val::object();
1207 options.set(
"priority", prio);
1208 element.call<
void>(
"ariaNotify", message, options);
1211void QWasmAccessibility::handleProgressBarUpdate(QAccessibleEvent *event)
1213 auto iface = event->accessibleInterface();
1214 auto valueInterface = iface->valueInterface();
1215 auto element = getHtmlElement(iface);
1217 switch (event->type()) {
1218 case QAccessible::Focus:
1219 case QAccessible::NameChanged: {
1220 const auto name = iface->text(QAccessible::Name).toStdString();
1221 setAttribute(element,
"aria-label", name);
1223 case QAccessible::ValueChanged: {
1224 if (valueInterface) {
1225 const auto min = valueInterface->minimumValue().toInt();
1226 const auto max = valueInterface->maximumValue().toInt();
1227 const auto cur = valueInterface->currentValue().toInt();
1230 const auto totalSteps = qint64(max) - min;
1231 const auto progress = (totalSteps == 0) ? 0 :
static_cast<
int>((qint64(cur) - min) * 100.0 / totalSteps);
1234 setAttribute(element,
"aria-valuenow",
"0");
1235 else if (cur >= max)
1236 setAttribute(element,
"aria-valuenow",
"100");
1238 setAttribute(element,
"aria-valuenow", std::to_string(progress));
1246void QWasmAccessibility::createObject(QAccessibleInterface *iface)
1248 if (getHtmlElement(iface).isUndefined())
1249 createHtmlElement(iface);
1252void QWasmAccessibility::removeObject(QAccessible::Id id)
1257 const auto it = m_elements.find(id);
1258 if (it == m_elements.end())
1261 const emscripten::val element = it.value();
1262 if (!element[
"qtA11yDescribedBy"].isUndefined())
1263 element[
"qtA11yDescribedBy"].call<
void>(
"remove");
1264 element.call<
void>(
"remove");
1265 m_elements.erase(it);
1268void QWasmAccessibility::unlinkParentForChildren(QAccessibleInterface *iface)
1270 auto element = getHtmlElement(iface);
1271 if (!element.isUndefined()) {
1272 auto oldContainer = element[
"parentElement"];
1273 auto newContainer = getElementContainer(iface);
1274 if (!oldContainer.isUndefined() &&
1275 !oldContainer.isNull() &&
1276 oldContainer != newContainer) {
1277 oldContainer.call<
void>(
"removeChild", element);
1280 for (
int i = 0; i < iface->childCount(); ++i)
1281 unlinkParentForChildren(iface->child(i));
1284void QWasmAccessibility::relinkParentForChildren(QAccessibleInterface *iface)
1286 auto element = getHtmlElement(iface);
1287 if (!element.isUndefined()) {
1288 if (element[
"parentElement"].isUndefined() ||
1289 element[
"parentElement"].isNull()) {
1290 linkToParent(iface);
1293 for (
int i = 0; i < iface->childCount(); ++i)
1294 relinkParentForChildren(iface->child(i));
1297void QWasmAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
1299 if (handleUpdateByEventType(event))
1300 handleUpdateByInterfaceRole(event);
1303bool QWasmAccessibility::handleUpdateByEventType(QAccessibleEvent *event)
1308 QAccessibleInterface *iface = event->accessibleInterface();
1310 qWarning() <<
"handleUpdateByEventType with null a11y interface" << event->type() << event->object();
1315 switch (event->type()) {
1316 case QAccessible::ObjectCreated:
1321 case QAccessible::ObjectDestroyed:
1323 removeObject(event->uniqueId());
1326 case QAccessible::ObjectShow:
1327 createObject(iface);
1330 case QAccessible::ParentChanged:
1331 unlinkParentForChildren(iface);
1332 relinkParentForChildren(iface);
1339 if (getHtmlElement(iface).isUndefined())
1344 switch (event->type()) {
1345 case QAccessible::Announcement:
1346 handleAnnouncement(iface,
static_cast<QAccessibleAnnouncementEvent *>(event));
1349 case QAccessible::StateChanged: {
1350 QAccessibleStateChangeEvent *stateChangeEvent = (QAccessibleStateChangeEvent *)event;
1351 if (stateChangeEvent->changedStates().disabled)
1352 setHtmlElementDisabled(iface);
1355 case QAccessible::DescriptionChanged:
1356 handleDescriptionChanged(iface);
1359 case QAccessible::Focus:
1362 setHtmlElementGeometry(iface);
1363 setHtmlElementFocus(iface);
1366 case QAccessible::IdentifierChanged:
1367 handleIdentifierUpdate(iface);
1370 case QAccessible::ObjectShow:
1371 linkToParent(iface);
1372 setHtmlElementVisibility(iface,
true);
1375 setHtmlElementGeometry(iface);
1376 sendEvent(iface, QAccessible::NameChanged);
1379 case QAccessible::ObjectHide:
1381 setHtmlElementVisibility(iface,
false);
1384 case QAccessible::LocationChanged:
1385 setHtmlElementGeometry(iface);
1396void QWasmAccessibility::handleUpdateByInterfaceRole(QAccessibleEvent *event)
1401 QAccessibleInterface *iface = event->accessibleInterface();
1403 qWarning() <<
"handleUpdateByInterfaceRole with null a11y interface" << event->type() << event->object();
1409 switch (iface->role()) {
1410 case QAccessible::List:
1412 case QAccessible::ListItem:
1413 handleListItemUpdate(event);
1415 case QAccessible::StaticText:
1416 handleStaticTextUpdate(event);
1418 case QAccessible::Button:
1419 handleButtonUpdate(event);
1421 case QAccessible::CheckBox:
1422 if (iface->childCount() > 0)
1423 handleGroupBoxUpdate(event);
1425 handleCheckBoxUpdate(event);
1427 case QAccessible::Switch:
1428 handleSwitchUpdate(event);
1430 case QAccessible::EditableText:
1431 handleLineEditUpdate(event);
1433 case QAccessible::Dialog:
1434 handleDialogUpdate(event);
1436 case QAccessible::MenuItem:
1437 case QAccessible::MenuBar:
1438 case QAccessible::PopupMenu:
1439 handleMenuUpdate(event);
1441 case QAccessible::ToolBar:
1442 case QAccessible::ButtonMenu:
1443 handleToolUpdate(event);
1445 case QAccessible::RadioButton:
1446 handleRadioButtonUpdate(event);
1448 case QAccessible::Dial:
1449 handleDialUpdate(event);
1451 case QAccessible::SpinBox:
1452 handleSpinBoxUpdate(event);
1454 case QAccessible::Slider:
1455 handleSliderUpdate(event);
1457 case QAccessible::PageTab:
1458 handlePageTabUpdate(event);
1460 case QAccessible::PageTabList:
1461 handlePageTabListUpdate(event);
1463 case QAccessible::ScrollBar:
1464 handleScrollBarUpdate(event);
1466 case QAccessible::Grouping:
1467 handleGroupBoxUpdate(event);
1469 case QAccessible::ProgressBar:
1470 handleProgressBarUpdate(event);
1473 qCDebug(lcQpaAccessibility) <<
"TODO: implement notifyAccessibilityUpdate for role" << iface->role();
void QWasmAccessibilityEnable()