Skip to content

Commit 0e7f799

Browse files
akoch-yattafedejeanne
authored andcommitted
[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 9771e17 commit 0e7f799

File tree

3 files changed

+234
-12
lines changed

3 files changed

+234
-12
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3970,6 +3970,11 @@ void subclass () {
39703970
public Point toControl (int x, int y) {
39713971
checkWidget ();
39723972
int zoom = getZoom();
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+
}
39733978
return DPIUtil.scaleDown(toControlInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
39743979
}
39753980

@@ -4003,9 +4008,7 @@ Point toControlInPixels (int x, int y) {
40034008
public Point toControl (Point point) {
40044009
checkWidget ();
40054010
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);
4011+
return toControl(point.x, point.y);
40094012
}
40104013

40114014
/**
@@ -4031,6 +4034,10 @@ public Point toControl (Point point) {
40314034
public Point toDisplay (int x, int y) {
40324035
checkWidget ();
40334036
int zoom = getZoom();
4037+
if (getDisplay().isRescalingAtRuntime()) {
4038+
Point displayPointInPixels = toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom));
4039+
return getDisplay().translateLocationInPointInDisplayCoordinateSystem(displayPointInPixels.x, displayPointInPixels.y);
4040+
}
40344041
return DPIUtil.scaleDown(toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
40354042
}
40364043

@@ -4064,9 +4071,7 @@ Point toDisplayInPixels (int x, int y) {
40644071
public Point toDisplay (Point point) {
40654072
checkWidget ();
40664073
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);
4074+
return toDisplay(point.x, point.y);
40704075
}
40714076

40724077
long topHandle () {

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

Lines changed: 173 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,11 @@ 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+
}
1711+
return DPIUtil.autoScaleDown(cursorLocationInPixels);
17081712
}
17091713

17101714
Point getCursorLocationInPixels () {
@@ -2183,14 +2187,21 @@ Monitor getMonitor (long hmonitor) {
21832187
OS.GetMonitorInfo (hmonitor, lpmi);
21842188
Monitor monitor = new Monitor ();
21852189
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));
2190+
Rectangle boundsInPixels = new Rectangle(lpmi.rcMonitor_left, lpmi.rcMonitor_top, lpmi.rcMonitor_right - lpmi.rcMonitor_left,lpmi.rcMonitor_bottom - lpmi.rcMonitor_top);
2191+
Rectangle clientAreaInPixels = new Rectangle(lpmi.rcWork_left, lpmi.rcWork_top, lpmi.rcWork_right - lpmi.rcWork_left, lpmi.rcWork_bottom - lpmi.rcWork_top);
21902192
int [] dpiX = new int[1];
21912193
int [] dpiY = new int[1];
21922194
int result = OS.GetDpiForMonitor (monitor.handle, OS.MDT_EFFECTIVE_DPI, dpiX, dpiY);
21932195
result = (result == OS.S_OK) ? DPIUtil.mapDPIToZoom (dpiX[0]) : 100;
2196+
2197+
if (DPIUtil.isAutoScaleOnRuntimeActive()) {
2198+
int autoscaleZoom = DPIUtil.getZoomForAutoscaleProperty(result);
2199+
monitor.setBounds(getMonitorBoundsInPointsInDisplayCoordinateSystem(boundsInPixels, autoscaleZoom));
2200+
monitor.setClientArea(getMonitorBoundsInPointsInDisplayCoordinateSystem(clientAreaInPixels, autoscaleZoom));
2201+
} else {
2202+
monitor.setBounds(DPIUtil.autoScaleDown(boundsInPixels));
2203+
monitor.setClientArea(DPIUtil.autoScaleDown(clientAreaInPixels));
2204+
}
21942205
if (result == 0) {
21952206
System.err.println("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor.");
21962207
result = 100;
@@ -2203,6 +2214,13 @@ Monitor getMonitor (long hmonitor) {
22032214
return monitor;
22042215
}
22052216

2217+
private Rectangle getMonitorBoundsInPointsInDisplayCoordinateSystem(Rectangle boundsInPixels, int zoom) {
2218+
Rectangle bounds = DPIUtil.scaleDown(boundsInPixels, zoom);
2219+
bounds.x = boundsInPixels.x;
2220+
bounds.y = boundsInPixels.y;
2221+
return bounds;
2222+
}
2223+
22062224
/**
22072225
* Returns an array of monitors attached to the device.
22082226
*
@@ -2944,6 +2962,9 @@ boolean isValidThread () {
29442962
public Point map (Control from, Control to, Point point) {
29452963
checkDevice ();
29462964
if (point == null) error (SWT.ERROR_NULL_ARGUMENT);
2965+
if (isRescalingAtRuntime()) {
2966+
return map(from, to, point.x, point.y);
2967+
}
29472968
int zoom = getZoomLevelForMapping(from, to);
29482969
point = DPIUtil.scaleUp(point, zoom);
29492970
return DPIUtil.scaleDown(mapInPixels(from, to, point), zoom);
@@ -2991,6 +3012,20 @@ Point mapInPixels (Control from, Control to, Point point) {
29913012
*/
29923013
public Point map (Control from, Control to, int x, int y) {
29933014
checkDevice ();
3015+
if (isRescalingAtRuntime()) {
3016+
Point mappedPointInPoints;
3017+
if (from == null) {
3018+
Point mappedPointInpixels = mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), x, y));
3019+
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
3020+
} else if (to == null) {
3021+
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
3022+
mappedPointInPoints = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x, mappedPointInpixels.y);
3023+
} else {
3024+
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
3025+
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
3026+
}
3027+
return mappedPointInPoints;
3028+
}
29943029
int zoom = getZoomLevelForMapping(from, to);
29953030
x = DPIUtil.scaleUp(x, zoom);
29963031
y = DPIUtil.scaleUp(y, zoom);
@@ -3058,6 +3093,9 @@ private int getZoomLevelForMapping(Control from, Control to) {
30583093
public Rectangle map (Control from, Control to, Rectangle rectangle) {
30593094
checkDevice ();
30603095
if (rectangle == null) error (SWT.ERROR_NULL_ARGUMENT);
3096+
if (isRescalingAtRuntime()) {
3097+
return map(from, to, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
3098+
}
30613099
int zoom = getZoomLevelForMapping(from, to);
30623100
rectangle = DPIUtil.scaleUp(rectangle, zoom);
30633101
return DPIUtil.scaleDown(mapInPixels(from, to, rectangle), zoom);
@@ -3107,6 +3145,20 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
31073145
*/
31083146
public Rectangle map (Control from, Control to, int x, int y, int width, int height) {
31093147
checkDevice ();
3148+
if (isRescalingAtRuntime()) {
3149+
Rectangle mappedRectangleInPoints;
3150+
if (from == null) {
3151+
Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, to.getShell().getMonitor()));
3152+
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
3153+
} else if (to == null) {
3154+
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
3155+
mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height, from.getShell().getMonitor());
3156+
} else {
3157+
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
3158+
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
3159+
}
3160+
return mappedRectangleInPoints;
3161+
}
31103162
int zoom = getZoomLevelForMapping(from, to);
31113163
x = DPIUtil.scaleUp(x, zoom);
31123164
y = DPIUtil.scaleUp(y, zoom);
@@ -3130,6 +3182,57 @@ Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int he
31303182
return new Rectangle (rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
31313183
}
31323184

3185+
Point translateLocationInPixelsInDisplayCoordinateSystem(int x, int y) {
3186+
Monitor monitor = getContainingMonitor(x, y);
3187+
return getPixelsFromPoint(monitor, x, y);
3188+
}
3189+
3190+
Point translateLocationInPointInDisplayCoordinateSystem(int x, int y) {
3191+
Monitor monitor = getContainingMonitorInPixelsCoordinate(x, y);
3192+
return getPointFromPixels(monitor, x, y);
3193+
}
3194+
3195+
Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment(int x, int y, int width, int height) {
3196+
Monitor monitorByLocation = getContainingMonitor(x, y);
3197+
Monitor monitorByContainment = getContainingMonitor(x, y, width, height);
3198+
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitorByLocation, monitorByContainment);
3199+
}
3200+
3201+
private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height, Monitor monitor) {
3202+
return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitor, monitor);
3203+
}
3204+
3205+
private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height, Monitor monitorOfLocation, Monitor monitorOfArea) {
3206+
Point topLeft = getPixelsFromPoint(monitorOfLocation, x, y);
3207+
int zoom = getApplicableMonitorZoom(monitorOfArea);
3208+
int widthInPixels = DPIUtil.scaleUp(width, zoom);
3209+
int heightInPixels = DPIUtil.scaleUp(height, zoom);
3210+
return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels);
3211+
}
3212+
3213+
Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment(int x, int y, int widthInPixels, int heightInPixels) {
3214+
Monitor monitorByLocation = getContainingMonitor(x, y);
3215+
Monitor monitorByContainment = getContainingMonitor(x, y, widthInPixels, heightInPixels);
3216+
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitorByLocation, monitorByContainment);
3217+
}
3218+
3219+
private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) {
3220+
return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitor, monitor);
3221+
}
3222+
3223+
3224+
private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels, int heightInPixels, Monitor monitorOfLocation, Monitor monitorOfArea) {
3225+
Point topLeft = getPointFromPixels(monitorOfLocation, x, y);
3226+
int zoom = getApplicableMonitorZoom(monitorOfArea);
3227+
int width = DPIUtil.scaleDown(widthInPixels, zoom);
3228+
int height = DPIUtil.scaleDown(heightInPixels, zoom);
3229+
return new Rectangle(topLeft.x, topLeft.y, width, height);
3230+
}
3231+
3232+
private int getApplicableMonitorZoom(Monitor monitor) {
3233+
return DPIUtil.getZoomForAutoscaleProperty(isRescalingAtRuntime() ? monitor.zoom : getDeviceZoom());
3234+
}
3235+
31333236
long messageProc (long hwnd, long msg, long wParam, long lParam) {
31343237
switch ((int)msg) {
31353238
case SWT_RUNASYNC: {
@@ -4355,7 +4458,12 @@ public void sendPostExternalEventDispatchEvent () {
43554458
*/
43564459
public void setCursorLocation (int x, int y) {
43574460
checkDevice ();
4358-
setCursorLocationInPixels (DPIUtil.autoScaleUp (x), DPIUtil.autoScaleUp (y));
4461+
if (isRescalingAtRuntime()) {
4462+
Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem(x, y);
4463+
setCursorLocationInPixels (cursorLocationInPixels.x, cursorLocationInPixels.y);
4464+
} else {
4465+
setCursorLocationInPixels (DPIUtil.autoScaleUp (x), DPIUtil.autoScaleUp (y));
4466+
}
43594467
}
43604468

43614469
void setCursorLocationInPixels (int x, int y) {
@@ -5390,4 +5498,63 @@ private boolean setDPIAwareness(int desiredDpiAwareness) {
53905498
return true;
53915499
}
53925500

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

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,56 @@ public void setAlpha (int alpha) {
15661566
}
15671567
}
15681568

1569+
@Override
1570+
public Rectangle getBounds() {
1571+
if (getDisplay().isRescalingAtRuntime()) {
1572+
Rectangle boundsInPixels = getBoundsInPixels();
1573+
return display.translateRectangleInPointsInDisplayCoordinateSystemByContainment(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height);
1574+
}
1575+
return super.getBounds();
1576+
}
1577+
1578+
@Override
1579+
public Point getLocation() {
1580+
if (getDisplay().isRescalingAtRuntime()) {
1581+
Point locationInPixels = getLocationInPixels();
1582+
return display.translateLocationInPointInDisplayCoordinateSystem(locationInPixels.x, locationInPixels.y);
1583+
}
1584+
return super.getLocation();
1585+
}
1586+
1587+
@Override
1588+
public void setLocation(Point location) {
1589+
if (location == null) error (SWT.ERROR_NULL_ARGUMENT);
1590+
setLocation(location.x, location.y);
1591+
}
1592+
1593+
@Override
1594+
public void setLocation(int x, int y) {
1595+
if (getDisplay().isRescalingAtRuntime()) {
1596+
Point location = display.translateLocationInPixelsInDisplayCoordinateSystem(x, y);
1597+
setLocationInPixels(location.x, location.y);
1598+
} else {
1599+
super.setLocation(x, y);
1600+
}
1601+
}
1602+
1603+
@Override
1604+
public void setBounds(Rectangle rect) {
1605+
if (rect == null) error (SWT.ERROR_NULL_ARGUMENT);
1606+
setBounds(rect.x, rect.y, rect.width, rect.height);
1607+
}
1608+
1609+
@Override
1610+
public void setBounds(int x, int y, int width, int height) {
1611+
if (getDisplay().isRescalingAtRuntime()) {
1612+
Rectangle boundsInPixels = display.translateRectangleInPixelsInDisplayCoordinateSystemByContainment(x, y, width, height);
1613+
setBoundsInPixels(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height);
1614+
} else {
1615+
super.setBounds(x, y, width, height);
1616+
}
1617+
}
1618+
15691619
@Override
15701620
void setBoundsInPixels (int x, int y, int width, int height, int flags, boolean defer) {
15711621
if (fullScreen) setFullScreen (false);

0 commit comments

Comments
 (0)