Skip to content

Commit e071469

Browse files
committed
Add OfFloat.from methods in Point/Rectangle
This commit adds Rectangle.OfFloat.from and Point.OfFloat.from methods and removes the Win32DPIUtils.FloatAwareGeometryFactory class to make these methods OS-independent.
1 parent 1ae0e18 commit e071469

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ public void setY(float y) {
158158
this.y = Math.round(y);
159159
this.residualY = y - this.y;
160160
}
161+
162+
public static Point.OfFloat from(Point point) {
163+
if (point instanceof Point.OfFloat pointOfFloat) {
164+
return new Point.OfFloat(pointOfFloat.getX(), pointOfFloat.getY());
165+
}
166+
return new Point.OfFloat(point.x, point.y);
167+
}
161168
}
162169

163170
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ public void setHeight(float height) {
459459
this.residualHeight = height - this.height;
460460
}
461461

462+
public static Rectangle.OfFloat from(Rectangle rectangle) {
463+
if (rectangle instanceof Rectangle.OfFloat rectangleOfFloat) {
464+
return new Rectangle.OfFloat(rectangleOfFloat.getX(), rectangleOfFloat.getY(), rectangleOfFloat.getWidth(), rectangleOfFloat.getHeight());
465+
}
466+
return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
467+
}
468+
462469
}
463470

464471
/**

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static float pixelToPoint(Drawable drawable, float size, int zoom) {
115115

116116
public static Point pixelToPoint(Point point, int zoom) {
117117
if (zoom == 100 || point == null) return point;
118-
Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point);
118+
Point.OfFloat fPoint = Point.OfFloat.from(point);
119119
float scaleFactor = DPIUtil.getScalingFactor(zoom);
120120
float scaledX = fPoint.getX() / scaleFactor;
121121
float scaledY = fPoint.getY() / scaleFactor;
@@ -170,7 +170,7 @@ public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int current
170170
*/
171171
private static Rectangle scaleBounds (Rectangle.OfFloat rect, int targetZoom, int currentZoom) {
172172
if (rect == null || targetZoom == currentZoom) return rect;
173-
Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect);
173+
Rectangle.OfFloat fRect = Rectangle.OfFloat.from(rect);
174174
float scaleFactor = DPIUtil.getScalingFactor(targetZoom, currentZoom);
175175
float scaledX = fRect.getX() * scaleFactor;
176176
float scaledY = fRect.getY() * scaleFactor;
@@ -221,7 +221,7 @@ public static float pointToPixel(Drawable drawable, float size, int zoom) {
221221

222222
public static Point pointToPixel(Point point, int zoom) {
223223
if (zoom == 100 || point == null) return point;
224-
Point.OfFloat fPoint = FloatAwareGeometryFactory.createFrom(point);
224+
Point.OfFloat fPoint = Point.OfFloat.from(point);
225225
float scaleFactor = DPIUtil.getScalingFactor(zoom);
226226
float scaledX = fPoint.getX() * scaleFactor;
227227
float scaledY = fPoint.getY() * scaleFactor;
@@ -323,20 +323,4 @@ public ImageData getImageData(int zoom) {
323323
return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom);
324324
}
325325
}
326-
327-
private class FloatAwareGeometryFactory {
328-
static Rectangle.OfFloat createFrom(Rectangle rectangle) {
329-
if (rectangle instanceof Rectangle.OfFloat) {
330-
return (Rectangle.OfFloat) rectangle;
331-
}
332-
return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
333-
}
334-
335-
static Point.OfFloat createFrom(Point point) {
336-
if (point instanceof Point.OfFloat) {
337-
return (Point.OfFloat) point;
338-
}
339-
return new Point.OfFloat(point.x, point.y);
340-
}
341-
}
342326
}

0 commit comments

Comments
 (0)