OS/X GUI applications
Michael Hall
mik3hall at gmail.com
Fri Jan 6 22:35:52 UTC 2023
> On Jan 3, 2023, at 1:15 PM, Philip Race <philip.race at oracle.com> wrote:
>
> PS actually java launcher code still reads -Dapple.awt.application.name
>
> -Phil.
Specific to getting a default application menu Cocoa ‘About’ menu item for javaFX applications. I could elaborate more if any interest but just that here to be brief.
Again, my concern was not the continued support of the apple system properties. It was my mistaken first assumption that it was the use of these that enabled the behavior like getting this menu item. Although the apple.awt.application.name does figure into what follows.
The javaFX Application class can include…
static {
//java.awt.Toolkit.getDefaultToolkit(); // Start AppKit
Thread t = new Thread(() -> { java.awt.Toolkit.getDefaultToolkit(); });
t.start();
}
ApplicationDelegate.m then can be changed as follows…
Add…
#import "PropertiesUtilities.h"
#import “ThreadUtilities.h"
Make the following change in the ‘init’…
//self.fAboutMenu = (NSMenuItem*)[appMenu itemAtIndex:0];
if ([[[appMenu itemAtIndex:0] title] hasPrefix:@"About"])
self.fAboutMenu = (NSMenuItem*)[appMenu itemAtIndex:0];
else {
JNIEnv *env = [ThreadUtilities getJNIEnv];
NSString* appName = [PropertiesUtilities javaSystemPropertyForKey:@"apple.awt.application.name" withEnv:env];
if (appName != nil) {
NSMenuItem *aboutItem = [[NSMenuItem alloc] init];
[aboutItem setTitle:[NSString stringWithFormat:@"About %@", appName]];
[appMenu insertItem:aboutItem atIndex:0];
self.fAboutMenu = aboutItem;
}
}
This fixes the previously mentioned bug where the code assumes it has an ‘About’ menu item when it doesn’t.
The code seems to need to run off the main thread to access the menus properly. Otherwise you get nil or nulls. Somewhat strangely the menu code seems to function as nop’s if they get nil or nulls. So the above change should be ok even if hit running on the main thread.
Also given that this in - (void)_updateAboutMenu:(BOOL)aboutAvailable enabled:(BOOL)aboutEnabled
if (aboutAvailable) {
// Make sure About is around
if ([self.fAboutMenu menu] == nil) {
addMenuItem(self.fAboutMenu, 0);
}
Is also sort of a bug. First, I’m not sure why if it’s nil you would want to add it to the menu. But even if you try it does nothing, it is a nop. I had first thought about using this method.
I used the apple.awt.application.name property for one reason to give another way to skip the changed code if it wasn’t set. But in testing this didn’t work. Something always seems to set that property so it just kept working.
Again, with these changes a javaFX application gets the default Cocoa ‘About’ menu item in the Application menu. I haven’t verified if this would further allow changing the java.awt.Desktop about handler. But it might be at least a first step in reconciling javaFX applications and java.awt.Desktop. At least on OS/X.
More information about the client-libs-dev
mailing list