@@ -1164,23 +1164,30 @@ void CSVDoc::View::onRequestFocus(const std::string& id)
1164
1164
QScreen* CSVDoc::View::getWidgetScreen (const QPoint& position)
1165
1165
{
1166
1166
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" );
1170
1173
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;
1177
1181
1178
- screen = QApplication::screenAt (clampedPosition);
1182
+ if (distance < closestDistance)
1183
+ {
1184
+ closestDistance = distance;
1185
+ screen = candidate;
1186
+ }
1179
1187
}
1180
1188
1181
1189
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 ();
1184
1191
1185
1192
return screen;
1186
1193
}
0 commit comments