diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eb7efde9..f19fda3d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -602,11 +602,7 @@ void MainWindow::loadUserSettings() { void MainWindow::restoreWindowState() { QMap geometry = porymapConfig.getMainGeometry(); const QByteArray mainWindowGeometry = geometry.value("main_window_geometry"); - if (mainWindowGeometry.isEmpty()) { - // If there's no saved geometry for the main window (i.e., first show) then we show it maximized. - // This simplifies the problem of picking a good window size depending on the screen. - setWindowState(Qt::WindowMaximized | Qt::WindowActive); - } else { + if (!mainWindowGeometry.isEmpty()) { logInfo("Restoring main window geometry from previous session."); restoreGeometry(mainWindowGeometry); restoreState(geometry.value("main_window_state")); @@ -614,6 +610,22 @@ void MainWindow::restoreWindowState() { ui->splitter_main->restoreState(geometry.value("main_splitter_state")); ui->splitter_Metatiles->restoreState(geometry.value("metatiles_splitter_state")); } + + // Resize the window if it exceeds the available screen size. + auto screen = windowHandle() ? windowHandle()->screen() : QGuiApplication::primaryScreen(); + if (!screen) return; + const QRect screenGeometry = screen->availableGeometry(); + if (this->width() > screenGeometry.width() || this->height() > screenGeometry.height()) { + auto pixelRatio = screen->devicePixelRatio(); + logInfo(QString("Resizing main window. Dimensions of %1x%2 exceed available screen size of %3x%4") + .arg(qRound(this->width() * pixelRatio)) + .arg(qRound(this->height() * pixelRatio)) + .arg(qRound(screenGeometry.width() * pixelRatio)) + .arg(qRound(screenGeometry.height() * pixelRatio))); + resize(qMin(this->width(), screenGeometry.width()), + qMin(this->height(), screenGeometry.height())); + move(screenGeometry.center() - this->rect().center()); + } } void MainWindow::setTheme(QString theme) { diff --git a/src/ui/colorpicker.cpp b/src/ui/colorpicker.cpp index 5d2d3992..6967c59a 100644 --- a/src/ui/colorpicker.cpp +++ b/src/ui/colorpicker.cpp @@ -43,11 +43,11 @@ ColorPicker::~ColorPicker() void ColorPicker::hover(const QPoint &pos) { QScreen *screen = QGuiApplication::screenAt(pos); if (!screen) { + // Try the screen the color picker is on, or the primary screen. const QWindow *window = windowHandle(); - if (window) screen = window->screen(); + screen = (window && window->screen()) ? window->screen() : QGuiApplication::primaryScreen(); + if (!screen) return; } - if (!screen) - return; // 15 X 15 box with 8x magnification = 120px square) QPixmap grab = screen->grabWindow(0, pos.x() - zoom_box_dimensions / 2, pos.y() - zoom_box_dimensions / 2, zoom_box_dimensions, zoom_box_dimensions);