Skip to content

Commit

Permalink
Native window decorations: cleaned-up/simplified JetBrains Runtime cu…
Browse files Browse the repository at this point in the history
…stom window decorations "enabled" checking:

- `FlatSystemProperties.USE_WINDOW_DECORATIONS` is now also used for JBR custom window decorations
- `FlatSystemProperties.USE_JETBRAINS_CUSTOM_DECORATIONS` is now only used to disable JBR custom window decorations; then FlatLaf native window decorations are used
- JBR custom window decorations are now disabled when running in JetBrains Projector, Webswing or WinPE
  • Loading branch information
DevCharly committed Apr 3, 2021
1 parent de1b0b1 commit 63639f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public interface FlatSystemProperties
String USE_UBUNTU_FONT = "flatlaf.useUbuntuFont";

/**
* Specifies whether FlatLaf native window decorations should be used
* Specifies whether native window decorations should be used
* when creating {@code JFrame} or {@code JDialog}.
* <p>
* Setting this to {@code true} forces using FlatLaf native window decorations
* Setting this to {@code true} forces using native window decorations
* even if they are not enabled by the application.
* <p>
* Setting this to {@code false} disables using FlatLaf native window decorations.
* Setting this to {@code false} disables using native window decorations.
* <p>
* This system property has higher priority than client property
* {@link FlatClientProperties#USE_WINDOW_DECORATIONS} and
Expand All @@ -81,15 +81,13 @@ public interface FlatSystemProperties
* <a href="https://confluence.jetbrains.com/display/JBR/JetBrains+Runtime">JetBrains Runtime</a>
* (based on OpenJDK).
* <p>
* Setting this to {@code true} forces using JetBrains Runtime custom window decorations
* even if they are not enabled by the application.
* <p>
* Setting this to {@code false} disables using JetBrains Runtime custom window decorations.
* Then FlatLaf native window decorations are used.
* <p>
* (requires Window 10)
* <p>
* <strong>Allowed Values</strong> {@code false} and {@code true}<br>
* <strong>Default</strong> none
* <strong>Default</strong> true
*/
String USE_JETBRAINS_CUSTOM_DECORATIONS = "flatlaf.useJetBrainsCustomDecorations";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,22 @@
*/
public class FlatNativeWindowBorder
{
// can use window decorations if:
// - on Windows 10
// - not when running in JetBrains Projector, Webswing or WinPE
// - not disabled via system property
private static final boolean canUseWindowDecorations =
SystemInfo.isWindows_10_orLater &&
!SystemInfo.isProjector &&
!SystemInfo.isWebswing &&
!SystemInfo.isWinPE &&
FlatSystemProperties.getBoolean( FlatSystemProperties.USE_WINDOW_DECORATIONS, true );

// check this field before using class JBRCustomDecorations to avoid unnecessary loading of that class
private static final boolean canUseJBRCustomDecorations
= SystemInfo.isJetBrainsJVM_11_orLater && SystemInfo.isWindows_10_orLater;
private static final boolean canUseJBRCustomDecorations =
canUseWindowDecorations &&
SystemInfo.isJetBrainsJVM_11_orLater &&
FlatSystemProperties.getBoolean( FlatSystemProperties.USE_JETBRAINS_CUSTOM_DECORATIONS, true );

private static Boolean supported;
private static Provider nativeProvider;
Expand Down Expand Up @@ -72,7 +85,7 @@ static Object install( JRootPane rootPane ) {
// It could be also be a window that is currently hidden, but may be shown later.
Window window = SwingUtilities.windowForComponent( rootPane );
if( window != null && window.isDisplayable() )
install( window, FlatSystemProperties.USE_WINDOW_DECORATIONS );
install( window );

// Install FlatLaf native window border, which must be done late,
// when the native window is already created, because it needs access to the window.
Expand All @@ -81,15 +94,15 @@ static Object install( JRootPane rootPane ) {
PropertyChangeListener ancestorListener = e -> {
Object newValue = e.getNewValue();
if( newValue instanceof Window )
install( (Window) newValue, FlatSystemProperties.USE_WINDOW_DECORATIONS );
install( (Window) newValue );
else if( newValue == null && e.getOldValue() instanceof Window )
uninstall( (Window) e.getOldValue() );
};
rootPane.addPropertyChangeListener( "ancestor", ancestorListener );
return ancestorListener;
}

static void install( Window window, String systemPropertyKey ) {
static void install( Window window ) {
if( hasCustomDecoration( window ) )
return;

Expand All @@ -102,7 +115,7 @@ static void install( Window window, String systemPropertyKey ) {
JRootPane rootPane = frame.getRootPane();

// check whether disabled via system property, client property or UI default
if( !useWindowDecorations( rootPane, systemPropertyKey ) )
if( !useWindowDecorations( rootPane ) )
return;

// do not enable native window border if frame is undecorated
Expand All @@ -120,7 +133,7 @@ static void install( Window window, String systemPropertyKey ) {
JRootPane rootPane = dialog.getRootPane();

// check whether disabled via system property, client property or UI default
if( !useWindowDecorations( rootPane, systemPropertyKey ) )
if( !useWindowDecorations( rootPane ) )
return;

// do not enable native window border if dialog is undecorated
Expand Down Expand Up @@ -149,7 +162,7 @@ static void uninstall( JRootPane rootPane, Object data ) {
rootPane.removePropertyChangeListener( "ancestor", (PropertyChangeListener) data );

// do not uninstall when switching to another FlatLaf theme and if still enabled
if( UIManager.getLookAndFeel() instanceof FlatLaf && useWindowDecorations( rootPane, FlatSystemProperties.USE_WINDOW_DECORATIONS ) )
if( UIManager.getLookAndFeel() instanceof FlatLaf && useWindowDecorations( rootPane ) )
return;

// uninstall native window border
Expand Down Expand Up @@ -179,9 +192,9 @@ private static void uninstall( Window window ) {
}
}

private static boolean useWindowDecorations( JRootPane rootPane, String systemPropertyKey ) {
private static boolean useWindowDecorations( JRootPane rootPane ) {
// check whether forced to enabled/disabled via system property
Boolean enabled = FlatSystemProperties.getBooleanStrict( systemPropertyKey, null );
Boolean enabled = FlatSystemProperties.getBooleanStrict( FlatSystemProperties.USE_WINDOW_DECORATIONS, null );
if( enabled != null )
return enabled;

Expand Down Expand Up @@ -243,16 +256,7 @@ private static void initialize() {
return;
supported = false;

// requires Windows 10
if( !SystemInfo.isWindows_10_orLater )
return;

// do not use when running in JetBrains Projector, Webswing or WinPE
if( SystemInfo.isProjector || SystemInfo.isWebswing || SystemInfo.isWinPE )
return;

// check whether disabled via system property
if( !FlatSystemProperties.getBoolean( FlatSystemProperties.USE_WINDOW_DECORATIONS, true ) )
if( !canUseWindowDecorations )
return;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import javax.swing.UIManager;
import javax.swing.plaf.BorderUIResource;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatSystemProperties;
import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.HiDPIUtils;
import com.formdev.flatlaf.util.SystemInfo;
Expand Down Expand Up @@ -73,7 +72,7 @@ static Object install( JRootPane rootPane ) {
// check whether root pane already has a parent, which is the case when switching LaF
Window window = SwingUtilities.windowForComponent( rootPane );
if( window != null ) {
FlatNativeWindowBorder.install( window, FlatSystemProperties.USE_JETBRAINS_CUSTOM_DECORATIONS );
FlatNativeWindowBorder.install( window );
return null;
}

Expand All @@ -89,7 +88,7 @@ public void hierarchyChanged( HierarchyEvent e ) {

Container parent = e.getChangedParent();
if( parent instanceof Window )
FlatNativeWindowBorder.install( (Window) parent, FlatSystemProperties.USE_JETBRAINS_CUSTOM_DECORATIONS );
FlatNativeWindowBorder.install( (Window) parent );

// remove listener since it is actually not possible to uninstall JBR decorations
// use invokeLater to remove listener to avoid that listener
Expand Down Expand Up @@ -165,10 +164,6 @@ private static void initialize() {
if( !SystemInfo.isJetBrainsJVM_11_orLater || !SystemInfo.isWindows_10_orLater )
return;

// check whether disabled via system property
if( !FlatSystemProperties.getBoolean( FlatSystemProperties.USE_JETBRAINS_CUSTOM_DECORATIONS, true ) )
return;

try {
Class<?> awtAcessorClass = Class.forName( "sun.awt.AWTAccessor" );
Class<?> compAccessorClass = Class.forName( "sun.awt.AWTAccessor$ComponentAccessor" );
Expand Down

0 comments on commit 63639f8

Please sign in to comment.