macOS window button style: support NSWindowToolbarStyleUnified (availaible since macOS 11+; standard in macOS Finder, etc) to allow even larger space around close/minimize/zoom buttons

This commit is contained in:
Karl Tauber
2023-12-15 18:21:35 +01:00
parent 13528b49cb
commit a1adde0888
6 changed files with 113 additions and 34 deletions

View File

@@ -527,21 +527,6 @@ public interface FlatClientProperties
*/
String WINDOW_STYLE_SMALL = "small";
/**
* Specifies whether the window should have a large title bar.
* This adds extra space around the close/minimize/zoom buttons.
* Useful if <a href="https://www.formdev.com/flatlaf/macos/#full_window_content">full window content</a>
* is enabled.
* <p>
* (requires macOS 10.14+, Java 17+ and client property {@code apple.awt.fullWindowContent} set to {@code true})
* <p>
* <strong>Component</strong> {@link javax.swing.JRootPane}<br>
* <strong>Value type</strong> {@link java.lang.Boolean}
*
* @since 3.3
*/
String MACOS_LARGE_WINDOW_TITLE_BAR = "FlatLaf.macOS.largeWindowTitleBar";
//---- JScrollBar / JScrollPane -------------------------------------------
@@ -1278,6 +1263,46 @@ public interface FlatClientProperties
String TREE_PAINT_SELECTION = "JTree.paintSelection";
//---- macOS --------------------------------------------------------------
/**
* Specifies the style of macOS window close/minimize/zoom buttons.
* This does not change visual appearance but adds extra space around the buttons.
* Useful if <a href="https://www.formdev.com/flatlaf/macos/#full_window_content">full window content</a>
* is enabled.
* <p>
* (requires macOS 10.14+ or 11+ for style 'large', Java 17+ and client property {@code apple.awt.fullWindowContent} set to {@code true})
* <p>
* <strong>Component</strong> {@link javax.swing.JRootPane}<br>
* <strong>Value type</strong> {@link java.lang.String} or {@link java.lang.Boolean}<br>
* <strong>Allowed Values</strong>
* {@link #MACOS_WINDOW_TITLE_BAR_STYLE_MEDIUM},
* {@link #MACOS_WINDOW_TITLE_BAR_STYLE_LARGE} (requires macOS 11+) or
* {@code true} (equal to 'large')
*
* @since 3.3
*/
String MACOS_WINDOW_BUTTON_STYLE = "FlatLaf.macOS.windowButtonStyle";
/**
* Add medium space around the macOS window close/minimize/zoom buttons.
*
* @see #MACOS_WINDOW_BUTTON_STYLE
* @since 3.3
*/
String MACOS_WINDOW_BUTTON_STYLE_MEDIUM = "medium";
/**
* Add large space around the macOS window close/minimize/zoom buttons.
* <p>
* (requires macOS 11+; 'medium' is used on older systems)
*
* @see #MACOS_WINDOW_BUTTON_STYLE
* @since 3.3
*/
String MACOS_WINDOW_BUTTON_STYLE_LARGE = "large";
//---- helper methods -----------------------------------------------------
/**

View File

@@ -54,7 +54,12 @@ public class FlatNativeMacLibrary
public native static boolean setWindowRoundedBorder( Window window, float radius, float borderWidth, int borderColor );
public native static boolean setWindowToolbar( Window window, boolean hasToolbar );
public static final int
BUTTON_STYLE_DEFAULT = 0,
BUTTON_STYLE_MEDIUM = 1,
BUTTON_STYLE_LARGE = 2;
public native static boolean setWindowButtonStyle( Window window, int buttonStyle );
public native static int getWindowButtonAreaWidth( Window window );
public native static int getWindowTitleBarHeight( Window window );
public native static boolean isWindowFullScreen( Window window );

View File

@@ -368,7 +368,7 @@ public class FlatRootPaneUI
throw new IllegalComponentStateException( "The client property 'Window.style' must be set before the window becomes displayable." );
break;
case FlatClientProperties.MACOS_LARGE_WINDOW_TITLE_BAR:
case FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE:
case "ancestor":
if( SystemInfo.isMacFullWindowContentSupported &&
SystemInfo.isJava_17_orLater &&
@@ -376,10 +376,21 @@ public class FlatRootPaneUI
FlatClientProperties.clientPropertyBoolean( rootPane, "apple.awt.fullWindowContent", false ) &&
FlatNativeMacLibrary.isLoaded() )
{
int buttonStyle = FlatNativeMacLibrary.BUTTON_STYLE_DEFAULT;
Object value = rootPane.getClientProperty( FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE );
switch( String.valueOf( value ) ) {
case FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE_MEDIUM:
buttonStyle = FlatNativeMacLibrary.BUTTON_STYLE_MEDIUM;
break;
case "true":
case FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE_LARGE:
buttonStyle = FlatNativeMacLibrary.BUTTON_STYLE_LARGE;
break;
}
Window window = SwingUtilities.windowForComponent( rootPane );
boolean enabled = FlatClientProperties.clientPropertyBoolean( rootPane,
FlatClientProperties.MACOS_LARGE_WINDOW_TITLE_BAR, false );
FlatNativeMacLibrary.setWindowToolbar( window, enabled );
FlatNativeMacLibrary.setWindowButtonStyle( window, buttonStyle );
}
break;
}