From 362dcb4759112a79462d7019e42fbe711eed58a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 30 Aug 2017 19:48:26 +0200 Subject: macOS: Respect responder chain when setting cursor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There's no need for us to walk our own ancestor chain to figure out which cursor to set. AppKit will automatically call cursorUpdate: on the view that would be the hitTest target of the current mouse position, and by falling back to super when no cursor is set for the current view, we automatically get the behavior that effectiveWindowCursor tried to solve. In addition, it solves the case of applyEffectiveWindowCursor applying the arrowCursor when no cursor was set, which would mean that if any native parent view of our view _did_ have a cursor set, we would not fall back to the native view's cursor, but instead override it with the arrow cursor. Following the responder chain gives the correct behavior in this case. Unfortunately, due to rdar://34183708, if a subview of one of our views uses the legacy cursorRect approach to cursor management, the cursor will not be reset back to our cursor via cursorUpdate: when leaving the child and entering the parent view (our view). Moving our implementation over to the legacy API would solve this problem, but just propagate it to native parent views of our views, which could potentially use NSTrackingAreas, and would not have _their_ cursors re-set. Change-Id: Id20cc03136f0b1d4b9120750fe63ddc455363aaf Reviewed-by: Tor Arne Vestbø --- tests/manual/cocoa/qt_on_cocoa/main.mm | 18 +++++++++++++++++- tests/manual/cocoa/qt_on_cocoa/rasterwindow.cpp | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'tests/manual/cocoa') diff --git a/tests/manual/cocoa/qt_on_cocoa/main.mm b/tests/manual/cocoa/qt_on_cocoa/main.mm index f99406f619..5e3b8fcd39 100644 --- a/tests/manual/cocoa/qt_on_cocoa/main.mm +++ b/tests/manual/cocoa/qt_on_cocoa/main.mm @@ -42,6 +42,12 @@ [[NSColor whiteColor] setFill]; NSRectFill(dirtyRect); } + +- (void)cursorUpdate:(NSEvent *)theEvent +{ + Q_UNUSED(theEvent); + [[NSCursor pointingHandCursor] set]; +} @end @interface AppDelegate : NSObject { @@ -76,18 +82,28 @@ [window setTitle:title]; [window setBackgroundColor:[NSColor blueColor]]; - window.contentView = [[[ContentView alloc] initWithFrame:frame] autorelease]; + NSView *contentView = [[[ContentView alloc] initWithFrame:frame] autorelease]; + [contentView addTrackingArea:[[NSTrackingArea alloc] initWithRect:[contentView frame] + options:NSTrackingActiveInActiveApp | NSTrackingInVisibleRect | NSTrackingCursorUpdate + owner:contentView userInfo:nil]]; + + window.contentView = contentView; // Create the QWindow, add its NSView to the content view m_window = new RasterWindow; m_window->setObjectName("RasterWindow"); + m_window->setCursor(Qt::CrossCursor); m_window->setGeometry(QRect(0, 0, 300, 300)); QWindow *childWindow = new RasterWindow; childWindow->setObjectName("RasterWindowChild"); childWindow->setParent(m_window); + childWindow->setCursor(Qt::BusyCursor); childWindow->setGeometry(50, 50, 100, 100); + NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 80, 25)]; + [(NSView*)childWindow->winId() addSubview:textField]; + [window.contentView addSubview:reinterpret_cast(m_window->winId())]; // Show the NSWindow diff --git a/tests/manual/cocoa/qt_on_cocoa/rasterwindow.cpp b/tests/manual/cocoa/qt_on_cocoa/rasterwindow.cpp index 8a451d5f7c..dca39839dd 100644 --- a/tests/manual/cocoa/qt_on_cocoa/rasterwindow.cpp +++ b/tests/manual/cocoa/qt_on_cocoa/rasterwindow.cpp @@ -64,6 +64,7 @@ void RasterWindow::initialize() void RasterWindow::mousePressEvent(QMouseEvent *event) { m_lastPos = event->pos(); + unsetCursor(); } void RasterWindow::mouseMoveEvent(QMouseEvent *event) -- cgit v1.2.3