Skip to content

Commit 54fbe7b

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Remove zoom parameter from CoordinateSystemMapper#mapMonitorBounds()
The CoordinateSystemMapper#mapMonitorBounds() method currently accepts a zoom value and that is wrong for SingleZoomCoordinateMapper since it should always use the global zoom (DPIUtil#getDeviceZoom()) instead of monitor zoom. This commit changes how zoom is passed through Rectangle with monitor for MultiZoomCoordinateMapper while that monitor will be ignored for SingleZoomCoordinateMapper.
1 parent 136b375 commit 54fbe7b

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.widgets;
1515

16-
import static org.junit.jupiter.api.Assertions.assertEquals;
17-
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
import static org.junit.jupiter.api.Assertions.*;
1817

1918
import java.util.function.*;
2019
import java.util.stream.*;
@@ -33,10 +32,11 @@ class CoordinateSystemMapperTests {
3332

3433
private Monitor createMonitor(CoordinateSystemMapper mapper, Rectangle boundsInPixels, int nativeZoom) {
3534
Monitor monitor = new Monitor();
36-
Rectangle bounds = mapper.mapMonitorBounds(boundsInPixels, DPIUtil.getZoomForAutoscaleProperty(nativeZoom));
35+
monitor.zoom = nativeZoom;
36+
Rectangle.WithMonitor boundsInPixelWithMonitor = new Rectangle.WithMonitor(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height, monitor);
37+
Rectangle bounds = mapper.mapMonitorBounds(boundsInPixelWithMonitor);
3738
monitor.setBounds(bounds);
3839
monitor.setClientArea(bounds);
39-
monitor.zoom = nativeZoom;
4040
return monitor;
4141
}
4242

@@ -59,6 +59,34 @@ private SingleZoomCoordinateSystemMapper getSingleZoomCoordinateSystemMapper() {
5959
return new SingleZoomCoordinateSystemMapper(null);
6060
}
6161

62+
@Test
63+
void mapMonitorBoundsWithSingleZoomCoordinateMapper() {
64+
CoordinateSystemMapper mapper = getSingleZoomCoordinateSystemMapper();
65+
Monitor monitor100 = new Monitor();
66+
monitor100.zoom = 100;
67+
Monitor monitor200 = new Monitor();
68+
monitor200.zoom = 200;
69+
70+
int oldDeviceZoom = DPIUtil.getDeviceZoom();
71+
try {
72+
Rectangle.WithMonitor rectInPixelsWithMonitorAt100 = new Rectangle.WithMonitor(10, 10, 20, 20, monitor100);
73+
Rectangle.WithMonitor rectInPixelsWithMonitorAt200 = new Rectangle.WithMonitor(10, 10, 20, 20, monitor200);
74+
// Result should not depend on monitor zoom
75+
assertEquals(mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100),
76+
mapper.mapMonitorBounds(rectInPixelsWithMonitorAt200));
77+
78+
DPIUtil.setDeviceZoom(100);
79+
Rectangle mappedRectAtDeviceZoom100 = mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100);
80+
DPIUtil.setDeviceZoom(200);
81+
// Result should depend on device zoom
82+
assertNotEquals(mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100), mappedRectAtDeviceZoom100);
83+
assertEquals(mapper.mapMonitorBounds(rectInPixelsWithMonitorAt100),
84+
mapper.mapMonitorBounds(rectInPixelsWithMonitorAt200));
85+
} finally {
86+
DPIUtil.setDeviceZoom(oldDeviceZoom);
87+
}
88+
}
89+
6290
@ParameterizedTest
6391
@MethodSource("provideCoordinateSystemMappers")
6492
void translatePointInNoMonitorBackAndForthShouldBeTheSame(CoordinateSystemMapper mapper) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface CoordinateSystemMapper {
2525

2626
Point map(Control from, Control to, int x, int y);
2727

28-
Rectangle mapMonitorBounds(Rectangle rectangle, int zoom);
28+
Rectangle mapMonitorBounds(Rectangle.WithMonitor rectangle);
2929

3030
Point translateFromDisplayCoordinates(Point point);
3131

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,16 +2225,10 @@ Monitor getMonitor (long hmonitor) {
22252225
OS.GetMonitorInfo (hmonitor, lpmi);
22262226
Monitor monitor = new Monitor ();
22272227
monitor.handle = hmonitor;
2228-
Rectangle boundsInPixels = new Rectangle(lpmi.rcMonitor_left, lpmi.rcMonitor_top, lpmi.rcMonitor_right - lpmi.rcMonitor_left,lpmi.rcMonitor_bottom - lpmi.rcMonitor_top);
2229-
Rectangle clientAreaInPixels = new Rectangle(lpmi.rcWork_left, lpmi.rcWork_top, lpmi.rcWork_right - lpmi.rcWork_left, lpmi.rcWork_bottom - lpmi.rcWork_top);
22302228
int [] dpiX = new int[1];
22312229
int [] dpiY = new int[1];
22322230
int result = OS.GetDpiForMonitor (monitor.handle, OS.MDT_EFFECTIVE_DPI, dpiX, dpiY);
22332231
result = (result == OS.S_OK) ? DPIUtil.mapDPIToZoom (dpiX[0]) : 100;
2234-
2235-
int autoscaleZoom = DPIUtil.getZoomForAutoscaleProperty(result);
2236-
monitor.setBounds(coordinateSystemMapper.mapMonitorBounds(boundsInPixels, autoscaleZoom));
2237-
monitor.setClientArea(coordinateSystemMapper.mapMonitorBounds(clientAreaInPixels, autoscaleZoom));
22382232
if (result == 0) {
22392233
System.err.println("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor.");
22402234
result = 100;
@@ -2244,6 +2238,12 @@ Monitor getMonitor (long hmonitor) {
22442238
* to scaling issue on OS Win8.1 and above, for more details refer bug 537614.
22452239
*/
22462240
monitor.zoom = result;
2241+
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);
2242+
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);
2243+
2244+
monitor.setBounds(coordinateSystemMapper.mapMonitorBounds(boundsInPixels));
2245+
monitor.setClientArea(coordinateSystemMapper.mapMonitorBounds(clientAreaInPixels));
2246+
22472247
return monitor;
22482248
}
22492249

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig
8484
}
8585

8686
@Override
87-
public Rectangle mapMonitorBounds(Rectangle rect, int zoom) {
87+
public Rectangle mapMonitorBounds(Rectangle.WithMonitor rect) {
88+
Monitor monitor = rect.getMonitor();
89+
int zoom = getApplicableMonitorZoom(monitor);
8890
Rectangle bounds = Win32DPIUtils.pixelToPoint(rect, zoom);
8991
bounds.x = rect.x;
9092
bounds.y = rect.y;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public Rectangle map(Control from, Control to, int x, int y, int width, int heig
7171
}
7272

7373
@Override
74-
public Rectangle mapMonitorBounds(Rectangle rect, int zoom) {
75-
return Win32DPIUtils.pixelToPoint(rect, zoom);
74+
public Rectangle mapMonitorBounds(Rectangle.WithMonitor rect) {
75+
return Win32DPIUtils.pixelToPoint(rect, DPIUtil.getDeviceZoom());
7676
}
7777

7878
@Override

0 commit comments

Comments
 (0)