Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c4be5d7

Browse files
committedMay 27, 2025·
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 c4be5d7

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.eclipse.swt.graphics;
2+
3+
import org.eclipse.swt.widgets.*;
4+
5+
/**
6+
* @since 3.130
7+
*/
8+
public interface MonitorAware {
9+
Monitor getMonitor();
10+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @since 3.129
2525
* @noreference This class is not intended to be referenced by clients
2626
*/
27-
public final class MonitorAwarePoint extends Point {
27+
public final class MonitorAwarePoint extends Point implements MonitorAware {
2828

2929
private static final long serialVersionUID = 6077427420686999194L;
3030

@@ -45,6 +45,7 @@ public MonitorAwarePoint(int x, int y, Monitor monitor) {
4545
/**
4646
* {@return the monitor with whose context the instance is created}
4747
*/
48+
@Override
4849
public Monitor getMonitor() {
4950
return monitor;
5051
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @since 3.129
2525
* @noreference This class is not intended to be referenced by clients
2626
*/
27-
public final class MonitorAwareRectangle extends Rectangle {
27+
public final class MonitorAwareRectangle extends Rectangle implements MonitorAware {
2828

2929
private static final long serialVersionUID = 5041911840525116925L;
3030

@@ -47,6 +47,7 @@ public MonitorAwareRectangle(int x, int y, int width, int height, Monitor monito
4747
/**
4848
* {@return the monitor with whose context the instance is created}
4949
*/
50+
@Override
5051
public Monitor getMonitor() {
5152
return monitor;
5253
}
@@ -61,4 +62,14 @@ public int hashCode() {
6162
return super.hashCode();
6263
}
6364

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

‎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 MonitorAware 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)
Please sign in to comment.