From c97e58bb61fc1d732df53347c22376b9610308e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 27 Aug 2024 11:20:59 +0200 Subject: [PATCH] Reset QWindow positionPolicy on destroy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a window is positioned with setFramePosition, we reflect that to the platform window via QPlatformWindow::setGeometry(), but with the QWindowPrivate::positionPolicy set to WindowFrameInclusive. On the platform side the position policy is read from QWindowPrivate to determine how to interpret the incoming geometry. When the resulting geometry is applied to the native window, it's reflected through QPlatformWindow::geometry() and reported back via QWSI::handleGeometryChange() as client geometry, ie. without the frame margins. If the platform layer later interprets this geometry in combination with the window's positionPolicy, it will incorrectly position the frame top left at the point of the client geometry's top left. It would make sense to reset the window's positionPolicy at the point of QGuiApplicationPrivate::processGeometryChangeEvent, but doing so might break situations where there's another in flight geometry request. What we can do as a first step is reset the positionPolicy when the platform window is destroyed. This ensures that the next time the platform window is created it will be placed at the same position, even if the initial position was requested via the setFramePosition() API. Change-Id: Ic555f19884ccd68c46421d0fe666717170ba7638 Reviewed-by: Oliver Wolff Reviewed-by: Tor Arne Vestbø Reviewed-by: Volker Hilsheimer (cherry picked from commit 11a09a9bd0a7e899a918b078a94c6c3b82240dee) Reviewed-by: Qt Cherry-pick Bot --- src/gui/kernel/qwindow.cpp | 5 +++++ tests/auto/gui/kernel/qwindow/tst_qwindow.cpp | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index cd3af8598dd..ea8c0be35e9 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -2125,6 +2125,11 @@ void QWindowPrivate::destroy() resizeEventPending = true; receivedExpose = false; exposed = false; + + // Position set via setFramePosition will have propagated back to + // our geometry member as client geometry, so when creating the + // window again we need to ensure the policy matches that. + positionPolicy = QWindowPrivate::WindowFrameExclusive; } /*! diff --git a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp index 23f99dad5f9..bf214cead5c 100644 --- a/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp +++ b/tests/auto/gui/kernel/qwindow/tst_qwindow.cpp @@ -47,6 +47,7 @@ private slots: void positioning(); void framePositioning(); void framePositioning_data(); + void framePositioningStableAfterDestroy(); void positioningDuringMinimized(); void childWindowPositioning_data(); void childWindowPositioning(); @@ -666,6 +667,23 @@ void tst_QWindow::framePositioning() QTRY_COMPARE(screenCenterAdjusted, window.position()); } +void tst_QWindow::framePositioningStableAfterDestroy() +{ + QWindow window; + window.setFramePosition(QPoint(100, 100)); + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + + const QPoint stablePosition = window.position(); + const QPoint stableFramePosition = window.framePosition(); + + window.destroy(); + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QCOMPARE(window.position(), stablePosition); + QCOMPARE(window.framePosition(), stableFramePosition); +} + void tst_QWindow::positioningDuringMinimized() { // QTBUG-39544, setting a geometry in minimized state should work as well.