macOS: Use enumerateWindowsWithOptions to implement QCocoaScreen::topLevelAt()

The [NSWidow windowNumberAtPoint:belowWindowWithWindowNumber] API has issues
with sometimes being out of sync with the window server, resulting in failing
to hit test windows that we know are there.

This has manifested in flakeyness in our tests, for example in tst_QWindow's
testsInputEvents: http://testresults.qt.io/grafana/goto/YNGj7TgIg

A workaround is to call [NSWindow windowNumbersWithOptions:0] to force a sync,
but we might as well use the more modern block based API to iterate our own
windows in Z-order. This API seems to do the required sync on our behalf,
or at least doesn't operate on stale data.

The logic has been otherwise kept as is, including treating non-top-level
windows as candidates for hit testing, which seems strange for a function
named topLevelAt(). This is to be investigated further.

Task-number: QTBUG-108402
Task-number: QTBUG-115945
Change-Id: I5599881c381a0a673d262e4b9585e2c6798c9810
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
(cherry picked from commit 108d2e44867acfa98c3b0c211d9b48f39d10efa9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-08-21 17:24:40 +02:00 committed by Qt Cherry-pick Bot
parent 61c8e411b5
commit 04efbda0df

View File

@ -15,6 +15,7 @@
#include <IOKit/graphics/IOGraphicsLib.h> #include <IOKit/graphics/IOGraphicsLib.h>
#include <QtGui/private/qwindow_p.h> #include <QtGui/private/qwindow_p.h>
#include <QtGui/private/qhighdpiscaling_p.h>
#include <QtCore/private/qcore_mac_p.h> #include <QtCore/private/qcore_mac_p.h>
#include <QtCore/private/qeventdispatcher_cf_p.h> #include <QtCore/private/qeventdispatcher_cf_p.h>
@ -523,39 +524,36 @@ QPlatformScreen::SubpixelAntialiasingType QCocoaScreen::subpixelAntialiasingType
QWindow *QCocoaScreen::topLevelAt(const QPoint &point) const QWindow *QCocoaScreen::topLevelAt(const QPoint &point) const
{ {
NSPoint screenPoint = mapToNative(point); __block QWindow *window = nullptr;
[NSApp enumerateWindowsWithOptions:NSWindowListOrderedFrontToBack
usingBlock:^(NSWindow *nsWindow, BOOL *stop) {
if (!nsWindow)
return;
// Search (hit test) for the top-level window. [NSWidow windowNumberAtPoint: // Continue the search if the window does not belong to Qt
// belowWindowWithWindowNumber] may return windows that are not interesting if (![nsWindow conformsToProtocol:@protocol(QNSWindowProtocol)])
// to Qt. The search iterates until a suitable window or no window is found. return;
NSInteger topWindowNumber = 0;
QWindow *window = nullptr;
do {
// Get the top-most window, below any previously rejected window.
topWindowNumber = [NSWindow windowNumberAtPoint:screenPoint
belowWindowWithWindowNumber:topWindowNumber];
// Continue the search if the window does not belong to this process. QCocoaWindow *cocoaWindow = qnsview_cast(nsWindow.contentView).platformWindow;
NSWindow *nsWindow = [NSApp windowWithWindowNumber:topWindowNumber]; if (!cocoaWindow)
if (!nsWindow) return;
continue;
// Continue the search if the window does not belong to Qt. QWindow *w = cocoaWindow->window();
if (![nsWindow conformsToProtocol:@protocol(QNSWindowProtocol)]) if (!w->isVisible())
continue; return;
QCocoaWindow *cocoaWindow = qnsview_cast(nsWindow.contentView).platformWindow; if (!QHighDpi::toNativePixels(w->geometry(), w).contains(point))
if (!cocoaWindow) return;
continue;
window = cocoaWindow->window();
// Continue the search if the window is not a top-level window. window = w;
if (!window->isTopLevel())
continue;
// Stop searching. The current window is the correct window. // Continue the search if the window is not a top-level window
break; if (!window->isTopLevel())
} while (topWindowNumber > 0); return;
*stop = true;
}
];
return window; return window;
} }