001/*
002 * Copyright 2019-2021 M. Sean Gilligan.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package app.supernaut.fx.util;
017
018import java.lang.module.ModuleDescriptor;
019import java.util.Locale;
020
021/**
022 * Various utilities methods for testing the current FX version and environment
023 */
024public class FxVersionUtil {
025    /** Predicted fix version for macOS Browser Open on GraalVM native-image */
026    static final ModuleDescriptor.Version MACOS_NATIVE_SHOW_DOC_VERSION = ModuleDescriptor.Version.parse("18-ea+4");
027    /**
028     * This doesn't work. You can't always trust Stack Overflow, I guess.
029     * @see <a href="https://stackoverflow.com/questions/50264604/how-can-you-tell-if-your-java-program-is-running-in-a-graalvm-aot-context">Stack Overflow</a>
030     */
031    static final boolean IS_AOT = Boolean.getBoolean("com.oracle.graalvm.isaot");
032
033
034    /**
035     * @return The JavaFX Runtime Version in {@link ModuleDescriptor.Version} format
036     */
037    public static ModuleDescriptor.Version getJavaFXVersion() {
038        return ModuleDescriptor.Version.parse(System.getProperty("javafx.runtime.version"));
039    }
040    
041    /**
042     * Is {@link javafx.application.HostServices#showDocument(String)} available?
043     * <p>
044     * Hyperlinks don't work on macOS in Graal native-image yet, but they should be available in the next EA release of OpenJFX 18.
045     * <p>
046     * For Browser open to be available one of the following must be true:
047     * <ul>
048     *     <li>OS is not macOS</li>
049     *     <li>Not running in GraalVM native image</li>
050     *     <li>JavaFX is later than {@link FxVersionUtil#MACOS_NATIVE_SHOW_DOC_VERSION}</li>
051     * </ul>
052     * NOTE: Currently we can only reliably check if we're macOS or not, so that's all we are checking.
053     * @see <a href="https://github.com/SupernautApp/SupernautFX/issues/25">Supernaut.FX Issue #25</a>
054     * @return true if available, false if not
055     */
056    public static boolean isShowDocumentAvailable() {
057        return !isMacOS() /* || !IS_AOT || hasMacShowDocumentFix() */;
058    }
059
060    /**
061     * @return true if this JavaFX version has the fix for macOS native-image showDocument
062     */
063    public static boolean hasMacShowDocumentFix() {
064        boolean hasFix = getJavaFXVersion().compareTo(MACOS_NATIVE_SHOW_DOC_VERSION) >= 0;
065        return hasFix;
066    }
067
068    /**
069     * @return true if we're running on macOS
070     */
071    public static boolean isMacOS() {
072        boolean isMacOS = System.getProperty("os.name", "").toLowerCase(Locale.US).contains("mac");
073        return isMacOS;
074    }
075}