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 b948bf2d51..d69cb0801d 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 @@ -13,8 +13,7 @@ *******************************************************************************/ package org.eclipse.swt.widgets; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import java.util.function.*; import java.util.stream.*; @@ -33,10 +32,11 @@ class CoordinateSystemMapperTests { private Monitor createMonitor(CoordinateSystemMapper mapper, Rectangle boundsInPixels, int nativeZoom) { Monitor monitor = new Monitor(); - Rectangle bounds = mapper.mapMonitorBounds(boundsInPixels, DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); + monitor.zoom = nativeZoom; + Rectangle.WithMonitor boundsInPixelWithMonitor = new Rectangle.WithMonitor(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height, monitor); + Rectangle bounds = mapper.mapMonitorBounds(boundsInPixelWithMonitor); monitor.setBounds(bounds); monitor.setClientArea(bounds); - monitor.zoom = nativeZoom; return monitor; } @@ -59,6 +59,34 @@ private SingleZoomCoordinateSystemMapper getSingleZoomCoordinateSystemMapper() { return new SingleZoomCoordinateSystemMapper(null); } + @Test + void mapMonitorBoundsWithSingleZoomCoordinateMapper() { + CoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper(); + Monitor monitor100 = new Monitor(); + monitor100.zoom = 100; + Monitor monitor200 = new Monitor(); + monitor200.zoom = 200; + + int oldDeviceZoom = DPIUtil.getDeviceZoom(); + try { + Rectangle.WithMonitor rectInPixelsWithMonitorAt100 = new Rectangle.WithMonitor(10, 10, 20, 20, monitor100); + Rectangle.WithMonitor rectInPixelsWithMonitorAt200 = new Rectangle.WithMonitor(10, 10, 20, 20, monitor200); + // Result should not depend on monitor zoom + assertEquals(mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100), + mapper.mapMonitorBounds(rectInPixelsWithMonitorAt200)); + + DPIUtil.setDeviceZoom(100); + Rectangle mappedRectAtDeviceZoom100 = mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100); + DPIUtil.setDeviceZoom(200); + // Result should depend on device zoom + assertNotEquals(mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100), mappedRectAtDeviceZoom100); + assertEquals(mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100), + mapper.mapMonitorBounds(rectInPixelsWithMonitorAt200)); + } finally { + DPIUtil.setDeviceZoom(oldDeviceZoom); + } + } + @ParameterizedTest @MethodSource("provideCoordinateSystemMappers") void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java index dbdd14aa23..515e312d9a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/CoordinateSystemMapper.java @@ -25,7 +25,7 @@ interface CoordinateSystemMapper { Point map(Control from, Control to, int x, int y); - Rectangle mapMonitorBounds(Rectangle rectangle, int zoom); + Rectangle mapMonitorBounds(Rectangle.WithMonitor rectangle); Point translateFromDisplayCoordinates(Point point); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java index a2b325bdbb..052a31f268 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java @@ -2225,16 +2225,10 @@ Monitor getMonitor (long hmonitor) { OS.GetMonitorInfo (hmonitor, lpmi); Monitor monitor = new Monitor (); monitor.handle = hmonitor; - Rectangle boundsInPixels = new Rectangle(lpmi.rcMonitor_left, lpmi.rcMonitor_top, lpmi.rcMonitor_right - lpmi.rcMonitor_left,lpmi.rcMonitor_bottom - lpmi.rcMonitor_top); - Rectangle clientAreaInPixels = new Rectangle(lpmi.rcWork_left, lpmi.rcWork_top, lpmi.rcWork_right - lpmi.rcWork_left, lpmi.rcWork_bottom - lpmi.rcWork_top); int [] dpiX = new int[1]; int [] dpiY = new int[1]; int result = OS.GetDpiForMonitor (monitor.handle, OS.MDT_EFFECTIVE_DPI, dpiX, dpiY); result = (result == OS.S_OK) ? DPIUtil.mapDPIToZoom (dpiX[0]) : 100; - - int autoscaleZoom = DPIUtil.getZoomForAutoscaleProperty(result); - monitor.setBounds(coordinateSystemMapper.mapMonitorBounds(boundsInPixels, autoscaleZoom)); - monitor.setClientArea(coordinateSystemMapper.mapMonitorBounds(clientAreaInPixels, autoscaleZoom)); if (result == 0) { System.err.println("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor."); result = 100; @@ -2244,6 +2238,12 @@ Monitor getMonitor (long hmonitor) { * to scaling issue on OS Win8.1 and above, for more details refer bug 537614. */ monitor.zoom = result; + Rectangle.WithMonitor boundsInPixels = new Rectangle.WithMonitor(lpmi.rcMonitor_left, lpmi.rcMonitor_top, lpmi.rcMonitor_right - lpmi.rcMonitor_left,lpmi.rcMonitor_bottom - lpmi.rcMonitor_top, monitor); + Rectangle.WithMonitor clientAreaInPixels = new Rectangle.WithMonitor(lpmi.rcWork_left, lpmi.rcWork_top, lpmi.rcWork_right - lpmi.rcWork_left, lpmi.rcWork_bottom - lpmi.rcWork_top, monitor); + + monitor.setBounds(coordinateSystemMapper.mapMonitorBounds(boundsInPixels)); + monitor.setClientArea(coordinateSystemMapper.mapMonitorBounds(clientAreaInPixels)); + return monitor; } 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 520729cff3..70daf5a83d 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 @@ -84,7 +84,9 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig } @Override - public Rectangle mapMonitorBounds(Rectangle rect, int zoom) { + public Rectangle mapMonitorBounds(Rectangle.WithMonitor rect) { + Monitor monitor = rect.getMonitor(); + int zoom = getApplicableMonitorZoom(monitor); Rectangle bounds = Win32DPIUtils.pixelToPoint(rect, zoom); bounds.x = rect.x; bounds.y = rect.y; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java index 144385d8cd..7a4b7b5357 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/SingleZoomCoordinateSystemMapper.java @@ -71,8 +71,8 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig } @Override - public Rectangle mapMonitorBounds(Rectangle rect, int zoom) { - return Win32DPIUtils.pixelToPoint(rect, zoom); + public Rectangle mapMonitorBounds(Rectangle.WithMonitor rect) { + return Win32DPIUtils.pixelToPoint(rect, DPIUtil.getDeviceZoom()); } @Override