macOS fullWindowContent mode:

- added title bar buttons placeholder
- added client property to root pane that contains title bar buttons bounds
- undone toolbar extensions from commit ea2447dcb7
This commit is contained in:
Karl Tauber
2024-01-20 19:54:26 +01:00
parent f68a871dd6
commit 28278a75a7
13 changed files with 542 additions and 323 deletions

View File

@@ -270,6 +270,76 @@ public interface FlatClientProperties
String COMPONENT_TITLE_BAR_CAPTION = "JComponent.titleBarCaption"; String COMPONENT_TITLE_BAR_CAPTION = "JComponent.titleBarCaption";
//---- Panel --------------------------------------------------------------
/**
* Marks the panel as placeholder for the iconfify/maximize/close buttons
* in fullWindowContent mode.
* <p>
* If fullWindowContent mode is enabled, the preferred size of the panel is equal
* to the size of the iconfify/maximize/close buttons. Otherwise is is {@code 0,0}.
* <p>
* You're responsible to layout that panel at the top-left or top-right corner,
* depending on platform, where the iconfify/maximize/close buttons are located.
* <p>
* Syntax of the value string is: {@code "win|mac [horizontal|vertical]"}.
* <p>
* The string must start with {@code "win"} (for Windows or Linux) or
* with {@code "mac"} (for macOS) and specifies the platform where the placeholder
* should be used. On macOS, you need the placeholder in the top-left corner,
* but on Windows/Linux you need it in the top-right corner. So if fullWindowContent mode
* is supported on both platforms, you can add two placeholders to your layout
* and FlatLaf automatically uses only one of them. The other gets size {@code 0,0}.
* <p>
* Optionally, you can append {@code " horizontal"} or {@code " vertical"} to the value string
* to specify that the placeholder preferred size should be limited to one orientation.
* E.g. {@code "win horizontal"} means that the placeholder preferred width is
* equal to iconfify/maximize/close buttons width, but preferred height is zero.
* <p>
* Example for adding placeholder to top-left corner on macOS:
* <pre>{@code
* JPanel placeholder = new JPanel();
* placeholder.putClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER, "mac" );
*
* JToolBar toolBar = new JToolBar();
* // add tool bar items
*
* JPanel toolBarPanel = new JPanel( new BorderLayout() );
* toolBarPanel.add( placeholder, BorderLayout.WEST );
* toolBarPanel.add( toolBar, BorderLayout.CENTER );
*
* frame.getContentPane().add( toolBarPanel, BorderLayout.NORTH );
* }</pre>
*
* Or add placeholder as first item to the tool bar:
* <pre>{@code
* JPanel placeholder = new JPanel();
* placeholder.putClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER, "mac" );
*
* JToolBar toolBar = new JToolBar();
* toolBar.add( placeholder );
* // add tool bar items
*
* frame.getContentPane().add( toolBar, BorderLayout.NORTH );
* }</pre>
*
* If a tabbed pane is located at the top, you can add the placeholder
* as leading component to that tabbed pane:
* <pre>{@code
* JPanel placeholder = new JPanel();
* placeholder.putClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER, "mac" );
*
* tabbedPane.putClientProperty( FlatClientProperties.TABBED_PANE_LEADING_COMPONENT, placeholder );
* }</pre>
* <p>
* <strong>Component</strong> {@link javax.swing.JPanel}<br>
* <strong>Value type</strong> {@link java.lang.String}
*
* @since 3.4
*/
String FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER = "FlatLaf.fullWindowContent.buttonsPlaceholder";
//---- Popup -------------------------------------------------------------- //---- Popup --------------------------------------------------------------
/** /**
@@ -388,6 +458,20 @@ public interface FlatClientProperties
*/ */
String MENU_BAR_EMBEDDED = "JRootPane.menuBarEmbedded"; String MENU_BAR_EMBEDDED = "JRootPane.menuBarEmbedded";
/**
* Contains the current bounds of the iconfify/maximize/close buttons
* (in root pane coordinates) if fullWindowContent mode is enabled.
* Otherwise its value is {@code null}.
* <p>
* <b>Note</b>: Do not set this client property. It is set by FlatLaf.
* <p>
* <strong>Component</strong> {@link javax.swing.JRootPane}<br>
* <strong>Value type</strong> {@link java.awt.Rectangle}
*
* @since 3.4
*/
String FULL_WINDOW_CONTENT_BUTTONS_BOUNDS = "FlatLaf.fullWindowContent.buttonsBounds";
/** /**
* Specifies whether the window icon should be shown in the window title bar * Specifies whether the window icon should be shown in the window title bar
* (requires enabled window decorations). Default is UI property {@code TitlePane.showIcon}. * (requires enabled window decorations). Default is UI property {@code TitlePane.showIcon}.

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import java.awt.Rectangle;
import java.awt.Window; import java.awt.Window;
/** /**
@@ -54,14 +55,14 @@ 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 );
/** @since 3.4 */
public static final int public static final int
BUTTON_STYLE_DEFAULT = 0, BUTTON_STYLE_DEFAULT = 0,
BUTTON_STYLE_MEDIUM = 1, BUTTON_STYLE_MEDIUM = 1,
BUTTON_STYLE_LARGE = 2; BUTTON_STYLE_LARGE = 2;
public native static boolean setWindowButtonStyle( Window window, int buttonStyle ); /** @since 3.4 */ public native static boolean setWindowButtonStyle( Window window, int buttonStyle );
public native static int getWindowButtonAreaWidth( Window window ); /** @since 3.4 */ public native static Rectangle getWindowButtonsBounds( Window window );
public native static int getWindowTitleBarHeight( Window window ); /** @since 3.4 */ public native static boolean isWindowFullScreen( Window window );
public native static boolean isWindowFullScreen( Window window ); /** @since 3.4 */ public native static boolean toggleWindowFullScreen( Window window );
public native static boolean windowToggleFullScreen( Window window );
} }

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
@@ -23,6 +24,7 @@ import java.beans.PropertyChangeListener;
import java.util.Map; import java.util.Map;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPanelUI; import javax.swing.plaf.basic.BasicPanelUI;
import com.formdev.flatlaf.FlatClientProperties; import com.formdev.flatlaf.FlatClientProperties;
@@ -69,6 +71,8 @@ public class FlatPanelUI
super.installUI( c ); super.installUI( c );
c.addPropertyChangeListener( this ); c.addPropertyChangeListener( this );
if( c.getClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER ) != null )
FullWindowContentSupport.registerPlaceholder( c );
installStyle( (JPanel) c ); installStyle( (JPanel) c );
} }
@@ -78,10 +82,20 @@ public class FlatPanelUI
super.uninstallUI( c ); super.uninstallUI( c );
c.removePropertyChangeListener( this ); c.removePropertyChangeListener( this );
if( c.getClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER ) != null )
FullWindowContentSupport.unregisterPlaceholder( c );
oldStyleValues = null; oldStyleValues = null;
} }
@Override
protected void installDefaults( JPanel p ) {
super.installDefaults( p );
if( p.getClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER ) != null )
LookAndFeel.installProperty( p, "opaque", false );
}
/** @since 2.0.1 */ /** @since 2.0.1 */
@Override @Override
public void propertyChange( PropertyChangeEvent e ) { public void propertyChange( PropertyChangeEvent e ) {
@@ -98,6 +112,17 @@ public class FlatPanelUI
c.revalidate(); c.revalidate();
c.repaint(); c.repaint();
break; break;
case FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER:
JPanel p = (JPanel) e.getSource();
if( e.getOldValue() != null )
FullWindowContentSupport.unregisterPlaceholder( p );
if( e.getNewValue() != null )
FullWindowContentSupport.registerPlaceholder( p );
// make panel non-opaque for placeholders
LookAndFeel.installProperty( p, "opaque", e.getNewValue() == null );
break;
} }
} }
@@ -162,4 +187,19 @@ public class FlatPanelUI
paint( g, c ); paint( g, c );
} }
@Override
public Dimension getPreferredSize( JComponent c ) {
Object value = c.getClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER );
if( value != null )
return FullWindowContentSupport.getPlaceholderPreferredSize( c, (String) value );
return super.getPreferredSize( c );
}
@Override
public void paint( Graphics g, JComponent c ) {
if( c.getClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER ) != null )
FullWindowContentSupport.debugPaint( g, c );
}
} }

View File

@@ -89,6 +89,7 @@ public class FlatRootPaneUI
private LayoutManager oldLayout; private LayoutManager oldLayout;
private PropertyChangeListener ancestorListener; private PropertyChangeListener ancestorListener;
private ComponentListener componentListener; private ComponentListener componentListener;
private ComponentListener macFullWindowContentListener;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatRootPaneUI(); return new FlatRootPaneUI();
@@ -207,6 +208,9 @@ public class FlatRootPaneUI
}; };
root.addPropertyChangeListener( "ancestor", ancestorListener ); root.addPropertyChangeListener( "ancestor", ancestorListener );
} }
if( SystemInfo.isMacFullWindowContentSupported )
macFullWindowContentListener = FullWindowContentSupport.macInstallListeners( root );
} }
@Override @Override
@@ -223,6 +227,11 @@ public class FlatRootPaneUI
root.removePropertyChangeListener( "ancestor", ancestorListener ); root.removePropertyChangeListener( "ancestor", ancestorListener );
ancestorListener = null; ancestorListener = null;
} }
if( SystemInfo.isMacFullWindowContentSupported ) {
FullWindowContentSupport.macUninstallListeners( root, macFullWindowContentListener );
macFullWindowContentListener = null;
}
} }
/** @since 1.1.2 */ /** @since 1.1.2 */
@@ -359,6 +368,10 @@ public class FlatRootPaneUI
titlePane.titleBarColorsChanged(); titlePane.titleBarColorsChanged();
break; break;
case FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_BOUNDS:
FullWindowContentSupport.revalidatePlaceholders( rootPane );
break;
case FlatClientProperties.GLASS_PANE_FULL_HEIGHT: case FlatClientProperties.GLASS_PANE_FULL_HEIGHT:
rootPane.revalidate(); rootPane.revalidate();
break; break;
@@ -371,11 +384,11 @@ public class FlatRootPaneUI
case FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE: case FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE:
case "ancestor": case "ancestor":
if( SystemInfo.isMacFullWindowContentSupported && if( SystemInfo.isMacFullWindowContentSupported &&
SystemInfo.isJava_17_orLater &&
rootPane.isDisplayable() && rootPane.isDisplayable() &&
FlatClientProperties.clientPropertyBoolean( rootPane, "apple.awt.fullWindowContent", false ) && FlatClientProperties.clientPropertyBoolean( rootPane, "apple.awt.fullWindowContent", false ) )
FlatNativeMacLibrary.isLoaded() )
{ {
// set window button style
if( SystemInfo.isJava_17_orLater && FlatNativeMacLibrary.isLoaded() ) {
int buttonStyle = FlatNativeMacLibrary.BUTTON_STYLE_DEFAULT; int buttonStyle = FlatNativeMacLibrary.BUTTON_STYLE_DEFAULT;
Object value = rootPane.getClientProperty( FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE ); Object value = rootPane.getClientProperty( FlatClientProperties.MACOS_WINDOW_BUTTON_STYLE );
switch( String.valueOf( value ) ) { switch( String.valueOf( value ) ) {
@@ -392,6 +405,10 @@ public class FlatRootPaneUI
Window window = SwingUtilities.windowForComponent( rootPane ); Window window = SwingUtilities.windowForComponent( rootPane );
FlatNativeMacLibrary.setWindowButtonStyle( window, buttonStyle ); FlatNativeMacLibrary.setWindowButtonStyle( window, buttonStyle );
} }
// update buttons bounds client property
FullWindowContentSupport.macUpdateFullWindowContentButtonsBoundsProperty( rootPane );
}
break; break;
} }
} }

View File

@@ -25,7 +25,6 @@ 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;
@@ -115,18 +114,6 @@ 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 ) ) {
// 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,45 +19,35 @@ 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;
/** /**
@@ -158,12 +148,6 @@ 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
@@ -176,8 +160,6 @@ public class FlatToolBarUI
toolBar.setFloatable( oldFloatable ); toolBar.setFloatable( oldFloatable );
oldFloatable = null; oldFloatable = null;
} }
toolBar.setLayout( null );
} }
@Override @Override
@@ -471,137 +453,6 @@ 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

@@ -0,0 +1,183 @@
/*
* Copyright 2024 FormDev Software GmbH
*
* 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
*
* https://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 com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JComponent;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.util.SystemInfo;
/**
* @author Karl Tauber
*/
class FullWindowContentSupport
{
private static final String KEY_DEBUG_SHOW_PLACEHOLDERS = "FlatLaf.debug.panel.showPlaceholders";
private static ArrayList<WeakReference<JComponent>> placeholders = new ArrayList<>();
static Dimension getPlaceholderPreferredSize( JComponent c, String options ) {
JRootPane rootPane;
Rectangle bounds;
if( options.startsWith( SystemInfo.isMacOS ? "mac" : "win" ) &&
c.isDisplayable() &&
(rootPane = SwingUtilities.getRootPane( c )) != null &&
(bounds = (Rectangle) rootPane.getClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_BOUNDS )) != null )
{
// On macOS, the client property is updated very late when toggling full screen,
// which results in "jumping" layout after full screen toggle finished.
// To avoid that, get up-to-date buttons bounds from macOS.
if( SystemInfo.isMacFullWindowContentSupported && FlatNativeMacLibrary.isLoaded() ) {
Rectangle r = FlatNativeMacLibrary.getWindowButtonsBounds( SwingUtilities.windowForComponent( c ) );
if( r != null )
bounds = r;
}
if( options.length() > 3 ) {
if( options.contains( "horizontal" ) )
return new Dimension( bounds.width, 0 );
if( options.contains( "vertical" ) )
return new Dimension( 0, bounds.height );
}
return bounds.getSize();
}
// default to 0,0
return new Dimension();
}
static void registerPlaceholder( JComponent c ) {
synchronized( placeholders ) {
if( indexOfPlaceholder( c ) < 0 )
placeholders.add( new WeakReference<>( c ) );
}
}
static void unregisterPlaceholder( JComponent c ) {
synchronized( placeholders ) {
int index = indexOfPlaceholder( c );
if( index >= 0 )
placeholders.remove( index );
}
}
private static int indexOfPlaceholder( JComponent c ) {
int size = placeholders.size();
for( int i = 0; i < size; i++ ) {
if( placeholders.get( i ).get() == c )
return i;
}
return -1;
}
static void revalidatePlaceholders( Component container ) {
synchronized( placeholders ) {
if( placeholders.isEmpty() )
return;
for( Iterator<WeakReference<JComponent>> it = placeholders.iterator(); it.hasNext(); ) {
WeakReference<JComponent> ref = it.next();
JComponent c = ref.get();
// remove already released placeholder
if( c == null ) {
it.remove();
continue;
}
// revalidate placeholder if is in given container
if( SwingUtilities.isDescendingFrom( c, container ) )
c.revalidate();
}
}
}
static ComponentListener macInstallListeners( JRootPane rootPane ) {
ComponentListener l = new ComponentAdapter() {
boolean lastFullScreen;
@Override
public void componentResized( ComponentEvent e ) {
Window window = SwingUtilities.windowForComponent( rootPane );
if( window == null )
return;
boolean fullScreen = FlatNativeMacLibrary.isLoaded() && FlatNativeMacLibrary.isWindowFullScreen( window );
if( fullScreen == lastFullScreen )
return;
lastFullScreen = fullScreen;
macUpdateFullWindowContentButtonsBoundsProperty( rootPane );
}
};
rootPane.addComponentListener( l );
return l;
}
static void macUninstallListeners( JRootPane rootPane, ComponentListener l ) {
if( l != null )
rootPane.removeComponentListener( l );
}
static void macUpdateFullWindowContentButtonsBoundsProperty( JRootPane rootPane ) {
if( !SystemInfo.isMacFullWindowContentSupported ||
!rootPane.isDisplayable() ||
!FlatClientProperties.clientPropertyBoolean( rootPane, "apple.awt.fullWindowContent", false ) )
return;
Rectangle bounds = FlatNativeMacLibrary.isLoaded()
? FlatNativeMacLibrary.getWindowButtonsBounds( SwingUtilities.windowForComponent( rootPane ) )
: new Rectangle( 68, 28 ); // default size
rootPane.putClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_BOUNDS, bounds );
}
static void debugPaint( Graphics g, JComponent c ) {
if( !UIManager.getBoolean( KEY_DEBUG_SHOW_PLACEHOLDERS ) )
return;
int width = c.getWidth() - 1;
int height = c.getHeight() - 1;
if( width <= 0 || height <= 0 )
return;
g.setColor( Color.red );
g.drawRect( 0, 0, width, height );
// draw diagonal cross
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints( g );
g.drawLine( 0, 0, width, height );
g.drawLine( 0, height, width, 0 );
FlatUIUtils.resetRenderingHints( g, oldRenderingHints );
}
}

View File

@@ -102,6 +102,9 @@ class DemoFrame
rootPane.putClientProperty( "apple.awt.windowTitleVisible", false ); rootPane.putClientProperty( "apple.awt.windowTitleVisible", false );
else else
setTitle( null ); setTitle( null );
// uncomment this line to see title bar buttons placeholders in fullWindowContent mode
// UIManager.put( "FlatLaf.debug.panel.showPlaceholders", true );
} }
// 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+)
@@ -509,6 +512,8 @@ class DemoFrame
JMenuItem showUIDefaultsInspectorMenuItem = new JMenuItem(); JMenuItem showUIDefaultsInspectorMenuItem = new JMenuItem();
JMenu helpMenu = new JMenu(); JMenu helpMenu = new JMenu();
aboutMenuItem = new JMenuItem(); aboutMenuItem = new JMenuItem();
JPanel toolBarPanel = new JPanel();
JPanel macFullWindowContentButtonsPlaceholder = new JPanel();
toolBar = new JToolBar(); toolBar = new JToolBar();
JButton backButton = new JButton(); JButton backButton = new JButton();
JButton forwardButton = new JButton(); JButton forwardButton = new JButton();
@@ -825,6 +830,16 @@ class DemoFrame
} }
setJMenuBar(menuBar1); setJMenuBar(menuBar1);
//======== toolBarPanel ========
{
toolBarPanel.setLayout(new BorderLayout());
//======== macFullWindowContentButtonsPlaceholder ========
{
macFullWindowContentButtonsPlaceholder.setLayout(new FlowLayout());
}
toolBarPanel.add(macFullWindowContentButtonsPlaceholder, BorderLayout.WEST);
//======== toolBar ======== //======== toolBar ========
{ {
toolBar.setMargin(new Insets(3, 3, 3, 3)); toolBar.setMargin(new Insets(3, 3, 3, 3));
@@ -868,7 +883,9 @@ class DemoFrame
showToggleButton.setIcon(new FlatSVGIcon("com/formdev/flatlaf/demo/icons/show.svg")); showToggleButton.setIcon(new FlatSVGIcon("com/formdev/flatlaf/demo/icons/show.svg"));
toolBar.add(showToggleButton); toolBar.add(showToggleButton);
} }
contentPane.add(toolBar, BorderLayout.NORTH); toolBarPanel.add(toolBar, BorderLayout.CENTER);
}
contentPane.add(toolBarPanel, BorderLayout.NORTH);
//======== contentPanel ======== //======== contentPanel ========
{ {
@@ -963,7 +980,7 @@ class DemoFrame
} ); } );
if( SystemInfo.isMacOS && FlatNativeMacLibrary.isLoaded() ) { if( SystemInfo.isMacOS && FlatNativeMacLibrary.isLoaded() ) {
showToggleButton.addActionListener( e -> { showToggleButton.addActionListener( e -> {
FlatNativeMacLibrary.windowToggleFullScreen( this ); FlatNativeMacLibrary.toggleWindowFullScreen( this );
} ); } );
} }
@@ -1007,6 +1024,9 @@ class DemoFrame
if( "false".equals( System.getProperty( "flatlaf.animatedLafChange" ) ) ) if( "false".equals( System.getProperty( "flatlaf.animatedLafChange" ) ) )
animatedLafChangeMenuItem.setSelected( false ); animatedLafChangeMenuItem.setSelected( false );
// on macOS, panel left to toolBar is a placeholder for title bar buttons in fullWindowContent mode
macFullWindowContentButtonsPlaceholder.putClientProperty( FlatClientProperties.FULL_WINDOW_CONTENT_BUTTONS_PLACEHOLDER, "mac" );
// remove contentPanel bottom insets // remove contentPanel bottom insets
MigLayout layout = (MigLayout) contentPanel.getLayout(); MigLayout layout = (MigLayout) contentPanel.getLayout();
LC lc = ConstraintParser.parseLayoutConstraint( (String) layout.getLayoutConstraints() ); LC lc = ConstraintParser.parseLayoutConstraint( (String) layout.getLayoutConstraints() );

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "8.1.0.0.283" Java: "19.0.2" encoding: "UTF-8" JFDML JFormDesigner: "8.2.1.0.348" Java: "21.0.1" encoding: "UTF-8"
new FormModel { new FormModel {
contentType: "form/swing" contentType: "form/swing"
@@ -12,6 +12,13 @@ new FormModel {
"defaultCloseOperation": 2 "defaultCloseOperation": 2
"$locationPolicy": 2 "$locationPolicy": 2
"$sizePolicy": 2 "$sizePolicy": 2
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.BorderLayout ) ) {
name: "toolBarPanel"
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.FlowLayout ) ) {
name: "macFullWindowContentButtonsPlaceholder"
}, new FormLayoutConstraints( class java.lang.String ) {
"value": "West"
} )
add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) { add( new FormContainer( "javax.swing.JToolBar", new FormLayoutManager( class javax.swing.JToolBar ) ) {
name: "toolBar" name: "toolBar"
"margin": new java.awt.Insets( 3, 3, 3, 3 ) "margin": new java.awt.Insets( 3, 3, 3, 3 )
@@ -63,6 +70,9 @@ new FormModel {
"toolTipText": "Show Details" "toolTipText": "Show Details"
"icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/demo/icons/show.svg" ) "icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/demo/icons/show.svg" )
} ) } )
}, new FormLayoutConstraints( class java.lang.String ) {
"value": "Center"
} )
}, new FormLayoutConstraints( class java.lang.String ) { }, new FormLayoutConstraints( class java.lang.String ) {
"value": "North" "value": "North"
} ) } )

View File

@@ -41,4 +41,6 @@
} }
jfieldID getFieldID( JNIEnv *env, const char* className, const char* fieldName, const char* fieldSignature ); jclass findClass( JNIEnv *env, const char* className, bool globalRef );
jfieldID getFieldID( JNIEnv *env, const char* className, const char* fieldName, const char* fieldSignature, bool staticField );
jmethodID getMethodID( JNIEnv *env, jclass cls, const char* methodName, const char* methodSignature, bool staticMethod );

View File

@@ -31,18 +31,10 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setW
/* /*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary * Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: getWindowButtonAreaWidth * Method: getWindowButtonsBounds
* Signature: (Ljava/awt/Window;)I * Signature: (Ljava/awt/Window;)Ljava/awt/Rectangle;
*/ */
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowButtonAreaWidth JNIEXPORT jobject JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowButtonsBounds
(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); (JNIEnv *, jclass, jobject);
/* /*
@@ -55,10 +47,10 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_isWi
/* /*
* Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary * Class: com_formdev_flatlaf_ui_FlatNativeMacLibrary
* Method: windowToggleFullScreen * Method: toggleWindowFullScreen
* Signature: (Ljava/awt/Window;)Z * Signature: (Ljava/awt/Window;)Z
*/ */
JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_windowToggleFullScreen JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_toggleWindowFullScreen
(JNIEnv *, jclass, jobject); (JNIEnv *, jclass, jobject);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -21,8 +21,8 @@
* @author Karl Tauber * @author Karl Tauber
*/ */
jfieldID getFieldID( JNIEnv *env, const char* className, const char* fieldName, const char* fieldSignature ) { jclass findClass( JNIEnv *env, const char* className, bool globalRef ) {
// NSLog( @"getFieldID %s %s %s", className, fieldName, fieldSignature ); // NSLog( @"findClass %s", className );
jclass cls = env->FindClass( className ); jclass cls = env->FindClass( className );
if( cls == NULL ) { if( cls == NULL ) {
@@ -32,7 +32,22 @@ jfieldID getFieldID( JNIEnv *env, const char* className, const char* fieldName,
return NULL; return NULL;
} }
jfieldID fieldID = env->GetFieldID( cls, fieldName, fieldSignature ); if( globalRef )
cls = reinterpret_cast<jclass>( env->NewGlobalRef( cls ) );
return cls;
}
jfieldID getFieldID( JNIEnv *env, const char* className, const char* fieldName, const char* fieldSignature, bool staticField ) {
// NSLog( @"getFieldID %s %s %s", className, fieldName, fieldSignature );
jclass cls = findClass( env, className, false );
if( cls == NULL )
return NULL;
jfieldID fieldID = staticField
? env->GetStaticFieldID( cls, fieldName, fieldSignature )
: env->GetFieldID( cls, fieldName, fieldSignature );
if( fieldID == NULL ) { if( fieldID == NULL ) {
NSLog( @"FlatLaf: failed to lookup field '%s' of type '%s' in class '%s'", fieldName, fieldSignature, className ); NSLog( @"FlatLaf: failed to lookup field '%s' of type '%s' in class '%s'", fieldName, fieldSignature, className );
env->ExceptionDescribe(); // print stack trace env->ExceptionDescribe(); // print stack trace
@@ -42,3 +57,22 @@ jfieldID getFieldID( JNIEnv *env, const char* className, const char* fieldName,
return fieldID; return fieldID;
} }
jmethodID getMethodID( JNIEnv *env, jclass cls, const char* methodName, const char* methodSignature, bool staticMethod ) {
// NSLog( @"getMethodID %s %s", methodName, methodSignature );
if( cls == NULL )
return NULL;
jmethodID methodID = staticMethod
? env->GetStaticMethodID( cls, methodName, methodSignature )
: env->GetMethodID( cls, methodName, methodSignature );
if( methodID == NULL ) {
NSLog( @"FlatLaf: failed to lookup method '%s' of type '%s'", methodName, methodSignature );
env->ExceptionDescribe(); // print stack trace
env->ExceptionClear();
return NULL;
}
return methodID;
}

View File

@@ -51,9 +51,9 @@ NSWindow* getNSWindow( JNIEnv* env, jclass cls, jobject window ) {
return NULL; return NULL;
// initialize field IDs (done only once because variables are static) // initialize field IDs (done only once because variables are static)
static jfieldID peerID = getFieldID( env, "java/awt/Component", "peer", "Ljava/awt/peer/ComponentPeer;" ); static jfieldID peerID = getFieldID( env, "java/awt/Component", "peer", "Ljava/awt/peer/ComponentPeer;", false );
static jfieldID platformWindowID = getFieldID( env, "sun/lwawt/LWWindowPeer", "platformWindow", "Lsun/lwawt/PlatformWindow;" ); static jfieldID platformWindowID = getFieldID( env, "sun/lwawt/LWWindowPeer", "platformWindow", "Lsun/lwawt/PlatformWindow;", false );
static jfieldID ptrID = getFieldID( env, "sun/lwawt/macosx/CFRetainedResource", "ptr", "J" ); static jfieldID ptrID = getFieldID( env, "sun/lwawt/macosx/CFRetainedResource", "ptr", "J", false );
if( peerID == NULL || platformWindowID == NULL || ptrID == NULL ) if( peerID == NULL || platformWindowID == NULL || ptrID == NULL )
return NULL; return NULL;
@@ -148,7 +148,7 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setW
WindowData* windowData = getWindowData( nsWindow, true ); WindowData* windowData = getWindowData( nsWindow, true );
[FlatJNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){ [FlatJNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
// NSLog( @"\n%@\n\n", [nsWindow.contentView.superview _subtreeDescription] ); // NSLog( @"\n%@\n\n", [nsWindow.contentView.superview _subtreeDescription] );
// add/remove toolbar // add/remove toolbar
@@ -222,29 +222,48 @@ JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_setW
} }
extern "C" extern "C"
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowButtonAreaWidth JNIEXPORT jobject JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_getWindowButtonsBounds
( JNIEnv* env, jclass cls, jobject window ) ( JNIEnv* env, jclass cls, jobject window )
{ {
JNI_COCOA_ENTER() JNI_COCOA_ENTER()
NSWindow* nsWindow = getNSWindow( env, cls, window ); NSWindow* nsWindow = getNSWindow( env, cls, window );
if( nsWindow == NULL ) if( nsWindow == NULL )
return -1; return NULL;
// return zero if window is full screen because close/minimize/zoom buttons are hidden WindowData* windowData = getWindowData( nsWindow, false );
if( isWindowFullScreen( nsWindow ) ) int width = 0;
return 0; int height = 0;
// get width
if( isWindowFullScreen( nsWindow ) ) {
// use zero if window is full screen because close/minimize/zoom buttons are hidden
width = 0;
} else if( windowData != NULL && windowData.lastWindowButtonAreaWidth > 0 ) {
// use remembered value if window is in transition from full screen to non-full screen // use remembered value if window is in transition from full screen to non-full screen
// because NSToolbar is not yet visible // because NSToolbar is not yet visible
WindowData* windowData = getWindowData( nsWindow, false ); width = windowData.lastWindowButtonAreaWidth;
if( windowData != NULL && windowData.lastWindowButtonAreaWidth > 0 ) } else
return windowData.lastWindowButtonAreaWidth; width = getWindowButtonAreaWidth( nsWindow );
return getWindowButtonAreaWidth( nsWindow ); // get height
if( windowData != NULL && windowData.lastWindowTitleBarHeight > 0 ) {
// use remembered value if window is full screen because NSToolbar is hidden
height = windowData.lastWindowTitleBarHeight;
} else
height = getWindowTitleBarHeight( nsWindow );
// initialize class and method ID (done only once because variables are static)
static jclass cls = findClass( env, "java/awt/Rectangle", true );
static jmethodID methodID = getMethodID( env, cls, "<init>", "(IIII)V", false );
if( cls == NULL || methodID == NULL )
return NULL;
// create and return Rectangle
return env->NewObject( cls, methodID, 0, 0, width, height );
JNI_COCOA_EXIT() JNI_COCOA_EXIT()
return -1; return NULL;
} }
int getWindowButtonAreaWidth( NSWindow* nsWindow ) { int getWindowButtonAreaWidth( NSWindow* nsWindow ) {
@@ -279,27 +298,6 @@ int getWindowButtonAreaWidth( NSWindow* nsWindow ) {
return right + left; return right + left;
} }
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;
// use remembered value if window is full screen because NSToolbar is hidden
WindowData* windowData = getWindowData( nsWindow, false );
if( windowData != NULL && windowData.lastWindowTitleBarHeight > 0 )
return windowData.lastWindowTitleBarHeight;
return getWindowTitleBarHeight( nsWindow );
JNI_COCOA_EXIT()
return -1;
}
int getWindowTitleBarHeight( NSWindow* nsWindow ) { int getWindowTitleBarHeight( NSWindow* nsWindow ) {
NSView* closeButton = [nsWindow standardWindowButton:NSWindowCloseButton]; NSView* closeButton = [nsWindow standardWindowButton:NSWindowCloseButton];
if( closeButton == NULL ) if( closeButton == NULL )
@@ -330,7 +328,7 @@ bool isWindowFullScreen( NSWindow* nsWindow ) {
} }
extern "C" extern "C"
JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_windowToggleFullScreen JNIEXPORT jboolean JNICALL Java_com_formdev_flatlaf_ui_FlatNativeMacLibrary_toggleWindowFullScreen
( JNIEnv* env, jclass cls, jobject window ) ( JNIEnv* env, jclass cls, jobject window )
{ {
JNI_COCOA_ENTER() JNI_COCOA_ENTER()