Merge PR #453: Add a system property to load pre-extracted native libraries from a directory

This commit is contained in:
Karl Tauber
2021-12-23 22:10:11 +01:00
3 changed files with 51 additions and 5 deletions

View File

@@ -139,6 +139,14 @@ public interface FlatSystemProperties
*/
String USE_TEXT_Y_CORRECTION = "flatlaf.useTextYCorrection";
/**
* Specifies a directory in which the native FlatLaf library have been extracted.
* This can be used to avoid extraction of the native libraries to the temporary directory at runtime.
*
* @since 2
*/
String NATIVE_LIBRARY_PATH = "flatlaf.nativeLibraryPath";
/**
* Checks whether a system property is set and returns {@code true} if its value
* is {@code "true"} (case-insensitive), otherwise it returns {@code false}.

View File

@@ -27,6 +27,7 @@ import java.awt.Window;
import java.awt.geom.AffineTransform;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
@@ -37,6 +38,7 @@ import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import com.formdev.flatlaf.FlatSystemProperties;
import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.NativeLibrary;
import com.formdev.flatlaf.util.SystemInfo;
@@ -114,11 +116,7 @@ class FlatWindowsNativeWindowBorder
}
}
String libraryName = "com/formdev/flatlaf/natives/flatlaf-windows-x86";
if( SystemInfo.isX86_64 )
libraryName += "_64";
nativeLibrary = new NativeLibrary( libraryName, null, true );
nativeLibrary = createNativeLibrary();
}
// check whether native library was successfully loaded
@@ -131,6 +129,23 @@ class FlatWindowsNativeWindowBorder
return instance;
}
private static NativeLibrary createNativeLibrary() {
String libraryName = "flatlaf-windows-x86";
if( SystemInfo.isX86_64 )
libraryName += "_64";
String libraryPath = System.getProperty( FlatSystemProperties.NATIVE_LIBRARY_PATH );
if( libraryPath != null ) {
File libraryFile = new File( libraryPath, libraryName + ".dll" );
if( libraryFile.exists() )
return new NativeLibrary( libraryFile, true );
else
LoggingFacade.INSTANCE.logSevere( "Did not find external library " + libraryFile + ", using extracted library instead", null );
}
return new NativeLibrary( "com/formdev/flatlaf/natives/" + libraryName, null, true );
}
private FlatWindowsNativeWindowBorder() {
}

View File

@@ -60,6 +60,19 @@ public class NativeLibrary
: false;
}
/**
* Load native library from given file.
*
* @param libraryFile the file of the native library
* @param supported whether the native library is supported on the current platform
* @since 2
*/
public NativeLibrary( File libraryFile, boolean supported ) {
this.loaded = supported
? loadLibraryFromFile( libraryFile )
: false;
}
/**
* Returns whether the native library is loaded.
* <p>
@@ -120,6 +133,16 @@ public class NativeLibrary
}
}
private boolean loadLibraryFromFile( File libraryFile ) {
try {
System.load( libraryFile.getPath() );
return true;
} catch( Throwable ex ) {
log( null, ex );
return false;
}
}
private static String decorateLibraryName( String libraryName ) {
if( SystemInfo.isWindows )
return libraryName.concat( ".dll" );