macOS: Respect responder chain when setting cursor

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ø <tor.arne.vestbo@qt.io>
This commit is contained in:
Tor Arne Vestbø 2017-08-30 19:48:26 +02:00
parent ca14d84197
commit 362dcb4759
6 changed files with 38 additions and 47 deletions

View File

@ -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;

View File

@ -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<QCocoaWindow *>(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)

View File

@ -82,6 +82,8 @@ Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(QNSViewMouseMoveHelper));
QSet<quint32> m_acceptedKeyDowns;
}
@property (nonatomic, retain) NSCursor *cursor;
- (id)init;
- (id)initWithCocoaWindow:(QCocoaWindow *)platformWindow;
#ifndef QT_NO_OPENGL

View File

@ -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

View File

@ -42,6 +42,12 @@
[[NSColor whiteColor] setFill];
NSRectFill(dirtyRect);
}
- (void)cursorUpdate:(NSEvent *)theEvent
{
Q_UNUSED(theEvent);
[[NSCursor pointingHandCursor] set];
}
@end
@interface AppDelegate : NSObject <NSApplicationDelegate> {
@ -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<NSView *>(m_window->winId())];
// Show the NSWindow

View File

@ -64,6 +64,7 @@ void RasterWindow::initialize()
void RasterWindow::mousePressEvent(QMouseEvent *event)
{
m_lastPos = event->pos();
unsetCursor();
}
void RasterWindow::mouseMoveEvent(QMouseEvent *event)