Skip to content

Commit

Permalink
Fixed Program Icon Scaling for Multi Zoom
Browse files Browse the repository at this point in the history
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
  • Loading branch information
amartya4256 committed Jan 9, 2025
1 parent 9a9747e commit 2b5e970
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Object nativeToJava(TransferData transferData) {
pSourceBits -= scanline;
}
}
Image image = Image.win32_new(null, SWT.BITMAP, memDib);
Image image = Image.win32_new(null, SWT.BITMAP, memDib, DPIUtil.getNativeDeviceZoom());
ImageData data = image.getImageData (DPIUtil.getDeviceZoom ());
OS.DeleteObject(memDib);
image.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,7 @@ public class OS extends C {
public static final int SHADEBLENDCAPS = 120;
public static final int SHGFI_ICON = 0x000000100;
public static final int SHGFI_SMALLICON= 0x1;
public static final int SHGFI_LARGEICON= 0x0;
public static final int SHGFI_USEFILEATTRIBUTES = 0x000000010;
public static final int SIGDN_FILESYSPATH = 0x80058000;
public static final int SIF_ALL = 0x17;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.widgets.*;

Expand Down Expand Up @@ -382,16 +381,21 @@ public ImageData getImageData () {
public ImageData getImageData (int zoom) {
// Windows API returns image data according to primary monitor zoom factor
// rather than at original scaling
int nativeZoomFactor = 100 * Display.getCurrent().getPrimaryMonitor().getZoom() / DPIUtil.getDeviceZoom();
int imageZoomFactor = 100 * zoom / nativeZoomFactor;
int initialNativeZoom = Display.getCurrent().getPrimaryMonitor().getZoom();
if (extension != null) {
SHFILEINFO shfi = new SHFILEINFO ();
int flags = OS.SHGFI_ICON | OS.SHGFI_SMALLICON | OS.SHGFI_USEFILEATTRIBUTES;
int flags = OS.SHGFI_ICON | OS.SHGFI_USEFILEATTRIBUTES;
if(zoom > initialNativeZoom) {
flags |= OS.SHGFI_LARGEICON;
initialNativeZoom *= 2;
} else {
flags |= OS.SHGFI_SMALLICON;
}
TCHAR pszPath = new TCHAR (0, extension, true);
OS.SHGetFileInfo (pszPath.chars, OS.FILE_ATTRIBUTE_NORMAL, shfi, SHFILEINFO.sizeof, flags);
if (shfi.hIcon != 0) {
Image image = Image.win32_new (null, SWT.ICON, shfi.hIcon);
ImageData imageData = image.getImageData (imageZoomFactor);
Image image = Image.win32_new (null, SWT.ICON, shfi.hIcon, initialNativeZoom);
ImageData imageData = image.getImageData (zoom);
image.dispose ();
return imageData;
}
Expand All @@ -416,8 +420,8 @@ public ImageData getImageData (int zoom) {
long [] phiconSmall = new long[1], phiconLarge = null;
OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1);
if (phiconSmall [0] == 0) return null;
Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0]);
ImageData imageData = image.getImageData (imageZoomFactor);
Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0], initialNativeZoom);
ImageData imageData = image.getImageData (zoom);
image.dispose ();
return imageData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ public final class Image extends Resource implements Drawable {
* Prevents uninitialized instances from being created outside the package.
*/
Image (Device device) {
this(device, DPIUtil.getNativeDeviceZoom());
}

private Image (Device device, int nativeZoom) {
super(device);
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
initialNativeZoom = nativeZoom;
this.device.registerResourceWithZoomSupport(this);
}

Expand Down Expand Up @@ -2030,10 +2034,10 @@ public String toString () {
*
* @noreference This method is not intended to be referenced by clients.
*/
public static Image win32_new(Device device, int type, long handle) {
Image image = new Image(device);
public static Image win32_new(Device device, int type, long handle, int nativeZoom) {
Image image = new Image(device, nativeZoom);
image.type = type;
image.new ImageHandle(handle, image.getZoom());
image.new ImageHandle(handle, nativeZoom);
return image;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ static Image createIcon (Image image, int zoom) {
if (hIcon == 0) SWT.error(SWT.ERROR_NO_HANDLES);
OS.DeleteObject (hBitmap);
OS.DeleteObject (hMask);
return Image.win32_new (device, SWT.ICON, hIcon);
return Image.win32_new (device, SWT.ICON, hIcon, zoom);
}

long getTextSearchIcon(int size) {
Expand Down Expand Up @@ -2542,27 +2542,28 @@ Font getSystemFont (int zoom) {
*/
public Image getSystemImage (int id) {
checkDevice ();
int primaryMonitorNativeZoom = getPrimaryMonitor().getZoom();
switch (id) {
case SWT.ICON_ERROR: {
if (errorImage != null) return errorImage;
long hIcon = OS.LoadImage (0, OS.OIC_HAND, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
return errorImage = Image.win32_new (this, SWT.ICON, hIcon);
return errorImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
}
case SWT.ICON_WORKING:
case SWT.ICON_INFORMATION: {
if (infoImage != null) return infoImage;
long hIcon = OS.LoadImage (0, OS.OIC_INFORMATION, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
return infoImage = Image.win32_new (this, SWT.ICON, hIcon);
return infoImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
}
case SWT.ICON_QUESTION: {
if (questionImage != null) return questionImage;
long hIcon = OS.LoadImage (0, OS.OIC_QUES, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
return questionImage = Image.win32_new (this, SWT.ICON, hIcon);
return questionImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
}
case SWT.ICON_WARNING: {
if (warningIcon != null) return warningIcon;
long hIcon = OS.LoadImage (0, OS.OIC_BANG, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
return warningIcon = Image.win32_new (this, SWT.ICON, hIcon);
return warningIcon = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ IShellLink createShellLink (MenuItem item) {
ImageData data;
if (item.hBitmap != 0) {
long handle = OS.CopyImage(item.hBitmap, SWT.BITMAP, 0, 0, 0);
Image image2 = Image.win32_new (display, SWT.BITMAP, handle);
Image image2 = Image.win32_new (display, SWT.BITMAP, handle, nativeZoom);
data = image2.getImageData (DPIUtil.getDeviceZoom ());
image2.dispose();
} else {
Expand Down

0 comments on commit 2b5e970

Please sign in to comment.