From 5003293f4ecb2fa1eb434c03669ebe5582f27b32 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Fri, 21 Mar 2025 13:16:45 +0100 Subject: [PATCH] [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 zeroi by just taking the x/y coordinate into account. --- .../eclipse/swt/widgets/CoordinateSystemMapperTests.java | 9 +++++++++ .../swt/widgets/MultiZoomCoordinateSystemMapper.java | 3 +++ 2 files changed, 12 insertions(+) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java index 9f207ee7a8b..5fc56ba05d1 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java @@ -210,6 +210,15 @@ void translateRectangleInPixelsInBothMonitorsBackAndForthShouldBeTheSame(Coordin assertEquals(rectInPxs, mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom())); } + @ParameterizedTest + @MethodSource("provideCoordinateSystemMappers") + void translateRectangleInPixelsForZeroSize(CoordinateSystemMapper mapper) { + setupMonitors(mapper); + Rectangle rectInPts = createExpectedRectangle(mapper, 0, 0, 0, 0, monitors[0]); + Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom()); + assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom())); + } + private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, Monitor monitor) { if (mapper instanceof SingleZoomCoordinateSystemMapper) { return new Point(x, y); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java index 2057c9305ed..0edae112b5f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java @@ -192,6 +192,9 @@ private Monitor getContainingMonitorForPoints(int x, int y) { } private Monitor getContainingMonitorForPoints(int x, int y, int width, int height) { + if (width <= 0 || height <= 0) { + return getContainingMonitorForPoints(x, y); + } Monitor[] monitors = monitorSupplier.get(); Monitor selectedMonitor = null; int highestIntersectionRatio = 0;