Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[win32] Revert and adapt fix for coordinate system for rescaling scenario #1590

Merged
merged 2 commits into from
Nov 13, 2024
Merged
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 @@ -3969,9 +3969,13 @@ void subclass () {
*/
public Point toControl (int x, int y) {
checkWidget ();
Point displayPointInPixels = getDisplay().translateLocationInPixelsInDisplayCoordinateSystem(x, y);
final Point controlPointInPixels = toControlInPixels(displayPointInPixels.x, displayPointInPixels.y);
return DPIUtil.scaleDown(controlPointInPixels, getZoom());
int zoom = getZoom();
if (getDisplay().isRescalingAtRuntime()) {
Point displayPointInPixels = getDisplay().translateLocationInPixelsInDisplayCoordinateSystem(x, y);
final Point controlPointInPixels = toControlInPixels(displayPointInPixels.x, displayPointInPixels.y);
return DPIUtil.scaleDown(controlPointInPixels, zoom);
}
return DPIUtil.scaleDown(toControlInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
}

Point toControlInPixels (int x, int y) {
Expand Down Expand Up @@ -4030,8 +4034,11 @@ public Point toControl (Point point) {
public Point toDisplay (int x, int y) {
checkWidget ();
int zoom = getZoom();
Point displayPointInPixels = toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom));
return getDisplay().translateLocationInPointInDisplayCoordinateSystem(displayPointInPixels.x, displayPointInPixels.y);
if (getDisplay().isRescalingAtRuntime()) {
Point displayPointInPixels = toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom));
return getDisplay().translateLocationInPointInDisplayCoordinateSystem(displayPointInPixels.x, displayPointInPixels.y);
}
return DPIUtil.scaleDown(toDisplayInPixels(DPIUtil.scaleUp(x, zoom), DPIUtil.scaleUp(y, zoom)), zoom);
}

Point toDisplayInPixels (int x, int y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,10 @@ public Control getCursorControl () {
public Point getCursorLocation () {
checkDevice ();
Point cursorLocationInPixels = getCursorLocationInPixels();
return translateLocationInPointInDisplayCoordinateSystem(cursorLocationInPixels.x, cursorLocationInPixels.y);
if (isRescalingAtRuntime()) {
return translateLocationInPointInDisplayCoordinateSystem(cursorLocationInPixels.x, cursorLocationInPixels.y);
}
return DPIUtil.autoScaleDown(cursorLocationInPixels);
}

Point getCursorLocationInPixels () {
Expand Down Expand Up @@ -2191,18 +2194,18 @@ Monitor getMonitor (long hmonitor) {
int result = OS.GetDpiForMonitor (monitor.handle, OS.MDT_EFFECTIVE_DPI, dpiX, dpiY);
result = (result == OS.S_OK) ? DPIUtil.mapDPIToZoom (dpiX[0]) : 100;

int autoscaleZoom;
if (DPIUtil.isAutoScaleOnRuntimeActive()) {
autoscaleZoom = DPIUtil.getZoomForAutoscaleProperty(result);
int autoscaleZoom = DPIUtil.getZoomForAutoscaleProperty(result);
monitor.setBounds(getMonitorBoundsInPointsInDisplayCoordinateSystem(boundsInPixels, autoscaleZoom));
monitor.setClientArea(getMonitorBoundsInPointsInDisplayCoordinateSystem(clientAreaInPixels, autoscaleZoom));
} else {
autoscaleZoom = DPIUtil.getDeviceZoom();
monitor.setBounds(DPIUtil.autoScaleDown(boundsInPixels));
monitor.setClientArea(DPIUtil.autoScaleDown(clientAreaInPixels));
}
if (result == 0) {
System.err.println("***WARNING: GetDpiForMonitor: SWT could not get valid monitor scaling factor.");
result = 100;
}
monitor.setBounds(getMonitorBoundsInPointsInDisplayCoordinateSystem(boundsInPixels, autoscaleZoom));
monitor.setClientArea(getMonitorBoundsInPointsInDisplayCoordinateSystem(clientAreaInPixels, autoscaleZoom));
/*
* Always return true monitor zoom value as fetched from native, else will lead
* to scaling issue on OS Win8.1 and above, for more details refer bug 537614.
Expand Down Expand Up @@ -2959,7 +2962,12 @@ boolean isValidThread () {
public Point map (Control from, Control to, Point point) {
checkDevice ();
if (point == null) error (SWT.ERROR_NULL_ARGUMENT);
return map(from, to, point.x, point.y);
if (isRescalingAtRuntime()) {
return map(from, to, point.x, point.y);
}
int zoom = getZoomLevelForMapping(from, to);
point = DPIUtil.scaleUp(point, zoom);
return DPIUtil.scaleDown(mapInPixels(from, to, point), zoom);
}

Point mapInPixels (Control from, Control to, Point point) {
Expand Down Expand Up @@ -3004,18 +3012,24 @@ Point mapInPixels (Control from, Control to, Point point) {
*/
public Point map (Control from, Control to, int x, int y) {
checkDevice ();
Point mappedPointInPoints;
if (from == null) {
Point mappedPointInpixels = mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), x, y));
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
} else if (to == null) {
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
mappedPointInPoints = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x, mappedPointInpixels.y);
} else {
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
if (isRescalingAtRuntime()) {
Point mappedPointInPoints;
if (from == null) {
Point mappedPointInpixels = mapInPixels(from, to, getPixelsFromPoint(to.getShell().getMonitor(), x, y));
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
} else if (to == null) {
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
mappedPointInPoints = getPointFromPixels(from.getShell().getMonitor(), mappedPointInpixels.x, mappedPointInpixels.y);
} else {
Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom()));
mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom());
}
return mappedPointInPoints;
}
return mappedPointInPoints;
int zoom = getZoomLevelForMapping(from, to);
x = DPIUtil.scaleUp(x, zoom);
y = DPIUtil.scaleUp(y, zoom);
return DPIUtil.scaleDown(mapInPixels(from, to, x, y), zoom);
}

Point mapInPixels (Control from, Control to, int x, int y) {
Expand All @@ -3031,6 +3045,15 @@ Point mapInPixels (Control from, Control to, int x, int y) {
return new Point (point.x, point.y);
}

private int getZoomLevelForMapping(Control from, Control to) {
if (from != null && from.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
if (to != null && to.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
if (to != null) {
return to.getZoom();
}
return from.getZoom();
}

/**
* Maps a point from one coordinate system to another.
* When the control is null, coordinates are mapped to
Expand Down Expand Up @@ -3070,7 +3093,12 @@ Point mapInPixels (Control from, Control to, int x, int y) {
public Rectangle map (Control from, Control to, Rectangle rectangle) {
checkDevice ();
if (rectangle == null) error (SWT.ERROR_NULL_ARGUMENT);
return map(from, to, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
if (isRescalingAtRuntime()) {
return map(from, to, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
int zoom = getZoomLevelForMapping(from, to);
rectangle = DPIUtil.scaleUp(rectangle, zoom);
return DPIUtil.scaleDown(mapInPixels(from, to, rectangle), zoom);
}

Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
Expand Down Expand Up @@ -3117,18 +3145,26 @@ Rectangle mapInPixels (Control from, Control to, Rectangle rectangle) {
*/
public Rectangle map (Control from, Control to, int x, int y, int width, int height) {
checkDevice ();
Rectangle mappedRectangleInPoints;
if (from == null) {
Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, to.getShell().getMonitor()));
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
} else if (to == null) {
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height, from.getShell().getMonitor());
} else {
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
if (isRescalingAtRuntime()) {
Rectangle mappedRectangleInPoints;
if (from == null) {
Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, to.getShell().getMonitor()));
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
} else if (to == null) {
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height, from.getShell().getMonitor());
} else {
Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom()));
mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom());
}
return mappedRectangleInPoints;
}
return mappedRectangleInPoints;
int zoom = getZoomLevelForMapping(from, to);
x = DPIUtil.scaleUp(x, zoom);
y = DPIUtil.scaleUp(y, zoom);
width = DPIUtil.scaleUp(width, zoom);
height = DPIUtil.scaleUp(height, zoom);
return DPIUtil.scaleDown(mapInPixels(from, to, x, y, width, height), zoom);
}

Rectangle mapInPixels (Control from, Control to, int x, int y, int width, int height) {
Expand Down Expand Up @@ -4422,8 +4458,12 @@ public void sendPostExternalEventDispatchEvent () {
*/
public void setCursorLocation (int x, int y) {
checkDevice ();
Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem(x, y);
setCursorLocationInPixels (cursorLocationInPixels.x, cursorLocationInPixels.y);
if (isRescalingAtRuntime()) {
Point cursorLocationInPixels = translateLocationInPixelsInDisplayCoordinateSystem(x, y);
setCursorLocationInPixels (cursorLocationInPixels.x, cursorLocationInPixels.y);
} else {
setCursorLocationInPixels (DPIUtil.autoScaleUp (x), DPIUtil.autoScaleUp (y));
}
}

void setCursorLocationInPixels (int x, int y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,14 +1568,20 @@ public void setAlpha (int alpha) {

@Override
public Rectangle getBounds() {
Rectangle boundsInPixels = getBoundsInPixels();
return display.translateRectangleInPointsInDisplayCoordinateSystemByContainment(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height);
if (getDisplay().isRescalingAtRuntime()) {
Rectangle boundsInPixels = getBoundsInPixels();
return display.translateRectangleInPointsInDisplayCoordinateSystemByContainment(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height);
}
return super.getBounds();
}

@Override
public Point getLocation() {
Point locationInPixels = getLocationInPixels();
return display.translateLocationInPointInDisplayCoordinateSystem(locationInPixels.x, locationInPixels.y);
if (getDisplay().isRescalingAtRuntime()) {
Point locationInPixels = getLocationInPixels();
return display.translateLocationInPointInDisplayCoordinateSystem(locationInPixels.x, locationInPixels.y);
}
return super.getLocation();
}

@Override
Expand All @@ -1586,8 +1592,12 @@ public void setLocation(Point location) {

@Override
public void setLocation(int x, int y) {
Point location = display.translateLocationInPixelsInDisplayCoordinateSystem(x, y);
setLocationInPixels(location.x, location.y);
if (getDisplay().isRescalingAtRuntime()) {
Point location = display.translateLocationInPixelsInDisplayCoordinateSystem(x, y);
setLocationInPixels(location.x, location.y);
} else {
super.setLocation(x, y);
}
}

@Override
Expand All @@ -1598,8 +1608,12 @@ public void setBounds(Rectangle rect) {

@Override
public void setBounds(int x, int y, int width, int height) {
Rectangle boundsInPixels = display.translateRectangleInPixelsInDisplayCoordinateSystemByContainment(x, y, width, height);
setBoundsInPixels(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height);
if (getDisplay().isRescalingAtRuntime()) {
Rectangle boundsInPixels = display.translateRectangleInPixelsInDisplayCoordinateSystemByContainment(x, y, width, height);
setBoundsInPixels(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height);
} else {
super.setBounds(x, y, width, height);
}
}

@Override
Expand Down
Loading