Skip to content

Commit 68ef8c8

Browse files
committed
Simplify and fix win32 Region.java
- Fix toString() missing closing brace (output was malformed) - Use computeIfAbsent() in getRegionHandle() instead of manual containsKey/put/get - Move RECT allocation inside isEmpty() lambda where it belongs - Replace getBounds() block lambda with expression lambda - Eliminate duplicate create/combine/delete patterns in OperationWithRectangle (3 methods -> combineWithRectInPixels) and OperationWithArray (2 methods -> combineWithPolyInPixels) - Convert OperationWithRegion block lambdas to expression lambdas - Remove redundant public modifiers on constructors of private inner classes OperationWithArray and OperationWithPoint
1 parent 188aed6 commit 68ef8c8

File tree

1 file changed

+25
-55
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics

1 file changed

+25
-55
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,8 @@ public boolean equals (Object object) {
288288
*/
289289
public Rectangle getBounds () {
290290
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
291-
return applyUsingAnyHandle(regionHandle -> {
292-
return Win32DPIUtils.pixelToPoint(getBoundsInPixels(regionHandle.handle()), regionHandle.zoom());
293-
});
291+
return applyUsingAnyHandle(regionHandle ->
292+
Win32DPIUtils.pixelToPoint(getBoundsInPixels(regionHandle.handle()), regionHandle.zoom()));
294293
}
295294

296295
private Rectangle getBoundsInPixels(long handle) {
@@ -470,8 +469,8 @@ public boolean isDisposed() {
470469
*/
471470
public boolean isEmpty () {
472471
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
473-
RECT rect = new RECT ();
474472
return applyUsingAnyHandle(regionHandle -> {
473+
RECT rect = new RECT();
475474
int result = OS.GetRgnBox(regionHandle.handle(), rect);
476475
if (result == OS.NULLREGION) return true;
477476
return ((rect.right - rect.left) <= 0) || ((rect.bottom - rect.top) <= 0);
@@ -664,11 +663,7 @@ private static RegionHandle newRegionHandle(int zoom, List<Operation> operations
664663
}
665664

666665
private RegionHandle getRegionHandle(int zoom) {
667-
if (!zoomToHandle.containsKey(zoom)) {
668-
RegionHandle regionHandle = newRegionHandle(zoom, operations);
669-
zoomToHandle.put(zoom, regionHandle);
670-
}
671-
return zoomToHandle.get(zoom);
666+
return zoomToHandle.computeIfAbsent(zoom, z -> newRegionHandle(z, operations));
672667
}
673668

674669
Region copy() {
@@ -708,7 +703,7 @@ public static long win32_getHandle(Region region, int zoom) {
708703
@Override
709704
public String toString () {
710705
if (isDisposed()) return "Region {*DISPOSED*}";
711-
return "Region {" + zoomToHandle.entrySet().stream().map(entry -> entry.getValue() + "(zoom:" + entry.getKey() + ")").collect(Collectors.joining(","));
706+
return "Region {" + zoomToHandle.entrySet().stream().map(entry -> entry.getValue() + "(zoom:" + entry.getKey() + ")").collect(Collectors.joining(",")) + "}";
712707
}
713708

714709
private record RegionHandle(long handle, int zoom) {
@@ -760,45 +755,31 @@ void set(long handle, int zoom) {
760755
@Override
761756
void add(long handle, int zoom) {
762757
Rectangle bounds = getScaledRectangle(zoom);
763-
addInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height);
758+
combineWithRectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height, OS.RGN_OR);
764759
}
765760

766761
@Override
767762
void subtract(long handle, int zoom) {
768763
Rectangle bounds = getScaledRectangle(zoom);
769-
subtractInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height);
764+
combineWithRectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height, OS.RGN_DIFF);
770765
}
771766

772767
@Override
773768
void intersect(long handle, int zoom) {
774769
Rectangle bounds = getScaledRectangle(zoom);
775-
intersectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height);
770+
combineWithRectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height, OS.RGN_AND);
776771
}
777772

778773
@Override
779774
void translate(long handle, int zoom) {
780775
throw new UnsupportedOperationException();
781776
}
782777

783-
private void addInPixels (long handle, int x, int y, int width, int height) {
784-
if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
785-
long rectRgn = OS.CreateRectRgn (x, y, x + width, y + height);
786-
OS.CombineRgn (handle, handle, rectRgn, OS.RGN_OR);
787-
OS.DeleteObject (rectRgn);
788-
}
789-
790-
private void subtractInPixels (long handle, int x, int y, int width, int height) {
778+
private static void combineWithRectInPixels(long handle, int x, int y, int width, int height, int mode) {
791779
if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
792-
long rectRgn = OS.CreateRectRgn (x, y, x + width, y + height);
793-
OS.CombineRgn (handle, handle, rectRgn, OS.RGN_DIFF);
794-
OS.DeleteObject (rectRgn);
795-
}
796-
797-
private void intersectInPixels (long handle, int x, int y, int width, int height) {
798-
if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
799-
long rectRgn = OS.CreateRectRgn (x, y, x + width, y + height);
800-
OS.CombineRgn (handle, handle, rectRgn, OS.RGN_AND);
801-
OS.DeleteObject (rectRgn);
780+
long rectRgn = OS.CreateRectRgn(x, y, x + width, y + height);
781+
OS.CombineRgn(handle, handle, rectRgn, mode);
782+
OS.DeleteObject(rectRgn);
802783
}
803784

804785
private Rectangle getScaledRectangle(int zoom) {
@@ -810,7 +791,7 @@ private Rectangle getScaledRectangle(int zoom) {
810791
private static class OperationWithArray extends Operation {
811792
private final int[] data;
812793

813-
public OperationWithArray(OperationStrategy operationStrategy, int[] data) {
794+
OperationWithArray(OperationStrategy operationStrategy, int[] data) {
814795
super(operationStrategy);
815796
this.data = data;
816797
}
@@ -822,14 +803,12 @@ void set(long handle, int zoom) {
822803

823804
@Override
824805
void add(long handle, int zoom) {
825-
int[] points = getScaledPoints(zoom);
826-
addInPixels(handle, points);
806+
combineWithPolyInPixels(handle, getScaledPoints(zoom), OS.RGN_OR);
827807
}
828808

829809
@Override
830810
void subtract(long handle, int zoom) {
831-
int[] pointArray = getScaledPoints(zoom);
832-
subtractInPixels(handle, pointArray);
811+
combineWithPolyInPixels(handle, getScaledPoints(zoom), OS.RGN_DIFF);
833812
}
834813

835814
@Override
@@ -842,16 +821,10 @@ void translate(long handle, int zoom) {
842821
throw new UnsupportedOperationException();
843822
}
844823

845-
private void addInPixels (long handle, int[] pointArray) {
846-
long polyRgn = OS.CreatePolygonRgn(pointArray, pointArray.length / 2, OS.ALTERNATE);
847-
OS.CombineRgn (handle, handle, polyRgn, OS.RGN_OR);
848-
OS.DeleteObject (polyRgn);
849-
}
850-
851-
private void subtractInPixels (long handle, int[] pointArray) {
824+
private static void combineWithPolyInPixels(long handle, int[] pointArray, int mode) {
852825
long polyRgn = OS.CreatePolygonRgn(pointArray, pointArray.length / 2, OS.ALTERNATE);
853-
OS.CombineRgn (handle, handle, polyRgn, OS.RGN_DIFF);
854-
OS.DeleteObject (polyRgn);
826+
OS.CombineRgn(handle, handle, polyRgn, mode);
827+
OS.DeleteObject(polyRgn);
855828
}
856829

857830
private int[] getScaledPoints(int zoom) {
@@ -862,7 +835,7 @@ private int[] getScaledPoints(int zoom) {
862835
private static class OperationWithPoint extends Operation {
863836
private final Point data;
864837

865-
public OperationWithPoint(OperationStrategy operationStrategy, Point data) {
838+
OperationWithPoint(OperationStrategy operationStrategy, Point data) {
866839
super(operationStrategy);
867840
this.data = data;
868841
}
@@ -910,23 +883,20 @@ void set(long handle, int zoom) {
910883

911884
@Override
912885
void add(long handle, int zoom) {
913-
applyUsingTemporaryHandle(zoom, operations, regionHandle -> {
914-
return OS.CombineRgn (handle, handle, regionHandle.handle(), OS.RGN_OR);
915-
});
886+
applyUsingTemporaryHandle(zoom, operations, regionHandle ->
887+
OS.CombineRgn(handle, handle, regionHandle.handle(), OS.RGN_OR));
916888
}
917889

918890
@Override
919891
void subtract(long handle, int zoom) {
920-
applyUsingTemporaryHandle(zoom, operations, regionHandle -> {
921-
return OS.CombineRgn (handle, handle, regionHandle.handle(), OS.RGN_DIFF);
922-
});
892+
applyUsingTemporaryHandle(zoom, operations, regionHandle ->
893+
OS.CombineRgn(handle, handle, regionHandle.handle(), OS.RGN_DIFF));
923894
}
924895

925896
@Override
926897
void intersect(long handle, int zoom) {
927-
applyUsingTemporaryHandle(zoom, operations, regionHandle -> {
928-
return OS.CombineRgn (handle, handle, regionHandle.handle(), OS.RGN_AND);
929-
});
898+
applyUsingTemporaryHandle(zoom, operations, regionHandle ->
899+
OS.CombineRgn(handle, handle, regionHandle.handle(), OS.RGN_AND));
930900
}
931901

932902
@Override

0 commit comments

Comments
 (0)