15
15
16
16
17
17
import java .util .*;
18
+ import java .util .function .*;
18
19
import java .util .stream .*;
19
20
20
21
import org .eclipse .swt .*;
@@ -80,9 +81,8 @@ public Region () {
80
81
public Region (Device device ) {
81
82
super (device );
82
83
initialZoom = DPIUtil .getDeviceZoom ();
83
- long handle = OS . CreateRectRgn ( 0 , 0 , 0 , 0 );
84
+ long handle = newEmptyRegionHandle ( );
84
85
zoomToHandle .put (initialZoom , handle );
85
- if (handle == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
86
86
init ();
87
87
this .device .registerResourceWithZoomSupport (this );
88
88
}
@@ -597,6 +597,29 @@ private void storeAndApplyOperationForAllHandles(Operation operation) {
597
597
zoomToHandle .forEach ((zoom , handle ) -> operation .apply (handle , zoom ));
598
598
}
599
599
600
+ private static <T > T applyUsingTemporaryHandle (int zoom , List <Operation > operations , Function <Long , T > function ) {
601
+ long temporaryHandle = newRegionHandle (operations , zoom );
602
+ try {
603
+ return function .apply (temporaryHandle );
604
+ } finally {
605
+ OS .DeleteObject (temporaryHandle );
606
+ }
607
+ }
608
+
609
+ private static long newEmptyRegionHandle () {
610
+ long newHandle = OS .CreateRectRgn (0 , 0 , 0 , 0 );
611
+ if (newHandle == 0 ) SWT .error (SWT .ERROR_NO_HANDLES );
612
+ return newHandle ;
613
+ }
614
+
615
+ private static long newRegionHandle (List <Operation > operations , int zoom ) {
616
+ long newHandle = newEmptyRegionHandle ();
617
+ for (Operation operation : operations ) {
618
+ operation .apply (newHandle , zoom );
619
+ }
620
+ return newHandle ;
621
+ }
622
+
600
623
/**
601
624
* <b>IMPORTANT:</b> This method is not part of the public
602
625
* API for Image. It is marked public only so that it
@@ -615,9 +638,9 @@ private void storeAndApplyOperationForAllHandles(Operation operation) {
615
638
* @noreference This method is not intended to be referenced by clients.
616
639
*/
617
640
public static long win32_getHandle (Region region , int zoom ) {
618
- if (!region .zoomToHandle .containsKey (zoom )) {
619
- long handle = OS . CreateRectRgn ( 0 , 0 , 0 , 0 );
620
- for (Operation operation : region .operations ) {
641
+ if (!region .zoomToHandle .containsKey (zoom )) {
642
+ long handle = newEmptyRegionHandle ( );
643
+ for (Operation operation : region .operations ) {
621
644
operation .apply (handle , zoom );
622
645
}
623
646
region .zoomToHandle .put (zoom , handle );
@@ -642,7 +665,7 @@ private interface OperationStrategy {
642
665
void apply (Operation operation , long handle , int zoom );
643
666
}
644
667
645
- private abstract class Operation {
668
+ private abstract static class Operation {
646
669
private OperationStrategy operationStrategy ;
647
670
648
671
Operation (OperationStrategy operationStrategy ) {
@@ -662,7 +685,7 @@ void apply(long handle, int zoom) {
662
685
abstract void translate (long handle , int zoom );
663
686
}
664
687
665
- private class OperationWithRectangle extends Operation {
688
+ private static class OperationWithRectangle extends Operation {
666
689
667
690
Rectangle data ;
668
691
@@ -721,7 +744,7 @@ private Rectangle getScaledRectangle(int zoom) {
721
744
722
745
}
723
746
724
- private class OperationWithArray extends Operation {
747
+ private static class OperationWithArray extends Operation {
725
748
726
749
int [] data ;
727
750
@@ -769,7 +792,7 @@ private int[] getScaledPoints(int zoom) {
769
792
}
770
793
}
771
794
772
- private class OperationWithPoint extends Operation {
795
+ private static class OperationWithPoint extends Operation {
773
796
774
797
Point data ;
775
798
@@ -801,41 +824,38 @@ void translate(long handle, int zoom) {
801
824
802
825
}
803
826
804
- private class OperationWithRegion extends Operation {
805
-
806
- Region data ;
827
+ private static class OperationWithRegion extends Operation {
828
+ private final List <Operation > operations ;
807
829
808
830
OperationWithRegion (OperationStrategy operationStrategy , Region data ) {
809
831
super (operationStrategy );
810
- this .data = data ;
832
+ this .operations = data . operations ;
811
833
}
812
834
813
835
@ Override
814
836
void add (long handle , int zoom ) {
815
- long scaledHandle = getHandleForScaledRegion (zoom );
816
- OS .CombineRgn (handle , handle , scaledHandle , OS .RGN_OR );
837
+ applyUsingTemporaryHandle (zoom , operations , regionHandle -> {
838
+ return OS .CombineRgn (handle , handle , regionHandle , OS .RGN_OR );
839
+ });
817
840
}
818
841
819
842
@ Override
820
843
void subtract (long handle , int zoom ) {
821
- long scaledHandle = getHandleForScaledRegion (zoom );
822
- OS .CombineRgn (handle , handle , scaledHandle , OS .RGN_DIFF );
844
+ applyUsingTemporaryHandle (zoom , operations , regionHandle -> {
845
+ return OS .CombineRgn (handle , handle , regionHandle , OS .RGN_DIFF );
846
+ });
823
847
}
824
848
825
849
@ Override
826
850
void intersect (long handle , int zoom ) {
827
- long scaledHandle = getHandleForScaledRegion (zoom );
828
- OS .CombineRgn (handle , handle , scaledHandle , OS .RGN_AND );
851
+ applyUsingTemporaryHandle (zoom , operations , regionHandle -> {
852
+ return OS .CombineRgn (handle , handle , regionHandle , OS .RGN_AND );
853
+ });
829
854
}
830
855
831
856
@ Override
832
857
void translate (long handle , int zoom ) {
833
858
throw new UnsupportedOperationException ();
834
859
}
835
-
836
- private long getHandleForScaledRegion (int zoom ) {
837
- if (data .isDisposed () || data == Region .this ) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
838
- return win32_getHandle (data , zoom );
839
- }
840
860
}
841
861
}
0 commit comments