Skip to content

Commit 172580b

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 0368a6b commit 172580b

File tree

1 file changed

+44
-57
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets

1 file changed

+44
-57
lines changed

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

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -274,26 +274,6 @@ void closeWidget () {
274274
if (event.doit && !isDisposed ()) dispose ();
275275
}
276276

277-
int compare (ImageData data1, ImageData data2, int width, int height, int depth) {
278-
int value1 = Math.abs (data1.width - width), value2 = Math.abs (data2.width - width);
279-
if (value1 == value2) {
280-
int transparent1 = data1.getTransparencyType ();
281-
int transparent2 = data2.getTransparencyType ();
282-
if (transparent1 == transparent2) {
283-
if (data1.depth == data2.depth) return 0;
284-
return data1.depth > data2.depth && data1.depth <= depth ? -1 : 1;
285-
}
286-
if (transparent1 == SWT.TRANSPARENCY_ALPHA) return -1;
287-
if (transparent2 == SWT.TRANSPARENCY_ALPHA) return 1;
288-
if (transparent1 == SWT.TRANSPARENCY_MASK) return -1;
289-
if (transparent2 == SWT.TRANSPARENCY_MASK) return 1;
290-
if (transparent1 == SWT.TRANSPARENCY_PIXEL) return -1;
291-
if (transparent2 == SWT.TRANSPARENCY_PIXEL) return 1;
292-
return 0;
293-
}
294-
return value1 < value2 ? -1 : 1;
295-
}
296-
297277
@Override
298278
Widget computeTabGroup () {
299279
return this;
@@ -882,7 +862,7 @@ public void setImage (Image image) {
882862
setImages (image, null);
883863
}
884864

885-
void setImages (Image image, Image [] images) {
865+
private void setImages (Image image, Image [] images) {
886866
if (smallImage != null) smallImage.dispose ();
887867
if (largeImage != null) largeImage.dispose ();
888868
smallImage = largeImage = null;
@@ -893,22 +873,9 @@ void setImages (Image image, Image [] images) {
893873
} else {
894874
if (images != null && images.length > 0) {
895875
int depth = display.getIconDepth ();
896-
ImageData [] datas = null;
897-
if (images.length > 1) {
898-
Image [] bestImages = new Image [images.length];
899-
System.arraycopy (images, 0, bestImages, 0, images.length);
900-
datas = new ImageData [images.length];
901-
for (int i=0; i<datas.length; i++) {
902-
datas [i] = images [i].getImageData ();
903-
}
904-
images = bestImages;
905-
sort (images, datas, getSystemMetrics (OS.SM_CXSMICON), getSystemMetrics (OS.SM_CYSMICON), depth);
906-
}
907-
smallIcon = images [0];
908-
if (images.length > 1) {
909-
sort (images, datas, getSystemMetrics (OS.SM_CXICON), getSystemMetrics (OS.SM_CYICON), depth);
910-
}
911-
largeIcon = images [0];
876+
877+
smallIcon = findClosest(images, getSystemMetrics (OS.SM_CXSMICON), getSystemMetrics (OS.SM_CYSMICON), depth);
878+
largeIcon = findClosest(images, getSystemMetrics (OS.SM_CXICON), getSystemMetrics (OS.SM_CYICON), depth);
912879
}
913880
}
914881
if (smallIcon != null) {
@@ -949,6 +916,46 @@ void setImages (Image image, Image [] images) {
949916
}
950917
}
951918

919+
private Image findClosest(Image[] images, int width, int height, int depth) {
920+
Image closest = images[0];
921+
ImageData closestData = images[0].getImageData();
922+
for (int i=1; i<images.length;i++) {
923+
ImageData data = images[i].getImageData();
924+
if (isCloserThan(data,closestData, width, height, depth)) {
925+
closest = images[i];
926+
closestData = data;
927+
}
928+
}
929+
930+
return closest;
931+
}
932+
933+
/**
934+
* @return <code>true</code> if <code>data1</code> is closer to the desired width, height and depth than <code>data2</code>.
935+
*/
936+
private boolean isCloserThan (ImageData data1, ImageData data2, int width, int height, int depth) {
937+
int diffWidth1 = Math.abs (data1.width - width);
938+
int diffWidth2 = Math.abs (data2.width - width);
939+
940+
if (diffWidth1 == diffWidth2) {
941+
int transparent1 = data1.getTransparencyType ();
942+
int transparent2 = data2.getTransparencyType ();
943+
if (transparent1 == transparent2) {
944+
if (data1.depth == data2.depth) return false;
945+
// TODO: analyze this, it seems broken.
946+
return data1.depth > data2.depth && data1.depth <= depth;
947+
}
948+
if (transparent1 == SWT.TRANSPARENCY_ALPHA) return true;
949+
if (transparent2 == SWT.TRANSPARENCY_ALPHA) return false;
950+
if (transparent1 == SWT.TRANSPARENCY_MASK) return true;
951+
if (transparent2 == SWT.TRANSPARENCY_MASK) return false;
952+
if (transparent1 == SWT.TRANSPARENCY_PIXEL) return true;
953+
if (transparent2 == SWT.TRANSPARENCY_PIXEL) return false;
954+
return false;
955+
}
956+
return diffWidth1 < diffWidth2;
957+
}
958+
952959
/**
953960
* Sets the receiver's images to the argument, which may
954961
* be an empty array. Images are typically displayed by the
@@ -1313,26 +1320,6 @@ public void setVisible (boolean visible) {
13131320
}
13141321
}
13151322

1316-
void sort (Image [] images, ImageData [] datas, int width, int height, int depth) {
1317-
/* Shell Sort from K&R, pg 108 */
1318-
int length = images.length;
1319-
if (length <= 1) return;
1320-
for (int gap=length/2; gap>0; gap/=2) {
1321-
for (int i=gap; i<length; i++) {
1322-
for (int j=i-gap; j>=0; j-=gap) {
1323-
if (compare (datas [j], datas [j + gap], width, height, depth) >= 0) {
1324-
Image swap = images [j];
1325-
images [j] = images [j + gap];
1326-
images [j + gap] = swap;
1327-
ImageData swapData = datas [j];
1328-
datas [j] = datas [j + gap];
1329-
datas [j + gap] = swapData;
1330-
}
1331-
}
1332-
}
1333-
}
1334-
}
1335-
13361323
@Override
13371324
boolean translateAccelerator (MSG msg) {
13381325
if (!isEnabled () || !isActive ()) return false;

0 commit comments

Comments
 (0)