Improve window state handling

It fixes multi-screen setups support and fullscreen in Windows
This commit is contained in:
luisangelsm
2026-04-04 09:38:59 +02:00
parent 4bcdb3c7da
commit e73e0d2a8f
9 changed files with 40 additions and 130 deletions

View File

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

View File

@ -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(); }

View File

@ -30,7 +30,6 @@
#include <QMessageBox>
#include <QStandardPaths>
#include <QToolButton>
#include <QWindow>
#include <algorithm>
#include <utility>
@ -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<int>(heightDesktopResolution * 0.84);
width = static_cast<int>(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();
}

View File

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