@@ -1164,23 +1164,30 @@ void CSVDoc::View::onRequestFocus(const std::string& id)
11641164QScreen* CSVDoc::View::getWidgetScreen (const QPoint& position)
11651165{
11661166 QScreen* screen = QApplication::screenAt (position);
1167- if (screen == nullptr )
1168- {
1169- QPoint clampedPosition = position;
1167+ if (screen)
1168+ return screen;
1169+
1170+ const QList<QScreen*> screens = QApplication::screens ();
1171+ if (screens.isEmpty ())
1172+ throw std::runtime_error (" No screens available" );
11701173
1171- // If we failed to find the screen,
1172- // clamp negative positions and try again
1173- if (clampedPosition.x () <= 0 )
1174- clampedPosition.setX (0 );
1175- if (clampedPosition.y () <= 0 )
1176- clampedPosition.setY (0 );
1174+ int closestDistance = std::numeric_limits<int >::max ();
1175+ for (QScreen* candidate : screens)
1176+ {
1177+ const QRect geometry = candidate->geometry ();
1178+ const int dx = position.x () - std::clamp (position.x (), geometry.left (), geometry.right ());
1179+ const int dy = position.y () - std::clamp (position.y (), geometry.top (), geometry.bottom ());
1180+ const int distance = dx * dx + dy * dy;
11771181
1178- screen = QApplication::screenAt (clampedPosition);
1182+ if (distance < closestDistance)
1183+ {
1184+ closestDistance = distance;
1185+ screen = candidate;
1186+ }
11791187 }
11801188
11811189 if (screen == nullptr )
1182- throw std::runtime_error (
1183- Misc::StringUtils::format (" Can not detect the screen for position [%d, %d]" , position.x (), position.y ()));
1190+ screen = screens.first ();
11841191
11851192 return screen;
11861193}
0 commit comments