macOS: Simplify QCocoaWindow::handleGeometryChange

There are really only two cases here, where the difference
is the coordinate system of the window position.

1) Child QWindow and embedded QWindow:
   The position is relative to parent view/window origin.
2) Top-level QWindow:
   The position is relative to screen origin.

Change-Id: I867133a5adbbf3a690f574aec06b70c2bc64ad95
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Morten Johan Sørvig 2017-06-02 11:37:53 +02:00 committed by Tor Arne Vestbø
parent 6be94e8f6d
commit 0e0034d93a
3 changed files with 21 additions and 12 deletions

View File

@ -92,6 +92,8 @@ public:
QPointF mapFromNative(const QPointF &pos) const { return flipCoordinate(pos); }
QRectF mapFromNative(const QRectF &rect) const { return flipCoordinate(rect); }
static QCocoaScreen *primaryScreen();
private:
QPointF flipCoordinate(const QPointF &pos) const;
QRectF flipCoordinate(const QRectF &rect) const;

View File

@ -145,7 +145,7 @@ void QCocoaScreen::updateGeometry()
// we may be in the process of creating and registering the primary screen, we
// must special-case that and assign it direcly.
QCocoaScreen *primaryScreen = (nsScreen == [[NSScreen screens] firstObject]) ?
this : static_cast<QCocoaScreen*>(QGuiApplication::primaryScreen()->handle());
this : QCocoaScreen::primaryScreen();
m_geometry = primaryScreen->mapFromNative(m_geometry).toRect();
m_availableGeometry = primaryScreen->mapFromNative(m_availableGeometry).toRect();
@ -293,6 +293,14 @@ QPixmap QCocoaScreen::grabWindow(WId window, int x, int y, int width, int height
return windowPixmap;
}
/*!
The screen used as a reference for global window geometry
*/
QCocoaScreen *QCocoaScreen::primaryScreen()
{
return static_cast<QCocoaScreen *>(QGuiApplication::primaryScreen()->handle());
}
static QCocoaIntegration::Options parseOptions(const QStringList &paramList)
{
QCocoaIntegration::Options options;

View File

@ -1043,19 +1043,18 @@ void QCocoaWindow::handleGeometryChange()
if (m_inSetStyleMask && !m_view.window)
return;
QRect newGeometry;
const bool isEmbedded = m_viewIsToBeEmbedded || m_viewIsEmbedded;
if (isContentView()) {
// Top level window, get window rect and flip y
NSRect rect = m_view.frame;
NSRect windowRect = m_view.window.frame;
newGeometry = QRect(windowRect.origin.x, qt_mac_flipYCoordinate(windowRect.origin.y + rect.size.height), rect.size.width, rect.size.height);
} else if (m_viewIsToBeEmbedded) {
// Embedded child window, use the frame rect ### merge with case below
newGeometry = QRectF::fromCGRect(NSRectToCGRect(m_view.bounds)).toRect();
QRect newGeometry;
if (isContentView() && !isEmbedded) {
// Content views are positioned at (0, 0) in the window, so we resolve via the window
CGRect contentRect = [m_view.window contentRectForFrameRect:m_view.window.frame];
// The result above is in native screen coordinates, so remap to the Qt coordinate system
newGeometry = QCocoaScreen::primaryScreen()->mapFromNative(QRectF::fromCGRect(contentRect)).toRect();
} else {
// Child window, use the frame rect
newGeometry = QRectF::fromCGRect(NSRectToCGRect(m_view.frame)).toRect();
// QNSView has isFlipped set, so no need to remap the geometry
newGeometry = QRectF::fromCGRect(m_view.frame).toRect();
}
qCDebug(lcQpaCocoaWindow) << "QCocoaWindow::handleGeometryChange" << window()