Skip to content

Commit b2aed6b

Browse files
committed
[win32] Apply coordinate system change with guards
This commit reapplies the essence of the reverted commits from 4f60cb6 and 7a04f7a with proper guards to differentiate between the scenario with rescaling active and inactive. contributes to #62 and #127
1 parent e5350ab commit b2aed6b

File tree

3 files changed

+264
-30
lines changed

3 files changed

+264
-30
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3970,7 +3970,13 @@ void subclass () {
39703970
public Point toControl (int x, int y) {
39713971
checkWidget ();
39723972
int zoom = getZoom();
3973-
return DPIUtil.scaleDown(toControlInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
3973+
if (getDisplay().isRescalingAtRuntime()) {
3974+
Point displayPointInPixels = getDisplay().translateLocationInPixelsInDisplayCoordinateSystem(x, y);
3975+
final Point controlPointInPixels = toControlInPixels(displayPointInPixels.x, displayPointInPixels.y);
3976+
return DPIUtil.scaleDown(controlPointInPixels, zoom);
3977+
} else {
3978+
return DPIUtil.scaleDown(toControlInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
3979+
}
39743980
}
39753981

39763982
Point toControlInPixels (int x, int y) {
@@ -4003,9 +4009,7 @@ Point toControlInPixels (int x, int y) {
40034009
public Point toControl (Point point) {
40044010
checkWidget ();
40054011
if (point == null) error (SWT.ERROR_NULL_ARGUMENT);
4006-
int zoom = getZoom();
4007-
point = DPIUtil.scaleUp(point, zoom);
4008-
return DPIUtil.scaleDown(toControlInPixels(point.x, point.y), zoom);
4012+
return toControl(point.x, point.y);
40094013
}
40104014

40114015
/**
@@ -4031,7 +4035,12 @@ public Point toControl (Point point) {
40314035
public Point toDisplay (int x, int y) {
40324036
checkWidget ();
40334037
int zoom = getZoom();
4034-
return DPIUtil.scaleDown(toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
4038+
if (getDisplay().isRescalingAtRuntime()) {
4039+
Point displayPointInPixels = toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom));
4040+
return getDisplay().translateLocationInPointInDisplayCoordinateSystem(displayPointInPixels.x, displayPointInPixels.y);
4041+
} else {
4042+
return DPIUtil.scaleDown(toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
4043+
}
40354044
}
40364045

40374046
Point toDisplayInPixels (int x, int y) {
@@ -4064,9 +4073,7 @@ Point toDisplayInPixels (int x, int y) {
40644073
public Point toDisplay (Point point) {
40654074
checkWidget ();
40664075
if (point == null) error (SWT.ERROR_NULL_ARGUMENT);
4067-
int zoom = getZoom();
4068-
point = DPIUtil.scaleUp(point, zoom);
4069-
return DPIUtil.scaleDown(toDisplayInPixels(point.x, point.y), zoom);
4076+
return toDisplay(point.x, point.y);
40704077
}
40714078

40724079
long topHandle () {

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

Lines changed: 197 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,12 @@ public Control getCursorControl () {
17041704
*/
17051705
public Point getCursorLocation () {
17061706
checkDevice ();
1707-
return DPIUtil.autoScaleDown(getCursorLocationInPixels());
1707+
Point cursorLocationInPixels = getCursorLocationInPixels();
1708+
if (isRescalingAtRuntime()) {
1709+
return translateLocationInPointInDisplayCoordinateSystem(cursorLocationInPixels.x, cursorLocationInPixels.y);
1710+
} else {
1711+
return DPIUtil.autoScaleDown(cursorLocationInPixels);
1712+
}
17081713
}
17091714

17101715
Point getCursorLocationInPixels () {
@@ -2183,14 +2188,24 @@ Monitor getMonitor (long hmonitor) {
21832188
OS.GetMonitorInfo (hmonitor, lpmi);
21842189
Monitor monitor = new Monitor ();
21852190
monitor.handle = hmonitor;
2186-
Rectangle boundsInPixels = new Rectangle (lpmi.rcMonitor_left, lpmi.rcMonitor_top, lpmi.rcMonitor_right - lpmi.rcMonitor_left,lpmi.rcMonitor_bottom - lpmi.rcMonitor_top);
2187-
monitor.setBounds (DPIUtil.autoScaleDown (boundsInPixels));
2188-
Rectangle clientAreaInPixels = new Rectangle (lpmi.rcWork_left, lpmi.rcWork_top, lpmi.rcWork_right - lpmi.rcWork_left, lpmi.rcWork_bottom - lpmi.rcWork_top);
2189-
monitor.setClientArea (DPIUtil.autoScaleDown (clientAreaInPixels));
2191+
Rectangle boundsInPixels = new Rectangle(lpmi.rcMonitor_left, lpmi.rcMonitor_top, lpmi.rcMonitor_right - lpmi.rcMonitor_left,lpmi.rcMonitor_bottom - lpmi.rcMonitor_top);
2192+
Rectangle clientAreaInPixels = new Rectangle(lpmi.rcWork_left, lpmi.rcWork_top, lpmi.rcWork_right - lpmi.rcWork_left, lpmi.rcWork_bottom - lpmi.rcWork_top);
21902193
int [] dpiX = new int[1];
21912194
int [] dpiY = new int[1];
21922195
int result = OS.GetDpiForMonitor (monitor.handle, OS.MDT_EFFECTIVE_DPI, dpiX, dpiY);
21932196
result = (result == OS.S_OK) ? DPIUtil.mapDPIToZoom (dpiX[0]) : 100;
2197+
2198+
int autoscaleZoom;
2199+
if (DPIUtil.isAutoScaleOnRuntimeActive()) {
2200+
autoscaleZoom = DPIUtil.getZoomForAutoscaleProperty(result);
2201+
monitor.setBounds(getMonitorBoundsInPointsInDisplayCoordinateSystem(boundsInPixels, autoscaleZoom));
2202+
monitor.setClientArea(getMonitorBoundsInPointsInDisplayCoordinateSystem(clientAreaInPixels, autoscaleZoom));
2203+
} else {
2204+
autoscaleZoom = DPIUtil.getDeviceZoom();
2205+
monitor.setBounds(DPIUtil.scaleDown (boundsInPixels, autoscaleZoom));
2206+
monitor.setClientArea(DPIUtil.scaleDown (clientAreaInPixels, autoscaleZoom));
2207+
2208+
}
21942209
if (result == 0) {
21952210
System.err.println("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor.");
21962211
result = 100;
@@ -2203,6 +2218,13 @@ Monitor getMonitor (long hmonitor) {
22032218
return monitor;
22042219
}
22052220

2221+
private Rectangle getMonitorBoundsInPointsInDisplayCoordinateSystem(Rectangle boundsInPixels, int zoom) {
2222+
Rectangle bounds = DPIUtil.scaleDown(boundsInPixels, zoom);
2223+
bounds.x = boundsInPixels.x;
2224+
bounds.y = boundsInPixels.y;
2225+
return bounds;
2226+
}
2227+
22062228
/**
22072229
* Returns an array of monitors attached to the device.
22082230
*
@@ -2944,9 +2966,13 @@ boolean isValidThread () {
29442966
public Point map (Control from, Control to, Point point) {
29452967
checkDevice ();
29462968
if (point == null) error (SWT.ERROR_NULL_ARGUMENT);
2947-
int zoom = getZoomLevelForMapping(from, to);
2948-
point = DPIUtil.scaleUp(point, zoom);
2949-
return DPIUtil.scaleDown(mapInPixels(from, to, point), zoom);
2969+
if (isRescalingAtRuntime()) {
2970+
return map(from, to, point.x, point.y);
2971+
} else {
2972+
int zoom = getZoomLevelForMapping(from, to);
2973+
point = DPIUtil.scaleUp(point, zoom);
2974+
return DPIUtil.scaleDown(mapInPixels(from, to, point), zoom);
2975+
}
29502976
}
29512977

29522978
Point mapInPixels (Control from, Control to, Point point) {
@@ -2991,10 +3017,25 @@ Point mapInPixels (Control from, Control to, Point point) {
29913017
*/
29923018
public Point map (Control from, Control to, int x, int y) {
29933019
checkDevice ();
2994-
int zoom = getZoomLevelForMapping(from, to);
2995-
x = DPIUtil.scaleUp(x, zoom);
2996-
y = DPIUtil.scaleUp(y, zoom);
2997-
return DPIUtil.scaleDown(mapInPixels(from, to, x, y), zoom);
3020+
if (isRescalingAtRuntime()) {
3021+
Point mappedPointInPoints;
3022+
if (from == null) {
3023+
Point mappedPointInpixels = mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), x, y));
3024+
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
3025+
} else if (to == null) {
3026+
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
3027+
mappedPointInPoints = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x, mappedPointInpixels.y);
3028+
} else {
3029+
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
3030+
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
3031+
}
3032+
return mappedPointInPoints;
3033+
} else {
3034+
int zoom = getZoomLevelForMapping(from, to);
3035+
x = DPIUtil.scaleUp(x, zoom);
3036+
y = DPIUtil.scaleUp(y, zoom);
3037+
return DPIUtil.scaleDown(mapInPixels(from, to, x, y), zoom);
3038+
}
29983039
}
29993040

30003041
Point mapInPixels (Control from, Control to, int x, int y) {
@@ -3058,9 +3099,13 @@ private int getZoomLevelForMapping(Control from, Control to) {
30583099
public Rectangle map (Control from, Control to, Rectangle rectangle) {
30593100
checkDevice ();
30603101
if (rectangle == null) error (SWT.ERROR_NULL_ARGUMENT);
3061-
int zoom = getZoomLevelForMapping(from, to);
3062-
rectangle = DPIUtil.scaleUp(rectangle, zoom);
3063-
return DPIUtil.scaleDown(mapInPixels(from, to, rectangle), zoom);
3102+
if (isRescalingAtRuntime()) {
3103+
return map(from, to, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
3104+
} else {
3105+
int zoom = getZoomLevelForMapping(from, to);
3106+
rectangle = DPIUtil.scaleUp(rectangle, zoom);
3107+
return DPIUtil.scaleDown(mapInPixels(from, to, rectangle), zoom);
3108+
}
30643109
}
30653110

30663111
Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
@@ -3107,12 +3152,27 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
31073152
*/
31083153
public Rectangle map (Control from, Control to, int x, int y, int width, int height) {
31093154
checkDevice ();
3110-
int zoom = getZoomLevelForMapping(from, to);
3111-
x = DPIUtil.scaleUp(x, zoom);
3112-
y = DPIUtil.scaleUp(y, zoom);
3113-
width = DPIUtil.scaleUp(width, zoom);
3114-
height = DPIUtil.scaleUp(height, zoom);
3115-
return DPIUtil.scaleDown(mapInPixels(from, to, x, y, width, height), zoom);
3155+
if (isRescalingAtRuntime()) {
3156+
Rectangle mappedRectangleInPoints;
3157+
if (from == null) {
3158+
Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, to.getShell().getMonitor()));
3159+
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
3160+
} else if (to == null) {
3161+
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
3162+
mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height, from.getShell().getMonitor());
3163+
} else {
3164+
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
3165+
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
3166+
}
3167+
return mappedRectangleInPoints;
3168+
} else {
3169+
int zoom = getZoomLevelForMapping(from, to);
3170+
x = DPIUtil.scaleUp(x, zoom);
3171+
y = DPIUtil.scaleUp(y, zoom);
3172+
width = DPIUtil.scaleUp(width, zoom);
3173+
height = DPIUtil.scaleUp(height, zoom);
3174+
return DPIUtil.scaleDown(mapInPixels(from, to, x, y, width, height), zoom);
3175+
}
31163176
}
31173177

31183178
Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int height) {
@@ -3130,6 +3190,57 @@ Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int he
31303190
return new Rectangle (rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
31313191
}
31323192

3193+
Point translateLocationInPixelsInDisplayCoordinateSystem(int x, int y) {
3194+
Monitor monitor = getContainingMonitor(x, y);
3195+
return getPixelsFromPoint(monitor, x, y);
3196+
}
3197+
3198+
Point translateLocationInPointInDisplayCoordinateSystem(int x, int y) {
3199+
Monitor monitor = getContainingMonitorInPixelsCoordinate(x, y);
3200+
return getPointFromPixels(monitor, x, y);
3201+
}
3202+
3203+
Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment(int x, int y, int width, int height) {
3204+
Monitor monitorByLocation = getContainingMonitor(x, y);
3205+
Monitor monitorByContainment = getContainingMonitor(x, y, width, height);
3206+
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitorByLocation, monitorByContainment);
3207+
}
3208+
3209+
private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height, Monitor monitor) {
3210+
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitor, monitor);
3211+
}
3212+
3213+
private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height, Monitor monitorOfLocation, Monitor monitorOfArea) {
3214+
Point topLeft = getPixelsFromPoint(monitorOfLocation, x, y);
3215+
int zoom = getApplicableMonitorZoom(monitorOfArea);
3216+
int widthInPixels = DPIUtil.scaleUp(width, zoom);
3217+
int heightInPixels = DPIUtil.scaleUp(height, zoom);
3218+
return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels);
3219+
}
3220+
3221+
Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment(int x, int y, int widthInPixels, int heightInPixels) {
3222+
Monitor monitorByLocation = getContainingMonitor(x, y);
3223+
Monitor monitorByContainment = getContainingMonitor(x, y, widthInPixels, heightInPixels);
3224+
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitorByLocation, monitorByContainment);
3225+
}
3226+
3227+
private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) {
3228+
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitor, monitor);
3229+
}
3230+
3231+
3232+
private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels, int heightInPixels, Monitor monitorOfLocation, Monitor monitorOfArea) {
3233+
Point topLeft = getPointFromPixels(monitorOfLocation, x, y);
3234+
int zoom = getApplicableMonitorZoom(monitorOfArea);
3235+
int width = DPIUtil.scaleDown(widthInPixels, zoom);
3236+
int height = DPIUtil.scaleDown(heightInPixels, zoom);
3237+
return new Rectangle(topLeft.x, topLeft.y, width, height);
3238+
}
3239+
3240+
private int getApplicableMonitorZoom(Monitor monitor) {
3241+
return DPIUtil.getZoomForAutoscaleProperty(isRescalingAtRuntime() ? monitor.zoom : getDeviceZoom());
3242+
}
3243+
31333244
long messageProc (long hwnd, long msg, long wParam, long lParam) {
31343245
switch ((int)msg) {
31353246
case SWT_RUNASYNC: {
@@ -4355,7 +4466,12 @@ public void sendPostExternalEventDispatchEvent () {
43554466
*/
43564467
public void setCursorLocation (int x, int y) {
43574468
checkDevice ();
4358-
setCursorLocationInPixels (DPIUtil.autoScaleUp (x), DPIUtil.autoScaleUp (y));
4469+
if (isRescalingAtRuntime()) {
4470+
Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem(x, y);
4471+
setCursorLocationInPixels (cursorLocationInPixels.x, cursorLocationInPixels.y);
4472+
} else {
4473+
setCursorLocationInPixels (DPIUtil.autoScaleUp (x), DPIUtil.autoScaleUp (y));
4474+
}
43594475
}
43604476

43614477
void setCursorLocationInPixels (int x, int y) {
@@ -5390,4 +5506,63 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
53905506
return true;
53915507
}
53925508

5509+
private Monitor getContainingMonitor(int x, int y) {
5510+
Monitor[] monitors = getMonitors();
5511+
for (Monitor currentMonitor : monitors) {
5512+
Rectangle clientArea = currentMonitor.getClientArea();
5513+
if (clientArea.contains(x, y)) {
5514+
return currentMonitor;
5515+
}
5516+
}
5517+
return getPrimaryMonitor();
5518+
}
5519+
5520+
private Monitor getContainingMonitor(int x, int y, int width, int height) {
5521+
Rectangle rectangle = new Rectangle(x, y, width, height);
5522+
Monitor[] monitors = getMonitors();
5523+
Monitor selectedMonitor = getPrimaryMonitor();
5524+
int highestArea = 0;
5525+
for (Monitor currentMonitor : monitors) {
5526+
Rectangle clientArea = currentMonitor.getClientArea();
5527+
Rectangle intersection = clientArea.intersection(rectangle);
5528+
int area = intersection.width * intersection.height;
5529+
if (area > highestArea) {
5530+
selectedMonitor = currentMonitor;
5531+
highestArea = area;
5532+
}
5533+
}
5534+
return selectedMonitor;
5535+
}
5536+
5537+
private Monitor getContainingMonitorInPixelsCoordinate(int xInPixels, int yInPixels) {
5538+
Monitor[] monitors = getMonitors();
5539+
for (Monitor current : monitors) {
5540+
Rectangle clientArea = getMonitorClientAreaInPixels(current);
5541+
if (clientArea.contains(xInPixels, yInPixels)) {
5542+
return current;
5543+
}
5544+
}
5545+
return getPrimaryMonitor();
5546+
}
5547+
5548+
private Rectangle getMonitorClientAreaInPixels(Monitor monitor) {
5549+
int zoom = getApplicableMonitorZoom(monitor);
5550+
int widthInPixels = DPIUtil.scaleUp(monitor.clientWidth, zoom);
5551+
int heightInPixels = DPIUtil.scaleUp(monitor.clientHeight, zoom);
5552+
return new Rectangle(monitor.clientX, monitor.clientY, widthInPixels, heightInPixels);
5553+
}
5554+
5555+
private Point getPixelsFromPoint(Monitor monitor, int x, int y) {
5556+
int zoom = getApplicableMonitorZoom(monitor);
5557+
int mappedX = DPIUtil.scaleUp(x - monitor.clientX, zoom) + monitor.clientX;
5558+
int mappedY = DPIUtil.scaleUp(y - monitor.clientY, zoom) + monitor.clientY;
5559+
return new Point(mappedX, mappedY);
5560+
}
5561+
5562+
private Point getPointFromPixels(Monitor monitor, int x, int y) {
5563+
int zoom = getApplicableMonitorZoom(monitor);
5564+
int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX;
5565+
int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY;
5566+
return new Point(mappedX, mappedY);
5567+
}
53935568
}

0 commit comments

Comments
 (0)