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

Added an ApplicationMenu class to the javafx.application package #1622

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ protected void finishTerminating() {
// The eventThread is null at this point, no need to check it
}

/**
* Installs a handler to show a custom About window for the application. The default
* implementation does nothing.
*
* @param handler - the handler to respond to the About menu item being selected
*/
public void setAboutHandler(Runnable handler) {
}

/**
* Installs a handler to show a custom Settings window for the application. The default
* implementation does nothing.
*
* @param handler - the handler to respond to the Settings menu item being selected
*/
public void setSettingsHandler(Runnable handler) {
}

/**
* Gets the name for the application. The application name may
* be used to identify the application in the user interface or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ private static void finishKeepAliveThread() {
}

private Menu appleMenu;
private Runnable aboutHandler;
private Runnable settingsHandler;

native void _runLoop(ClassLoader classLoader, Runnable launchable,
boolean isTaskbarApplication);
Expand Down Expand Up @@ -214,8 +216,46 @@ private void setEventThread() {
native private void _hideOtherApplications();
native private void _unhideAllApplications();

public void installAppleMenu(MenuBar menubar) {
this.appleMenu = createMenu("Apple");
@Override
public void setAboutHandler(Runnable handler) {
aboutHandler = handler;
rebuildAppleMenu();
}

@Override
public void setSettingsHandler(Runnable handler) {
settingsHandler = handler;
rebuildAppleMenu();
}

private void rebuildAppleMenu() {
for (int index = this.appleMenu.getItems().size() - 1; index >= 0; index--) {
this.appleMenu.remove(index);
}

if (null != aboutHandler) {
MenuItem aboutMenu = createMenuItem("About " + getName(), new MenuItem.Callback() {
@Override public void action() {
aboutHandler.run();
}
@Override public void validate() {
}
});
this.appleMenu.add(aboutMenu);
this.appleMenu.add(MenuItem.Separator);
}

if (null != settingsHandler) {
MenuItem aboutMenu = createMenuItem("Settings...", new MenuItem.Callback() {
@Override public void action() {
settingsHandler.run();
}
@Override public void validate() {
}
}, ',', KeyEvent.MODIFIER_COMMAND);
this.appleMenu.add(aboutMenu);
this.appleMenu.add(MenuItem.Separator);
}

MenuItem hideMenu = createMenuItem("Hide " + getName(), new MenuItem.Callback() {
@Override public void action() {
Expand Down Expand Up @@ -257,6 +297,12 @@ public void installAppleMenu(MenuBar menubar) {
}
}, 'q', KeyEvent.MODIFIER_COMMAND);
this.appleMenu.add(quitMenu);
}

public void installAppleMenu(MenuBar menubar) {
this.appleMenu = createMenu("Apple");

rebuildAppleMenu();

menubar.add(this.appleMenu);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ public static boolean isTaskbarApplication() {
return taskbarApplication;
}

/**
* Installs a handler to show a custom About window for your application.
* <p>
* Setting the handler to null reverts it to the default behavior.
*
* @param handler - the handler to respond to the About menu item being selected
*/
public static void setAboutHandler(Runnable handler) {
com.sun.glass.ui.Application.GetApplication().setAboutHandler(handler);
}

/**
* Installs a handler to show a custom Settings window for your application.
* <p>
* Setting the handler to null reverts it to the default behavior.
*
* @param handler - the handler to respond to the Settings menu item being selected
*/
public static void setSettingsHandler(Runnable handler) {
com.sun.glass.ui.Application.GetApplication().setSettingsHandler(handler);
}

/**
* Sets the name of the this application based on the Application class.
* This method is called by the launcher, and is not
Expand Down Expand Up @@ -927,6 +949,8 @@ private static boolean isSupportedImpl(ConditionalFeature feature) {
return Toolkit.getToolkit().isSupported(feature);
}
return hasPointer;
case APPLICATION_MENU:
return PlatformUtil.isMac();
default:
return Toolkit.getToolkit().isSupported(feature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,32 @@ public void init() throws Exception {
public void stop() throws Exception {
}

private ApplicationMenu applicationMenu = null;

/**
* Gets the ApplicationMenu for this application. This provides
* the ability to interact with the platform provided application menu.
*
* @return the ApplicationMenu
*
* @throws java.lang.UnsupportedOperationException if
* javafx.application.ConditionalFeature.APPLICATION_MENU is not supported on this platform
*
* @since JavaFX 24.0
*/
public final ApplicationMenu getApplicationMenu() {
if (!PlatformImpl.isSupported(ConditionalFeature.APPLICATION_MENU)) {
throw new UnsupportedOperationException("ConditionalFeature.APPLICATION_MENU is not"
+ " supported on this platform");
}
synchronized (this) {
if (applicationMenu == null) {
applicationMenu = new ApplicationMenu();
}
return applicationMenu;
}
}

private HostServices hostServices = null;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package javafx.application;

import com.sun.javafx.application.PlatformImpl;

/**
* Application menu class that provides support for interacting with the platform provided
* application menu. This is currently only supported on macOS.
*
* @author David Kopp
*
* @since JavaFX 24.0
*/
public final class ApplicationMenu
{
ApplicationMenu() {
}

/**
* Installs a handler to show a custom About window for your application.
* <p>
* Setting the handler to null reverts it to the default behavior.
*
* @param handler - the handler to respond to the About menu item being selected
*/
public void setAboutHandler(Runnable handler) {
PlatformImpl.setAboutHandler(handler);
}

/**
* Installs a handler to show a custom Settings window for your application.
* <p>
* Setting the handler to null reverts it to the default behavior.
*
* @param handler - the handler to respond to the Settings menu item being selected
*/
public void setSettingsHandler(Runnable handler) {
PlatformImpl.setSettingsHandler(handler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ public enum ConditionalFeature {
* true.
* @since JavaFX 8.0
*/
INPUT_POINTER
INPUT_POINTER,

/**
* Indicates whether or this platform supports the concept of an application menu.
* <p>
* This is currently only supported on macOS.
* @since JavaFX 24.0
*/
APPLICATION_MENU

}