From d14b4267d250df4637cf43880bacbcf3faca4f2f Mon Sep 17 00:00:00 2001 From: Patrick Tief Date: Wed, 21 Sep 2022 17:39:08 +0200 Subject: [PATCH 1/2] add support for zoom level of program images Signed-off-by: Patrick Tief --- .../org/eclipse/swt/program/Program.java | 23 ++++++++++++++--- .../gtk/org/eclipse/swt/program/Program.java | 23 ++++++++++++++--- .../org/eclipse/swt/program/Program.java | 25 +++++++++++++++---- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java b/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java index fe81c5bbfcb..0c568157ea5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java @@ -333,14 +333,29 @@ public boolean execute (String fileName) { } } + /** - * Returns the receiver's image data. This is the icon - * that is associated with the receiver in the operating - * system. + * Returns the receiver's image data at 100% zoom level. + * This is the icon that is associated with the receiver + * in the operating system. * * @return the image data for the program, may be null */ public ImageData getImageData () { + return getImageData (100); +} + +/** + * Returns the receiver's image data based on the given zoom level. + * This is the icon that is associated with the receiver in the + * operating system. + * + * @param zoom + * The zoom level in % of the standard resolution + * + * @return the image data for the program, may be null + */ +public ImageData getImageData (int zoom) { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); @@ -358,7 +373,7 @@ public ImageData getImageData () { nsImage.setSize(size); nsImage.retain(); Image image = Image.cocoa_new(Display.getCurrent(), SWT.BITMAP, nsImage); - ImageData imageData = image.getImageData(); + ImageData imageData = image.getImageData(zoom); image.dispose(); return imageData; } diff --git a/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java b/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java index d78ea393793..165ca462aa5 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java @@ -17,7 +17,6 @@ import java.nio.file.*; import java.nio.file.Path; import java.util.*; -import java.util.List; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -121,13 +120,29 @@ public static Program findProgram(String extension) { } /** - * Returns the receiver's image data. This is the icon - * that is associated with the receiver in the operating - * system. + * Returns the receiver's image data at 100% zoom level. + * This is the icon that is associated with the receiver + * in the operating system. * * @return the image data for the program, may be null */ public ImageData getImageData() { + return getImageData(100); +} + + + +/** + * Returns the receiver's image data based on the given zoom level. + * This is the icon that is associated with the receiver in the + * operating system. + * + * @param zoom + * The zoom level in % of the standard resolution + * + * @return the image data for the program, may be null + */ +public ImageData getImageData(int zoom) { if (iconPath == null) return null; ImageData data = null; diff --git a/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java b/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java index 6531da28fc9..5bb6de26c91 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java @@ -350,14 +350,30 @@ public boolean execute (String fileName) { return success; } + + /** - * Returns the receiver's image data. This is the icon - * that is associated with the receiver in the operating - * system. + * Returns the receiver's image data at 100% zoom level. + * This is the icon that is associated with the receiver + * in the operating system. * * @return the image data for the program, may be null */ public ImageData getImageData () { + return getImageData (100); +} + +/** + * Returns the receiver's image data based on the given zoom level. + * This is the icon that is associated with the receiver in the + * operating system. + * + * @param zoom + * The zoom level in % of the standard resolution + * + * @return the image data for the program, may be null + */ +public ImageData getImageData (int zoom) { int nIconIndex = 0; String fileName = iconName; int index = iconName.indexOf (','); @@ -379,8 +395,7 @@ public ImageData getImageData () { OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1); if (phiconSmall [0] == 0) return null; Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0]); - /* Fetch the ImageData at 100% zoom and return */ - ImageData imageData = image.getImageData (); + ImageData imageData = image.getImageData (zoom); image.dispose (); return imageData; } From 19145ae5f7a5160b4fbbdcda8c39f102c963246d Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Wed, 24 Jan 2024 14:31:10 +0100 Subject: [PATCH 2/2] Consider that program icons have scaling of primary monitor When loading icons for programs, their image data will be scaled according to the primary monitor scaling. This is different from other images, which when loaded have their original size. This change reflects that behavior when creating the image data for a target zoom factor by considering the icon's pre-scaling. It also add a @since tag for the previously introduced zoom-aware getImageData() method for the Program class. --- .../cocoa/org/eclipse/swt/program/Program.java | 1 + .../gtk/org/eclipse/swt/program/Program.java | 1 + .../win32/org/eclipse/swt/program/Program.java | 8 +++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java b/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java index 0c568157ea5..92c0b080e97 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Program/cocoa/org/eclipse/swt/program/Program.java @@ -354,6 +354,7 @@ public ImageData getImageData () { * The zoom level in % of the standard resolution * * @return the image data for the program, may be null + * @since 3.125 */ public ImageData getImageData (int zoom) { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); diff --git a/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java b/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java index 165ca462aa5..1e5208a3987 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Program/gtk/org/eclipse/swt/program/Program.java @@ -141,6 +141,7 @@ public ImageData getImageData() { * The zoom level in % of the standard resolution * * @return the image data for the program, may be null + * @since 3.125 */ public ImageData getImageData(int zoom) { if (iconPath == null) return null; diff --git a/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java b/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java index 5bb6de26c91..b5a2b6252a9 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java @@ -19,7 +19,9 @@ 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.*; /** * Instances of this class represent programs and @@ -372,6 +374,7 @@ public ImageData getImageData () { * The zoom level in % of the standard resolution * * @return the image data for the program, may be null + * @since 3.125 */ public ImageData getImageData (int zoom) { int nIconIndex = 0; @@ -395,7 +398,10 @@ public ImageData getImageData (int zoom) { 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 (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(); + ImageData imageData = image.getImageData (100 * zoom / nativeZoomFactor); image.dispose (); return imageData; }