Skip to content

Commit 99093d5

Browse files
committed
Rewrite the algorithm that sets smallIcon/largeIcon in Decorations class
Don't sort, compare and keep the image that is closest to the desired dimensions, pixel transparency and pixel depth. The new method "isCloserThan" is based on the (now deleted) "compare" method and returns true only if "compare" would have returned -1 Reduce visibility of Decorations::setImages
1 parent 74f094a commit 99093d5

File tree

1 file changed

+23
-15
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets

1 file changed

+23
-15
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Decorations.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@ private static ImageData[] getImageDataAt100(Image[] images) {
934934
private static int findIndexOfClosest(ImageData[] imageData, int targetWidth, int targetDepth) {
935935
int closestIndex = 0;
936936
ImageData closestData = imageData[0];
937-
for (int i=1; i<imageData.length;i++) {
938-
if (isCloserThan(imageData[i],closestData, targetWidth, targetDepth)) {
937+
for (int i = 1; i < imageData.length; i++) {
938+
if (isCloserThan(imageData[i], closestData, targetWidth, targetDepth)) {
939939
closestIndex = i;
940940
closestData = imageData[i];
941941
}
@@ -944,32 +944,40 @@ private static int findIndexOfClosest(ImageData[] imageData, int targetWidth, in
944944
return closestIndex;
945945
}
946946

947-
private static boolean isCloserThan (ImageData dataToTest, ImageData referenceData, int targetWidth, int targetDepth) {
948-
int diffWidthToTest = Math.abs (dataToTest.width - targetWidth);
949-
int diffReferenceWidth = Math.abs (referenceData.width - targetWidth);
947+
private static boolean isCloserThan(ImageData dataToTest, ImageData referenceData, int targetWidth, int targetDepth) {
948+
int diffWidthToTest = Math.abs(dataToTest.width - targetWidth);
949+
int diffReferenceWidth = Math.abs(referenceData.width - targetWidth);
950950

951951
// The closer the width the better
952-
if (diffWidthToTest != diffReferenceWidth) return diffWidthToTest < diffReferenceWidth;
952+
if (diffWidthToTest != diffReferenceWidth)
953+
return diffWidthToTest < diffReferenceWidth;
953954

954-
int transparencyToTest = dataToTest.getTransparencyType ();
955-
int referenceTransparency = referenceData.getTransparencyType ();
955+
int transparencyToTest = dataToTest.getTransparencyType();
956+
int referenceTransparency = referenceData.getTransparencyType();
956957

957958
// If they have the same transparency then the bigger the pixel depth (without
958959
// surpassing the target depth) the better
959960
if (transparencyToTest == referenceTransparency) {
960-
if (dataToTest.depth == referenceData.depth) return false;
961+
if (dataToTest.depth == referenceData.depth)
962+
return false;
961963

962964
return dataToTest.depth > referenceData.depth && dataToTest.depth <= targetDepth;
963965
}
964966

965967
// If they have different transparency, favor (in this order): the one with
966968
// an alpha channel, the one with a mask, the one with a transparency pixel
967-
if (transparencyToTest == SWT.TRANSPARENCY_ALPHA) return true;
968-
if (referenceTransparency == SWT.TRANSPARENCY_ALPHA) return false;
969-
if (transparencyToTest == SWT.TRANSPARENCY_MASK) return true;
970-
if (referenceTransparency == SWT.TRANSPARENCY_MASK) return false;
971-
if (transparencyToTest == SWT.TRANSPARENCY_PIXEL) return true;
972-
if (referenceTransparency == SWT.TRANSPARENCY_PIXEL) return false;
969+
if (transparencyToTest == SWT.TRANSPARENCY_ALPHA)
970+
return true;
971+
if (referenceTransparency == SWT.TRANSPARENCY_ALPHA)
972+
return false;
973+
if (transparencyToTest == SWT.TRANSPARENCY_MASK)
974+
return true;
975+
if (referenceTransparency == SWT.TRANSPARENCY_MASK)
976+
return false;
977+
if (transparencyToTest == SWT.TRANSPARENCY_PIXEL)
978+
return true;
979+
if (referenceTransparency == SWT.TRANSPARENCY_PIXEL)
980+
return false;
973981
return false;
974982
}
975983

0 commit comments

Comments
 (0)