macOS large title bar: main JToolBar automatically:

- uses height of macOS window title bar
- adds left insets for close/minimize/zoom buttons (except if full screen, where those buttons are hidden)
This commit is contained in:
Karl Tauber
2023-12-11 15:05:37 +01:00
parent f40baed65e
commit ea2447dcb7
7 changed files with 292 additions and 11 deletions

View File

@@ -54,5 +54,8 @@ public class FlatNativeMacLibrary
public native static boolean setWindowRoundedBorder( Window window, float radius, float borderWidth, int borderColor ); public native static boolean setWindowRoundedBorder( Window window, float radius, float borderWidth, int borderColor );
public native static void setWindowToolbar( Window window ); public native static void setWindowToolbar( Window window, boolean hasToolbar );
public native static int getWindowButtonAreaWidth( Window window );
public native static int getWindowTitleBarHeight( Window window );
public native static boolean isWindowFullScreen( Window window );
} }

View File

@@ -25,6 +25,7 @@ import java.awt.Rectangle;
import java.util.function.Function; import java.util.function.Function;
import javax.swing.JToolBar; import javax.swing.JToolBar;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.ToolBarUI; import javax.swing.plaf.ToolBarUI;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
@@ -114,6 +115,22 @@ public class FlatToolBarBorder
insets.top += gripInset; insets.top += gripInset;
} }
// on macOS, add some extra space to left side for close/minimize/zoom buttons (if necessary)
if( c instanceof JToolBar &&
FlatToolBarUI.isMacOSMainToolbar( (JToolBar) c ) &&
(!FlatNativeMacLibrary.isLoaded() ||
!FlatNativeMacLibrary.isWindowFullScreen( SwingUtilities.windowForComponent( c ) )) )
{
// get button area width from macOS
int buttonBarWidth = FlatNativeMacLibrary.isLoaded()
? FlatNativeMacLibrary.getWindowButtonAreaWidth( SwingUtilities.windowForComponent( c ) )
: -1;
if( buttonBarWidth < 0 )
buttonBarWidth = 68; // default width if NSWindow does not have a toolbar
insets.left += buttonBarWidth;
}
return insets; return insets;
} }

View File

@@ -19,35 +19,45 @@ package com.formdev.flatlaf.ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Container; import java.awt.Container;
import java.awt.Dimension;
import java.awt.FocusTraversalPolicy; import java.awt.FocusTraversalPolicy;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.LayoutManager2;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ContainerEvent; import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener; import java.awt.event.ContainerListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Map; import java.util.Map;
import javax.swing.AbstractButton; import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup; import javax.swing.ButtonGroup;
import javax.swing.ButtonModel; import javax.swing.ButtonModel;
import javax.swing.DefaultButtonModel; import javax.swing.DefaultButtonModel;
import javax.swing.InputMap; import javax.swing.InputMap;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.JToolBar; import javax.swing.JToolBar;
import javax.swing.LayoutFocusTraversalPolicy; import javax.swing.LayoutFocusTraversalPolicy;
import javax.swing.RootPaneContainer; import javax.swing.RootPaneContainer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.border.Border; import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicToolBarUI; import javax.swing.plaf.basic.BasicToolBarUI;
import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable; import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI; import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
import com.formdev.flatlaf.util.LoggingFacade; import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.SystemInfo;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
/** /**
@@ -148,6 +158,12 @@ public class FlatToolBarUI
toolBar.setFloatable( false ); toolBar.setFloatable( false );
} else } else
oldFloatable = null; oldFloatable = null;
// layout manager
LayoutManager layout = createLayout();
toolBar.setLayout( layout );
if( layout instanceof PropertyChangeListener )
toolBar.addPropertyChangeListener( (PropertyChangeListener) layout );
} }
@Override @Override
@@ -160,6 +176,8 @@ public class FlatToolBarUI
toolBar.setFloatable( oldFloatable ); toolBar.setFloatable( oldFloatable );
oldFloatable = null; oldFloatable = null;
} }
toolBar.setLayout( null );
} }
@Override @Override
@@ -453,6 +471,137 @@ public class FlatToolBarUI
: null; : null;
} }
/** @since 3.3 */
protected LayoutManager createLayout() {
return new FlatToolBarLayoutManager();
}
/**
* Returns whether the given toolbar is used in window titlebar on macOS.
* <p>
* Returns {@code true} if:
* <ul>
* <li>running on macOS
* <li>Java supports "full window content"
* <li>"full window content" is enabled for window
* <li>toolbar orientation is horizontal
* <li>toolbar is located at {@code 0,0} in window
* </ul>
*
* @since 3.3
*/
public static boolean isMacOSMainToolbar( JToolBar toolBar ) {
if( !SystemInfo.isMacFullWindowContentSupported ||
toolBar.getOrientation() != JToolBar.HORIZONTAL ||
toolBar.getX() != 0 ||
toolBar.getY() != 0 )
return false;
JRootPane rootPane = SwingUtilities.getRootPane( toolBar );
if( rootPane == null )
return false;
if( !FlatClientProperties.clientPropertyBoolean( rootPane, "apple.awt.fullWindowContent", false ) )
return false;
for( Component p = toolBar.getParent(); p != null && !(p instanceof Window); p = p.getParent() ) {
if( p.getX() != 0 || p.getY() != 0 )
return false;
}
return true;
}
//---- class FlatToolBarLayoutManager -------------------------------------
/**
* @since 3.3
*/
protected class FlatToolBarLayoutManager
implements LayoutManager2, PropertyChangeListener, UIResource
{
private BoxLayout delegate;
FlatToolBarLayoutManager() {
initBoxLayout();
}
private void initBoxLayout() {
delegate = new BoxLayout( toolBar, (toolBar.getOrientation() == JToolBar.HORIZONTAL)
? BoxLayout.LINE_AXIS : BoxLayout.PAGE_AXIS );
}
@Override
public void addLayoutComponent( Component comp, Object constraints ) {
delegate.addLayoutComponent( comp, constraints );
}
@Override
public void addLayoutComponent( String name, Component comp ) {
delegate.addLayoutComponent( name, comp );
}
@Override
public void removeLayoutComponent( Component comp ) {
delegate.removeLayoutComponent( comp );
}
@Override
public Dimension preferredLayoutSize( Container parent ) {
return minimumHeightOnMacOS( delegate.preferredLayoutSize( parent ) );
}
@Override
public Dimension minimumLayoutSize( Container parent ) {
return minimumHeightOnMacOS( delegate.minimumLayoutSize( parent ) );
}
@Override
public Dimension maximumLayoutSize( Container target ) {
return minimumHeightOnMacOS( delegate.maximumLayoutSize( target ) );
}
private Dimension minimumHeightOnMacOS( Dimension size ) {
if( isMacOSMainToolbar( toolBar ) ) {
// get title bar height from macOS
int titleBarHeight = FlatNativeMacLibrary.isLoaded()
? FlatNativeMacLibrary.getWindowTitleBarHeight( SwingUtilities.windowForComponent( toolBar ) )
: -1;
if( titleBarHeight < 0 )
titleBarHeight = 28; // default height if NSWindow does not have a toolbar
size.height = Math.max( size.height, titleBarHeight );
}
return size;
}
@Override
public void layoutContainer( Container parent ) {
delegate.layoutContainer( parent );
}
@Override
public void invalidateLayout( Container target ) {
delegate.invalidateLayout( target );
}
@Override
public float getLayoutAlignmentX( Container target ) {
return delegate.getLayoutAlignmentX( target );
}
@Override
public float getLayoutAlignmentY( Container target ) {
return delegate.getLayoutAlignmentY( target );
}
@Override
public void propertyChange( PropertyChangeEvent e ) {
if( "orientation".equals( e.getPropertyName() ) )
initBoxLayout();
}
}
//---- class FlatToolBarFocusTraversalPolicy ------------------------------ //---- class FlatToolBarFocusTraversalPolicy ------------------------------
/** /**

View File

@@ -46,6 +46,7 @@ import com.formdev.flatlaf.icons.FlatAbstractIcon;
import com.formdev.flatlaf.themes.FlatMacDarkLaf; import com.formdev.flatlaf.themes.FlatMacDarkLaf;
import com.formdev.flatlaf.themes.FlatMacLightLaf; import com.formdev.flatlaf.themes.FlatMacLightLaf;
import com.formdev.flatlaf.extras.FlatSVGUtils; import com.formdev.flatlaf.extras.FlatSVGUtils;
import com.formdev.flatlaf.ui.FlatNativeMacLibrary;
import com.formdev.flatlaf.util.ColorFunctions; import com.formdev.flatlaf.util.ColorFunctions;
import com.formdev.flatlaf.util.FontUtils; import com.formdev.flatlaf.util.FontUtils;
import com.formdev.flatlaf.util.LoggingFacade; import com.formdev.flatlaf.util.LoggingFacade;
@@ -99,9 +100,6 @@ class DemoFrame
getRootPane().putClientProperty( "apple.awt.windowTitleVisible", false ); getRootPane().putClientProperty( "apple.awt.windowTitleVisible", false );
else else
setTitle( null ); setTitle( null );
// add gap to left side of toolbar
toolBar.add( Box.createHorizontalStrut( 80 ), 0 );
} }
// enable full screen mode for this window (for Java 8 - 10; not necessary for Java 11+) // enable full screen mode for this window (for Java 8 - 10; not necessary for Java 11+)
@@ -903,8 +901,15 @@ class DemoFrame
buttonGroup1.add(radioButtonMenuItem3); buttonGroup1.add(radioButtonMenuItem3);
// JFormDesigner - End of component initialization //GEN-END:initComponents // JFormDesigner - End of component initialization //GEN-END:initComponents
backButton.addActionListener( e -> System.out.println( e ) ); backButton.addActionListener( e -> {
backButton.addMouseListener( new MouseListener() { FlatNativeMacLibrary.setWindowToolbar( this, true );
});
forwardButton.addActionListener( e -> {
FlatNativeMacLibrary.setWindowToolbar( this, false );
});
cutButton.addActionListener( e -> System.out.println( e ) );
cutButton.addMouseListener( new MouseListener() {
@Override @Override
public void mouseReleased( MouseEvent e ) { public void mouseReleased( MouseEvent e ) {
@@ -936,7 +941,7 @@ class DemoFrame
System.out.println( "m click" ); System.out.println( "m click" );
} }
} ); } );
backButton.addMouseMotionListener( new MouseMotionListener() { cutButton.addMouseMotionListener( new MouseMotionListener() {
@Override @Override
public void mouseMoved( MouseEvent e ) { public void mouseMoved( MouseEvent e ) {

View File

@@ -120,7 +120,7 @@ public class FlatLafDemo
frame.setLocationRelativeTo( null ); frame.setLocationRelativeTo( null );
if( SystemInfo.isMacOS && FlatNativeMacLibrary.isLoaded() ) { if( SystemInfo.isMacOS && FlatNativeMacLibrary.isLoaded() ) {
// TODO use client property // TODO use client property
FlatNativeMacLibrary.setWindowToolbar( frame ); FlatNativeMacLibrary.setWindowToolbar( frame, true );
} }
frame.setVisible( true ); frame.setVisible( true );
} ); } );

View File

@@ -18,9 +18,33 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setW
/* /*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary * Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: setWindowToolbar * Method: setWindowToolbar
* Signature: (Ljava/awt/Window;)V * Signature: (Ljava/awt/Window;Z)V
*/ */
JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindowToolbar JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindowToolbar
(JNIEnv *, jclass, jobject, jboolean);
/*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: getWindowButtonAreaWidth
* Signature: (Ljava/awt/Window;)I
*/
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowButtonAreaWidth
(JNIEnv *, jclass, jobject);
/*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: getWindowTitleBarHeight
* Signature: (Ljava/awt/Window;)I
*/
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowTitleBarHeight
(JNIEnv *, jclass, jobject);
/*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: isWindowFullScreen
* Signature: (Ljava/awt/Window;)Z
*/
JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_isWindowFullScreen
(JNIEnv *, jclass, jobject); (JNIEnv *, jclass, jobject);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -90,7 +90,7 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setW
extern "C" extern "C"
JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindowToolbar JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindowToolbar
( JNIEnv* env, jclass cls, jobject window ) ( JNIEnv* env, jclass cls, jobject window, jboolean hasToolbar )
{ {
JNI_COCOA_ENTER() JNI_COCOA_ENTER()
@@ -101,7 +101,11 @@ JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindo
[FlatJNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){ [FlatJNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
NSLog( @"\n%@\n\n", [nsWindow.contentView.superview _subtreeDescription] ); NSLog( @"\n%@\n\n", [nsWindow.contentView.superview _subtreeDescription] );
NSToolbar* toolbar = [NSToolbar new]; NSToolbar* toolbar = NULL;
if( hasToolbar ) {
toolbar = [NSToolbar new];
toolbar.showsBaselineSeparator = NO; // necessary for older macOS versions
}
nsWindow.toolbar = toolbar; nsWindow.toolbar = toolbar;
// TODO handle fullscreen // TODO handle fullscreen
@@ -111,3 +115,82 @@ JNIEXPORT void JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setWindo
JNI_COCOA_EXIT() JNI_COCOA_EXIT()
} }
extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowButtonAreaWidth
( JNIEnv* env, jclass cls, jobject window )
{
JNI_COCOA_ENTER()
NSWindow* nsWindow = getNSWindow( env, cls, window );
if( nsWindow == NULL )
return -1;
// get buttons
NSView* buttons[3] = {
[nsWindow standardWindowButton:NSWindowCloseButton],
[nsWindow standardWindowButton:NSWindowMiniaturizeButton],
[nsWindow standardWindowButton:NSWindowZoomButton]
};
// get most left and right coordinates
int left = -1;
int right = -1;
for( int i = 0; i < 3; i++ ) {
NSView* button = buttons[i];
if( button == NULL )
continue;
int x = [button convertRect: [button bounds] toView:button.superview].origin.x;
int width = button.bounds.size.width;
if( left == -1 || x < left )
left = x;
if( right == -1 || x + width > right )
right = x + width;
}
if( left == -1 || right == -1 )
return -1;
// 'right' is the actual button area width (from left window edge)
// adding 'left' to add same empty space on right side as on left side
return right + left;
JNI_COCOA_EXIT()
}
extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowTitleBarHeight
( JNIEnv* env, jclass cls, jobject window )
{
JNI_COCOA_ENTER()
NSWindow* nsWindow = getNSWindow( env, cls, window );
if( nsWindow == NULL )
return -1;
NSView* closeButton = [nsWindow standardWindowButton:NSWindowCloseButton];
if( closeButton == NULL )
return -1;
NSView* titlebar = closeButton.superview;
return titlebar.bounds.size.height;
JNI_COCOA_EXIT()
}
extern "C"
JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_isWindowFullScreen
( JNIEnv* env, jclass cls, jobject window )
{
JNI_COCOA_ENTER()
NSWindow* nsWindow = getNSWindow( env, cls, window );
if( nsWindow == NULL )
return FALSE;
return (jboolean) (([nsWindow styleMask] & NSWindowStyleMaskFullScreen) != 0);
JNI_COCOA_EXIT()
}