a11y atspi: Set correct child index in children-changed:add event

When sending an object:children-changed:add AT-SPI2 event,
set the actual index of the child instead of always using
the child count of its parent.

If the new child were added at the end, its index would be
"[child count] - 1", but the new child doesn't necessarily
have to have been added at the end.

Therefore, use the actual child index either already used as loop
variable or retrieved via QAccessibleInterface::indexOfChild
instead.

The mismatch could e.g. be demonstrated with a simple pyatspi
script when typing Enter in LibreOffice Writer to create
new paragraphs.

Script:

    #!/usr/bin/python3
    import pyatspi
    def listener(e):
        if not e.host_application.name.startswith('soffice'):
            return
        print(e)
        print(f'index in parent set in event: {e.detail1}')
        print(f'index in parent reported by child: {e.any_data.get_index_in_parent()}')
    pyatspi.Registry.registerEventListener(listener, "object:children-changed:add")
    pyatspi.Registry.start()

Sample output without this commit in place:

    object:children-changed:add(4, 0, [paragraph | ])
            source: [document frame | Untitled 1 - LibreOfficeDev Document]
            host_application: [application | soffice.bin]
            sender: [application | soffice.bin]
    index in parent set in event: 4
    index in parent reported by child: 2

Sample output with this commit in place:

    object:children-changed:add(2, 0, [paragraph | ])
            source: [document frame | Untitled 1 - LibreOfficeDev Document]
            host_application: [application | soffice.bin]
            sender: [application | soffice.bin]
    index in parent set in event: 2
    index in parent reported by child: 2

Pick-to: 6.10 6.9 6.8
Change-Id: I30316c59f8ad6fd018089a8dca8e7a8d1d92d7ec
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Michael Weghorn 2025-05-01 19:04:10 +02:00 committed by Volker Hilsheimer
parent e0130876ba
commit 5e7891f73f

View File

@ -1384,10 +1384,10 @@ void AtSpiAdaptor::sendFocusChanged(QAccessibleInterface *interface) const
void AtSpiAdaptor::childrenChanged(QAccessibleInterface *interface) const
{
QString parentPath = pathForInterface(interface);
int childCount = interface->childCount();
for (int i = 0; i < interface->childCount(); ++i) {
const int childCount = interface->childCount();
for (int i = 0; i < childCount; ++i) {
QString childPath = pathForInterface(interface->child(i));
QVariantList args = packDBusSignalArguments("add"_L1, childCount, 0, childPath);
QVariantList args = packDBusSignalArguments("add"_L1, i, 0, childPath);
sendDBusSignal(parentPath, ATSPI_DBUS_INTERFACE_EVENT_OBJECT ""_L1, "ChildrenChanged"_L1, args);
}
}
@ -1401,9 +1401,9 @@ void AtSpiAdaptor::notifyAboutCreation(QAccessibleInterface *interface) const
return;
}
QString path = pathForInterface(interface);
int childCount = parent->childCount();
const int childIndex = parent->indexOfChild(interface);
QString parentPath = pathForInterface(parent);
QVariantList args = packDBusSignalArguments("add"_L1, childCount, 0, variantForPath(path));
QVariantList args = packDBusSignalArguments("add"_L1, childIndex, 0, variantForPath(path));
sendDBusSignal(parentPath, ATSPI_DBUS_INTERFACE_EVENT_OBJECT ""_L1, "ChildrenChanged"_L1, args);
}