Merge pull request #636 from GriffinRichards/windowsize

Resize main window to fit available screen geometry
This commit is contained in:
GriffinR 2025-05-08 22:29:58 -04:00 committed by GitHub
commit cc547b7cb6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 9 deletions

View File

@ -612,13 +612,32 @@ void MainWindow::loadUserSettings() {
}
void MainWindow::restoreWindowState() {
logInfo("Restoring main window geometry from previous session.");
QMap<QString, QByteArray> geometry = porymapConfig.getMainGeometry();
this->restoreGeometry(geometry.value("main_window_geometry"));
this->restoreState(geometry.value("main_window_state"));
this->ui->splitter_map->restoreState(geometry.value("map_splitter_state"));
this->ui->splitter_main->restoreState(geometry.value("main_splitter_state"));
this->ui->splitter_Metatiles->restoreState(geometry.value("metatiles_splitter_state"));
const QByteArray mainWindowGeometry = geometry.value("main_window_geometry");
if (!mainWindowGeometry.isEmpty()) {
logInfo("Restoring main window geometry from previous session.");
restoreGeometry(mainWindowGeometry);
restoreState(geometry.value("main_window_state"));
ui->splitter_map->restoreState(geometry.value("map_splitter_state"));
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) {

View File

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