From 2af546b6ddf59ff0ec941ffc332e4d18342edbaf Mon Sep 17 00:00:00 2001 From: Phillipus Date: Sun, 22 Oct 2023 12:45:09 +0100 Subject: [PATCH] Workaround Mac Sonoma Menu bug - See https://github.com/eclipse-platform/eclipse.platform.swt/issues/779 - Not needed in Sonoma 14.1 --- .../com/archimatetool/editor/Application.java | 5 +- .../com/archimatetool/editor/MacMenuFix.java | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 com.archimatetool.editor/src/com/archimatetool/editor/MacMenuFix.java diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/Application.java b/com.archimatetool.editor/src/com/archimatetool/editor/Application.java index e993b7562..2c74ad008 100644 --- a/com.archimatetool.editor/src/com/archimatetool/editor/Application.java +++ b/com.archimatetool.editor/src/com/archimatetool/editor/Application.java @@ -61,7 +61,10 @@ public Object start(IApplicationContext context) throws Exception { // Create Main Display Display display = PlatformUI.createDisplay(); - + + // Check for Mac Sonoma menu fix + MacMenuFix.apply(display); + // Hook into opening documents from the desktop OpenDocumentHandler.getInstance().hook(display); diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/MacMenuFix.java b/com.archimatetool.editor/src/com/archimatetool/editor/MacMenuFix.java new file mode 100644 index 000000000..d8e5e2a42 --- /dev/null +++ b/com.archimatetool.editor/src/com/archimatetool/editor/MacMenuFix.java @@ -0,0 +1,73 @@ +/** + * This program and the accompanying materials + * are made available under the terms of the License + * which accompanies this distribution in the file LICENSE.txt + */ +package com.archimatetool.editor; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import org.eclipse.swt.widgets.Display; + +import com.archimatetool.editor.utils.PlatformUtils; + + + + +/** + * Workaround for Mac Sonoma displaying "NewAppplication" main menu instead of app name + * + * See https://github.com/eclipse-platform/eclipse.platform.swt/issues/779 + * + * Inspired by https://github.com/knime/knime-product/commit/bb0bd4498d7844df9c897ad984f028ff80d208f0 + * + * TODO: Remove this after Eclipe 4.30 + * + * @author Phillip Beauvoir + */ +@SuppressWarnings("nls") +public class MacMenuFix { + + static void apply(Display display) throws Exception { + if(!PlatformUtils.isMac()) { + return; + } + + try { + // Get the Display#application field + Field applicationField = display.getClass().getDeclaredField("application"); + applicationField.setAccessible(true); + Object application = applicationField.get(display); + + // NSMenu mainmenu = application.mainMenu(); + Method mainMenuMethod = getMethod(application, "mainMenu"); + Object mainmenu = mainMenuMethod.invoke(application); + + // NSMenuItem appitem = mainmenu.itemAtIndex(0); + Method itemAtIndexMethod = getMethod(mainmenu, "itemAtIndex", long.class); + Object appItem = itemAtIndexMethod.invoke(mainmenu, 0); + + if(appItem != null) { + // NSMenu sm = appitem.submenu(); + Method subMenuMethod = getMethod(appItem, "submenu"); + Object sm = subMenuMethod.invoke(appItem); + + // NSString name = appItem.title(); + Method titleMethod = getMethod(appItem, "title"); + Object name = titleMethod.invoke(appItem); + + // sm.setTitle(name); + Method setTitleMethod = getMethod(sm, "setTitle", name.getClass()); + setTitleMethod.invoke(sm, name); + } + } + catch(Exception ex) { + Logger.logError("Could not apply Mac Menu fix", ex); + } + } + + private static Method getMethod(Object obj, String name, Class... parameters) throws NoSuchMethodException { + return obj.getClass().getDeclaredMethod(name, parameters); + } +}