Skip to content

Commit 3493f75

Browse files
amartya4256fedejeanne
authored andcommitted
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. It additionally adds clone methods in Point class and sub-classes and Rectangle.OfFloat::clone.
1 parent d9e3302 commit 3493f75

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4444
*/
4545

46-
public sealed class Point implements Serializable permits Point.OfFloat {
46+
public sealed class Point implements Serializable, Cloneable permits Point.OfFloat {
4747

4848
/**
4949
* the x coordinate of the point
@@ -118,6 +118,15 @@ public String toString () {
118118
return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
119119
}
120120

121+
/**
122+
* Creates and returns a shallow copy of this {@code Point}.
123+
* @since 3.132
124+
*/
125+
@Override
126+
public Point clone() {
127+
return new Point(x, y);
128+
}
129+
121130
/**
122131
* Instances of this class represent {@link org.eclipse.swt.graphics.Point}
123132
* objects with the fields capable of storing more precise value in float.
@@ -158,6 +167,21 @@ public void setY(float y) {
158167
this.y = Math.round(y);
159168
this.residualY = y - this.y;
160169
}
170+
171+
@Override
172+
public Point.OfFloat clone() {
173+
return new Point.OfFloat(getX(), getY());
174+
}
175+
176+
/**
177+
* Creates a shallow copy of the provided point as a Point.OfFloat instance.
178+
*/
179+
public static Point.OfFloat from(Point point) {
180+
if (point instanceof Point.OfFloat pointOfFloat) {
181+
return pointOfFloat.clone();
182+
}
183+
return new Point.OfFloat(point.x, point.y);
184+
}
161185
}
162186

163187
/**
@@ -187,13 +211,22 @@ public WithMonitor(int x, int y, Monitor monitor) {
187211
this.monitor = monitor;
188212
}
189213

214+
private WithMonitor(float x, float y, Monitor monitor) {
215+
super(x, y);
216+
this.monitor = monitor;
217+
}
218+
190219
/**
191220
* {@return the monitor with whose context the instance is created}
192221
*/
193222
public Monitor getMonitor() {
194223
return monitor;
195224
}
196225

226+
@Override
227+
public Point.WithMonitor clone() {
228+
return new WithMonitor(getX(), getY(), monitor);
229+
}
197230
}
198231

199232
}

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

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

462+
@Override
463+
public Rectangle.OfFloat clone() {
464+
return new Rectangle.OfFloat(getX(), getY(), getWidth(), getHeight());
465+
}
466+
467+
/**
468+
* Creates a shallow copy of the provided Rectangle as a Rectangle.OfFloat instance.
469+
*/
470+
public static Rectangle.OfFloat from(Rectangle rectangle) {
471+
if (rectangle instanceof Rectangle.OfFloat rectangleOfFloat) {
472+
return rectangleOfFloat.clone();
473+
}
474+
return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
475+
}
476+
462477
}
463478

464479
/**

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;
@@ -330,20 +330,4 @@ public ImageData getImageData(int zoom) {
330330
return DPIUtil.scaleImageData(device, imageData, zoom, currentZoom);
331331
}
332332
}
333-
334-
private class FloatAwareGeometryFactory {
335-
static Rectangle.OfFloat createFrom(Rectangle rectangle) {
336-
if (rectangle instanceof Rectangle.OfFloat) {
337-
return (Rectangle.OfFloat) rectangle;
338-
}
339-
return new Rectangle.OfFloat(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
340-
}
341-
342-
static Point.OfFloat createFrom(Point point) {
343-
if (point instanceof Point.OfFloat) {
344-
return (Point.OfFloat) point;
345-
}
346-
return new Point.OfFloat(point.x, point.y);
347-
}
348-
}
349333
}

0 commit comments

Comments
 (0)