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: [PATCH] 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ø --- src/plugins/platforms/cocoa/qcocoawindow.h | 3 -- src/plugins/platforms/cocoa/qcocoawindow.mm | 48 +++---------------- src/plugins/platforms/cocoa/qnsview.h | 2 + src/plugins/platforms/cocoa/qnsview.mm | 13 ++++- tests/manual/cocoa/qt_on_cocoa/main.mm | 18 ++++++- .../manual/cocoa/qt_on_cocoa/rasterwindow.cpp | 1 + 6 files changed, 38 insertions(+), 47 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoawindow.h b/src/plugins/platforms/cocoa/qcocoawindow.h index 3357f91bede..c650c863797 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.h +++ b/src/plugins/platforms/cocoa/qcocoawindow.h @@ -175,8 +175,6 @@ public: void setMenubar(QCocoaMenuBar *mb); QCocoaMenuBar *menubar() const; - NSCursor *effectiveWindowCursor() const; - void applyEffectiveWindowCursor(); void setWindowCursor(NSCursor *cursor); void registerTouch(bool enable); @@ -258,7 +256,6 @@ public: // for QNSView QCocoaGLContext *m_glContext; #endif QCocoaMenuBar *m_menubar; - NSCursor *m_windowCursor; bool m_needsInvalidateShadow; diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 606a90c9af9..63ee8c10acf 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -150,7 +150,6 @@ QCocoaWindow::QCocoaWindow(QWindow *win, WId nativeHandle) , m_glContext(0) #endif , m_menubar(0) - , m_windowCursor(0) , m_needsInvalidateShadow(false) , m_hasModalSession(false) , m_frameStrutEventsEnabled(false) @@ -230,7 +229,6 @@ QCocoaWindow::~QCocoaWindow() [m_view release]; [m_nsWindow release]; - [m_windowCursor release]; } QSurfaceFormat QCocoaWindow::format() const @@ -1556,51 +1554,19 @@ QCocoaMenuBar *QCocoaWindow::menubar() const return m_menubar; } -// Finds the effective cursor for this window by walking up the -// ancestor chain (including this window) until a set cursor is -// found. Returns nil if there is not set cursor. -NSCursor *QCocoaWindow::effectiveWindowCursor() const -{ - - if (m_windowCursor) - return m_windowCursor; - if (!QPlatformWindow::parent()) - return nil; - return static_cast(QPlatformWindow::parent())->effectiveWindowCursor(); -} - -// Applies the cursor as returned by effectiveWindowCursor(), handles -// the special no-cursor-set case by setting the arrow cursor. -void QCocoaWindow::applyEffectiveWindowCursor() -{ - NSCursor *effectiveCursor = effectiveWindowCursor(); - if (effectiveCursor) { - [effectiveCursor set]; - } else { - // We wold like to _unset_ the cursor here; but there is no such - // API. Fall back to setting the default arrow cursor. - [[NSCursor arrowCursor] set]; - } -} - void QCocoaWindow::setWindowCursor(NSCursor *cursor) { - if (m_windowCursor == cursor) - return; - - // Setting a cursor in a foregin view is not supported. + // Setting a cursor in a foreign view is not supported if (isForeignWindow()) return; - [m_windowCursor release]; - m_windowCursor = cursor; - [m_windowCursor retain]; + QNSView *view = qnsview_cast(m_view); + if (cursor == view.cursor) + return; - // The installed view tracking area (see QNSView updateTrackingAreas) will - // handle cursor updates on mouse enter/leave. Handle the case where the - // mouse is on the this window by changing the cursor immediately. - if (m_windowUnderMouse) - applyEffectiveWindowCursor(); + view.cursor = cursor; + + [m_view.window invalidateCursorRectsForView:m_view]; } void QCocoaWindow::registerTouch(bool enable) diff --git a/src/plugins/platforms/cocoa/qnsview.h b/src/plugins/platforms/cocoa/qnsview.h index 004c9f3dabc..a27599b690b 100644 --- a/src/plugins/platforms/cocoa/qnsview.h +++ b/src/plugins/platforms/cocoa/qnsview.h @@ -82,6 +82,8 @@ Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(QNSViewMouseMoveHelper)); QSet m_acceptedKeyDowns; } +@property (nonatomic, retain) NSCursor *cursor; + - (id)init; - (id)initWithCocoaWindow:(QCocoaWindow *)platformWindow; #ifndef QT_NO_OPENGL diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm index fd7141d3392..666b3fe4456 100644 --- a/src/plugins/platforms/cocoa/qnsview.mm +++ b/src/plugins/platforms/cocoa/qnsview.mm @@ -155,6 +155,7 @@ static QTouchDevice *touchDevice = 0; m_isMenuView = false; self.focusRingType = NSFocusRingTypeNone; + self.cursor = nil; } return self; } @@ -774,8 +775,16 @@ static QTouchDevice *touchDevice = 0; - (void)cursorUpdate:(NSEvent *)theEvent { - Q_UNUSED(theEvent); - m_platformWindow->applyEffectiveWindowCursor(); + qCDebug(lcQpaCocoaWindow) << "[QNSView cursorUpdate:]" << self.cursor; + + // Note: We do not get this callback when moving from a subview that + // uses the legacy cursorRect API, so the cursor is reset to the arrow + // cursor. See rdar://34183708 + + if (self.cursor) + [self.cursor set]; + else + [super cursorUpdate:theEvent]; } - (void)mouseMovedImpl:(NSEvent *)theEvent diff --git a/tests/manual/cocoa/qt_on_cocoa/main.mm b/tests/manual/cocoa/qt_on_cocoa/main.mm index f99406f6198..5e3b8fcd39c 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 8a451d5f7ca..dca39839dda 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)