From 2030dac11828740aee3335fc05d063e7924a4a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Wed, 26 Aug 2026 18:02:22 +0200 Subject: [PATCH] Fixed restoring of the layout for maximized windows on startup The layout was restored from the second resize event (#590) or after a 200 ms timeout. When the timeout was hit, the layout of the nested editor main windows was clamped to the initial window size (#4580). Now the layout is restored synchronously and, since a maximized window only reaches its final size once the event loop has started, again from the first spontaneous resize event. On Windows the pending window system events are flushed instead, so that the layout and session are restored with the window already at its final size. Not needed on macOS, where the final size is reached synchronously. Resolves #4580 --- NEWS.md | 1 + src/tiled/mainwindow.cpp | 28 +++++++++++++++------------- src/tiled/mainwindow.h | 2 +- src/tiledapp/main.cpp | 7 +++++++ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/NEWS.md b/NEWS.md index 754f0e6187..714bb2f432 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,7 @@ * Scripting: Added MapObject.resolvedClassName() (by MatusGuy, #4529) * Fixed crash when the selection becomes empty while starting a move (#4536) * Fixed Properties view update on 'Reset Template Instance' and 'Replace With Template' actions +* Fixed restoring of the layout for maximized windows on startup (#4580) * Linux: Added file associations for .tmj, .tsj, .tiled-project and .world files (by miffe, #4550) * Linux: Fixed the window icon on some Wayland compositors (by Balló György, #4567) * macOS: Declared UTIs and file associations for all supported Tiled formats (with Jyotish, #4469) diff --git a/src/tiled/mainwindow.cpp b/src/tiled/mainwindow.cpp index c05b77ec77..8835bc1bd0 100644 --- a/src/tiled/mainwindow.cpp +++ b/src/tiled/mainwindow.cpp @@ -976,14 +976,14 @@ void MainWindow::dropEvent(QDropEvent *e) void MainWindow::resizeEvent(QResizeEvent *e) { - // When the window is maximized, we need to delay restoring the internal - // layout until after the second resize event (issue #590). This does not - // appear to affect macOS, where only a single resize event is observed. - if (!mHasRestoredLayout) -#ifndef Q_OS_MAC - if (!isMaximized() || e->oldSize().isValid()) -#endif + // A maximized window only reaches its final size with the first + // spontaneous resize event, so the layout may need to be restored again + // to avoid it being clamped to the normal geometry (#4580). + if (mReapplyLayoutOnResize && e->spontaneous()) { + mReapplyLayoutOnResize = false; + if (isMaximized()) restoreLayout(); + } if (mPopupWidget) updatePopupGeometry(e->size()); @@ -2344,8 +2344,14 @@ void MainWindow::readSettings() else resize(Utils::dpiScaled(QSize(1200, 700))); - // Make sure layout is restored eventually (see resizeEvent) - QTimer::singleShot(200, this, &MainWindow::restoreLayout); + restoreLayout(); + + // A maximized window does not have its final size yet (see resizeEvent). + // Not needed on macOS, where the size is final already, nor on Windows, + // where pending window system events are flushed first (see main.cpp). +#if !defined(Q_OS_MAC) && !defined(Q_OS_WIN) + mReapplyLayoutOnResize = isMaximized(); +#endif updateRecentFilesMenu(); updateRecentProjectsMenu(); @@ -2355,10 +2361,6 @@ void MainWindow::readSettings() void MainWindow::restoreLayout() { - if (mHasRestoredLayout) - return; - - mHasRestoredLayout = true; restoreState(preferences::mainWindowState); mDocumentManager->restoreState(); } diff --git a/src/tiled/mainwindow.h b/src/tiled/mainwindow.h index 8c1b7a4788..354f56eb28 100644 --- a/src/tiled/mainwindow.h +++ b/src/tiled/mainwindow.h @@ -272,7 +272,7 @@ class TILED_EDITOR_EXPORT MainWindow : public QMainWindow QPointer mPreferencesDialog; QMap mMainWindowStates; - bool mHasRestoredLayout = false; + bool mReapplyLayoutOnResize = false; SessionOption mLoadedWorlds { "loadedWorlds" }; diff --git a/src/tiledapp/main.cpp b/src/tiledapp/main.cpp index 1471a93a2c..c1785e7871 100644 --- a/src/tiledapp/main.cpp +++ b/src/tiledapp/main.cpp @@ -50,6 +50,7 @@ #include #include +#include #ifdef ERROR #undef ERROR @@ -594,6 +595,12 @@ int main(int argc, char *argv[]) MainWindow w; w.show(); +#ifdef Q_OS_WIN + // Let a maximized window reach its final size before the layout and + // session are restored (see MainWindow::resizeEvent) + QWindowSystemInterface::flushWindowSystemEvents(); +#endif + a.setActivationWindow(&w); #ifdef Q_OS_WIN using QWindowsApplication = QNativeInterface::Private::QWindowsApplication;