Allow any window to be bounds-checked (#4802)
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/FlagsEnum.hpp"
|
||||
#include "util/WidgetHelpers.hpp"
|
||||
#include "widgets/BaseWidget.hpp"
|
||||
|
||||
#include <pajlada/signals/signalholder.hpp>
|
||||
@@ -36,17 +37,6 @@ public:
|
||||
DisableLayoutSave = 128,
|
||||
};
|
||||
|
||||
enum class BoundsChecker {
|
||||
// Don't attempt to do any "stay in screen" stuff, just move me!
|
||||
Off,
|
||||
|
||||
// Attempt to keep the window within bounds of the screen the cursor is on
|
||||
CursorPosition,
|
||||
|
||||
// Attempt to keep the window within bounds of the screen the desired position is on
|
||||
DesiredPosition,
|
||||
};
|
||||
|
||||
enum ActionOnFocusLoss { Nothing, Delete, Close, Hide };
|
||||
|
||||
explicit BaseWindow(FlagsEnum<Flags> flags_ = None,
|
||||
@@ -65,7 +55,7 @@ public:
|
||||
void setActionOnFocusLoss(ActionOnFocusLoss value);
|
||||
ActionOnFocusLoss getActionOnFocusLoss() const;
|
||||
|
||||
void moveTo(QPoint point, BoundsChecker boundsChecker);
|
||||
void moveTo(QPoint point, widgets::BoundsChecking mode);
|
||||
|
||||
float scale() const override;
|
||||
float qtFontScale() const;
|
||||
@@ -110,11 +100,6 @@ protected:
|
||||
private:
|
||||
void init();
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
void moveWithinScreen(QPoint point, QPoint origin);
|
||||
|
||||
void calcButtonsSizes();
|
||||
void drawCustomWindowFrame(QPainter &painter);
|
||||
void onFocusLost();
|
||||
|
||||
@@ -207,7 +207,7 @@ EmotePopup::EmotePopup(QWidget *parent)
|
||||
{
|
||||
// this->setStayInScreenRect(true);
|
||||
this->moveTo(getApp()->windows->emotePopupPos(),
|
||||
BaseWindow::BoundsChecker::DesiredPosition);
|
||||
widgets::BoundsChecking::DesiredPosition);
|
||||
|
||||
auto *layout = new QVBoxLayout();
|
||||
this->getLayoutContainer()->setLayout(layout);
|
||||
|
||||
@@ -1832,7 +1832,7 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
|
||||
}
|
||||
|
||||
tooltipWidget->moveTo(event->globalPos() + QPoint(16, 16),
|
||||
BaseWindow::BoundsChecker::CursorPosition);
|
||||
widgets::BoundsChecking::CursorPosition);
|
||||
tooltipWidget->setWordWrap(isLinkValid);
|
||||
tooltipWidget->show();
|
||||
}
|
||||
@@ -2687,7 +2687,7 @@ void ChannelView::showUserInfoPopup(const QString &userName,
|
||||
|
||||
QPoint offset(userPopup->width() / 3, userPopup->height() / 5);
|
||||
userPopup->moveTo(QCursor::pos() - offset,
|
||||
BaseWindow::BoundsChecker::CursorPosition);
|
||||
widgets::BoundsChecking::CursorPosition);
|
||||
userPopup->show();
|
||||
}
|
||||
|
||||
|
||||
@@ -956,7 +956,7 @@ void SplitHeader::enterEvent(QEvent *event)
|
||||
auto pos = this->mapToGlobal(this->rect().bottomLeft()) +
|
||||
QPoint((this->width() - tooltip->width()) / 2, 1);
|
||||
|
||||
tooltip->moveTo(pos, BaseWindow::BoundsChecker::CursorPosition);
|
||||
tooltip->moveTo(pos, widgets::BoundsChecking::CursorPosition);
|
||||
tooltip->show();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user