From e73e0d2a8f3e7359a1f00cf1a5bf103f57ed0d5f Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Sat, 4 Apr 2026 09:38:59 +0200 Subject: [PATCH] Improve window state handling It fixes multi-screen setups support and fullscreen in Windows --- CHANGELOG.md | 2 + YACReader/configuration.cpp | 2 - YACReader/configuration.h | 2 - YACReader/main_window_viewer.cpp | 82 +++++++---------------------- YACReader/main_window_viewer.h | 9 ---- YACReaderLibrary/library_window.cpp | 65 ++++++----------------- YACReaderLibrary/library_window.h | 4 -- common/yacreader_global_gui.h | 1 - custom_widgets/whats_new_dialog.cpp | 3 ++ 9 files changed, 40 insertions(+), 130 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b90c75d6..beb27f04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Version counting is based on semantic versioning (Major.Feature.Patch) * Add a theme editor and support for custom themes. * The apps include 12 built in themes to pick from. * Add an application language setting with a system default option in YACReader and YACReaderLibrary. +* Fix fullscreen mode in Windows, interaction with the OS is now possible while the apps are in fullscreen. +* Improve support for multi-screen setups. ### All apps * Add support for user-installed Qt image format plugins via the shared `plugins/imageformats` folder in the YACReader settings directory. diff --git a/YACReader/configuration.cpp b/YACReader/configuration.cpp index 9e2b0d4a..a424aef7 100644 --- a/YACReader/configuration.cpp +++ b/YACReader/configuration.cpp @@ -32,8 +32,6 @@ void Configuration::load(QSettings *settings) settings->setValue(FLOW_TYPE, 0); if (!settings->contains(FULLSCREEN)) settings->setValue(FULLSCREEN, false); - if (!settings->contains(MAXIMIZED)) - settings->setValue(MAXIMIZED, false); if (!settings->contains(DOUBLE_PAGE)) settings->setValue(DOUBLE_PAGE, false); if (!settings->contains(SHOW_TOOLBARS)) diff --git a/YACReader/configuration.h b/YACReader/configuration.h index 2556b6fb..d872cd3e 100644 --- a/YACReader/configuration.h +++ b/YACReader/configuration.h @@ -77,8 +77,6 @@ public: QByteArray getGeometry() const { return settings->value(Y_WINDOW_GEOMETRY).toByteArray(); } void setGeometry(const QByteArray &g) { settings->setValue(Y_WINDOW_GEOMETRY, g); } - bool getMaximized() { return settings->value(MAXIMIZED).toBool(); } - void setMaximized(bool b) { settings->setValue(MAXIMIZED, b); } bool getDoublePage() { return settings->value(DOUBLE_PAGE).toBool(); } void setDoublePage(bool b) { settings->setValue(DOUBLE_PAGE, b); } bool getDoubleMangaPage() { return settings->value(DOUBLE_MANGA_PAGE).toBool(); } diff --git a/YACReader/main_window_viewer.cpp b/YACReader/main_window_viewer.cpp index 6070cd3c..b1ae8b2e 100644 --- a/YACReader/main_window_viewer.cpp +++ b/YACReader/main_window_viewer.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include @@ -190,7 +189,7 @@ void MainWindowViewer::setupUI() } int heightDesktopResolution = screen != nullptr ? screen->size().height() : 600; - int widthDesktopResolution = screen != nullptr ? screen->size().height() : 1024; + int widthDesktopResolution = screen != nullptr ? screen->size().width() : 1024; int height, width; height = static_cast(heightDesktopResolution * 0.84); width = static_cast(height * 0.70); @@ -198,6 +197,19 @@ void MainWindowViewer::setupUI() if (!restoreGeometry(conf.getGeometry())) { move(QPoint((widthDesktopResolution - width) / 2, ((heightDesktopResolution - height) - 40) / 2)); resize(QSize(width, height)); + } else { + // Guard against the window landing off-screen when a monitor is unplugged + // between sessions. Qt 6 tries to remap the geometry to the primary screen + // when the saved screen is gone, but the result can still be off-screen. + const QRect restored = geometry(); + const auto availableScreens = QApplication::screens(); + const bool onScreen = std::any_of( + availableScreens.cbegin(), availableScreens.cend(), + [&restored](QScreen *s) { return s->availableGeometry().intersects(restored); }); + if (!onScreen) { + const QRect avail = QApplication::primaryScreen()->availableGeometry(); + move(avail.center() - QPoint(width / 2, height / 2)); + } } had = new HelpAboutDialog(this); // TODO load data @@ -229,14 +241,8 @@ void MainWindowViewer::setupUI() viewer->setFocusPolicy(Qt::StrongFocus); - previousWindowFlags = windowFlags(); - previousPos = pos(); - previousSize = size(); - if (fullscreen) toFullScreen(); - if (conf.getMaximized()) - showMaximized(); setAcceptDrops(true); @@ -1049,58 +1055,6 @@ void MainWindowViewer::toggleFullScreen() Configuration::getConfiguration().setFullScreen(fullscreen = !fullscreen); } -#ifdef Q_OS_WIN // fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309 - -void MainWindowViewer::toFullScreen() -{ - fromMaximized = this->isMaximized(); - - hideToolBars(); - viewer->hide(); - viewer->fullscreen = true; // TODO, change by the right use of windowState(); - - previousWindowFlags = windowFlags(); - previousPos = pos(); - previousSize = size(); - - showNormal(); - setWindowFlags(previousWindowFlags | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); - - QRect r = windowHandle()->screen()->geometry(); - - r.setHeight(r.height() + 1); - - setGeometry(r); - show(); - - viewer->show(); - if (viewer->magnifyingGlassIsVisible()) - viewer->showMagnifyingGlass(); -} - -void MainWindowViewer::toNormal() -{ - // show all - viewer->hide(); - viewer->fullscreen = false; // TODO, change by the right use of windowState(); - // viewer->hideMagnifyingGlass(); - - setWindowFlags(previousWindowFlags); - move(previousPos); - resize(previousSize); - show(); - - if (fromMaximized) - showMaximized(); - - if (Configuration::getConfiguration().getShowToolbars()) - showToolBars(); - viewer->show(); - if (viewer->magnifyingGlassIsVisible()) - viewer->showMagnifyingGlass(); -} - -#else void MainWindowViewer::toFullScreen() { fromMaximized = this->isMaximized(); @@ -1131,7 +1085,6 @@ void MainWindowViewer::toNormal() if (viewer->magnifyingGlassIsVisible()) viewer->showMagnifyingGlass(); } -#endif void MainWindowViewer::toggleToolBars() { @@ -1441,9 +1394,10 @@ void MainWindowViewer::closeEvent(QCloseEvent *event) viewer->save(); Configuration &conf = Configuration::getConfiguration(); - if (!fullscreen && !isMaximized()) - conf.setGeometry(saveGeometry()); - conf.setMaximized(isMaximized()); + // saveGeometry() encodes the current screen, the maximized/fullscreen state, and + // the normal (pre-maximize) geometry in one blob, so restoreGeometry() puts the + // window back on the right monitor in the right state regardless of how it was closed. + conf.setGeometry(saveGeometry()); event->accept(); } diff --git a/YACReader/main_window_viewer.h b/YACReader/main_window_viewer.h index b1c1b5d0..5cf50a31 100644 --- a/YACReader/main_window_viewer.h +++ b/YACReader/main_window_viewer.h @@ -91,10 +91,6 @@ private: bool toolbars; bool fromMaximized; - // QTBUG-41883 - QSize _size; - QPoint _pos; - QString currentDirectory; QString currentDirectoryImgDest; //! Widgets @@ -193,11 +189,6 @@ private: quint64 libraryId; OpenComicSource source; - // fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309 - Qt::WindowFlags previousWindowFlags; - QPoint previousPos; - QSize previousSize; - protected: void closeEvent(QCloseEvent *event) override; void sendComic(); diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index f7ceb374..57e6f5f7 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include @@ -215,11 +214,25 @@ void LibraryWindow::setupUI() setMinimumSize(800, 480); // restore - if (settings->contains(MAIN_WINDOW_GEOMETRY)) + if (settings->contains(MAIN_WINDOW_GEOMETRY)) { restoreGeometry(settings->value(MAIN_WINDOW_GEOMETRY).toByteArray()); - else + restoreState(settings->value(MAIN_WINDOW_STATE).toByteArray()); + // Guard against the window landing off-screen when a monitor is unplugged + // between sessions. Qt 6 tries to remap the geometry to the primary screen + // when the saved screen is gone, but the result can still be off-screen. + const QRect restored = geometry(); + const auto availableScreens = QApplication::screens(); + const bool onScreen = std::any_of( + availableScreens.cbegin(), availableScreens.cend(), + [&restored](QScreen *s) { return s->availableGeometry().intersects(restored); }); + if (!onScreen) { + const QRect avail = QApplication::primaryScreen()->availableGeometry(); + setGeometry(QRect(avail.center() - QPoint(width() / 2, height() / 2), size())); + } + } else { // if(settings->value(USE_OPEN_GL).toBool() == false) showMaximized(); + } trayIconController = new TrayIconController(settings, this); @@ -1988,49 +2001,6 @@ void LibraryWindow::toggleFullScreen() fullscreen = !fullscreen; } -#ifdef Q_OS_WIN // fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309 -void LibraryWindow::toFullScreen() -{ - fromMaximized = this->isMaximized(); - - sideBar->hide(); - libraryToolBar->hide(); - - previousWindowFlags = windowFlags(); - previousPos = pos(); - previousSize = size(); - - showNormal(); - setWindowFlags(previousWindowFlags | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); - - QRect r = windowHandle()->screen()->geometry(); - - r.setHeight(r.height() + 1); - - setGeometry(r); - show(); - - contentViewsManager->toFullscreen(); -} - -void LibraryWindow::toNormal() -{ - sideBar->show(); - libraryToolBar->show(); - - setWindowFlags(previousWindowFlags); - move(previousPos); - resize(previousSize); - show(); - - if (fromMaximized) - showMaximized(); - - contentViewsManager->toNormal(); -} - -#else - void LibraryWindow::toFullScreen() { fromMaximized = this->isMaximized(); @@ -2065,8 +2035,6 @@ void LibraryWindow::toNormal() #endif } -#endif - void LibraryWindow::setSearchFilter(QString filter) { if (!filter.isEmpty()) { @@ -2399,6 +2367,7 @@ void LibraryWindow::prepareToCloseApp() librariesUpdateCoordinator->stop(); settings->setValue(MAIN_WINDOW_GEOMETRY, saveGeometry()); + settings->setValue(MAIN_WINDOW_STATE, saveState()); contentViewsManager->comicsView->close(); sideBar->close(); diff --git a/YACReaderLibrary/library_window.h b/YACReaderLibrary/library_window.h index ec25b09c..11656785 100644 --- a/YACReaderLibrary/library_window.h +++ b/YACReaderLibrary/library_window.h @@ -348,10 +348,6 @@ private: //! @return true If the search mode was active when this function was called. bool exitSearchMode(); - // fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309 - Qt::WindowFlags previousWindowFlags; - QPoint previousPos; - QSize previousSize; std::future upgradeLibraryFuture; TrayIconController *trayIconController; diff --git a/common/yacreader_global_gui.h b/common/yacreader_global_gui.h index e8e1c419..b567e6fe 100644 --- a/common/yacreader_global_gui.h +++ b/common/yacreader_global_gui.h @@ -18,7 +18,6 @@ #define FLOW_TYPE "FLOW_TYPE" #define FULLSCREEN "FULLSCREEN" #define Y_WINDOW_GEOMETRY "GEOMETRY" -#define MAXIMIZED "MAXIMIZED" #define CLOSE_TO_TRAY "CLOSE_TO_TRAY" #define CLOSE_TO_TRAY_NOTIFIED "CLOSE_TO_TRAY_NOTIFIED" #define START_TO_TRAY "START_TO_TRAY" diff --git a/custom_widgets/whats_new_dialog.cpp b/custom_widgets/whats_new_dialog.cpp index 7fd3be6b..dda8a179 100644 --- a/custom_widgets/whats_new_dialog.cpp +++ b/custom_widgets/whats_new_dialog.cpp @@ -55,7 +55,10 @@ YACReader::WhatsNewDialog::WhatsNewDialog(QWidget *parent) " • Migrate Flow implementation from OpenGL to QRhi. This is a full new implementation with better performance and compatibility with operating systems and hardware
" " • Add light/dark themes support that follow the system configuration
" " • Add a theme editor and support for custom themes
" + " • The apps include 12 built in themes to pick from
" " • Add an application language setting with a system default option in YACReader and YACReaderLibrary
" + " • Fix fullscreen mode in Windows, interaction with the OS is now possible while the apps are in fullscreen
" + " • Improve support for multi-screen setups
" "
" "All apps
" " • Add support for user-installed Qt image format plugins via the shared plugins/imageformats folder in the YACReader settings directory
"