diff --git a/src/dorkbox/util/Desktop.java b/src/dorkbox/util/Desktop.java index 1e2dc66..c4dbf76 100644 --- a/src/dorkbox/util/Desktop.java +++ b/src/dorkbox/util/Desktop.java @@ -25,6 +25,7 @@ import java.net.URI; import java.net.URISyntaxException; import dorkbox.util.jna.linux.Gtk2; +import dorkbox.util.jna.linux.GtkCheck; import dorkbox.util.jna.linux.GtkEventDispatch; import dorkbox.util.process.ShellExecutor; @@ -73,7 +74,7 @@ class Desktop { // Prefer JNA method over AWT, since there are fewer chances for JNA to fail (even though they call the same method) // Additionally, xdg-open can cause problems in Linux with Chrome installed but not the default browser. It will crash Chrome // if Chrome was open before this app opened a URL - if ((OS.isUnix() || OS.isLinux()) && OSUtil.DesktopEnv.isGtkLoaded) { + if ((OS.isUnix() || OS.isLinux()) && GtkCheck.isGtkLoaded) { GtkEventDispatch.dispatch(new Runnable() { @Override public @@ -132,7 +133,7 @@ class Desktop { } // Prevent GTK2/3 conflict caused by Desktop.getDesktop(), which is GTK2 only (via AWT) - if ((OS.isUnix() || OS.isLinux()) && OSUtil.DesktopEnv.isGtkLoaded) { + if ((OS.isUnix() || OS.isLinux()) && GtkCheck.isGtkLoaded) { GtkEventDispatch.dispatch(new Runnable() { @Override public @@ -181,7 +182,7 @@ class Desktop { } // Prevent GTK2/3 conflict caused by Desktop.getDesktop(), which is GTK2 only (via AWT) // Prefer JNA method over AWT, since there are fewer chances for JNA to fail (even though they call the same method) - else if ((OS.isUnix() || OS.isLinux()) && OSUtil.DesktopEnv.isGtkLoaded) { + else if ((OS.isUnix() || OS.isLinux()) && GtkCheck.isGtkLoaded) { // it can actually be MORE that just "file://" (ie, "ftp://" is legit as well) if (!path.contains("://")) { path = "file://" + path; diff --git a/src/dorkbox/util/OSUtil.java b/src/dorkbox/util/OSUtil.java index 3030c04..c23660d 100644 --- a/src/dorkbox/util/OSUtil.java +++ b/src/dorkbox/util/OSUtil.java @@ -402,21 +402,6 @@ class OSUtil { public static class DesktopEnv { - /** - * Determine if the application is running via GTK2. This does not cause GTK to load, where calls to Gtk.isGtk2 will - */ - public static volatile boolean isGtk2 = false; - - /** - * Determine if the application is running via GTK3. This does not cause GTK to load, where calls to Gtk.isGtk3 will - */ - public static volatile boolean isGtk3 = false; - - /** - * Determine if the application has loaded GTK yet or not. This does not cause GTK to load, where calls to Gtk.isLoaded will - */ - public static volatile boolean isGtkLoaded = false; - public enum Env { Gnome, KDE, diff --git a/src/dorkbox/util/SwingUtil.java b/src/dorkbox/util/SwingUtil.java index 1ab1c33..649d63a 100644 --- a/src/dorkbox/util/SwingUtil.java +++ b/src/dorkbox/util/SwingUtil.java @@ -168,6 +168,9 @@ class SwingUtil { /** * Checks to see if GTK is loaded by Swing, and if so - which version is loaded. * + * NOTE: if the UI uses the 'getSystemLookAndFeelClassName' and is on Linux and it's the GtkLookAndFeel, this will cause GTK2 + * to get loaded first, which will cause conflicts if one tries to use GTK3 + * * @return the version of GTK loaded (if any) otherwise 0 for no version of GTK is loaded */ public static diff --git a/src/dorkbox/util/jna/linux/GtkCheck.java b/src/dorkbox/util/jna/linux/GtkCheck.java new file mode 100644 index 0000000..bfbe4ad --- /dev/null +++ b/src/dorkbox/util/jna/linux/GtkCheck.java @@ -0,0 +1,83 @@ +/* + * Copyright 2017 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.util.jna.linux; + +import dorkbox.util.JavaFX; +import dorkbox.util.SwingUtil; +import dorkbox.util.Swt; + +/** + * Accessor methods/logic for determining if GTK is already loaded by the Swing/JavaFX/SWT, or if GTK has been manually loaded via + * GtkEventDispatch.startGui(). + */ +@SuppressWarnings("WeakerAccess") +public +class GtkCheck { + /** + * Only valid if `isGtkLoaded=true`. Determine if the application is running via GTK2. + *

+ * This does not cause GTK to load, where calls to Gtk.isGtk2 will + */ + public static volatile boolean isGtk2 = false; + + /** + * Only valid if `isGtkLoaded=true`. Determine if the application is running via GTK3. + *

+ * This does not cause GTK to load, where calls to Gtk.isGtk2 will + */ + public static volatile boolean isGtk3 = false; + + /** + * Determine if the application has *MANUALLY* loaded GTK yet or not. This does not cause GTK to load, where calls to Gtk.isLoaded will + */ + public static volatile boolean isGtkLoaded = false; + + /** + * This method is agnostic w.r.t. how GTK is loaded, which can be manually loaded or loaded via JavaFX/SWT/Swing. + * + * @return the version of GTK loaded. 0=no GTK loaded, 2=GTK2, 3=GTK3 + */ + public static + int getLoadedGtkVersion() { + // if we have ALREADY loaded GTK, then return that information. + if (isGtkLoaded) { + if (isGtk3) { + return 3; + } else { + return 2; + } + } + + if (Swt.isLoaded) { + if (Swt.isGtk3) { + return 3; + } else { + return 2; + } + } + + if (JavaFX.isLoaded) { + if (JavaFX.isGtk3) { + return 3; + } else { + return 2; + } + } + + // now check if swing has loaded GTK from the Look and Feel + return SwingUtil.getLoadedGtkVersion(); + } +} diff --git a/src/dorkbox/util/jna/linux/GtkLoader.java b/src/dorkbox/util/jna/linux/GtkLoader.java index d29a9b8..3d91391 100644 --- a/src/dorkbox/util/jna/linux/GtkLoader.java +++ b/src/dorkbox/util/jna/linux/GtkLoader.java @@ -21,7 +21,6 @@ import com.sun.jna.Function; import com.sun.jna.NativeLibrary; import dorkbox.util.OS; -import dorkbox.util.OSUtil; import dorkbox.util.Swt; import dorkbox.util.jna.JnaHelper; @@ -211,9 +210,9 @@ class GtkLoader { } // This is so that queries for the GTK version DO NOT try to load GTK - OSUtil.DesktopEnv.isGtk2 = isGtk2; - OSUtil.DesktopEnv.isGtk3 = isGtk3; - OSUtil.DesktopEnv.isGtkLoaded = isLoaded; + GtkCheck.isGtk2 = isGtk2; + GtkCheck.isGtk3 = isGtk3; + GtkCheck.isGtkLoaded = isLoaded; if (shouldLoadGtk) { if (!_isLoaded) {