Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading