Skip to content

Commit 379393a

Browse files
committed
[Win32] MultiZoomCoordinateSystemMapper: zero-sized rectangle handling
The MultiZoomCoordinateSystemMapper currently produces a devide-by-zero error when transforming a rectangle of zero size. This can, for example, happen when a (dummy) shell with width=height=0 is created and its bounds are passed to the mapper. This change ensures that the problematic calculation is avoided in case width or height are zero. In that case, no reasonable monitor can be identified anyway.
1 parent 3e59697 commit 379393a

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame(Coordin
210210
assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()));
211211
}
212212

213+
@ParameterizedTest
214+
@MethodSource("provideCoordinateSystemMappers")
215+
void translateRectangleInPixelsForZeroSize(CoordinateSystemMapper mapper) {
216+
setupMonitors(mapper);
217+
Rectangle rectInPts = createExpectedRectangle(mapper, 0, 0, 0, 0, monitors[0]);
218+
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
219+
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
220+
}
221+
213222
private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, Monitor monitor) {
214223
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
215224
return new Point(x, y);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ private Monitor getContainingMonitorForPoints(int x, int y) {
192192
}
193193

194194
private Monitor getContainingMonitorForPoints(int x, int y, int width, int height) {
195+
if (width <= 0 || height <= 0) {
196+
return null;
197+
}
195198
Monitor[] monitors = monitorSupplier.get();
196199
Monitor selectedMonitor = null;
197200
int highestIntersectionRatio = 0;

0 commit comments

Comments
 (0)