The system tray and GTK backend no longer have a hard-dependency on SWT/JavaFX modules. They now use a "plugin" type of system to provide a "renderer", which is used for various determination and dispatch tasks.
This repository only serves now as an EXAMPLE of a correct provider.master
parent
46500e8b26
commit
0edf0a688b
@ -0,0 +1,22 @@
|
||||
BSD License
|
||||
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL <ORGANIZATION> BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -0,0 +1,26 @@
|
||||
BSD License
|
||||
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of the <ORGANIZATION> nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software
|
||||
without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL <ORGANIZATION> BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -0,0 +1,121 @@
|
||||
Creative Commons Legal Code
|
||||
|
||||
CC0 1.0 Universal
|
||||
|
||||
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
||||
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
||||
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
||||
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
||||
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
||||
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
||||
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
||||
HEREUNDER.
|
||||
|
||||
Statement of Purpose
|
||||
|
||||
The laws of most jurisdictions throughout the world automatically confer
|
||||
exclusive Copyright and Related Rights (defined below) upon the creator
|
||||
and subsequent owner(s) (each and all, an "owner") of an original work of
|
||||
authorship and/or a database (each, a "Work").
|
||||
|
||||
Certain owners wish to permanently relinquish those rights to a Work for
|
||||
the purpose of contributing to a commons of creative, cultural and
|
||||
scientific works ("Commons") that the public can reliably and without fear
|
||||
of later claims of infringement build upon, modify, incorporate in other
|
||||
works, reuse and redistribute as freely as possible in any form whatsoever
|
||||
and for any purposes, including without limitation commercial purposes.
|
||||
These owners may contribute to the Commons to promote the ideal of a free
|
||||
culture and the further production of creative, cultural and scientific
|
||||
works, or to gain reputation or greater distribution for their Work in
|
||||
part through the use and efforts of others.
|
||||
|
||||
For these and/or other purposes and motivations, and without any
|
||||
expectation of additional consideration or compensation, the person
|
||||
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
||||
is an owner of Copyright and Related Rights in the Work, voluntarily
|
||||
elects to apply CC0 to the Work and publicly distribute the Work under its
|
||||
terms, with knowledge of his or her Copyright and Related Rights in the
|
||||
Work and the meaning and intended legal effect of CC0 on those rights.
|
||||
|
||||
1. Copyright and Related Rights. A Work made available under CC0 may be
|
||||
protected by copyright and related or neighboring rights ("Copyright and
|
||||
Related Rights"). Copyright and Related Rights include, but are not
|
||||
limited to, the following:
|
||||
|
||||
i. the right to reproduce, adapt, distribute, perform, display,
|
||||
communicate, and translate a Work;
|
||||
ii. moral rights retained by the original author(s) and/or performer(s);
|
||||
iii. publicity and privacy rights pertaining to a person's image or
|
||||
likeness depicted in a Work;
|
||||
iv. rights protecting against unfair competition in regards to a Work,
|
||||
subject to the limitations in paragraph 4(a), below;
|
||||
v. rights protecting the extraction, dissemination, use and reuse of data
|
||||
in a Work;
|
||||
vi. database rights (such as those arising under Directive 96/9/EC of the
|
||||
European Parliament and of the Council of 11 March 1996 on the legal
|
||||
protection of databases, and under any national implementation
|
||||
thereof, including any amended or successor version of such
|
||||
directive); and
|
||||
vii. other similar, equivalent or corresponding rights throughout the
|
||||
world based on applicable law or treaty, and any national
|
||||
implementations thereof.
|
||||
|
||||
2. Waiver. To the greatest extent permitted by, but not in contravention
|
||||
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
||||
irrevocably and unconditionally waives, abandons, and surrenders all of
|
||||
Affirmer's Copyright and Related Rights and associated claims and causes
|
||||
of action, whether now known or unknown (including existing as well as
|
||||
future claims and causes of action), in the Work (i) in all territories
|
||||
worldwide, (ii) for the maximum duration provided by applicable law or
|
||||
treaty (including future time extensions), (iii) in any current or future
|
||||
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
||||
including without limitation commercial, advertising or promotional
|
||||
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
||||
member of the public at large and to the detriment of Affirmer's heirs and
|
||||
successors, fully intending that such Waiver shall not be subject to
|
||||
revocation, rescission, cancellation, termination, or any other legal or
|
||||
equitable action to disrupt the quiet enjoyment of the Work by the public
|
||||
as contemplated by Affirmer's express Statement of Purpose.
|
||||
|
||||
3. Public License Fallback. Should any part of the Waiver for any reason
|
||||
be judged legally invalid or ineffective under applicable law, then the
|
||||
Waiver shall be preserved to the maximum extent permitted taking into
|
||||
account Affirmer's express Statement of Purpose. In addition, to the
|
||||
extent the Waiver is so judged Affirmer hereby grants to each affected
|
||||
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
||||
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
||||
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
||||
maximum duration provided by applicable law or treaty (including future
|
||||
time extensions), (iii) in any current or future medium and for any number
|
||||
of copies, and (iv) for any purpose whatsoever, including without
|
||||
limitation commercial, advertising or promotional purposes (the
|
||||
"License"). The License shall be deemed effective as of the date CC0 was
|
||||
applied by Affirmer to the Work. Should any part of the License for any
|
||||
reason be judged legally invalid or ineffective under applicable law, such
|
||||
partial invalidity or ineffectiveness shall not invalidate the remainder
|
||||
of the License, and in such case Affirmer hereby affirms that he or she
|
||||
will not (i) exercise any of his or her remaining Copyright and Related
|
||||
Rights in the Work or (ii) assert any associated claims and causes of
|
||||
action with respect to the Work, in either case contrary to Affirmer's
|
||||
express Statement of Purpose.
|
||||
|
||||
4. Limitations and Disclaimers.
|
||||
|
||||
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
||||
surrendered, licensed or otherwise affected by this document.
|
||||
b. Affirmer offers the Work as-is and makes no representations or
|
||||
warranties of any kind concerning the Work, express, implied,
|
||||
statutory or otherwise, including without limitation warranties of
|
||||
title, merchantability, fitness for a particular purpose, non
|
||||
infringement, or the absence of latent or other defects, accuracy, or
|
||||
the present or absence of errors, whether or not discoverable, all to
|
||||
the greatest extent permissible under applicable law.
|
||||
c. Affirmer disclaims responsibility for clearing rights of other persons
|
||||
that may apply to the Work or any use thereof, including without
|
||||
limitation any person's Copyright and Related Rights in the Work.
|
||||
Further, Affirmer disclaims responsibility for obtaining any necessary
|
||||
consents, permissions or other rights required for any use of the
|
||||
Work.
|
||||
d. Affirmer understands and acknowledges that Creative Commons is not a
|
||||
party to this document and has no duty or obligation with respect to
|
||||
this CC0 or use of the Work.
|
@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 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.javaFx;
|
||||
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import dorkbox.swt.Swt;
|
||||
|
||||
/**
|
||||
* Utility methods for JavaFX.
|
||||
*/
|
||||
public
|
||||
class JavaFx {
|
||||
public final static boolean isLoaded;
|
||||
public final static boolean isGtk3;
|
||||
|
||||
|
||||
static {
|
||||
// There is a silly amount of redirection, simply because we have to be able to access JavaFX, but only if it's in use.
|
||||
// Since this class is the place other code interacts with, we can use JavaFX stuff if necessary without loading/linking
|
||||
// the JavaFX classes by accident
|
||||
|
||||
// We cannot use getToolkit(), because if JavaFX is not being used, calling getToolkit() will initialize it...
|
||||
// see: https://bugs.openjdk.java.net/browse/JDK-8090933
|
||||
|
||||
String fullJavaVersion = System.getProperty("java.version", "");
|
||||
boolean isJava8 = fullJavaVersion.startsWith("1.") && fullJavaVersion.charAt(2) == '8';
|
||||
|
||||
Class<?> javaFxLoggerClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
|
||||
@Override
|
||||
public
|
||||
Class<?> run() {
|
||||
try {
|
||||
return Class.forName("com.sun.javafx.logging.PlatformLogger", true, ClassLoader.getSystemClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
return Class.forName("com.sun.javafx.logging.PlatformLogger", true, Thread.currentThread().getContextClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
boolean isJavaFxLoaded_ = false;
|
||||
try {
|
||||
if (javaFxLoggerClass != null) {
|
||||
// try to avoid using reflection!
|
||||
|
||||
// first, walk the stack to see if any JavaFX classes are used (which indicate that we are running with JavaFX)
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
for (int i = 0; i < stackTrace.length; i++) {
|
||||
if (stackTrace[i].getClassName().contains("javafx")) {
|
||||
isJavaFxLoaded_ = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// this is important to use reflection, because if JavaFX is not being used, calling getToolkit() will initialize it...
|
||||
if (!isJavaFxLoaded_) {
|
||||
// stack walking failed, resort to using a property (emit a warning)
|
||||
String usingJavFx = System.getProperty("usingJavaFx", "false");
|
||||
if (!usingJavFx.equals("false")) {
|
||||
// if it is set to ANYTHING other than exactly "false", then we are using javaFX. Skip reflection testing
|
||||
isJavaFxLoaded_ = true;
|
||||
} else {
|
||||
// stack walking and property check failed, resort to using reflection.
|
||||
// WE DON'T always want to do this BECAUSE Java9+ really doesn't like reflection!
|
||||
LoggerFactory.getLogger(JavaFx.class).debug("Using reflection to detect if JavaFX is loaded.\n" +
|
||||
"To avoid using reflection, set the system property \"usingJavaFx\" to \"true\" before your application starts. " +
|
||||
"For example: System.setProperty(\"usingJavaFx\", \"true\");");
|
||||
|
||||
Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
|
||||
m.setAccessible(true);
|
||||
ClassLoader cl = ClassLoader.getSystemClassLoader();
|
||||
|
||||
isJavaFxLoaded_ = (null != m.invoke(cl, "com.sun.javafx.tk.Toolkit")) || (null != m.invoke(cl, "javafx.application.Application"));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
LoggerFactory.getLogger(JavaFx.class).debug("Error detecting if JavaFX is loaded", e);
|
||||
}
|
||||
|
||||
boolean isJavaFxGtk3_ = false;
|
||||
if (isJavaFxLoaded_) {
|
||||
// JavaFX Java7,8 is GTK2 only. Java9 can MAYBE have it be GTK3 if `-Djdk.gtk.version=3` is specified
|
||||
// see
|
||||
// http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-May/019100.html
|
||||
// https://docs.oracle.com/javafx/2/system_requirements_2-2-3/jfxpub-system_requirements_2-2-3.htm
|
||||
// from the page: JavaFX 2.2.3 for Linux requires gtk2 2.18+.
|
||||
|
||||
// HILARIOUSLY enough, you can use JavaFX + SWT..... And the javaFX GTK version info SHOULD
|
||||
// be based on what SWT has loaded
|
||||
|
||||
// https://github.com/teamfx/openjfx-9-dev-rt/blob/master/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java
|
||||
|
||||
if (isJava8) {
|
||||
// JavaFX from Oracle Java 8 is GTK2 only. Java9 can have it be GTK3 if -Djdk.gtk.version=3 is specified
|
||||
// see http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-May/019100.html
|
||||
isJavaFxGtk3_ = false;
|
||||
} else {
|
||||
// Only possible Java9+ (so our case, Java11+ since 9 is no longer available, 11 is officially LTS)
|
||||
if (Swt.isLoaded && !Swt.isGtk3) {
|
||||
isJavaFxGtk3_ = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
|
||||
@Override
|
||||
public
|
||||
Boolean run() {
|
||||
String version = System.getProperty("jdk.gtk.version", "2");
|
||||
return "3".equals(version) || version.startsWith("3.");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isLoaded = isJavaFxLoaded_;
|
||||
isGtk3 = isJavaFxGtk3_;
|
||||
}
|
||||
|
||||
public static
|
||||
void dispatch(final Runnable runnable) {
|
||||
JavaFxAccess.dispatch(runnable);
|
||||
}
|
||||
|
||||
public static
|
||||
boolean isEventThread() {
|
||||
return JavaFxAccess.isEventThread();
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 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.javaFx;
|
||||
|
||||
/**
|
||||
* Utility methods for JavaFX.
|
||||
*/
|
||||
public
|
||||
class JavaFxAccess {
|
||||
static
|
||||
void dispatch(final Runnable runnable) {
|
||||
javafx.application.Platform.runLater(runnable);
|
||||
}
|
||||
|
||||
static
|
||||
boolean isEventThread() {
|
||||
// JAVA 8
|
||||
return com.sun.javafx.tk.Toolkit.getToolkit().isFxUserThread();
|
||||
}
|
||||
}
|
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2021 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.javaFx;
|
||||
|
||||
import dorkbox.jna.rendering.ProviderType;
|
||||
import dorkbox.jna.rendering.Renderer;
|
||||
import dorkbox.os.OS;
|
||||
|
||||
|
||||
public
|
||||
class JavaFxProvider implements Renderer {
|
||||
@Override
|
||||
public
|
||||
boolean isSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
ProviderType getType() {
|
||||
return ProviderType.JAVAFX;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean alreadyRunning() {
|
||||
// this is only true for SWT. JavaFX running detection is elsewhere
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean isEventThread() {
|
||||
return javafx.application.Platform.isFxApplicationThread();
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
int getGtkVersion() {
|
||||
if (!OS.isLinux()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// JavaFX Java7,8 is GTK2 only. Java9 can MAYBE have it be GTK3 if `-Djdk.gtk.version=3` is specified
|
||||
// see
|
||||
// http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-May/019100.html
|
||||
// https://docs.oracle.com/javafx/2/system_requirements_2-2-3/jfxpub-system_requirements_2-2-3.htm
|
||||
// from the page: JavaFX 2.2.3 for Linux requires gtk2 2.18+.
|
||||
|
||||
// HILARIOUSLY enough, you can use JavaFX + SWT..... And the javaFX GTK version info SHOULD
|
||||
// be based on what SWT has loaded
|
||||
|
||||
// https://github.com/teamfx/openjfx-9-dev-rt/blob/master/modules/javafx.graphics/src/main/java/com/sun/glass/ui/gtk/GtkApplication.java
|
||||
|
||||
if (OS.javaVersion < 9) {
|
||||
// JavaFX from Oracle Java 8 is GTK2 only. Java9 can have it be GTK3 if -Djdk.gtk.version=3 is specified
|
||||
// see http://mail.openjdk.java.net/pipermail/openjfx-dev/2016-May/019100.html
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
// Only possible Java9+ (so our case, Java11+ since 9 is no longer available, 11 is officially LTS)
|
||||
String version = OS.getProperty("jdk.gtk.version", "2");
|
||||
if ("3".equals(version) || version.startsWith("3.")) {
|
||||
return 3;
|
||||
}
|
||||
else {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean dispatch(final Runnable runnable) {
|
||||
// JavaFX only
|
||||
if (isEventThread()) {
|
||||
// Run directly on the JavaFX event thread
|
||||
runnable.run();
|
||||
}
|
||||
else {
|
||||
javafx.application.Platform.runLater(runnable);
|
||||
}
|
||||
|
||||
// javaFX always manages the runnable
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 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.swt;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
/**
|
||||
* Utility methods for SWT. SWT is always available for compiling, so it is not necessary to use reflection to compile it.
|
||||
* <p>
|
||||
* SWT system tray types are GtkStatusIcon trays (so we don't want to use them)
|
||||
*/
|
||||
public
|
||||
class Swt {
|
||||
public final static boolean isLoaded;
|
||||
public final static boolean isGtk3;
|
||||
private static final int version;
|
||||
|
||||
// NOTE: This class cannot have SWT **ANYTHING** in it
|
||||
static {
|
||||
// There is a silly amount of redirection, simply because we have to be able to access SWT, but only if it's in use.
|
||||
|
||||
// Since this class is the place other code interacts with, we can use SWT stuff if necessary without loading/linking
|
||||
// the SWT classes by accident
|
||||
|
||||
Class<?> swtErrorClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
|
||||
@Override
|
||||
public
|
||||
Class<?> run() {
|
||||
try {
|
||||
return Class.forName("org.eclipse.swt.SWTError", true, ClassLoader.getSystemClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
try {
|
||||
return Class.forName("org.eclipse.swt.SWTError", true, Thread.currentThread().getContextClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
if (swtErrorClass != null && SwtAccess.isLoadable()) {
|
||||
// this means that SWT is available in the system at runtime. We use the error class because that DOES NOT initialize anything
|
||||
version = SwtAccess.getVersion();
|
||||
isLoaded = true;
|
||||
isGtk3 = SwtAccess.isGtk3();
|
||||
SwtAccess.init();
|
||||
} else {
|
||||
version = 0;
|
||||
isLoaded = false;
|
||||
isGtk3 = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static
|
||||
int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public static
|
||||
void dispatch(final Runnable runnable) {
|
||||
SwtAccess.dispatch(runnable);
|
||||
}
|
||||
|
||||
public static
|
||||
boolean isEventThread() {
|
||||
return SwtAccess.isEventThread();
|
||||
}
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
package dorkbox.swt;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
|
||||
public
|
||||
class SwtAccess {
|
||||
private static Display currentDisplay = null;
|
||||
private static Thread currentDisplayThread = null;
|
||||
|
||||
public static
|
||||
void init() {
|
||||
// we MUST save this on init, otherwise it is "null" when methods are run from the swing EDT.
|
||||
currentDisplay = org.eclipse.swt.widgets.Display.getCurrent();
|
||||
currentDisplayThread = currentDisplay.getThread();
|
||||
}
|
||||
|
||||
static
|
||||
boolean isLoadable() {
|
||||
return org.eclipse.swt.SWT.isLoadable();
|
||||
}
|
||||
|
||||
static
|
||||
void onShutdown(final org.eclipse.swt.widgets.Display currentDisplay, final Runnable runnable) {
|
||||
// currentDisplay.getShells() must only be called inside the event thread!
|
||||
|
||||
org.eclipse.swt.widgets.Shell shell = currentDisplay.getShells()[0];
|
||||
shell.addListener(org.eclipse.swt.SWT.Close, new org.eclipse.swt.widgets.Listener() {
|
||||
@Override
|
||||
public
|
||||
void handleEvent(final org.eclipse.swt.widgets.Event event) {
|
||||
runnable.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static
|
||||
int getVersion() {
|
||||
return SWT.getVersion();
|
||||
}
|
||||
|
||||
|
||||
private static
|
||||
int getViaReflection(String clazz) throws InvocationTargetException, IllegalAccessException {
|
||||
final Class<?> osClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
|
||||
@Override
|
||||
public
|
||||
Class<?> run() {
|
||||
try {
|
||||
return Class.forName(clazz, true, ClassLoader.getSystemClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
try {
|
||||
return Class.forName(clazz, true, Thread.currentThread().getContextClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
Method method = AccessController.doPrivileged(new PrivilegedAction<Method>() {
|
||||
@Override
|
||||
public
|
||||
Method run() {
|
||||
try {
|
||||
return osClass.getMethod("gtk_major_version");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// might throw an exception!
|
||||
return ((Number)method.invoke(osClass)).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is only necessary for linux.
|
||||
*
|
||||
* @return true if SWT is GTK3. False if SWT is GTK2 or unknown.
|
||||
*/
|
||||
static boolean isGtk3() {
|
||||
boolean isLinux = System.getProperty("os.name", "").toLowerCase(Locale.US).startsWith("linux");
|
||||
if (!isLinux) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return org.eclipse.swt.internal.gtk.GTK.gtk_get_major_version() == 3;
|
||||
} catch (Exception e) {
|
||||
// this might be an older/different version of SWT! Try reflection.
|
||||
try {
|
||||
return getViaReflection("org.eclipse.swt.internal.gtk.GTK") == 3;
|
||||
} catch (Exception e1) {
|
||||
try {
|
||||
return getViaReflection("org.eclipse.swt.internal.gtk.OS") == 3;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static
|
||||
void dispatch(final Runnable runnable) {
|
||||
currentDisplay.syncExec(runnable);
|
||||
}
|
||||
|
||||
static
|
||||
boolean isEventThread() {
|
||||
return Thread.currentThread() == currentDisplayThread;
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2021 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.swt;
|
||||
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
|
||||
import dorkbox.jna.rendering.ProviderType;
|
||||
import dorkbox.jna.rendering.Renderer;
|
||||
import dorkbox.os.OS;
|
||||
|
||||
public class SwtProvider implements Renderer {
|
||||
private static Display currentDisplay = null;
|
||||
private static Thread currentDisplayThread = null;
|
||||
|
||||
static {
|
||||
// we MUST save this on init, otherwise it is "null" when methods are run from the swing EDT.
|
||||
currentDisplay = org.eclipse.swt.widgets.Display.getCurrent();
|
||||
currentDisplayThread = currentDisplay.getThread();
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean isSupported() {
|
||||
// versions of SWT older than v4.4, are INCOMPATIBLE with us.
|
||||
// Of note, v4.3 is the "last released" version of SWT by eclipse AND IT WILL NOT WORK!!
|
||||
// for NEWER versions of SWT via maven, use http://maven-eclipse.github.io/maven
|
||||
return SWT.getVersion() > 4430;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
ProviderType getType() {
|
||||
return ProviderType.SWT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean alreadyRunning() {
|
||||
// ONLY true for SWT. JavaFX/etc are checked elsewhere
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean isEventThread() {
|
||||
return Thread.currentThread() == currentDisplayThread;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
int getGtkVersion() {
|
||||
if (!OS.isLinux()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Swt has a property that tells us the version information
|
||||
// "org.eclipse.swt.internal.gtk.version=3.12.2"
|
||||
// Only possible Java9+ (so our case, Java11+ since 9 is no longer available, 11 is officially LTS)
|
||||
String version = OS.getProperty("org.eclipse.swt.internal.gtk.version", "2");
|
||||
if ("3".equals(version) || version.startsWith("3.")) {
|
||||
return 3;
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
boolean dispatch(final Runnable runnable) {
|
||||
if (isEventThread()) {
|
||||
// Run directly on the SWT event thread. If it's not on the dispatch thread, we will use the DEFAULT GTK to put it there.
|
||||
// We do not need to dispatch manually onto swt??
|
||||
runnable.run();
|
||||
|
||||
// we only directly run if we are SWT, and in SWTs event thread
|
||||
return true;
|
||||
}
|
||||
|
||||
// let the default handler manage it
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
module dorkbox.swtjavafx {
|
||||
exports dorkbox.javaFx;
|
||||
exports dorkbox.swt;
|
||||
|
||||
requires dorkbox.utilities;
|
||||
|
||||
requires javafx.graphics;
|
||||
|
||||
// 32-bit support was dropped by eclipse since 4.10 (3.108.0 is the oldest that is 32 bit)
|
||||
// this must match what your release target and SWT dependency is
|
||||
requires org.eclipse.swt.gtk.linux.x86_64;
|
||||
// requires static org.eclipse.swt.win32.win32.x86_64;
|
||||
// requires static org.eclipse.swt.cocoa.macosx.x86_64;
|
||||
|
||||
requires java.base;
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 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.javaFx;
|
||||
|
||||
/**
|
||||
* Utility methods for JavaFX.
|
||||
*/
|
||||
public
|
||||
class JavaFxAccess {
|
||||
static
|
||||
void dispatch(final Runnable runnable) {
|
||||
javafx.application.Platform.runLater(runnable);
|
||||
}
|
||||
|
||||
static
|
||||
boolean isEventThread() {
|
||||
// JAVA 9+
|
||||
return javafx.application.Platform.isFxApplicationThread();
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright 2021 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.swt;
|
||||
|
||||
/**
|
||||
* Required for intellij to not complain regarding `module-info` for a multi-release jar.
|
||||
* This file is completely ignored by the gradle build process
|
||||
*/
|
||||
public
|
||||
class EmptyClass {}
|
Loading…
Reference in new issue