Skip to content

Commit 6fdc08d

Browse files
committed
Add float precision to MonitorAwarePoint and MonitorAwareRectangle
1 parent edc5529 commit 6fdc08d

File tree

8 files changed

+160
-97
lines changed

8 files changed

+160
-97
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {
100100

101101
button.setBounds(new Rectangle(0, 47, 200, 47));
102102
assertEquals("Control::setBounds(Rectangle) doesn't scale up correctly",
103-
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
103+
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
104104

105105
button.setBounds(0, 47, 200, 47);
106106
assertEquals("Control::setBounds(int, int, int, int) doesn't scale up correctly",
107-
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
107+
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
108108
}
109109

110110
record FontComparison(int originalFontHeight, int currentFontHeight) {
Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
/*******************************************************************************
2-
* Copyright (c) 2025 Yatta Solutions and others.
3-
*
4-
* This program and the accompanying materials
5-
* are made available under the terms of the Eclipse Public License 2.0
6-
* which accompanies this distribution, and is available at
7-
* https://www.eclipse.org/legal/epl-2.0/
8-
*
9-
* SPDX-License-Identifier: EPL-2.0
10-
*
11-
* Contributors:
12-
* Yatta Solutions - initial API and implementation
13-
*******************************************************************************/
141
package org.eclipse.swt.graphics;
152

163
import org.eclipse.swt.widgets.*;
@@ -26,10 +13,24 @@
2613
*/
2714
public final class MonitorAwarePoint extends Point {
2815

29-
private static final long serialVersionUID = 6077427420686999194L;
16+
private static final long serialVersionUID = 7516155847004716654L;
17+
18+
public float residualX, residualY;
3019

3120
private final Monitor monitor;
3221

22+
public MonitorAwarePoint(int x, int y) {
23+
this(x, y, null);
24+
}
25+
26+
public MonitorAwarePoint(float x, float y) {
27+
super(Math.round(x), Math.round(y));
28+
this.residualX = x - this.x;
29+
this.residualY = y - this.y;
30+
this.monitor = null;
31+
}
32+
33+
3334
/**
3435
* Constructs a new MonitorAwarePoint
3536
*
@@ -49,14 +50,21 @@ public Monitor getMonitor() {
4950
return monitor;
5051
}
5152

52-
@Override
53-
public boolean equals(Object object) {
54-
return super.equals(object);
53+
public float getX() {
54+
return x + residualX;
5555
}
5656

57-
@Override
58-
public int hashCode() {
59-
return super.hashCode();
57+
public float getY() {
58+
return y + residualY;
6059
}
6160

62-
}
61+
public void setX(float x) {
62+
this.x = Math.round(x);
63+
this.residualX = x - this.x;
64+
}
65+
66+
public void setY(float y) {
67+
this.y = Math.round(y);
68+
this.residualY = y - this.y;
69+
}
70+
}
Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
/*******************************************************************************
2-
* Copyright (c) 2025 Yatta Solutions and others.
3-
*
4-
* This program and the accompanying materials
5-
* are made available under the terms of the Eclipse Public License 2.0
6-
* which accompanies this distribution, and is available at
7-
* https://www.eclipse.org/legal/epl-2.0/
8-
*
9-
* SPDX-License-Identifier: EPL-2.0
10-
*
11-
* Contributors:
12-
* Yatta Solutions - initial API and implementation
13-
*******************************************************************************/
141
package org.eclipse.swt.graphics;
152

163
import org.eclipse.swt.widgets.*;
@@ -26,24 +13,34 @@
2613
*/
2714
public final class MonitorAwareRectangle extends Rectangle {
2815

29-
private static final long serialVersionUID = 5041911840525116925L;
16+
private static final long serialVersionUID = -54807918875027527L;
17+
18+
private float residualX, residualY, residualWidth, residualHeight;
3019

3120
private final Monitor monitor;
3221

33-
/**
34-
* Constructs a new MonitorAwareRectangle
35-
*
36-
* @param x the x coordinate of the top left corner of the rectangle
37-
* @param y the y coordinate of the top left corner of the rectangle
38-
* @param width the width of the rectangle
39-
* @param height the height of the rectangle
40-
* @param monitor the monitor with whose context the rectangle is created
41-
*/
22+
public MonitorAwareRectangle(int x, int y, int width, int height) {
23+
this(x, y, width, height, null);
24+
}
25+
26+
public MonitorAwareRectangle(float x, float y, float width, float height) {
27+
this(x, y, width, height, null);
28+
}
29+
4230
public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monitor) {
4331
super(x, y, width, height);
4432
this.monitor = monitor;
4533
}
4634

35+
MonitorAwareRectangle(float x, float y, float width, float height, Monitor monitor) {
36+
super(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
37+
this.residualX = x - this.x;
38+
this.residualY = y - this.y;
39+
this.residualWidth = width - this.width;
40+
this.residualHeight = height - this.height;
41+
this.monitor = monitor;
42+
}
43+
4744
/**
4845
* {@return the monitor with whose context the instance is created}
4946
*/
@@ -52,18 +49,44 @@ public Monitor getMonitor() {
5249
}
5350

5451
@Override
55-
public boolean equals(Object object) {
56-
return super.equals(object);
52+
public MonitorAwareRectangle clone() {
53+
return new MonitorAwareRectangle(getX(), getY(), getWidth(), getHeight(), monitor);
5754
}
5855

59-
@Override
60-
public int hashCode() {
61-
return super.hashCode();
56+
public float getX() {
57+
return x + residualX;
6258
}
6359

64-
@Override
65-
public MonitorAwareRectangle clone() {
66-
return new MonitorAwareRectangle(x, y, width, height, monitor);
60+
public float getY() {
61+
return y + residualY;
62+
}
63+
64+
public float getWidth() {
65+
return width + residualWidth;
66+
}
67+
68+
public float getHeight() {
69+
return height + residualHeight;
70+
}
71+
72+
public void setX(float x) {
73+
this.x = Math.round(x);
74+
this.residualX = x - this.x;
75+
}
76+
77+
public void setY(float y) {
78+
this.y = Math.round(y);
79+
this.residualY = y - this.y;
80+
}
81+
82+
public void setWidth(float width) {
83+
this.width = Math.round(width);
84+
this.residualWidth = width - this.width;
85+
}
86+
87+
public void setHeight(float height) {
88+
this.height = Math.round(height);
89+
this.residualHeight = height - this.height;
6790
}
6891

69-
}
92+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,4 @@ public int hashCode () {
115115
public String toString () {
116116
return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
117117
}
118-
119118
}
120-

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public Rectangle union (Rectangle rect) {
374374
*/
375375
public static Rectangle of(Point topLeft, int width, int height) {
376376
if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) {
377-
return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor());
377+
return new MonitorAwareRectangle(monitorAwareTopLeft.getX(), monitorAwareTopLeft.getY(), width, height, monitorAwareTopLeft.getMonitor());
378378
}
379379
return new Rectangle(topLeft.x, topLeft.y, width, height);
380380
}
@@ -396,4 +396,4 @@ public static Rectangle of(Point topLeft, int width, int height) {
396396
public Rectangle clone() {
397397
return new Rectangle(x, y, width, height);
398398
}
399-
}
399+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ public static Point autoScaleDown(Point point) {
228228

229229
public static Point scaleDown(Point point, int zoom) {
230230
if (zoom == 100 || point == null) return point;
231+
MonitorAwarePoint fPoint = FloatAwareGeometryFactory.createFrom(point);
231232
float scaleFactor = getScalingFactor(zoom);
232-
Point scaledPoint = new Point (0,0);
233-
scaledPoint.x = Math.round (point.x / scaleFactor);
234-
scaledPoint.y = Math.round (point.y / scaleFactor);
235-
return scaledPoint;
233+
float scaledX = fPoint.getX() / scaleFactor;
234+
float scaledY = fPoint.getY() / scaleFactor;
235+
return new MonitorAwarePoint(scaledX, scaledY);
236236
}
237237

238238
/**
@@ -255,16 +255,7 @@ public static Rectangle autoScaleDown(Rectangle rect) {
255255
}
256256

257257
public static Rectangle scaleDown(Rectangle rect, int zoom) {
258-
if (zoom == 100 || rect == null) return rect;
259-
Rectangle scaledRect = new Rectangle (0,0,0,0);
260-
Point scaledTopLeft = scaleDown(new Point (rect.x, rect.y), zoom);
261-
Point scaledBottomRight = scaleDown(new Point (rect.x + rect.width, rect.y + rect.height), zoom);
262-
263-
scaledRect.x = scaledTopLeft.x;
264-
scaledRect.y = scaledTopLeft.y;
265-
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
266-
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
267-
return scaledRect;
258+
return scaleBounds(rect, 100, zoom);
268259
}
269260
/**
270261
* Returns a new scaled down Rectangle if enabled for Drawable class.
@@ -333,13 +324,13 @@ public static boolean isSmoothScalingEnabled() {
333324
*/
334325
public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) {
335326
if (rect == null || targetZoom == currentZoom) return rect;
327+
MonitorAwareRectangle fRect = FloatAwareGeometryFactory.createFrom(rect);
336328
float scaleFactor = ((float)targetZoom) / (float)currentZoom;
337-
Rectangle returnRect = new Rectangle (0,0,0,0);
338-
returnRect.x = Math.round (rect.x * scaleFactor);
339-
returnRect.y = Math.round (rect.y * scaleFactor);
340-
returnRect.width = Math.round (rect.width * scaleFactor);
341-
returnRect.height = Math.round (rect.height * scaleFactor);
342-
return returnRect;
329+
float scaledX = fRect.getX() * scaleFactor;
330+
float scaledY = fRect.getY() * scaleFactor;
331+
float scaledWidth = fRect.getWidth() * scaleFactor;
332+
float scaledHeight = fRect.getHeight() * scaleFactor;
333+
return new MonitorAwareRectangle(scaledX, scaledY, scaledWidth, scaledHeight);
343334
}
344335

345336
/**
@@ -436,11 +427,11 @@ public static Point autoScaleUp(Point point) {
436427

437428
public static Point scaleUp(Point point, int zoom) {
438429
if (zoom == 100 || point == null) return point;
430+
MonitorAwarePoint fPoint = FloatAwareGeometryFactory.createFrom(point);
439431
float scaleFactor = getScalingFactor(zoom);
440-
Point scaledPoint = new Point(0,0);
441-
scaledPoint.x = Math.round (point.x * scaleFactor);
442-
scaledPoint.y = Math.round (point.y * scaleFactor);
443-
return scaledPoint;
432+
float scaledX = fPoint.getX() * scaleFactor;
433+
float scaledY = fPoint.getY() * scaleFactor;
434+
return new MonitorAwarePoint(scaledX, scaledY);
444435
}
445436

446437
/**
@@ -463,16 +454,7 @@ public static Rectangle autoScaleUp(Rectangle rect) {
463454
}
464455

465456
public static Rectangle scaleUp(Rectangle rect, int zoom) {
466-
if (zoom == 100 || rect == null) return rect;
467-
Rectangle scaledRect = new Rectangle(0,0,0,0);
468-
Point scaledTopLeft = scaleUp (new Point(rect.x, rect.y), zoom);
469-
Point scaledBottomRight = scaleUp (new Point(rect.x + rect.width, rect.y + rect.height), zoom);
470-
471-
scaledRect.x = scaledTopLeft.x;
472-
scaledRect.y = scaledTopLeft.y;
473-
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
474-
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
475-
return scaledRect;
457+
return scaleBounds(rect, zoom, 100);
476458
}
477459

478460
/**
@@ -751,4 +733,20 @@ public ImageData getImageData(int zoom) {
751733
return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom);
752734
}
753735
}
736+
737+
private class FloatAwareGeometryFactory {
738+
static MonitorAwareRectangle createFrom(Rectangle rectangle) {
739+
if (rectangle instanceof MonitorAwareRectangle mar) {
740+
return mar;
741+
}
742+
return new MonitorAwareRectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
743+
}
744+
745+
static MonitorAwarePoint createFrom(Point point) {
746+
if (point instanceof MonitorAwarePoint map) {
747+
return map;
748+
}
749+
return new MonitorAwarePoint(point.x, point.y);
750+
}
751+
}
754752
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ public Point translateFromDisplayCoordinates(Point point, int zoom) {
9898

9999
@Override
100100
public Point translateToDisplayCoordinates(Point point, int zoom) {
101-
Monitor monitor = point instanceof MonitorAwarePoint monitorAwarePoint ? monitorAwarePoint.getMonitor() : null;
101+
Monitor monitor = point instanceof MonitorAwarePoint pointWithMonitor ? pointWithMonitor.getMonitor() : null;
102102
return translateLocationInPointsToPixels(point.x, point.y, monitor);
103103
}
104104

105105
@Override
106106
public Rectangle translateFromDisplayCoordinates(Rectangle rect, int zoom) {
107-
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null;
107+
Monitor monitor = rect instanceof MonitorAwareRectangle rectWithMonitor ? rectWithMonitor.getMonitor() : null;
108108
return translateRectangleInPixelsToPoints(rect.x, rect.y, rect.width, rect.height, monitor);
109109
}
110110

111111
@Override
112112
public Rectangle translateToDisplayCoordinates(Rectangle rect, int zoom) {
113-
Monitor monitor = rect instanceof MonitorAwareRectangle monitorAwareRect ? monitorAwareRect.getMonitor() : null;
113+
Monitor monitor = rect instanceof MonitorAwareRectangle rectWithMonitor ? rectWithMonitor.getMonitor() : null;
114114
return translateRectangleInPointsToPixels(rect.x, rect.y, rect.width, rect.height, monitor);
115115
}
116116

0 commit comments

Comments
 (0)