9#include <QtCore/private/qwasmsuspendresumecontrol_p.h>
10#include <QtGui/qwindow.h>
16 QWasmAccessibility::enable();
19#if QT_CONFIG(accessibility)
21#include <QtGui/private/qaccessiblebridgeutils_p.h>
23Q_LOGGING_CATEGORY(lcQpaAccessibility,
"qt.qpa.accessibility")
26EM_JS(emscripten::EM_VAL, getActiveElement_js, (emscripten::EM_VAL undefHandle), {
27 var activeEl = document.activeElement;
31 }
else if (activeEl.shadowRoot) {
32 activeEl = activeEl.shadowRoot.activeElement;
34 return Emval.toHandle(activeEl);
50QWasmAccessibility::QWasmAccessibility()
54 if (qEnvironmentVariableIntValue(
"QT_WASM_ENABLE_ACCESSIBILITY") == 1)
55 enableAccessibility();
58 QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get();
60 m_eventHandlerIndex = suspendResume->registerEventHandler([
this](
const emscripten::val event){
61 this->handleEventFromHtmlElement(event);
65QWasmAccessibility::~QWasmAccessibility()
68 QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get();
69 suspendResume->removeEventHandler(m_eventHandlerIndex);
74QWasmAccessibility *QWasmAccessibility::s_instance =
nullptr;
76QWasmAccessibility* QWasmAccessibility::get()
81void QWasmAccessibility::addAccessibilityEnableButton(QWindow *window)
83 get()->addAccessibilityEnableButtonImpl(window);
86void QWasmAccessibility::onShowWindow(QWindow *window)
88 get()->onShowWindowImpl(window);
91void QWasmAccessibility::onRemoveWindow(QWindow *window)
93 get()->onRemoveWindowImpl(window);
96bool QWasmAccessibility::isEnabled()
98 return get()->m_accessibilityEnabled;
100void QWasmAccessibility::enable()
103 get()->enableAccessibility();
106void QWasmAccessibility::addAccessibilityEnableButtonImpl(QWindow *window)
108 if (m_accessibilityEnabled)
111 emscripten::val container = getElementContainer(window);
112 emscripten::val document = getDocument(container);
113 emscripten::val button = document.call<emscripten::val>(
"createElement", std::string(
"button"));
114 setProperty(button,
"innerText",
"Enable Screen Reader");
115 button[
"classList"].call<
void>(
"add", emscripten::val(
"hidden-visually-read-by-screen-reader"));
116 container.call<
void>(
"appendChild", button);
118 auto enableContext = std::make_tuple(button, std::make_unique<qstdweb::EventCallback>
119 (button, std::string(
"click"), [
this](emscripten::val) { enableAccessibility(); }));
120 m_enableButtons.insert(std::make_pair(window, std::move(enableContext)));
123void QWasmAccessibility::onShowWindowImpl(QWindow *window)
125 if (!m_accessibilityEnabled)
127 populateAccessibilityTree(window->accessibleRoot());
130void QWasmAccessibility::onRemoveWindowImpl(QWindow *window)
133 const auto it = m_enableButtons.find(window);
134 if (it != m_enableButtons.end()) {
136 auto [element, callback] = it->second;
138 element[
"parentElement"].call<
void>(
"removeChild", element);
139 m_enableButtons.erase(it);
143 auto a11yContainer = getA11yContainer(window);
144 auto describedByContainer =
145 getDescribedByContainer(window);
146 auto elementContainer = getElementContainer(window);
147 auto document = getDocument(a11yContainer);
150 if (!describedByContainer.isUndefined()) {
151 a11yContainer.call<
void>(
"removeChild", describedByContainer);
152 describedByContainer = document.call<emscripten::val>(
"createElement", std::string(
"div"));
154 a11yContainer.call<
void>(
"appendChild", elementContainer);
155 a11yContainer.call<
void>(
"appendChild", describedByContainer);
160void QWasmAccessibility::enableAccessibility()
165 Q_ASSERT(!m_accessibilityEnabled);
166 m_accessibilityEnabled =
true;
168 for (
const auto& [key, value] : m_enableButtons) {
169 const auto &[element, callback] = value;
171 if (
auto wasmWindow = QWasmWindow::fromWindow(key))
172 wasmWindow->onAccessibilityEnable();
173 onShowWindowImpl(key);
174 element[
"parentElement"].call<
void>(
"removeChild", element);
176 m_enableButtons.clear();
179bool QWasmAccessibility::isWindowNode(QAccessibleInterface *iface)
181 return (iface && !getWindow(iface->parent()) && getWindow(iface));
184emscripten::val QWasmAccessibility::getA11yContainer(QWindow *window)
186 const auto wasmWindow = QWasmWindow::fromWindow(window);
188 return emscripten::val::undefined();
190 auto a11yContainer = wasmWindow->a11yContainer();
191 if (a11yContainer[
"childElementCount"].as<
unsigned>() == 2)
192 return a11yContainer;
194 Q_ASSERT(a11yContainer[
"childElementCount"].as<
unsigned>() == 0);
196 const auto document = getDocument(a11yContainer);
197 if (document.isUndefined())
198 return emscripten::val::undefined();
200 auto elementContainer = document.call<emscripten::val>(
"createElement", std::string(
"div"));
201 elementContainer[
"classList"].call<
void>(
"add", emscripten::val(
"qt-window-a11y-elements-container"));
202 auto describedByContainer = document.call<emscripten::val>(
"createElement", std::string(
"div"));
203 describedByContainer[
"classList"].call<
void>(
"add", emscripten::val(
"qt-window-a11y-describedby-container"));
205 a11yContainer.call<
void>(
"appendChild", elementContainer);
206 a11yContainer.call<
void>(
"appendChild", describedByContainer);
208 return a11yContainer;
211emscripten::val QWasmAccessibility::getA11yContainer(QAccessibleInterface *iface)
213 return getA11yContainer(getWindow(iface));
216emscripten::val QWasmAccessibility::getDescribedByContainer(QWindow *window)
218 auto a11yContainer = getA11yContainer(window);
219 if (a11yContainer.isUndefined())
220 return emscripten ::val::undefined();
222 Q_ASSERT(a11yContainer[
"childElementCount"].as<
unsigned>() == 2);
223 Q_ASSERT(!a11yContainer[
"children"][1].isUndefined());
225 return a11yContainer[
"children"][1];
228emscripten::val QWasmAccessibility::getDescribedByContainer(QAccessibleInterface *iface)
230 return getDescribedByContainer(getWindow(iface));
233emscripten::val QWasmAccessibility::getElementContainer(QWindow *window)
235 auto a11yContainer = getA11yContainer(window);
236 if (a11yContainer.isUndefined())
237 return emscripten ::val::undefined();
239 Q_ASSERT(a11yContainer[
"childElementCount"].as<
unsigned>() == 2);
240 Q_ASSERT(!a11yContainer[
"children"][0].isUndefined());
241 return a11yContainer[
"children"][0];
244emscripten::val QWasmAccessibility::getElementContainer(QAccessibleInterface *iface)
251 if (!getWindow(iface))
252 return emscripten::val::undefined();
254 if (isWindowNode(iface))
255 return emscripten::val::undefined();
257 if (isWindowNode(iface->parent()))
258 return getElementContainer(getWindow(iface->parent()));
261 return getHtmlElement(iface->parent());
264QWindow *QWasmAccessibility::getWindow(QAccessibleInterface *iface)
269 QWindow *window = iface->window();
271 if (!window && iface->parent())
272 window = iface->parent()->window();
276emscripten::val QWasmAccessibility::getDocument(
const emscripten::val &container)
278 if (container.isUndefined())
279 return emscripten::val::global(
"document");
280 return container[
"ownerDocument"];
283emscripten::val QWasmAccessibility::getDocument(QAccessibleInterface *iface)
285 return getDocument(getA11yContainer(iface));
288void QWasmAccessibility::setAttribute(emscripten::val element,
const std::string &attr,
289 const std::string &val)
292 element.call<
void>(
"setAttribute", attr, val);
294 element.call<
void>(
"removeAttribute", attr);
297void QWasmAccessibility::setAttribute(emscripten::val element,
const std::string &attr,
300 setAttribute(element, attr, std::string(val));
303void QWasmAccessibility::setAttribute(emscripten::val element,
const std::string &attr,
bool val)
306 element.call<
void>(
"setAttribute", attr, val);
308 element.call<
void>(
"removeAttribute", attr);
311void QWasmAccessibility::setProperty(emscripten::val element,
const std::string &property,
312 const std::string &val)
314 element.set(property, val);
317void QWasmAccessibility::setProperty(emscripten::val element,
const std::string &property,
const char *val)
319 setProperty(element, property, std::string(val));
322void QWasmAccessibility::setProperty(emscripten::val element,
const std::string &property,
bool val)
324 element.set(property, val);
327void QWasmAccessibility::setNamedAttribute(QAccessibleInterface *iface,
const std::string &attribute, QAccessible::Text text)
329 const emscripten::val element = getHtmlElement(iface);
330 setAttribute(element, attribute, iface->text(text).toStdString());
332void QWasmAccessibility::setNamedProperty(QAccessibleInterface *iface,
const std::string &property, QAccessible::Text text)
334 const emscripten::val element = getHtmlElement(iface);
335 setProperty(element, property, iface->text(text).toStdString());
338void QWasmAccessibility::addEventListener(QAccessibleInterface *iface, emscripten::val element,
const char *eventType)
340 element.set(
"data-qta11yinterface",
reinterpret_cast<size_t>(iface));
341 element.call<
void>(
"addEventListener", emscripten::val(eventType),
342 QWasmSuspendResumeControl::get()->jsEventHandlerAt(m_eventHandlerIndex),
346void QWasmAccessibility::sendEvent(QAccessibleInterface *iface, QAccessible::Event eventType)
348 if (iface->object()) {
349 QAccessibleEvent event(iface->object(), eventType);
350 handleUpdateByInterfaceRole(&event);
352 QAccessibleEvent event(iface, eventType);
353 handleUpdateByInterfaceRole(&event);
357emscripten::val QWasmAccessibility::createHtmlElement(QAccessibleInterface *iface)
362 emscripten::val container = getElementContainer(iface);
366 emscripten::val document = getDocument(container);
371 emscripten::val element = [
this, iface, document] {
373 emscripten::val element = emscripten::val::undefined();
375 switch (iface->role()) {
377 case QAccessible::Button: {
378 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
379 addEventListener(iface, element,
"click");
382 case QAccessible::Grouping:
383 case QAccessible::CheckBox: {
389 emscripten::val checkbox = emscripten::val::undefined();
390 if (iface->role() == QAccessible::CheckBox) {
391 checkbox = document.call<emscripten::val>(
"createElement", std::string(
"input"));
392 setAttribute(checkbox,
"type",
"checkbox");
393 setAttribute(checkbox,
"checked", iface->state().checked);
394 setProperty(checkbox,
"indeterminate", iface->state().checkStateMixed);
395 addEventListener(iface, checkbox,
"change");
398 if (iface->childCount() > 0 || iface->role() == QAccessible::Grouping) {
399 auto label = document.call<emscripten::val>(
"createElement", std::string(
"span"));
401 const std::string id = QString::asprintf(
"lbid%p", iface).toStdString();
402 setAttribute(label,
"id", id);
404 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
405 element.call<
void>(
"appendChild", label);
407 setAttribute(element,
"role",
"group");
408 setAttribute(element,
"aria-labelledby", id);
410 if (!checkbox.isUndefined()) {
411 element.call<
void>(
"appendChild", checkbox);
412 addEventListener(iface, checkbox,
"focus");
419 case QAccessible::Switch: {
420 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
421 setAttribute(element,
"type",
"button");
422 setAttribute(element,
"role",
"switch");
423 if (iface->state().checked)
424 setAttribute(element,
"aria-checked",
"true");
426 setAttribute(element,
"aria-checked",
"false");
427 addEventListener(iface, element,
"click");
430 case QAccessible::RadioButton: {
431 element = document.call<emscripten::val>(
"createElement", std::string(
"input"));
432 setAttribute(element,
"type",
"radio");
433 setAttribute(element,
"checked", iface->state().checked);
434 setProperty(element,
"name",
"buttonGroup");
435 addEventListener(iface, element,
"change");
438 case QAccessible::Dial:
439 case QAccessible::SpinBox:
440 case QAccessible::Slider: {
441 const auto minValue = iface->valueInterface()->minimumValue().toString().toStdString();
442 const auto maxValue = iface->valueInterface()->maximumValue().toString().toStdString();
443 const auto stepValue =
444 iface->valueInterface()->minimumStepSize().toString().toStdString();
445 const auto value = iface->valueInterface()->currentValue().toString().toStdString();
446 element = document.call<emscripten::val>(
"createElement", std::string(
"input"));
447 setAttribute(element,
"type",
"number");
448 setAttribute(element,
"min", minValue);
449 setAttribute(element,
"max", maxValue);
450 setAttribute(element,
"step", stepValue);
451 setProperty(element,
"value", value);
452 addEventListener(iface, element,
"change");
455 case QAccessible::PageTabList:{
456 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
457 setAttribute(element,
"role",
"tablist");
458 setHtmlElementOrientation(element, iface);
460 m_elements[iface] = element;
462 for (
int i = 0; i < iface->childCount(); ++i)
463 createHtmlElement(iface->child(i));
467 case QAccessible::PageTab:{
468 const QString text = iface->text(QAccessible::Name);
469 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
470 setAttribute(element,
"role",
"tab");
471 setAttribute(element,
"title", text.toStdString());
472 addEventListener(iface, element,
"click");
475 case QAccessible::ScrollBar: {
478 const auto minValue = iface->valueInterface()->minimumValue().toString().toStdString();
479 const auto maxValue = iface->valueInterface()->maximumValue().toString().toStdString();
480 const auto value = iface->valueInterface()->currentValue().toString().toStdString();
481 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
482 setAttribute(element,
"tabindex",
"0");
483 setAttribute(element,
"role",
"scrollbar");
484 setAttribute(element,
"aria-valuemin", minValue);
485 setAttribute(element,
"aria-valuemax", maxValue);
486 setAttribute(element,
"aria-valuenow", value);
487 setHtmlElementOrientation(element, iface);
488 addEventListener(iface, element,
"change");
491 case QAccessible::StaticText: {
492 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
494 case QAccessible::Dialog: {
495 element = document.call<emscripten::val>(
"createElement", std::string(
"dialog"));
497 case QAccessible::ToolBar:{
498 const QString text = iface->text(QAccessible::Name);
499 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
500 setAttribute(element,
"role",
"toolbar");
501 setAttribute(element,
"title", text.toStdString());
502 setHtmlElementOrientation(element, iface);
503 addEventListener(iface, element,
"click");
505 case QAccessible::MenuItem:
506 case QAccessible::ButtonDropDown:
507 case QAccessible::ButtonMenu: {
508 const QString text = iface->text(QAccessible::Name);
509 element = document.call<emscripten::val>(
"createElement", std::string(
"button"));
510 setAttribute(element,
"role",
"menuitem");
511 setAttribute(element,
"title", text.toStdString());
512 addEventListener(iface, element,
"click");
514 case QAccessible::MenuBar:
515 case QAccessible::PopupMenu: {
516 const QString text = iface->text(QAccessible::Name);
517 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
518 setAttribute(element,
"role",
"menubar");
519 setAttribute(element,
"title", text.toStdString());
520 setHtmlElementOrientation(element, iface);
521 m_elements[iface] = element;
523 for (
int i = 0; i < iface->childCount(); ++i) {
524 emscripten::val childElement = createHtmlElement(iface->child(i));
525 setAttribute(childElement,
"aria-owns", text.toStdString());
528 case QAccessible::EditableText: {
529 element = document.call<emscripten::val>(
"createElement", std::string(
"input"));
530 setAttribute(element,
"type",
"text");
531 setAttribute(element,
"contenteditable",
"true");
532 setAttribute(element,
"readonly", iface->state().readOnly);
533 setAttribute(element,
"autocorrect",
"off");
534 setProperty(element,
"inputMode",
"text");
536 case QAccessible::List: {
537 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
538 setAttribute(element,
"tabindex",
"0");
539 setAttribute(element,
"role",
"list");
541 case QAccessible::ListItem: {
542 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
543 setAttribute(element,
"tabindex",
"0");
544 setAttribute(element,
"role",
"listitem");
546 case QAccessible::ProgressBar: {
547 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
548 setAttribute(element,
"tabindex",
"0");
549 setAttribute(element,
"role",
"progressbar");
550 setAttribute(element,
"aria-live",
"polite");
551 if (
auto valueInterface = iface->valueInterface()) {
552 const auto name = iface->text(QAccessible::Name).toStdString();
553 const auto min = valueInterface->minimumValue().toInt();
554 const auto max = valueInterface->maximumValue().toInt();
555 const auto cur = valueInterface->currentValue().toInt();
558 const auto totalSteps = qint64(max) - min;
559 const auto progress = (totalSteps == 0) ? 0 :
static_cast<
int>((qint64(cur) - min) * 100.0 / totalSteps);
562 setAttribute(element,
"aria-valuenow",
"0");
564 setAttribute(element,
"aria-valuenow",
"100");
566 setAttribute(element,
"aria-valuenow", std::to_string(progress));
567 setAttribute(element,
"aria-valuemin",
"0");
568 setAttribute(element,
"aria-valuemax",
"100");
569 setAttribute(element,
"aria-label", name);
573 qCDebug(lcQpaAccessibility) <<
"TODO: createHtmlElement() handle" << iface->role();
574 element = document.call<emscripten::val>(
"createElement", std::string(
"div"));
577 addEventListener(iface, element,
"focus");
582 m_elements[iface] = element;
584 setHtmlElementGeometry(iface);
585 setHtmlElementDisabled(iface);
586 setHtmlElementVisibility(iface, !iface->state().invisible);
587 handleIdentifierUpdate(iface);
588 handleDescriptionChanged(iface);
589 sendEvent(iface, QAccessible::NameChanged);
593 for (
int i = 0; i < iface->childCount(); ++i) {
594 if (!getHtmlElement(iface->child(i)).isUndefined())
595 linkToParent(iface->child(i));
601void QWasmAccessibility::destroyHtmlElement(QAccessibleInterface *iface)
604 qCDebug(lcQpaAccessibility) <<
"TODO destroyHtmlElement";
607emscripten::val QWasmAccessibility::getHtmlElement(QAccessibleInterface *iface)
609 auto it = m_elements.find(iface);
610 if (it != m_elements.end())
613 return emscripten::val::undefined();
616void QWasmAccessibility::repairLinks(QAccessibleInterface *iface)
621 for (
int i = 0; i < iface->childCount(); ++i) {
622 const auto elementI = getHtmlElement(iface->child(i));
623 const auto containerI = getElementContainer(iface->child(i));
625 if (!elementI.isUndefined() &&
626 !containerI.isUndefined() &&
627 !elementI[
"parentElement"].isUndefined() &&
628 !elementI[
"parentElement"].isNull() &&
629 elementI[
"parentElement"] != containerI) {
635 for (
int i = 0; i < iface->childCount(); ++i) {
636 const auto elementI = getHtmlElement(iface->child(i));
637 const auto containerI = getElementContainer(iface->child(i));
638 if (!elementI.isUndefined() && !containerI.isUndefined())
639 containerI.call<
void>(
"appendChild", elementI);
644void QWasmAccessibility::linkToParent(QAccessibleInterface *iface)
646 emscripten::val element = getHtmlElement(iface);
647 emscripten::val container = getElementContainer(iface);
649 if (container.isUndefined() || element.isUndefined())
653 const auto activeElementBefore = emscripten::val::take_ownership(
654 getActiveElement_js(emscripten::val::undefined().as_handle()));
657 repairLinks(iface->parent());
659 emscripten::val next = emscripten::val::undefined();
660 const int thisIndex = iface->parent()->indexOfChild(iface);
661 if (thisIndex >= 0) {
662 Q_ASSERT(thisIndex < iface->parent()->childCount());
663 for (
int i = thisIndex + 1; i < iface->parent()->childCount(); ++i) {
664 const auto elementI = getHtmlElement(iface->parent()->child(i));
665 if (!elementI.isUndefined() &&
666 elementI[
"parentElement"] == container) {
671 if (next.isUndefined())
672 container.call<
void>(
"appendChild", element);
674 container.call<
void>(
"insertBefore", element, next);
676 const auto activeElementAfter = emscripten::val::take_ownership(
677 getActiveElement_js(emscripten::val::undefined().as_handle()));
678 if (activeElementBefore != activeElementAfter) {
679 if (!activeElementBefore.isUndefined() && !activeElementBefore.isNull())
680 activeElementBefore.call<
void>(
"focus");
684void QWasmAccessibility::setHtmlElementVisibility(QAccessibleInterface *iface,
bool visible)
686 emscripten::val element = getHtmlElement(iface);
687 setAttribute(element,
"inert", !visible);
690void QWasmAccessibility::setHtmlElementGeometry(QAccessibleInterface *iface)
692 const emscripten::val element = getHtmlElement(iface);
694 QRect windowGeometry = iface->rect();
695 if (iface->parent()) {
699 const QRect parentRect = iface->parent()->rect();
700 const QRect thisRect = iface->rect();
701 const QRect result(thisRect.topLeft() - parentRect.topLeft(), thisRect.size());
702 windowGeometry = result;
706 Q_ASSERT(!getWindow(iface));
708 setHtmlElementGeometry(element, windowGeometry);
711void QWasmAccessibility::setHtmlElementGeometry(emscripten::val element, QRect geometry)
715 emscripten::val style = element[
"style"];
716 style.set(
"position", std::string(
"absolute"));
717 style.set(
"z-index", std::string(
"-1"));
719 style.set(
"left", std::to_string(geometry.x()) +
"px");
720 style.set(
"top", std::to_string(geometry.y()) +
"px");
721 style.set(
"width", std::to_string(geometry.width()) +
"px");
722 style.set(
"height", std::to_string(geometry.height()) +
"px");
725void QWasmAccessibility::setHtmlElementFocus(QAccessibleInterface *iface)
727 const auto element = getHtmlElement(iface);
728 element.call<
void>(
"focus");
731void QWasmAccessibility::setHtmlElementDisabled(QAccessibleInterface *iface)
733 auto element = getHtmlElement(iface);
734 setAttribute(element,
"aria-disabled", iface->state().disabled);
737void QWasmAccessibility::setHtmlElementOrientation(emscripten::val element, QAccessibleInterface *iface)
740 if (QAccessibleAttributesInterface *attributesIface = iface->attributesInterface()) {
741 const QVariant orientationVariant =
742 attributesIface->attributeValue(QAccessible::Attribute::Orientation);
743 if (orientationVariant.isValid()) {
744 Q_ASSERT(orientationVariant.canConvert<Qt::Orientation>());
745 const Qt::Orientation orientation = orientationVariant.value<Qt::Orientation>();
746 const std::string value = orientation == Qt::Horizontal ?
"horizontal" :
"vertical";
747 setAttribute(element,
"aria-orientation", value);
752void QWasmAccessibility::handleListItemUpdate(QAccessibleEvent *event)
754 auto iface = event->accessibleInterface();
756 switch (event->type()) {
757 case QAccessible::NameChanged: {
759 setNamedProperty(iface,
"innerText", QAccessible::Name);
762 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleListItemUpdate for event" << event->type();
767void QWasmAccessibility::handleDialUpdate(QAccessibleEvent *event)
769 switch (event->type()) {
770 case QAccessible::ObjectCreated:
771 case QAccessible::StateChanged: {
773 case QAccessible::Focus:
774 case QAccessible::NameChanged: {
775 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
777 case QAccessible::ValueChanged: {
778 QAccessibleInterface *accessible = event->accessibleInterface();
779 const emscripten::val element = getHtmlElement(accessible);
780 std::string valueString = accessible->valueInterface()->currentValue().toString().toStdString();
781 setProperty(element,
"value", valueString);
784 qDebug() <<
"TODO: implement handleSpinBoxUpdate for event" << event->type();
789void QWasmAccessibility::handleStaticTextUpdate(QAccessibleEvent *event)
791 switch (event->type()) {
792 case QAccessible::NameChanged: {
794 setNamedProperty(event->accessibleInterface(),
"innerText", QAccessible::Name);
797 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleStaticTextUpdate for event" << event->type();
802void QWasmAccessibility::handleLineEditUpdate(QAccessibleEvent *event)
804 switch (event->type()) {
805 case QAccessible::StateChanged: {
806 auto iface = event->accessibleInterface();
807 auto element = getHtmlElement(iface);
808 setAttribute(element,
"readonly", iface->state().readOnly);
809 if (iface->state().passwordEdit)
810 setProperty(element,
"type",
"password");
812 setProperty(element,
"type",
"text");
814 case QAccessible::ValueChanged:
815 case QAccessible::NameChanged: {
816 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
818 case QAccessible::ObjectShow:
819 case QAccessible::Focus: {
820 auto iface = event->accessibleInterface();
821 auto element = getHtmlElement(iface);
822 if (!element.isUndefined()) {
823 setAttribute(element,
"readonly", iface->state().readOnly);
824 if (iface->state().passwordEdit)
825 setProperty(element,
"type",
"password");
827 setProperty(element,
"type",
"text");
829 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
831 case QAccessible::TextRemoved:
832 case QAccessible::TextInserted: {
833 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
835 case QAccessible::TextCaretMoved: {
837 setNamedProperty(event->accessibleInterface(),
"value", QAccessible::Value);
840 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleLineEditUpdate for event" << event->type();
845void QWasmAccessibility::handleEventFromHtmlElement(
const emscripten::val event)
847 if (event[
"target"].isNull() || event[
"target"].isUndefined())
850 if (event[
"target"][
"data-qta11yinterface"].isNull() || event[
"target"][
"data-qta11yinterface"].isUndefined())
853 auto iface =
reinterpret_cast<QAccessibleInterface *>(event[
"target"][
"data-qta11yinterface"].as<size_t>());
854 if (m_elements.find(iface) == m_elements.end())
857 const auto eventType = QString::fromStdString(event[
"type"].as<std::string>());
858 const auto& actionNames = QAccessibleBridgeUtils::effectiveActionNames(iface);
859 bool handled =
false;
861 if (eventType ==
"blur") {
863 }
else if (eventType ==
"keydown") {
865 }
else if (eventType ==
"keyup") {
867 }
else if (eventType ==
"focus") {
868 if (actionNames.contains(QAccessibleActionInterface::setFocusAction()))
869 iface->actionInterface()->doAction(QAccessibleActionInterface::setFocusAction());
870 }
else if (eventType ==
"click") {
872 if (actionNames.contains(QAccessibleActionInterface::pressAction()))
873 iface->actionInterface()->doAction(QAccessibleActionInterface::pressAction());
874 else if (actionNames.contains(QAccessibleActionInterface::showMenuAction()))
875 iface->actionInterface()->doAction(QAccessibleActionInterface::showMenuAction());
876 }
else if (eventType ==
"change") {
880 if (actionNames.contains(QAccessibleActionInterface::pressAction()))
881 iface->actionInterface()->doAction(QAccessibleActionInterface::pressAction());
882 else if (actionNames.contains(QAccessibleActionInterface::toggleAction()))
883 iface->actionInterface()->doAction(QAccessibleActionInterface::toggleAction());
885 qWarning() <<
" Unknown event" << eventType;
889 event.call<
void>(
"preventDefault");
890 event.call<
void>(
"stopPropagation");
894void QWasmAccessibility::handleButtonUpdate(QAccessibleEvent *event)
896 switch (event->type()) {
897 case QAccessible::Focus:
898 case QAccessible::NameChanged: {
899 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
902 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleCheckBoxUpdate for event" << event->type();
907void QWasmAccessibility::handleCheckBoxUpdate(QAccessibleEvent *event)
909 switch (event->type()) {
910 case QAccessible::Focus:
911 case QAccessible::NameChanged: {
912 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
914 case QAccessible::StateChanged: {
915 QAccessibleInterface *accessible = event->accessibleInterface();
916 const emscripten::val element = getHtmlElement(accessible);
917 setAttribute(element,
"checked", accessible->state().checked);
918 setProperty(element,
"indeterminate", accessible->state().checkStateMixed);
921 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleCheckBoxUpdate for event" << event->type();
926void QWasmAccessibility::handleGroupBoxUpdate(QAccessibleEvent *event)
928 QAccessibleInterface *iface = event->accessibleInterface();
930 emscripten::val parent = getHtmlElement(iface);
931 emscripten::val label = parent[
"children"][0];
932 emscripten::val checkbox = emscripten::val::undefined();
933 if (iface->role() == QAccessible::CheckBox)
934 checkbox = parent[
"children"][1];
936 switch (event->type()) {
937 case QAccessible::Focus:
938 case QAccessible::NameChanged: {
939 setProperty(label,
"innerText", iface->text(QAccessible::Name).toStdString());
940 if (!checkbox.isUndefined())
941 setAttribute(checkbox,
"aria-label", iface->text(QAccessible::Name).toStdString());
943 case QAccessible::StateChanged: {
944 if (!checkbox.isUndefined()) {
945 setAttribute(checkbox,
"checked", iface->state().checked);
946 setProperty(checkbox,
"indeterminate", iface->state().checkStateMixed);
950 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleCheckBoxUpdate for event" << event->type();
955void QWasmAccessibility::handleSwitchUpdate(QAccessibleEvent *event)
957 switch (event->type()) {
958 case QAccessible::Focus:
959 case QAccessible::NameChanged: {
961 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
963 case QAccessible::StateChanged: {
964 QAccessibleInterface *accessible = event->accessibleInterface();
965 const emscripten::val element = getHtmlElement(accessible);
966 if (accessible->state().checked)
967 setAttribute(element,
"aria-checked",
"true");
969 setAttribute(element,
"aria-checked",
"false");
972 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleSwitchUpdate for event" << event->type();
977void QWasmAccessibility::handleToolUpdate(QAccessibleEvent *event)
979 QAccessibleInterface *iface = event->accessibleInterface();
980 QString text = iface->text(QAccessible::Name);
981 QString desc = iface->text(QAccessible::Description);
982 switch (event->type()) {
983 case QAccessible::NameChanged:
984 case QAccessible::StateChanged:{
985 const emscripten::val element = getHtmlElement(iface);
986 setAttribute(element,
"title", text.toStdString());
989 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleToolUpdate for event" << event->type();
993void QWasmAccessibility::handleMenuUpdate(QAccessibleEvent *event)
995 QAccessibleInterface *iface = event->accessibleInterface();
996 QString text = iface->text(QAccessible::Name);
997 QString desc = iface->text(QAccessible::Description);
998 switch (event->type()) {
999 case QAccessible::Focus:
1000 case QAccessible::NameChanged:
1001 case QAccessible::MenuStart :
1002 case QAccessible::StateChanged:{
1003 const emscripten::val element = getHtmlElement(iface);
1004 setAttribute(element,
"title", text.toStdString());
1006 case QAccessible::PopupMenuStart: {
1007 if (iface->childCount() > 0) {
1008 const auto childElement = getHtmlElement(iface->child(0));
1009 childElement.call<
void>(
"focus");
1013 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleMenuUpdate for event" << event->type();
1017void QWasmAccessibility::handleDialogUpdate(QAccessibleEvent *event) {
1019 switch (event->type()) {
1020 case QAccessible::NameChanged:
1021 case QAccessible::Focus:
1022 case QAccessible::DialogStart:
1023 case QAccessible::StateChanged: {
1024 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1027 qCDebug(lcQpaAccessibility) <<
"TODO: implement handleLineEditUpdate for event" << event->type();
1032void QWasmAccessibility::populateAccessibilityTree(QAccessibleInterface *iface)
1039 const QWindow *window1 = getWindow(iface);
1040 const QWindow *window0 = (iface->parent()) ? getWindow(iface->parent()) :
nullptr;
1042 if (window1 && window0 == window1) {
1044 bool exists = !getHtmlElement(iface).isUndefined();
1046 exists = !createHtmlElement(iface).isUndefined();
1049 linkToParent(iface);
1050 setHtmlElementVisibility(iface, !iface->state().invisible);
1051 setHtmlElementGeometry(iface);
1052 setHtmlElementDisabled(iface);
1053 handleIdentifierUpdate(iface);
1054 handleDescriptionChanged(iface);
1055 sendEvent(iface, QAccessible::NameChanged);
1058 for (
int i = 0; i < iface->childCount(); ++i)
1059 populateAccessibilityTree(iface->child(i));
1062void QWasmAccessibility::handleRadioButtonUpdate(QAccessibleEvent *event)
1064 switch (event->type()) {
1065 case QAccessible::Focus:
1066 case QAccessible::NameChanged: {
1067 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1069 case QAccessible::StateChanged: {
1070 QAccessibleInterface *accessible = event->accessibleInterface();
1071 const emscripten::val element = getHtmlElement(accessible);
1072 setAttribute(element,
"checked", accessible->state().checked);
1075 qDebug() <<
"TODO: implement handleRadioButtonUpdate for event" << event->type();
1080void QWasmAccessibility::handleSpinBoxUpdate(QAccessibleEvent *event)
1082 switch (event->type()) {
1083 case QAccessible::ObjectCreated:
1084 case QAccessible::StateChanged: {
1086 case QAccessible::Focus:
1087 case QAccessible::NameChanged: {
1088 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1090 case QAccessible::ValueChanged: {
1091 QAccessibleInterface *accessible = event->accessibleInterface();
1092 const emscripten::val element = getHtmlElement(accessible);
1093 std::string valueString = accessible->valueInterface()->currentValue().toString().toStdString();
1094 setProperty(element,
"value", valueString);
1097 qDebug() <<
"TODO: implement handleSpinBoxUpdate for event" << event->type();
1102void QWasmAccessibility::handleSliderUpdate(QAccessibleEvent *event)
1104 switch (event->type()) {
1105 case QAccessible::ObjectCreated:
1106 case QAccessible::StateChanged: {
1108 case QAccessible::Focus:
1109 case QAccessible::NameChanged: {
1110 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1112 case QAccessible::ValueChanged: {
1113 QAccessibleInterface *accessible = event->accessibleInterface();
1114 const emscripten::val element = getHtmlElement(accessible);
1115 std::string valueString = accessible->valueInterface()->currentValue().toString().toStdString();
1116 setProperty(element,
"value", valueString);
1119 qDebug() <<
"TODO: implement handleSliderUpdate for event" << event->type();
1124void QWasmAccessibility::handleScrollBarUpdate(QAccessibleEvent *event)
1126 switch (event->type()) {
1127 case QAccessible::Focus:
1128 case QAccessible::NameChanged: {
1129 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1131 case QAccessible::ValueChanged: {
1132 QAccessibleInterface *accessible = event->accessibleInterface();
1133 const emscripten::val element = getHtmlElement(accessible);
1134 std::string valueString = accessible->valueInterface()->currentValue().toString().toStdString();
1135 setAttribute(element,
"aria-valuenow", valueString);
1138 qDebug() <<
"TODO: implement handleSliderUpdate for event" << event->type();
1144void QWasmAccessibility::handlePageTabUpdate(QAccessibleEvent *event)
1146 switch (event->type()) {
1147 case QAccessible::NameChanged: {
1148 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1150 case QAccessible::Focus: {
1151 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1154 qDebug() <<
"TODO: implement handlePageTabUpdate for event" << event->type();
1159void QWasmAccessibility::handlePageTabListUpdate(QAccessibleEvent *event)
1161 switch (event->type()) {
1162 case QAccessible::NameChanged: {
1163 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1165 case QAccessible::Focus: {
1166 setNamedAttribute(event->accessibleInterface(),
"aria-label", QAccessible::Name);
1169 qDebug() <<
"TODO: implement handlePageTabUpdate for event" << event->type();
1174void QWasmAccessibility::handleIdentifierUpdate(QAccessibleInterface *iface)
1176 const emscripten::val element = getHtmlElement(iface);
1177 QString id = iface->text(QAccessible::Identifier).replace(
" ",
"_");
1178 if (id.isEmpty() && iface->role() == QAccessible::PageTabList) {
1179 std::ostringstream oss;
1180 oss <<
"tabList_0x" << (
void *)iface;
1181 id = QString::fromUtf8(oss.str());
1184 setAttribute(element,
"id", id.toStdString());
1185 if (!id.isEmpty()) {
1186 if (iface->role() == QAccessible::PageTabList) {
1187 for (
int i = 0; i < iface->childCount(); ++i) {
1188 const auto child = getHtmlElement(iface->child(i));
1189 setAttribute(child,
"aria-owns", id.toStdString());
1195void QWasmAccessibility::handleDescriptionChanged(QAccessibleInterface *iface)
1197 const auto desc = iface->text(QAccessible::Description).toStdString();
1198 auto element = getHtmlElement(iface);
1199 auto container = getDescribedByContainer(iface);
1200 if (!container.isUndefined()) {
1201 std::ostringstream oss;
1202 oss <<
"dbid_" << (
void *)iface;
1203 auto id = oss.str();
1205 auto describedBy = container.call<emscripten::val>(
"querySelector",
"#" + std::string(id));
1207 if (!describedBy.isUndefined() && !describedBy.isNull()) {
1208 container.call<
void>(
"removeChild", describedBy);
1210 setAttribute(element,
"aria-describedby",
"");
1212 if (describedBy.isUndefined() || describedBy.isNull()) {
1213 auto document = getDocument(container);
1214 describedBy = document.call<emscripten::val>(
"createElement", std::string(
"p"));
1216 container.call<
void>(
"appendChild", describedBy);
1218 setAttribute(describedBy,
"id", id);
1219 setAttribute(describedBy,
"inert",
true);
1220 setAttribute(element,
"aria-describedby", id);
1221 setProperty(describedBy,
"innerText", desc);
1226void QWasmAccessibility::handleAnnouncement(QAccessibleInterface *iface,
1227 QAccessibleAnnouncementEvent *announcementEvent)
1229 emscripten::val element = getHtmlElement(iface);
1230 if (element[
"ariaNotify"].isUndefined())
1233 const std::string message = announcementEvent->message().toStdString();
1234 const std::string prio(announcementEvent->politeness()
1235 == QAccessible::AnnouncementPoliteness::Assertive
1238 emscripten::val options = emscripten::val::object();
1239 options.set(
"priority", prio);
1240 element.call<
void>(
"ariaNotify", message, options);
1243void QWasmAccessibility::handleProgressBarUpdate(QAccessibleEvent *event)
1245 auto iface = event->accessibleInterface();
1246 auto valueInterface = iface->valueInterface();
1247 auto element = getHtmlElement(iface);
1249 switch (event->type()) {
1250 case QAccessible::Focus:
1251 case QAccessible::NameChanged: {
1252 const auto name = iface->text(QAccessible::Name).toStdString();
1253 setAttribute(element,
"aria-label", name);
1255 case QAccessible::ValueChanged: {
1256 if (valueInterface) {
1257 const auto min = valueInterface->minimumValue().toInt();
1258 const auto max = valueInterface->maximumValue().toInt();
1259 const auto cur = valueInterface->currentValue().toInt();
1262 const auto totalSteps = qint64(max) - min;
1263 const auto progress = (totalSteps == 0) ? 0 :
static_cast<
int>((qint64(cur) - min) * 100.0 / totalSteps);
1266 setAttribute(element,
"aria-valuenow",
"0");
1267 else if (cur >= max)
1268 setAttribute(element,
"aria-valuenow",
"100");
1270 setAttribute(element,
"aria-valuenow", std::to_string(progress));
1278void QWasmAccessibility::createObject(QAccessibleInterface *iface)
1280 if (getHtmlElement(iface).isUndefined())
1281 createHtmlElement(iface);
1284void QWasmAccessibility::removeObject(QAccessibleInterface *iface)
1291 const auto it = m_elements.find(iface);
1292 if (it != m_elements.end()) {
1293 auto element = it.value();
1294 auto container = getDescribedByContainer(iface);
1295 if (!container.isUndefined()) {
1296 std::ostringstream oss;
1297 oss <<
"dbid_" << (
void *)iface;
1298 auto id = oss.str();
1299 auto describedBy = container.call<emscripten::val>(
"querySelector",
"#" + std::string(id));
1300 if (!describedBy.isUndefined() && !describedBy.isNull() &&
1301 !describedBy[
"parentElement"].isUndefined() && !describedBy[
"parentElement"].isNull())
1302 describedBy[
"parentElement"].call<
void>(
"removeChild", describedBy);
1304 if (!element[
"parentElement"].isUndefined() && !element[
"parentElement"].isNull())
1305 element[
"parentElement"].call<
void>(
"removeChild", element);
1306 m_elements.erase(it);
1310void QWasmAccessibility::unlinkParentForChildren(QAccessibleInterface *iface)
1312 auto element = getHtmlElement(iface);
1313 if (!element.isUndefined()) {
1314 auto oldContainer = element[
"parentElement"];
1315 auto newContainer = getElementContainer(iface);
1316 if (!oldContainer.isUndefined() &&
1317 !oldContainer.isNull() &&
1318 oldContainer != newContainer) {
1319 oldContainer.call<
void>(
"removeChild", element);
1322 for (
int i = 0; i < iface->childCount(); ++i)
1323 unlinkParentForChildren(iface->child(i));
1326void QWasmAccessibility::relinkParentForChildren(QAccessibleInterface *iface)
1328 auto element = getHtmlElement(iface);
1329 if (!element.isUndefined()) {
1330 if (element[
"parentElement"].isUndefined() ||
1331 element[
"parentElement"].isNull()) {
1332 linkToParent(iface);
1335 for (
int i = 0; i < iface->childCount(); ++i)
1336 relinkParentForChildren(iface->child(i));
1339void QWasmAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
1341 if (handleUpdateByEventType(event))
1342 handleUpdateByInterfaceRole(event);
1345bool QWasmAccessibility::handleUpdateByEventType(QAccessibleEvent *event)
1347 if (!m_accessibilityEnabled)
1350 QAccessibleInterface *iface = event->accessibleInterface();
1352 qWarning() <<
"handleUpdateByEventType with null a11y interface" << event->type() << event->object();
1357 switch (event->type()) {
1358 case QAccessible::ObjectCreated:
1363 case QAccessible::ObjectDestroyed:
1366 removeObject(iface);
1369 case QAccessible::ObjectShow:
1370 createObject(iface);
1373 case QAccessible::ParentChanged:
1374 unlinkParentForChildren(iface);
1375 relinkParentForChildren(iface);
1382 if (getHtmlElement(iface).isUndefined())
1387 switch (event->type()) {
1388 case QAccessible::Announcement:
1389 handleAnnouncement(iface,
static_cast<QAccessibleAnnouncementEvent *>(event));
1392 case QAccessible::StateChanged: {
1393 QAccessibleStateChangeEvent *stateChangeEvent = (QAccessibleStateChangeEvent *)event;
1394 if (stateChangeEvent->changedStates().disabled)
1395 setHtmlElementDisabled(iface);
1398 case QAccessible::DescriptionChanged:
1399 handleDescriptionChanged(iface);
1402 case QAccessible::Focus:
1405 setHtmlElementGeometry(iface);
1406 setHtmlElementFocus(iface);
1409 case QAccessible::IdentifierChanged:
1410 handleIdentifierUpdate(iface);
1413 case QAccessible::ObjectShow:
1414 linkToParent(iface);
1415 setHtmlElementVisibility(iface,
true);
1418 setHtmlElementGeometry(iface);
1419 sendEvent(iface, QAccessible::NameChanged);
1422 case QAccessible::ObjectHide:
1424 setHtmlElementVisibility(iface,
false);
1427 case QAccessible::LocationChanged:
1428 setHtmlElementGeometry(iface);
1439void QWasmAccessibility::handleUpdateByInterfaceRole(QAccessibleEvent *event)
1441 if (!m_accessibilityEnabled)
1444 QAccessibleInterface *iface = event->accessibleInterface();
1446 qWarning() <<
"handleUpdateByInterfaceRole with null a11y interface" << event->type() << event->object();
1452 switch (iface->role()) {
1453 case QAccessible::List:
1455 case QAccessible::ListItem:
1456 handleListItemUpdate(event);
1458 case QAccessible::StaticText:
1459 handleStaticTextUpdate(event);
1461 case QAccessible::Button:
1462 handleButtonUpdate(event);
1464 case QAccessible::CheckBox:
1465 if (iface->childCount() > 0)
1466 handleGroupBoxUpdate(event);
1468 handleCheckBoxUpdate(event);
1470 case QAccessible::Switch:
1471 handleSwitchUpdate(event);
1473 case QAccessible::EditableText:
1474 handleLineEditUpdate(event);
1476 case QAccessible::Dialog:
1477 handleDialogUpdate(event);
1479 case QAccessible::MenuItem:
1480 case QAccessible::MenuBar:
1481 case QAccessible::PopupMenu:
1482 handleMenuUpdate(event);
1484 case QAccessible::ToolBar:
1485 case QAccessible::ButtonMenu:
1486 handleToolUpdate(event);
1487 case QAccessible::RadioButton:
1488 handleRadioButtonUpdate(event);
1490 case QAccessible::Dial:
1491 handleDialUpdate(event);
1493 case QAccessible::SpinBox:
1494 handleSpinBoxUpdate(event);
1496 case QAccessible::Slider:
1497 handleSliderUpdate(event);
1499 case QAccessible::PageTab:
1500 handlePageTabUpdate(event);
1502 case QAccessible::PageTabList:
1503 handlePageTabListUpdate(event);
1505 case QAccessible::ScrollBar:
1506 handleScrollBarUpdate(event);
1508 case QAccessible::Grouping:
1509 handleGroupBoxUpdate(event);
1511 case QAccessible::ProgressBar:
1512 handleProgressBarUpdate(event);
1515 qCDebug(lcQpaAccessibility) <<
"TODO: implement notifyAccessibilityUpdate for role" << iface->role();
1519void QWasmAccessibility::setRootObject(QObject *root)
1521 m_rootObject = root;
1524void QWasmAccessibility::initialize()
1529void QWasmAccessibility::cleanup()
void QWasmAccessibilityEnable()