diff options
author | Jan Arve Sæther <jan-arve.saether@qt.io> | 2022-05-13 11:01:43 +0200 |
---|---|---|
committer | Jan Arve Sæther <jan-arve.saether@qt.io> | 2022-06-13 09:19:17 +0200 |
commit | e7477e8934ab38722f5589914d08b3f15e870109 (patch) | |
tree | 353a381e2aa714ef077b60ec26f91196741f8f70 /src/gui/kernel/qwindow.cpp | |
parent | e57ab1b454255b5d64b8463cc3ccdb96e7de836e (diff) |
Update window geometry when QWindow::m{in|ax}imumSize is updated
More specifically, if either minimum or maximum size are updated, make
sure that the current window geometry is updated so that it is within
the minimum and maximum sizes.
Previously, these constraints was only respected by the window manager
when the user resized the window.
For widgets this already worked, because a top-level widget will take
care of respecting these constraints if they are changed.
Since QWindow::setMinimumSize and QWindow::setMaximumSize started to
share so many common things, a new function (setMinOrMaxSize_helper())
is added.
Task-number: QTBUG-102771
Change-Id: Ia4b2680dcf865f84a3cf6424187f9a6036b76386
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/gui/kernel/qwindow.cpp')
-rw-r--r-- | src/gui/kernel/qwindow.cpp | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 4d863d088e..72c13c5f95 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -567,6 +567,36 @@ QRectF QWindowPrivate::closestAcceptableGeometry(const QRectF &rect) const return QRectF(); } +void QWindowPrivate::setMinOrMaxSize(QSize *oldSizeMember, const QSize &size, + std::function<void()> funcWidthChanged, + std::function<void()> funcHeightChanged) +{ + Q_Q(QWindow); + Q_ASSERT(oldSizeMember); + Q_ASSERT(funcWidthChanged && funcHeightChanged); + const QSize adjustedSize = + size.expandedTo(QSize(0, 0)).boundedTo(QSize(QWINDOWSIZE_MAX, QWINDOWSIZE_MAX)); + if (*oldSizeMember == adjustedSize) + return; + const bool widthChanged = adjustedSize.width() != oldSizeMember->width(); + const bool heightChanged = adjustedSize.height() != oldSizeMember->height(); + *oldSizeMember = adjustedSize; + + if (platformWindow && q->isTopLevel()) + platformWindow->propagateSizeHints(); + + if (widthChanged) + funcWidthChanged(); + if (heightChanged) + funcHeightChanged(); + + // resize window if current size is outside of min and max limits + if (minimumSize.width() <= maximumSize.width() + || minimumSize.height() <= maximumSize.height()) { + q->resize(q->geometry().size().expandedTo(minimumSize).boundedTo(maximumSize)); + } +} + /*! Sets the \a surfaceType of the window. @@ -1531,17 +1561,9 @@ QSize QWindow::sizeIncrement() const void QWindow::setMinimumSize(const QSize &size) { Q_D(QWindow); - QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX)); - if (d->minimumSize == adjustedSize) - return; - QSize oldSize = d->minimumSize; - d->minimumSize = adjustedSize; - if (d->platformWindow && isTopLevel()) - d->platformWindow->propagateSizeHints(); - if (d->minimumSize.width() != oldSize.width()) - emit minimumWidthChanged(d->minimumSize.width()); - if (d->minimumSize.height() != oldSize.height()) - emit minimumHeightChanged(d->minimumSize.height()); + d->setMinOrMaxSize( + &d->minimumSize, size, [=]() { emit minimumWidthChanged(d->minimumSize.width()); }, + [=]() { emit minimumHeightChanged(d->minimumSize.height()); }); } /*! @@ -1618,17 +1640,9 @@ void QWindow::setMinimumHeight(int h) void QWindow::setMaximumSize(const QSize &size) { Q_D(QWindow); - QSize adjustedSize = QSize(qBound(0, size.width(), QWINDOWSIZE_MAX), qBound(0, size.height(), QWINDOWSIZE_MAX)); - if (d->maximumSize == adjustedSize) - return; - QSize oldSize = d->maximumSize; - d->maximumSize = adjustedSize; - if (d->platformWindow && isTopLevel()) - d->platformWindow->propagateSizeHints(); - if (d->maximumSize.width() != oldSize.width()) - emit maximumWidthChanged(d->maximumSize.width()); - if (d->maximumSize.height() != oldSize.height()) - emit maximumHeightChanged(d->maximumSize.height()); + d->setMinOrMaxSize( + &d->maximumSize, size, [=]() { emit maximumWidthChanged(d->maximumSize.width()); }, + [=]() { emit maximumHeightChanged(d->maximumSize.height()); }); } /*! |