Skip to content

Commit e06a5ec

Browse files
committed
Reintroduce OfFloat (internal) classes #62
Deprecate MonitorAwarePoint and MonitorAwareRectangle for removal
1 parent 8a8c67d commit e06a5ec

File tree

9 files changed

+289
-54
lines changed

9 files changed

+289
-54
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) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void translateRectangleInGapPartiallyInRightBackAndForthInSingleZoomShouldBeTheS
137137
void translateRectangleInGapPartiallyInRightBackAndForthInMultiZoomShouldBeInside() {
138138
MultiZoomCoordinateSystemMapper mapper = getMultiZoomCoordinateSystemMapper();
139139
setupMonitors(mapper);
140-
Rectangle rectInPts = new MonitorAwareRectangle(1950, 400, 150, 100, monitors[1]);
140+
Rectangle rectInPts = new Rectangle.WithMonitor(1950, 400, 150, 100, monitors[1]);
141141
Rectangle rectInPxs = mapper.translateToDisplayCoordinates(rectInPts, monitors[0].getZoom());
142142
assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs, monitors[0].getZoom()));
143143
}
@@ -223,15 +223,15 @@ private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, M
223223
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
224224
return new Point(x, y);
225225
} else {
226-
return new MonitorAwarePoint(x, y, monitor);
226+
return new Point.WithMonitor(x, y, monitor);
227227
}
228228
}
229229

230230
private Rectangle createExpectedRectangle(CoordinateSystemMapper mapper, int x, int y, int width, int height, Monitor monitor) {
231231
if (mapper instanceof SingleZoomCoordinateSystemMapper) {
232232
return new Rectangle(x, y, width, height);
233233
} else {
234-
return new MonitorAwareRectangle(x, y, width, height, monitor);
234+
return new Rectangle.WithMonitor(x, y, width, height, monitor);
235235
}
236236
}
237237

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
*
2424
* @since 3.129
2525
* @noreference This class is not intended to be referenced by clients
26+
* @deprecated
2627
*/
28+
@Deprecated(forRemoval = true, since = "2025-09")
2729
public final class MonitorAwarePoint extends Point {
2830

2931
private static final long serialVersionUID = 6077427420686999194L;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
*
2424
* @since 3.129
2525
* @noreference This class is not intended to be referenced by clients
26+
* @deprecated
2627
*/
28+
@Deprecated(forRemoval = true, since = "2025-09")
2729
public final class MonitorAwareRectangle extends Rectangle {
2830

2931
private static final long serialVersionUID = 5041911840525116925L;

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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import java.io.*;
1818

19+
import org.eclipse.swt.widgets.*;
20+
1921
/**
2022
* Instances of this class represent places on the (x, y)
2123
* coordinate plane.
@@ -41,7 +43,8 @@
4143
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4244
*/
4345

44-
public sealed class Point implements Serializable permits MonitorAwarePoint {
46+
@SuppressWarnings("removal")
47+
public sealed class Point implements Serializable permits MonitorAwarePoint, Point.OfFloat {
4548

4649
/**
4750
* the x coordinate of the point
@@ -116,5 +119,82 @@ public String toString () {
116119
return "Point {" + x + ", " + y + "}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
117120
}
118121

122+
/**
123+
* Instances of this class represent {@link org.eclipse.swt.graphics.Point}
124+
* objects with the fields capable of storing more precise value in float.
125+
*
126+
* @since 3.131
127+
* @noreference This class is not intended to be referenced by clients
128+
*/
129+
public static sealed class OfFloat extends Point permits Point.WithMonitor {
130+
131+
private static final long serialVersionUID = -1862062276431597053L;
132+
133+
public float residualX, residualY;
134+
135+
public OfFloat(int x, int y) {
136+
super(x, y);
137+
}
138+
139+
public OfFloat(float x, float y) {
140+
super(Math.round(x), Math.round(y));
141+
this.residualX = x - this.x;
142+
this.residualY = y - this.y;
143+
}
144+
145+
public float getX() {
146+
return x + residualX;
147+
}
148+
149+
public float getY() {
150+
return y + residualY;
151+
}
152+
153+
public void setX(float x) {
154+
this.x = Math.round(x);
155+
this.residualX = x - this.x;
156+
}
157+
158+
public void setY(float y) {
159+
this.y = Math.round(y);
160+
this.residualY = y - this.y;
161+
}
119162
}
120163

164+
/**
165+
* Instances of this class represent {@link org.eclipse.swt.graphics.Point.OfFloat}
166+
* objects along with the context of the monitor in relation to which they are
167+
* placed on the display. The monitor awareness makes it easy to scale and
168+
* translate the points between pixels and points.
169+
*
170+
* @since 3.131
171+
* @noreference This class is not intended to be referenced by clients
172+
*/
173+
public static final class WithMonitor extends Point.OfFloat {
174+
175+
private static final long serialVersionUID = 6077427420686999194L;
176+
177+
private final Monitor monitor;
178+
179+
/**
180+
* Constructs a new Point.WithMonitor
181+
*
182+
* @param x the x coordinate of the point
183+
* @param y the y coordinate of the point
184+
* @param monitor the monitor with whose context the point is created
185+
*/
186+
public WithMonitor(int x, int y, Monitor monitor) {
187+
super(x, y);
188+
this.monitor = monitor;
189+
}
190+
191+
/**
192+
* {@return the monitor with whose context the instance is created}
193+
*/
194+
public Monitor getMonitor() {
195+
return monitor;
196+
}
197+
198+
}
199+
200+
}

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

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.*;
1818

1919
import org.eclipse.swt.*;
20+
import org.eclipse.swt.widgets.*;
2021

2122
/**
2223
* Instances of this class represent rectangular areas in an
@@ -45,7 +46,8 @@
4546
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
4647
*/
4748

48-
public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle {
49+
@SuppressWarnings("removal")
50+
public sealed class Rectangle implements Serializable, Cloneable permits MonitorAwareRectangle, Rectangle.OfFloat {
4951

5052
/**
5153
* the x coordinate of the rectangle
@@ -373,8 +375,8 @@ public Rectangle union (Rectangle rect) {
373375
* @since 3.131
374376
*/
375377
public static Rectangle of(Point topLeft, int width, int height) {
376-
if (topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) {
377-
return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor());
378+
if (topLeft instanceof Point.WithMonitor monitorAwareTopLeft) {
379+
return new Rectangle.WithMonitor(monitorAwareTopLeft.getX(), monitorAwareTopLeft.getY(), width, height, monitorAwareTopLeft.getMonitor());
378380
}
379381
return new Rectangle(topLeft.x, topLeft.y, width, height);
380382
}
@@ -396,4 +398,115 @@ public static Rectangle of(Point topLeft, int width, int height) {
396398
public Rectangle clone() {
397399
return new Rectangle(x, y, width, height);
398400
}
401+
402+
/**
403+
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle}
404+
* objects which supports values of Float type for it's fields
405+
*
406+
* @since 3.131
407+
* @noreference This class is not intended to be referenced by clients
408+
*/
409+
public static sealed class OfFloat extends Rectangle permits Rectangle.WithMonitor {
410+
411+
private static final long serialVersionUID = -3006999002677468391L;
412+
413+
private float residualX, residualY, residualWidth, residualHeight;
414+
415+
public OfFloat(int x, int y, int width, int height) {
416+
super(x, y, width, height);
417+
}
418+
419+
public OfFloat(float x, float y, float width, float height) {
420+
super(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
421+
this.residualX = x - this.x;
422+
this.residualY = y - this.y;
423+
this.residualWidth = width - this.width;
424+
this.residualHeight = height - this.height;
425+
}
426+
427+
public float getX() {
428+
return x + residualX;
429+
}
430+
431+
public float getY() {
432+
return y + residualY;
433+
}
434+
435+
public float getWidth() {
436+
return width + residualWidth;
437+
}
438+
439+
public float getHeight() {
440+
return height + residualHeight;
441+
}
442+
443+
public void setX(float x) {
444+
this.x = Math.round(x);
445+
this.residualX = x - this.x;
446+
}
447+
448+
public void setY(float y) {
449+
this.y = Math.round(y);
450+
this.residualY = y - this.y;
451+
}
452+
453+
public void setWidth(float width) {
454+
this.width = Math.round(width);
455+
this.residualWidth = width - this.width;
456+
}
457+
458+
public void setHeight(float height) {
459+
this.height = Math.round(height);
460+
this.residualHeight = height - this.height;
461+
}
462+
463+
}
464+
465+
/**
466+
* Instances of this class represent {@link org.eclipse.swt.graphics.Rectangle.OfFloat}
467+
* objects along with the context of the monitor in relation to which they are
468+
* placed on the display. The monitor awareness makes it easy to scale and
469+
* translate the rectangles between pixels and points.
470+
*
471+
* @since 3.131
472+
* @noreference This class is not intended to be referenced by clients
473+
*/
474+
public static final class WithMonitor extends Rectangle.OfFloat {
475+
476+
private static final long serialVersionUID = 5041911840525116925L;
477+
478+
private final Monitor monitor;
479+
480+
/**
481+
* Constructs a new Rectangle.WithMonitor
482+
*
483+
* @param x the x coordinate of the top left corner of the rectangle
484+
* @param y the y coordinate of the top left corner of the rectangle
485+
* @param width the width of the rectangle
486+
* @param height the height of the rectangle
487+
* @param monitor the monitor with whose context the rectangle is created
488+
*/
489+
public WithMonitor(int x, int y, int width, int height, Monitor monitor) {
490+
super(x, y, width, height);
491+
this.monitor = monitor;
492+
}
493+
494+
private WithMonitor(float x, float y, float width, float height, Monitor monitor) {
495+
super(x, y, width, height);
496+
this.monitor = monitor;
497+
}
498+
499+
/**
500+
* {@return the monitor with whose context the instance is created}
501+
*/
502+
public Monitor getMonitor() {
503+
return monitor;
504+
}
505+
506+
@Override
507+
public Rectangle.WithMonitor clone() {
508+
return new Rectangle.WithMonitor(getX(), getY(), getWidth(), getHeight(), monitor);
509+
}
510+
399511
}
512+
}

0 commit comments

Comments
 (0)