Allow any window to be bounds-checked (#4802)

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2023-09-24 15:32:43 +02:00
committed by GitHub
parent 37009e8e6b
commit 783c7530f3
9 changed files with 122 additions and 88 deletions
+2 -66
View File
@@ -503,28 +503,9 @@ void BaseWindow::leaveEvent(QEvent *)
TooltipWidget::instance()->hide();
}
void BaseWindow::moveTo(QPoint point, BoundsChecker boundsChecker)
void BaseWindow::moveTo(QPoint point, widgets::BoundsChecking mode)
{
switch (boundsChecker)
{
case BoundsChecker::Off: {
// The bounds checker is off, *just* move the window
this->move(point);
}
break;
case BoundsChecker::CursorPosition: {
// The bounds checker is on, use the cursor position as the origin
this->moveWithinScreen(point, QCursor::pos());
}
break;
case BoundsChecker::DesiredPosition: {
// The bounds checker is on, use the desired position as the origin
this->moveWithinScreen(point, point);
}
break;
}
widgets::moveWindowTo(this, point, mode);
}
void BaseWindow::resizeEvent(QResizeEvent *)
@@ -580,51 +561,6 @@ void BaseWindow::showEvent(QShowEvent *)
{
}
void BaseWindow::moveWithinScreen(QPoint point, QPoint origin)
{
// move the widget into the screen geometry if it's not already in there
auto *screen = QApplication::screenAt(origin);
if (screen == nullptr)
{
screen = QApplication::primaryScreen();
}
const QRect bounds = screen->availableGeometry();
bool stickRight = false;
bool stickBottom = false;
const auto w = this->frameGeometry().width();
const auto h = this->frameGeometry().height();
if (point.x() < bounds.left())
{
point.setX(bounds.left());
}
if (point.y() < bounds.top())
{
point.setY(bounds.top());
}
if (point.x() + w > bounds.right())
{
stickRight = true;
point.setX(bounds.right() - w);
}
if (point.y() + h > bounds.bottom())
{
stickBottom = true;
point.setY(bounds.bottom() - h);
}
if (stickRight && stickBottom)
{
const QPoint globalCursorPos = QCursor::pos();
point.setY(globalCursorPos.y() - this->height() - 16);
}
this->move(point);
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message,
qintptr *result)