Ensure the window is not out of available geometry on geometry restoring

This commit is contained in:
Ilya Fedin 2021-01-31 12:14:22 +04:00 committed by John Preston
parent 9c86755546
commit f51055d606
1 changed files with 33 additions and 3 deletions

View File

@ -466,14 +466,44 @@ void MainWindow::initSize() {
for (auto screen : QGuiApplication::screens()) { for (auto screen : QGuiApplication::screens()) {
if (position.moncrc == screenNameChecksum(screen->name())) { if (position.moncrc == screenNameChecksum(screen->name())) {
auto screenGeometry = screen->geometry(); auto screenGeometry = screen->geometry();
auto availableGeometry = screen->availableGeometry();
DEBUG_LOG(("Window Pos: Screen found, screen geometry: %1, %2, %3, %4").arg(screenGeometry.x()).arg(screenGeometry.y()).arg(screenGeometry.width()).arg(screenGeometry.height())); DEBUG_LOG(("Window Pos: Screen found, screen geometry: %1, %2, %3, %4").arg(screenGeometry.x()).arg(screenGeometry.y()).arg(screenGeometry.width()).arg(screenGeometry.height()));
auto w = screenGeometry.width(), h = screenGeometry.height(); const auto x = availableGeometry.x() - screenGeometry.x();
const auto y = availableGeometry.y() - screenGeometry.y();
const auto w = availableGeometry.width();
const auto h = availableGeometry.height();
if (w >= st::windowMinWidth && h >= st::windowMinHeight) { if (w >= st::windowMinWidth && h >= st::windowMinHeight) {
if (position.x < 0) position.x = 0; if (position.x < x) position.x = x;
if (position.y < 0) position.y = 0; if (position.y < y) position.y = y;
if (position.w > w) position.w = w; if (position.w > w) position.w = w;
if (position.h > h) position.h = h; if (position.h > h) position.h = h;
const auto rightPoint = position.x + position.w;
if (rightPoint > w) {
const auto distance = rightPoint - w;
const auto newXPos = position.x - distance;
if (newXPos >= 0) {
position.x = newXPos;
} else {
position.x = 0;
const auto newRightPoint = position.x + position.w;
const auto newDistance = newRightPoint - w;
position.w -= newDistance;
}
}
const auto bottomPoint = position.y + position.h;
if (bottomPoint > h) {
const auto distance = bottomPoint - h;
const auto newYPos = position.y - distance;
if (newYPos >= 0) {
position.y = newYPos;
} else {
position.y = 0;
const auto newBottomPoint = position.y + position.h;
const auto newDistance = newBottomPoint - h;
position.h -= newDistance;
}
}
position.x += screenGeometry.x(); position.x += screenGeometry.x();
position.y += screenGeometry.y(); position.y += screenGeometry.y();
if (position.x + st::windowMinWidth <= screenGeometry.x() + screenGeometry.width() && if (position.x + st::windowMinWidth <= screenGeometry.x() + screenGeometry.width() &&