Skip to content

Commit 9ee0370

Browse files
amartya4256HeikoKlare
authored andcommitted
Fixed Program Icon Scaling for Multi Zoom
This contribution reimplements the logic for scaling the Program Icons while getting rid of the DPIUtil::getDeviceZoom usage and enhancing the image quality of icons on scaling up. contributes to #62 and #127
1 parent 0fa596c commit 9ee0370

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/ImageTransfer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public Object nativeToJava(TransferData transferData) {
184184
pSourceBits -= scanline;
185185
}
186186
}
187-
Image image = Image.win32_new(null, SWT.BITMAP, memDib);
187+
Image image = Image.win32_new(null, SWT.BITMAP, memDib, DPIUtil.getNativeDeviceZoom());
188188
ImageData data = image.getImageData (DPIUtil.getDeviceZoom ());
189189
OS.DeleteObject(memDib);
190190
image.dispose();

bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@ public class OS extends C {
12101210
public static final int SHADEBLENDCAPS = 120;
12111211
public static final int SHGFI_ICON = 0x000000100;
12121212
public static final int SHGFI_SMALLICON= 0x1;
1213+
public static final int SHGFI_LARGEICON= 0x0;
12131214
public static final int SHGFI_USEFILEATTRIBUTES = 0x000000010;
12141215
public static final int SIGDN_FILESYSPATH = 0x80058000;
12151216
public static final int SIF_ALL = 0x17;

bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.eclipse.swt.*;
2121
import org.eclipse.swt.graphics.*;
22-
import org.eclipse.swt.internal.*;
2322
import org.eclipse.swt.internal.win32.*;
2423
import org.eclipse.swt.widgets.*;
2524

@@ -382,16 +381,22 @@ public ImageData getImageData () {
382381
public ImageData getImageData (int zoom) {
383382
// Windows API returns image data according to primary monitor zoom factor
384383
// rather than at original scaling
385-
int nativeZoomFactor = 100 * Display.getCurrent().getPrimaryMonitor().getZoom() / DPIUtil.getDeviceZoom();
386-
int imageZoomFactor = 100 * zoom / nativeZoomFactor;
384+
int initialNativeZoom = Display.getCurrent().getPrimaryMonitor().getZoom();
387385
if (extension != null) {
388386
SHFILEINFO shfi = new SHFILEINFO ();
389-
int flags = OS.SHGFI_ICON | OS.SHGFI_SMALLICON | OS.SHGFI_USEFILEATTRIBUTES;
387+
int flags = OS.SHGFI_ICON | OS.SHGFI_USEFILEATTRIBUTES;
388+
boolean useLargeIcon = 100 * zoom / initialNativeZoom >= 200;
389+
if(useLargeIcon) {
390+
flags |= OS.SHGFI_LARGEICON;
391+
initialNativeZoom *= 2;
392+
} else {
393+
flags |= OS.SHGFI_SMALLICON;
394+
}
390395
TCHAR pszPath = new TCHAR (0, extension, true);
391396
OS.SHGetFileInfo (pszPath.chars, OS.FILE_ATTRIBUTE_NORMAL, shfi, SHFILEINFO.sizeof, flags);
392397
if (shfi.hIcon != 0) {
393-
Image image = Image.win32_new (null, SWT.ICON, shfi.hIcon);
394-
ImageData imageData = image.getImageData (imageZoomFactor);
398+
Image image = Image.win32_new (null, SWT.ICON, shfi.hIcon, initialNativeZoom);
399+
ImageData imageData = image.getImageData (zoom);
395400
image.dispose ();
396401
return imageData;
397402
}
@@ -416,8 +421,8 @@ public ImageData getImageData (int zoom) {
416421
long [] phiconSmall = new long[1], phiconLarge = null;
417422
OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1);
418423
if (phiconSmall [0] == 0) return null;
419-
Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0]);
420-
ImageData imageData = image.getImageData (imageZoomFactor);
424+
Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0], initialNativeZoom);
425+
ImageData imageData = image.getImageData (zoom);
421426
image.dispose ();
422427
return imageData;
423428
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ public final class Image extends Resource implements Drawable {
127127
* Prevents uninitialized instances from being created outside the package.
128128
*/
129129
Image (Device device) {
130+
this(device, DPIUtil.getNativeDeviceZoom());
131+
}
132+
133+
private Image (Device device, int nativeZoom) {
130134
super(device);
131-
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
135+
initialNativeZoom = nativeZoom;
132136
this.device.registerResourceWithZoomSupport(this);
133137
}
134138

@@ -2030,10 +2034,10 @@ public String toString () {
20302034
*
20312035
* @noreference This method is not intended to be referenced by clients.
20322036
*/
2033-
public static Image win32_new(Device device, int type, long handle) {
2034-
Image image = new Image(device);
2037+
public static Image win32_new(Device device, int type, long handle, int nativeZoom) {
2038+
Image image = new Image(device, nativeZoom);
20352039
image.type = type;
2036-
image.new ImageHandle(handle, image.getZoom());
2040+
image.new ImageHandle(handle, nativeZoom);
20372041
return image;
20382042
}
20392043

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ static Image createIcon (Image image, int zoom) {
12041204
if (hIcon == 0) SWT.error(SWT.ERROR_NO_HANDLES);
12051205
OS.DeleteObject (hBitmap);
12061206
OS.DeleteObject (hMask);
1207-
return Image.win32_new (device, SWT.ICON, hIcon);
1207+
return Image.win32_new (device, SWT.ICON, hIcon, zoom);
12081208
}
12091209

12101210
long getTextSearchIcon(int size) {
@@ -2543,27 +2543,28 @@ Font getSystemFont (int zoom) {
25432543
*/
25442544
public Image getSystemImage (int id) {
25452545
checkDevice ();
2546+
int primaryMonitorNativeZoom = getPrimaryMonitor().getZoom();
25462547
switch (id) {
25472548
case SWT.ICON_ERROR: {
25482549
if (errorImage != null) return errorImage;
25492550
long hIcon = OS.LoadImage (0, OS.OIC_HAND, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2550-
return errorImage = Image.win32_new (this, SWT.ICON, hIcon);
2551+
return errorImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
25512552
}
25522553
case SWT.ICON_WORKING:
25532554
case SWT.ICON_INFORMATION: {
25542555
if (infoImage != null) return infoImage;
25552556
long hIcon = OS.LoadImage (0, OS.OIC_INFORMATION, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2556-
return infoImage = Image.win32_new (this, SWT.ICON, hIcon);
2557+
return infoImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
25572558
}
25582559
case SWT.ICON_QUESTION: {
25592560
if (questionImage != null) return questionImage;
25602561
long hIcon = OS.LoadImage (0, OS.OIC_QUES, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2561-
return questionImage = Image.win32_new (this, SWT.ICON, hIcon);
2562+
return questionImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
25622563
}
25632564
case SWT.ICON_WARNING: {
25642565
if (warningIcon != null) return warningIcon;
25652566
long hIcon = OS.LoadImage (0, OS.OIC_BANG, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2566-
return warningIcon = Image.win32_new (this, SWT.ICON, hIcon);
2567+
return warningIcon = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
25672568
}
25682569
}
25692570
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ IShellLink createShellLink (MenuItem item) {
166166
ImageData data;
167167
if (item.hBitmap != 0) {
168168
long handle = OS.CopyImage(item.hBitmap, SWT.BITMAP, 0, 0, 0);
169-
Image image2 = Image.win32_new (display, SWT.BITMAP, handle);
169+
Image image2 = Image.win32_new (display, SWT.BITMAP, handle, nativeZoom);
170170
data = image2.getImageData (DPIUtil.getDeviceZoom ());
171171
image2.dispose();
172172
} else {

0 commit comments

Comments
 (0)