Skip to content

Commit b701ab0

Browse files
Add Rectangle.of(Point,Point) supporting OfFloat
When the size point is Point.OfFloat, return Rectangle.OfFloat to retain sub-pixel precision; otherwise return the classic Rectangle.
1 parent 892be33 commit b701ab0

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,32 @@ public static Rectangle of(Point topLeft, int width, int height) {
383383
return new Rectangle(topLeft.x, topLeft.y, width, height);
384384
}
385385

386+
/**
387+
* Creates a new {@code Rectangle} using the specified Point instances for the
388+
* dimensions.
389+
* <p>
390+
* If the provided {@code Point} instance carries additional OfFloat
391+
* information, an extended {@code Rectangle} type may be returned to preserve
392+
* that context. Otherwise, a standard {@code Rectangle} is returned.
393+
* </p>
394+
*
395+
* @param topLeft the top-left corner of the rectangle
396+
* @param dimension the Point(width, height) of the rectangle
397+
* @return a new {@code Rectangle} instance appropriate for the given point and
398+
* dimensions
399+
* @since 3.133
400+
*/
401+
public static Rectangle of(Point topLeft, Point dimension) {
402+
final boolean isFloat = (topLeft instanceof Point.OfFloat) || (dimension instanceof Point.OfFloat);
403+
404+
final float x = (topLeft instanceof Point.OfFloat p) ? p.getX() : topLeft.x;
405+
final float y = (topLeft instanceof Point.OfFloat p) ? p.getY() : topLeft.y;
406+
final float w = (dimension instanceof Point.OfFloat p) ? p.getX() : dimension.x;
407+
final float h = (dimension instanceof Point.OfFloat p) ? p.getY() : dimension.y;
408+
409+
return isFloat ? new Rectangle.OfFloat(x, y, w, h) : new Rectangle((int) x, (int) y, (int) w, (int) h);
410+
}
411+
386412
/**
387413
* Creates and returns a copy of this {@code Rectangle}.
388414
* <p>

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Rectangle.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,66 @@ public void test_unionLorg_eclipse_swt_graphics_Rectangle() {
298298
assertSWTProblem("Incorrect exception thrown for rectangle == null", SWT.ERROR_NULL_ARGUMENT, e);
299299
}
300300

301+
@Test
302+
void test_bothPointsAreOfFloat() {
303+
Point.OfFloat topLeft = new Point.OfFloat(10.5f, 20.5f);
304+
Point.OfFloat dimension = new Point.OfFloat(30.5f, 40.5f);
305+
306+
Rectangle rect = Rectangle.of(topLeft, dimension);
307+
308+
assertTrue(rect instanceof Rectangle.OfFloat);
309+
Rectangle.OfFloat f = (Rectangle.OfFloat) rect;
310+
311+
assertEquals(10.5f, f.getX());
312+
assertEquals(20.5f, f.getY());
313+
assertEquals(30.5f, f.getWidth());
314+
assertEquals(40.5f, f.getHeight());
315+
}
316+
317+
@Test
318+
void testOnlyTopLeftIsOfFloat() {
319+
Point.OfFloat topLeft = new Point.OfFloat(5.5f, 15.5f);
320+
Point dimension = new Point(100, 200);
321+
322+
Rectangle rect = Rectangle.of(topLeft, dimension);
323+
324+
assertTrue(rect instanceof Rectangle.OfFloat);
325+
Rectangle.OfFloat f = (Rectangle.OfFloat) rect;
326+
327+
assertEquals(5.5f, f.getX());
328+
assertEquals(15.5f, f.getY());
329+
assertEquals(100, f.getWidth());
330+
assertEquals(200, f.getHeight());
331+
}
332+
333+
@Test
334+
void testOnlyDimensionIsOfFloat() {
335+
Point topLeft = new Point(7, 9);
336+
Point.OfFloat dimension = new Point.OfFloat(55.5f, 66.5f);
337+
338+
Rectangle rect = Rectangle.of(topLeft, dimension);
339+
340+
assertTrue(rect instanceof Rectangle.OfFloat);
341+
Rectangle.OfFloat f = (Rectangle.OfFloat) rect;
342+
343+
assertEquals(7, f.getX());
344+
assertEquals(9, f.getY());
345+
assertEquals(55.5f, f.getWidth());
346+
assertEquals(66.5f, f.getHeight());
347+
}
348+
349+
@Test
350+
void testNeitherPointIsOfFloat() {
351+
Point topLeft = new Point(1, 2);
352+
Point dimension = new Point(10, 20);
353+
354+
Rectangle rect = Rectangle.of(topLeft, dimension);
355+
356+
assertFalse(rect instanceof Rectangle.OfFloat);
357+
assertEquals(1, rect.x);
358+
assertEquals(2, rect.y);
359+
assertEquals(10, rect.width);
360+
assertEquals(20, rect.height);
361+
}
362+
301363
}

0 commit comments

Comments
 (0)