Skip to content

Commit efcb233

Browse files
Add Rectangle.of(Point,Point) supporting OfFloat
Extract coordinates once, return WithMonitor if present, otherwise choose OfFloat when needed or fall back to a normal Rectangle.
1 parent 892be33 commit efcb233

2 files changed

Lines changed: 101 additions & 2 deletions

File tree

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,49 @@ public Rectangle union (Rectangle rect) {
377377
* @since 3.131
378378
*/
379379
public static Rectangle of(Point topLeft, int width, int height) {
380-
if (topLeft instanceof Point.WithMonitor monitorAwareTopLeft) {
381-
return new Rectangle.WithMonitor(monitorAwareTopLeft.getX(), monitorAwareTopLeft.getY(), width, height, monitorAwareTopLeft.getMonitor());
380+
if (topLeft instanceof Point.WithMonitor pm) {
381+
return new Rectangle.WithMonitor(pm.getX(), pm.getY(), width, height, pm.getMonitor());
382382
}
383+
384+
if (topLeft instanceof Point.OfFloat p) {
385+
return new Rectangle.OfFloat(p.getX(), p.getY(), width, height);
386+
}
387+
383388
return new Rectangle(topLeft.x, topLeft.y, width, height);
384389
}
385390

391+
/**
392+
* Creates a new {@code Rectangle} using the specified Point instances for the
393+
* dimensions.
394+
* <p>
395+
* If the provided {@code Point} instance carries additional contextual
396+
* information, an extended {@code Rectangle} type may be returned to preserve
397+
* that context. Otherwise, a standard {@code Rectangle} is returned.
398+
* </p>
399+
*
400+
* @param topLeft the top-left corner of the rectangle
401+
* @param dimension the Point(width, height) of the rectangle
402+
* @return a new {@code Rectangle} instance appropriate for the given point and
403+
* dimensions
404+
* @since 3.133
405+
*/
406+
public static Rectangle of(Point topLeft, Point dimension) {
407+
final float x = (topLeft instanceof Point.OfFloat p) ? p.getX() : topLeft.x;
408+
final float y = (topLeft instanceof Point.OfFloat p) ? p.getY() : topLeft.y;
409+
final float w = (dimension instanceof Point.OfFloat p) ? p.getX() : dimension.x;
410+
final float h = (dimension instanceof Point.OfFloat p) ? p.getY() : dimension.y;
411+
412+
if (topLeft instanceof Point.WithMonitor pm) {
413+
return new Rectangle.WithMonitor(x, y, w, h, pm.getMonitor());
414+
}
415+
416+
if (topLeft instanceof Point.OfFloat || dimension instanceof Point.OfFloat) {
417+
return new Rectangle.OfFloat(x, y, w, h);
418+
}
419+
420+
return new Rectangle((int) x, (int) y, (int) w, (int) h);
421+
}
422+
386423
/**
387424
* Creates and returns a copy of this {@code Rectangle}.
388425
* <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)