Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -664,11 +663,7 @@ private static RegionHandle newRegionHandle(int zoom, List<Operation> 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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -760,45 +755,31 @@ 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
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) {
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
Loading