From d7c7e23cd2621e4ee7a8322fedea6fb2153a939e Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Mon, 3 Mar 2025 14:27:47 +0100 Subject: [PATCH] MenuItem Image Half Scaling #62 This commit contributes to enforcing half scaling on the MenuItem Image. On Win32, the OS is responsible for painting the Image of a MenuItem and it expects images to be in standard sizes i.e. 16px, 24 px, 32 px, etc. If the images are not provided within these sizes, Windows tries to rescale them, leading to unexpected sizes and masking. Since, half scaling yields the images in standard sizes, MenuItems are scaled accordingly. contributes to https://github.com/eclipse-platform/eclipse.platform.swt/issues/62 and https://github.com/eclipse-platform/eclipse.platform.swt/issues/127 --- .../common/org/eclipse/swt/internal/DPIUtil.java | 12 ++++++++++++ .../win32/org/eclipse/swt/widgets/MenuItem.java | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java index 0f08710d468..ba8e721d5fb 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java @@ -631,7 +631,19 @@ public static boolean useCairoAutoScale() { return useCairoAutoScale; } +public static int getZoomForMenuItemImage(int nativeDeviceZoom) { + String autoScaleValueForMenuItemImage = DPIUtil.autoScaleValue; + if(autoScaleValueForMenuItemImage.equals("quarter") || autoScaleValueForMenuItemImage.equals("exact")) { + autoScaleValueForMenuItemImage = "half"; + } + return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleValueForMenuItemImage); +} + public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) { + return getZoomForAutoscaleProperty(nativeDeviceZoom, autoScaleValue); +} + +private static int getZoomForAutoscaleProperty (int nativeDeviceZoom, String autoScaleValue) { int zoom = 0; if (autoScaleValue != null) { if ("false".equalsIgnoreCase (autoScaleValue)) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java index 1729a2a9a43..543419b6311 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MenuItem.java @@ -782,7 +782,15 @@ public void setImage (Image image) { } else { if (OS.IsAppThemed ()) { if (hBitmap != 0) OS.DeleteObject (hBitmap); - info.hbmpItem = hBitmap = image != null ? Display.create32bitDIB (image, getZoom()) : 0; + int desiredSize = getSystemMetrics(72); + int zoom = (int) (((double) desiredSize / image.getBounds().height) * 100); + int currentSize = image.getImageData(zoom).height; + while(currentSize == desiredSize) { + zoom--; + currentSize = image.getImageData(zoom).height; + } + zoom++; + info.hbmpItem = hBitmap = image != null ? Display.create32bitDIB (image, zoom) : 0; } else { info.hbmpItem = image != null ? OS.HBMMENU_CALLBACK : 0; }