Skip to content

Commit dcb6310

Browse files
committed
Better API for handling MonitorAware Coordinates
This commit contributes to providing better interfaces and APIs for handling MontiorAware Rectangles and Points with Abstraction. contributes to #62 and #128
1 parent 925a294 commit dcb6310

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.eclipse.swt.graphics;
2+
3+
/**
4+
* @since 3.130
5+
*/
6+
public interface Copyable<T> {
7+
public T copy();
8+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ public int hashCode() {
6161
return super.hashCode();
6262
}
6363

64+
@Override
65+
public Rectangle copy() {
66+
return new MonitorAwareRectangle(x, y, width, height, monitor);
67+
}
68+
69+
@Override
70+
public Rectangle copyWith(int dx, int dy, int dWidth, int dHeight) {
71+
return new MonitorAwareRectangle(x + dx, y + dy, width + dWidth, height + dHeight, monitor);
72+
}
73+
6474
}

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

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

48-
public sealed class Rectangle implements Serializable permits MonitorAwareRectangle {
48+
public sealed class Rectangle implements Serializable, Copyable<Rectangle> permits MonitorAwareRectangle {
4949

5050
/**
5151
* the x coordinate of the rectangle
@@ -356,4 +356,29 @@ public Rectangle union (Rectangle rect) {
356356
return new Rectangle (left, top, right - left, bottom - top);
357357
}
358358

359+
/**
360+
* @since 3.130
361+
*/
362+
public static Rectangle of(Point topLeft, int width, int height) {
363+
if(topLeft instanceof MonitorAwarePoint monitorAwareTopLeft) {
364+
return new MonitorAwareRectangle(topLeft.x, topLeft.y, width, height, monitorAwareTopLeft.getMonitor());
365+
}
366+
return new Rectangle(topLeft.x, topLeft.y, width, height);
367+
}
368+
369+
/**
370+
* @since 3.130
371+
*/
372+
@Override
373+
public Rectangle copy() {
374+
return new Rectangle(x, y, width, height);
375+
}
376+
377+
/**
378+
* @since 3.130
379+
*/
380+
public Rectangle copyWith(int dx, int dy, int dWidth, int dHeight) {
381+
return new Rectangle(x + dx, y + dy, width + dWidth, height + dHeight);
382+
}
383+
359384
}

0 commit comments

Comments
 (0)