This is an automated email from the ASF dual-hosted git repository.

mattcasters pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new c73f74b71a show menu images on MacOS 27, fixes #9074 (#9075)
c73f74b71a is described below

commit c73f74b71af94b6fad9f602e19e5f19659738e40
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Thu Oct 22 14:54:25 2026 +0200

    show menu images on MacOS 27, fixes #9074 (#9075)
---
 .../org/apache/hop/ui/core/widget/OsHelper.java    | 68 ++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/ui/src/main/java/org/apache/hop/ui/core/widget/OsHelper.java 
b/ui/src/main/java/org/apache/hop/ui/core/widget/OsHelper.java
index 28517417ab..214093869d 100644
--- a/ui/src/main/java/org/apache/hop/ui/core/widget/OsHelper.java
+++ b/ui/src/main/java/org/apache/hop/ui/core/widget/OsHelper.java
@@ -17,6 +17,8 @@
 
 package org.apache.hop.ui.core.widget;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -39,6 +41,14 @@ public class OsHelper {
   public static final String WS_WPF = "wpf";
   public static final String WS_UNKNOWN = "unknown";
 
+  /** NSMenuItemImageVisibilityVisible in the macOS 27 SDK (NSMenuItem.h). */
+  private static final long NS_MENU_ITEM_IMAGE_VISIBILITY_VISIBLE = 1L;
+
+  private static boolean menuItemImageVisibilityUnsupported;
+  private static Field nsItemField;
+  private static Method objcMsgSend;
+  private static Long setPreferredImageVisibility;
+
   public static final boolean isWindows() {
     final String ws = SWT.getPlatform();
     return WS_WIN32.equals(ws) || WS_WPF.equals(ws);
@@ -146,6 +156,64 @@ public class OsHelper {
             break;
         }
       }
+
+      // macOS 27 hides menu item images unless the item opts in, so opt in 
every item of a
+      // menu right before it opens.
+      //
+      display.addFilter(
+          SWT.Show,
+          event -> {
+            if (event.widget instanceof Menu menu) {
+              for (MenuItem item : menu.getItems()) {
+                showMenuItemImage(item);
+              }
+            }
+          });
+    }
+  }
+
+  /**
+   * Sets NSMenuItem.preferredImageVisibility to visible. Without it, macOS 27 
hides the images of
+   * menu items in applications linked against the macOS 27 SDK. SWT does not 
expose the property
+   * yet and the Cocoa classes only exist in the macOS SWT jar, so this goes 
through reflection. On
+   * older macOS versions the item does not respond to the selector and 
nothing happens.
+   */
+  private static void showMenuItemImage(MenuItem item) {
+    if (menuItemImageVisibilityUnsupported || item.isDisposed()) {
+      return;
+    }
+    try {
+      if (setPreferredImageVisibility == null) {
+        nsItemField = MenuItem.class.getDeclaredField("nsItem");
+        nsItemField.setAccessible(true);
+        Class<?> osClass = Class.forName("org.eclipse.swt.internal.cocoa.OS");
+        objcMsgSend = osClass.getMethod("objc_msgSend", long.class, 
long.class, long.class);
+        setPreferredImageVisibility =
+            (Long)
+                osClass
+                    .getMethod("sel_registerName", String.class)
+                    .invoke(null, "setPreferredImageVisibility:");
+      }
+      Object nsItem = nsItemField.get(item);
+      if (nsItem == null) {
+        return;
+      }
+      boolean supported =
+          (Boolean)
+              nsItem
+                  .getClass()
+                  .getMethod("respondsToSelector", long.class)
+                  .invoke(nsItem, setPreferredImageVisibility);
+      if (!supported) {
+        menuItemImageVisibilityUnsupported = true;
+        return;
+      }
+      long id = nsItem.getClass().getField("id").getLong(nsItem);
+      objcMsgSend.invoke(
+          null, id, setPreferredImageVisibility, 
NS_MENU_ITEM_IMAGE_VISIBILITY_VISIBLE);
+    } catch (Exception e) {
+      // Not running on Cocoa SWT, or SWT internals changed: keep the default 
visibility.
+      menuItemImageVisibilityUnsupported = true;
     }
   }
 }

Reply via email to