From 68ef8c811a81ffe0832efda424a76b61025e163b Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Sat, 11 Apr 2026 13:10:31 +0200 Subject: [PATCH] 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 --- .../org/eclipse/swt/graphics/Region.java | 80 ++++++------------- 1 file changed, 25 insertions(+), 55 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java index 8aa648f378..48de1c4615 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java @@ -288,9 +288,8 @@ public boolean equals (Object object) { */ public Rectangle getBounds () { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - return applyUsingAnyHandle(regionHandle -> { - return Win32DPIUtils.pixelToPoint(getBoundsInPixels(regionHandle.handle()), regionHandle.zoom()); - }); + return applyUsingAnyHandle(regionHandle -> + Win32DPIUtils.pixelToPoint(getBoundsInPixels(regionHandle.handle()), regionHandle.zoom())); } private Rectangle getBoundsInPixels(long handle) { @@ -470,8 +469,8 @@ public boolean isDisposed() { */ public boolean isEmpty () { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); - RECT rect = new RECT (); return applyUsingAnyHandle(regionHandle -> { + RECT rect = new RECT(); int result = OS.GetRgnBox(regionHandle.handle(), rect); if (result == OS.NULLREGION) return true; return ((rect.right - rect.left) <= 0) || ((rect.bottom - rect.top) <= 0); @@ -664,11 +663,7 @@ private static RegionHandle newRegionHandle(int zoom, List operations } private RegionHandle getRegionHandle(int zoom) { - if (!zoomToHandle.containsKey(zoom)) { - RegionHandle regionHandle = newRegionHandle(zoom, operations); - zoomToHandle.put(zoom, regionHandle); - } - return zoomToHandle.get(zoom); + return zoomToHandle.computeIfAbsent(zoom, z -> newRegionHandle(z, operations)); } Region copy() { @@ -708,7 +703,7 @@ public static long win32_getHandle(Region region, int zoom) { @Override public String toString () { if (isDisposed()) return "Region {*DISPOSED*}"; - return "Region {" + zoomToHandle.entrySet().stream().map(entry -> entry.getValue() + "(zoom:" + entry.getKey() + ")").collect(Collectors.joining(",")); + return "Region {" + zoomToHandle.entrySet().stream().map(entry -> entry.getValue() + "(zoom:" + entry.getKey() + ")").collect(Collectors.joining(",")) + "}"; } private record RegionHandle(long handle, int zoom) { @@ -760,19 +755,19 @@ void set(long handle, int zoom) { @Override void add(long handle, int zoom) { Rectangle bounds = getScaledRectangle(zoom); - addInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height); + combineWithRectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height, OS.RGN_OR); } @Override void subtract(long handle, int zoom) { Rectangle bounds = getScaledRectangle(zoom); - subtractInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height); + combineWithRectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height, OS.RGN_DIFF); } @Override void intersect(long handle, int zoom) { Rectangle bounds = getScaledRectangle(zoom); - intersectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height); + combineWithRectInPixels(handle, bounds.x, bounds.y, bounds.width, bounds.height, OS.RGN_AND); } @Override @@ -780,25 +775,11 @@ void translate(long handle, int zoom) { throw new UnsupportedOperationException(); } - private void addInPixels (long handle, int x, int y, int width, int height) { - if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - long rectRgn = OS.CreateRectRgn (x, y, x + width, y + height); - OS.CombineRgn (handle, handle, rectRgn, OS.RGN_OR); - OS.DeleteObject (rectRgn); - } - - private void subtractInPixels (long handle, int x, int y, int width, int height) { + private static void combineWithRectInPixels(long handle, int x, int y, int width, int height, int mode) { if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - long rectRgn = OS.CreateRectRgn (x, y, x + width, y + height); - OS.CombineRgn (handle, handle, rectRgn, OS.RGN_DIFF); - OS.DeleteObject (rectRgn); - } - - private void intersectInPixels (long handle, int x, int y, int width, int height) { - if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - long rectRgn = OS.CreateRectRgn (x, y, x + width, y + height); - OS.CombineRgn (handle, handle, rectRgn, OS.RGN_AND); - OS.DeleteObject (rectRgn); + long rectRgn = OS.CreateRectRgn(x, y, x + width, y + height); + OS.CombineRgn(handle, handle, rectRgn, mode); + OS.DeleteObject(rectRgn); } private Rectangle getScaledRectangle(int zoom) { @@ -810,7 +791,7 @@ private Rectangle getScaledRectangle(int zoom) { private static class OperationWithArray extends Operation { private final int[] data; - public OperationWithArray(OperationStrategy operationStrategy, int[] data) { + OperationWithArray(OperationStrategy operationStrategy, int[] data) { super(operationStrategy); this.data = data; } @@ -822,14 +803,12 @@ void set(long handle, int zoom) { @Override void add(long handle, int zoom) { - int[] points = getScaledPoints(zoom); - addInPixels(handle, points); + combineWithPolyInPixels(handle, getScaledPoints(zoom), OS.RGN_OR); } @Override void subtract(long handle, int zoom) { - int[] pointArray = getScaledPoints(zoom); - subtractInPixels(handle, pointArray); + combineWithPolyInPixels(handle, getScaledPoints(zoom), OS.RGN_DIFF); } @Override @@ -842,16 +821,10 @@ void translate(long handle, int zoom) { throw new UnsupportedOperationException(); } - private void addInPixels (long handle, int[] pointArray) { - long polyRgn = OS.CreatePolygonRgn(pointArray, pointArray.length / 2, OS.ALTERNATE); - OS.CombineRgn (handle, handle, polyRgn, OS.RGN_OR); - OS.DeleteObject (polyRgn); - } - - private void subtractInPixels (long handle, int[] pointArray) { + private static void combineWithPolyInPixels(long handle, int[] pointArray, int mode) { long polyRgn = OS.CreatePolygonRgn(pointArray, pointArray.length / 2, OS.ALTERNATE); - OS.CombineRgn (handle, handle, polyRgn, OS.RGN_DIFF); - OS.DeleteObject (polyRgn); + OS.CombineRgn(handle, handle, polyRgn, mode); + OS.DeleteObject(polyRgn); } private int[] getScaledPoints(int zoom) { @@ -862,7 +835,7 @@ private int[] getScaledPoints(int zoom) { private static class OperationWithPoint extends Operation { private final Point data; - public OperationWithPoint(OperationStrategy operationStrategy, Point data) { + OperationWithPoint(OperationStrategy operationStrategy, Point data) { super(operationStrategy); this.data = data; } @@ -910,23 +883,20 @@ void set(long handle, int zoom) { @Override void add(long handle, int zoom) { - applyUsingTemporaryHandle(zoom, operations, regionHandle -> { - return OS.CombineRgn (handle, handle, regionHandle.handle(), OS.RGN_OR); - }); + applyUsingTemporaryHandle(zoom, operations, regionHandle -> + OS.CombineRgn(handle, handle, regionHandle.handle(), OS.RGN_OR)); } @Override void subtract(long handle, int zoom) { - applyUsingTemporaryHandle(zoom, operations, regionHandle -> { - return OS.CombineRgn (handle, handle, regionHandle.handle(), OS.RGN_DIFF); - }); + applyUsingTemporaryHandle(zoom, operations, regionHandle -> + OS.CombineRgn(handle, handle, regionHandle.handle(), OS.RGN_DIFF)); } @Override void intersect(long handle, int zoom) { - applyUsingTemporaryHandle(zoom, operations, regionHandle -> { - return OS.CombineRgn (handle, handle, regionHandle.handle(), OS.RGN_AND); - }); + applyUsingTemporaryHandle(zoom, operations, regionHandle -> + OS.CombineRgn(handle, handle, regionHandle.handle(), OS.RGN_AND)); } @Override