1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-24 07:41:37 +00:00

Editor: Fall back to the closest screen when necessary (#8354)

This commit is contained in:
Alexei Kotov 2025-02-18 13:14:20 +03:00
parent 3b05ec0ab1
commit d71e4ec9f0

View file

@ -1164,18 +1164,26 @@ void CSVDoc::View::onRequestFocus(const std::string& id)
QScreen* CSVDoc::View::getWidgetScreen(const QPoint& position) QScreen* CSVDoc::View::getWidgetScreen(const QPoint& position)
{ {
QScreen* screen = QApplication::screenAt(position); QScreen* screen = QApplication::screenAt(position);
if (screen == nullptr) if (screen)
return screen;
const QList<QScreen*> screens = QApplication::screens();
if (screens.isEmpty())
throw std::runtime_error("No screens available");
int closestDistance = std::numeric_limits<int>::max();
for (QScreen* candidate : screens)
{ {
QPoint clampedPosition = position; const QRect geometry = candidate->geometry();
const int dx = position.x() - std::clamp(position.x(), geometry.left(), geometry.right());
const int dy = position.y() - std::clamp(position.y(), geometry.top(), geometry.bottom());
const int distance = dx * dx + dy * dy;
// If we failed to find the screen, if (distance < closestDistance)
// clamp negative positions and try again {
if (clampedPosition.x() <= 0) closestDistance = distance;
clampedPosition.setX(0); screen = candidate;
if (clampedPosition.y() <= 0) }
clampedPosition.setY(0);
screen = QApplication::screenAt(clampedPosition);
} }
if (screen == nullptr) if (screen == nullptr)