Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS System Menu Bar Support #107

Closed
wiverson opened this issue Oct 8, 2020 · 9 comments
Closed

macOS System Menu Bar Support #107

wiverson opened this issue Oct 8, 2020 · 9 comments

Comments

@wiverson
Copy link

wiverson commented Oct 8, 2020

Trying to add support for NSMenuFX in a Hello World JavaFX Maven project.

Lots of errors related to modules / access. Here is the trail for the issue on the NSMenuFX page:

codecentric/NSMenuFX#29

That issue should have enough for repro steps, but the short version:

  • Create a HelloWorld JavaFX Maven project
  • Add the NSMenuFX dependency and simple test case

This is both a really nice module (for native macOS menu integration) and also a nice test case for working with/around the Java module system.

Here is sample of the error output. Let me know if you need any additional information.

[INFO] --- javafx-maven-plugin:0.0.4:run (default-cli) @ hellofx --- Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071) Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195) at java.base/java.lang.Thread.run(Thread.java:832) Caused by: java.lang.IllegalAccessError: class de.codecentric.centerdevice.glass.AdapterContext (in unnamed module @0x797d0761) cannot access class com.sun.javafx.tk.Toolkit (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.tk to unnamed module @0x797d0761 at de.codecentric.centerdevice.glass.AdapterContext.createContext(AdapterContext.java:34) at de.codecentric.centerdevice.glass.AdapterContext.getContext(AdapterContext.java:19) at de.codecentric.centerdevice.MenuToolkit.toolkit(MenuToolkit.java:57) at de.codecentric.centerdevice.MenuToolkit.toolkit(MenuToolkit.java:53) at de.codecentric.centerdevice.MenuToolkit.toolkit(MenuToolkit.java:49) at sample.Main.start(Main.java:52) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) Exception running application sample.Main

@betanzos
Copy link
Contributor

betanzos commented Oct 9, 2020

This issue is not related to the plugin but to the Java module system. NSMenuFX uses an internal package from the javafx.graphics module and the current version of the plugin always adds the JavaFX jars to the modulepath.

Next version should include an option to use only the classpath, so problems like this could be avoided (if you don't need to use the modulepath)

You can fix this by using options:

    <configuration>
        <options>
            <option>--add-exports</option>
            <option>javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED</option>
        </options>
    </configuration>

@kevinrushforth
Copy link

The --add-exports will allow it to work for now, but applications should not be dipping into the internals of JavaFX in the first place. The best long-term solution is to remove this dependency.

@wiverson wiverson changed the title Support for NSMenuFX macOS System Menu Bar Support Oct 12, 2020
@wiverson
Copy link
Author

First off, thanks for the right module configuration tip.

I did a bit more research, and found the built-in JavaFX MenuBar.setUseSystemMenuBar(). On macOS Catalina with JavaFX, the menubar is completely unresponsive when MenuBar.setUseSystemMenuBar(true) when the application first launches. I just was going to record the behavior when I discovered that switching to another application and then back again makes the system menubar work again.

It looks like there is some confusion about how to get menus working nicely on macOS.

https://stackoverflow.com/questions/49221738/how-to-implement-javafx-macos-app-with-system-menu-bar-and-handle-desktop-events

I just updated this issue title to reflect the goal, as opposed to a particular solution - I'd like to be able to at least minimally support the macOS native menu bar.

To summarize:

  • NSMenuFX reaches in to JavaFX internals and requires poor practices for module configuration.
  • MenuBar.setUseSystemMenuBar(true) locks the menu until the application is unfocused and refocused.
  • Ambiguous interaction / support / threading behavior when using the java.awt.Desktop Preferences/Quit/About/setDefaultMenuBar.

This might be as simple as documenting the correct API usage (possibly with a workaround for the focus issue).

Let me know if you have any additional questions or if there is anything else I can do to help.

@betanzos
Copy link
Contributor

betanzos commented Oct 12, 2020

I think this is a JavaFX issue and, unlike the other one, this can not be fixed by using any configuration trick. I suggest you report it in JavaFX repository.

@wiverson
Copy link
Author

I did submit a bug via the JBS. Not a particularly user friendly system. Got a ticket number, but no way to add anything to it, update, etc. :\

For some of these issues, it looks like there is an elaborate choreography for a workaround. For example, I found this:

https://github.com/dustinkredmond/FXTrayIcon/blob/main/src/main/java/com/jfxdev/fxtrayicon/FXTrayIcon.java

... for sorting out system tray icon / menu stuff with JavaFX. I was kind of hoping that "put a working menu in the system menu on macOS" was supported.

@kevinrushforth
Copy link

If you mean this part?

I did a bit more research, and found the built-in JavaFX MenuBar.setUseSystemMenuBar(). On macOS Catalina with JavaFX, the menubar is completely unresponsive when MenuBar.setUseSystemMenuBar(true) when the application first launches. I just was going to record the behavior when I discovered that switching to another application and then back again makes the system menubar work again.

This is due to the following bug:

JDK-8233678: [macos 10.15] System menu bar does not work initially on macOS Catalina

@wiverson
Copy link
Author

Yup. That's it. Can't follow links for fixes, did you submit a fix for openjfx16 or is that just a defer?

@kevinrushforth
Copy link

I had hoped to get it in for openjfx15, but it missed that. It's on the list of fixes planned for openjfx16, but isn't done yet.

@abhinayagarwal
Copy link
Collaborator

Closing as this is a JavaFX issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants