- support different selection colors for top-level JMenus
- fixed styling of underline selection properties for top-level JMenus
This commit is contained in:
Karl Tauber
2022-05-15 16:39:11 +02:00
parent 84e9c36280
commit f1792e46c6
8 changed files with 60 additions and 26 deletions

View File

@@ -74,6 +74,8 @@ public class FlatMenuBarUI
/** @since 3 */ @Styleable protected Insets selectionEmbeddedInsets; /** @since 3 */ @Styleable protected Insets selectionEmbeddedInsets;
/** @since 3 */ @Styleable protected int selectionArc = -1; /** @since 3 */ @Styleable protected int selectionArc = -1;
/** @since 2 */ @Styleable protected Color hoverBackground; /** @since 2 */ @Styleable protected Color hoverBackground;
/** @since 3 */ @Styleable protected Color selectionBackground;
/** @since 3 */ @Styleable protected Color selectionForeground;
/** @since 2 */ @Styleable protected Color underlineSelectionBackground; /** @since 2 */ @Styleable protected Color underlineSelectionBackground;
/** @since 2 */ @Styleable protected Color underlineSelectionColor; /** @since 2 */ @Styleable protected Color underlineSelectionColor;
/** @since 2 */ @Styleable protected int underlineSelectionHeight = -1; /** @since 2 */ @Styleable protected int underlineSelectionHeight = -1;

View File

@@ -21,6 +21,7 @@ import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Window; import java.awt.Window;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
@@ -79,6 +80,8 @@ import com.formdev.flatlaf.util.LoggingFacade;
* @uiDefault MenuBar.selectionEmbeddedInsets Insets * @uiDefault MenuBar.selectionEmbeddedInsets Insets
* @uiDefault MenuBar.selectionArc int * @uiDefault MenuBar.selectionArc int
* @uiDefault MenuBar.hoverBackground Color * @uiDefault MenuBar.hoverBackground Color
* @uiDefault MenuBar.selectionBackground Color optional; defaults to Menu.selectionBackground
* @uiDefault MenuBar.selectionForeground Color optional; defaults to Menu.selectionForeground
* @uiDefault MenuBar.underlineSelectionBackground Color * @uiDefault MenuBar.underlineSelectionBackground Color
* @uiDefault MenuBar.underlineSelectionColor Color * @uiDefault MenuBar.underlineSelectionColor Color
* @uiDefault MenuBar.underlineSelectionHeight int * @uiDefault MenuBar.underlineSelectionHeight int
@@ -226,9 +229,11 @@ public class FlatMenuUI
/** @since 3 */ protected Insets menuBarSelectionEmbeddedInsets = UIManager.getInsets( "MenuBar.selectionEmbeddedInsets" ); /** @since 3 */ protected Insets menuBarSelectionEmbeddedInsets = UIManager.getInsets( "MenuBar.selectionEmbeddedInsets" );
/** @since 3 */ protected int menuBarSelectionArc = UIManager.getInt( "MenuBar.selectionArc" ); /** @since 3 */ protected int menuBarSelectionArc = UIManager.getInt( "MenuBar.selectionArc" );
protected Color hoverBackground = UIManager.getColor( "MenuBar.hoverBackground" ); protected Color hoverBackground = UIManager.getColor( "MenuBar.hoverBackground" );
protected Color menuBarUnderlineSelectionBackground = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionBackground", underlineSelectionBackground ); /** @since 3 */ protected Color menuBarSelectionBackground = UIManager.getColor( "MenuBar.selectionBackground" );
protected Color menuBarUnderlineSelectionColor = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionColor", underlineSelectionColor ); /** @since 3 */ protected Color menuBarSelectionForeground = UIManager.getColor( "MenuBar.selectionForeground" );
protected int menuBarUnderlineSelectionHeight = FlatUIUtils.getUIInt( "MenuBar.underlineSelectionHeight", underlineSelectionHeight ); protected Color menuBarUnderlineSelectionBackground = UIManager.getColor( "MenuBar.underlineSelectionBackground" );
protected Color menuBarUnderlineSelectionColor = UIManager.getColor( "MenuBar.underlineSelectionColor" );
protected int menuBarUnderlineSelectionHeight = FlatUIUtils.getUIInt( "MenuBar.underlineSelectionHeight", -1 );
protected FlatMenuRenderer( JMenuItem menuItem, Icon checkIcon, Icon arrowIcon, protected FlatMenuRenderer( JMenuItem menuItem, Icon checkIcon, Icon arrowIcon,
Font acceleratorFont, String acceleratorDelimiter ) Font acceleratorFont, String acceleratorDelimiter )
@@ -239,9 +244,7 @@ public class FlatMenuUI
/** @since 3 */ /** @since 3 */
@Override @Override
protected void paintBackground( Graphics g ) { protected void paintBackground( Graphics g ) {
if( ((JMenu)menuItem).isTopLevelMenu() ) { if( ((JMenu)menuItem).isTopLevelMenu() && isHover() ) {
ButtonModel model = menuItem.getModel();
if( model.isRollover() && !model.isArmed() && !model.isSelected() && model.isEnabled() ) {
// paint hover background // paint hover background
Color color = deriveBackground( getStyleFromMenuBarUI( ui -> ui.hoverBackground, hoverBackground ) ); Color color = deriveBackground( getStyleFromMenuBarUI( ui -> ui.hoverBackground, hoverBackground ) );
if( isUnderlineSelection() ) { if( isUnderlineSelection() ) {
@@ -251,17 +254,17 @@ public class FlatMenuUI
paintSelection( g, color, selectionInsets, selectionArc ); paintSelection( g, color, selectionInsets, selectionArc );
return; return;
} }
}
super.paintBackground( g ); super.paintBackground( g );
} }
/** @since 3 */ /** @since 3 */
@Override @Override
protected void paintSelection( Graphics g, Color selectionBackground, Insets selectionInsets, protected void paintSelection( Graphics g, Color selectionBackground, Insets selectionInsets, int selectionArc ) {
int selectionArc )
{
if( ((JMenu)menuItem).isTopLevelMenu() ) { if( ((JMenu)menuItem).isTopLevelMenu() ) {
if( !isHover() )
selectionBackground = getStyleFromMenuBarUI( ui -> ui.selectionBackground, menuBarSelectionBackground, selectionBackground );
JMenuBar menuBar = (JMenuBar) menuItem.getParent(); JMenuBar menuBar = (JMenuBar) menuItem.getParent();
JRootPane rootPane = SwingUtilities.getRootPane( menuBar ); JRootPane rootPane = SwingUtilities.getRootPane( menuBar );
if( rootPane != null && rootPane.getParent() instanceof Window && if( rootPane != null && rootPane.getParent() instanceof Window &&
@@ -271,6 +274,7 @@ public class FlatMenuUI
selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionEmbeddedInsets, menuBarSelectionEmbeddedInsets ); selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionEmbeddedInsets, menuBarSelectionEmbeddedInsets );
} else } else
selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionInsets, menuBarSelectionInsets ); selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionInsets, menuBarSelectionInsets );
selectionArc = getStyleFromMenuBarUI( ui -> (ui.selectionArc != -1) selectionArc = getStyleFromMenuBarUI( ui -> (ui.selectionArc != -1)
? ui.selectionArc : null, menuBarSelectionArc ); ? ui.selectionArc : null, menuBarSelectionArc );
} }
@@ -284,15 +288,32 @@ public class FlatMenuUI
Color underlineSelectionColor, int underlineSelectionHeight ) Color underlineSelectionColor, int underlineSelectionHeight )
{ {
if( ((JMenu)menuItem).isTopLevelMenu() ) { if( ((JMenu)menuItem).isTopLevelMenu() ) {
underlineSelectionBackground = getStyleFromMenuBarUI( ui -> ui.underlineSelectionBackground, menuBarUnderlineSelectionBackground ); underlineSelectionBackground = getStyleFromMenuBarUI( ui -> ui.underlineSelectionBackground, menuBarUnderlineSelectionBackground, underlineSelectionBackground );
underlineSelectionColor = getStyleFromMenuBarUI( ui -> ui.underlineSelectionColor, menuBarUnderlineSelectionColor ); underlineSelectionColor = getStyleFromMenuBarUI( ui -> ui.underlineSelectionColor, menuBarUnderlineSelectionColor, underlineSelectionColor );
underlineSelectionHeight = getStyleFromMenuBarUI( ui -> (ui.underlineSelectionHeight != -1) underlineSelectionHeight = getStyleFromMenuBarUI( ui -> (ui.underlineSelectionHeight != -1) ? ui.underlineSelectionHeight : null,
? ui.underlineSelectionHeight : null, menuBarUnderlineSelectionHeight ); (menuBarUnderlineSelectionHeight != -1) ? menuBarUnderlineSelectionHeight : underlineSelectionHeight );
} }
super.paintUnderlineSelection( g, underlineSelectionBackground, underlineSelectionColor, underlineSelectionHeight ); super.paintUnderlineSelection( g, underlineSelectionBackground, underlineSelectionColor, underlineSelectionHeight );
} }
@Override
protected void paintText( Graphics g, Rectangle textRect, String text, Color selectionForeground, Color disabledForeground ) {
if( ((JMenu)menuItem).isTopLevelMenu() )
selectionForeground = getStyleFromMenuBarUI( ui -> ui.selectionForeground, menuBarSelectionForeground, selectionForeground );
super.paintText( g, textRect, text, selectionForeground, disabledForeground );
}
private boolean isHover() {
ButtonModel model = menuItem.getModel();
return model.isRollover() && !model.isArmed() && !model.isSelected() && model.isEnabled();
}
private <T> T getStyleFromMenuBarUI( Function<FlatMenuBarUI, T> f, T defaultValue, T defaultValue2 ) {
return getStyleFromMenuBarUI( f, (defaultValue != null) ? defaultValue : defaultValue2 );
}
private <T> T getStyleFromMenuBarUI( Function<FlatMenuBarUI, T> f, T defaultValue ) { private <T> T getStyleFromMenuBarUI( Function<FlatMenuBarUI, T> f, T defaultValue ) {
MenuBarUI ui = ((JMenuBar)menuItem.getParent()).getUI(); MenuBarUI ui = ((JMenuBar)menuItem.getParent()).getUI();
if( !(ui instanceof FlatMenuBarUI) ) if( !(ui instanceof FlatMenuBarUI) )

View File

@@ -422,9 +422,9 @@ MenuBar.border = com.formdev.flatlaf.ui.FlatMenuBarBorder
MenuBar.background = @menuBackground MenuBar.background = @menuBackground
MenuBar.hoverBackground = @menuHoverBackground MenuBar.hoverBackground = @menuHoverBackground
MenuBar.itemMargins = 3,8,3,8 MenuBar.itemMargins = 3,8,3,8
MenuBar.selectionInsets = 0,0,0,0 MenuBar.selectionInsets = $MenuItem.selectionInsets
MenuBar.selectionEmbeddedInsets = 0,0,0,0 MenuBar.selectionEmbeddedInsets = $MenuItem.selectionInsets
MenuBar.selectionArc = 0 MenuBar.selectionArc = $MenuItem.selectionArc
#---- MenuItem ---- #---- MenuItem ----

View File

@@ -275,6 +275,8 @@ public class TestFlatStyleableInfo
"selectionEmbeddedInsets", Insets.class, "selectionEmbeddedInsets", Insets.class,
"selectionArc", int.class, "selectionArc", int.class,
"hoverBackground", Color.class, "hoverBackground", Color.class,
"selectionBackground", Color.class,
"selectionForeground", Color.class,
"underlineSelectionBackground", Color.class, "underlineSelectionBackground", Color.class,
"underlineSelectionColor", Color.class, "underlineSelectionColor", Color.class,
"underlineSelectionHeight", int.class, "underlineSelectionHeight", int.class,

View File

@@ -427,6 +427,8 @@ public class TestFlatStyling
ui.applyStyle( "selectionEmbeddedInsets: 1,2,3,4" ); ui.applyStyle( "selectionEmbeddedInsets: 1,2,3,4" );
ui.applyStyle( "selectionArc: 8" ); ui.applyStyle( "selectionArc: 8" );
ui.applyStyle( "hoverBackground: #fff" ); ui.applyStyle( "hoverBackground: #fff" );
ui.applyStyle( "selectionBackground: #fff" );
ui.applyStyle( "selectionForeground: #fff" );
ui.applyStyle( "underlineSelectionBackground: #fff" ); ui.applyStyle( "underlineSelectionBackground: #fff" );
ui.applyStyle( "underlineSelectionColor: #fff" ); ui.applyStyle( "underlineSelectionColor: #fff" );
ui.applyStyle( "underlineSelectionHeight: 3" ); ui.applyStyle( "underlineSelectionHeight: 3" );

View File

@@ -601,7 +601,9 @@ MenuBar.highlight #ffffff HSL 0 0 100 javax.swing.plaf.Colo
MenuBar.hoverBackground #ffdddd HSL 0 100 93 javax.swing.plaf.ColorUIResource [UI] MenuBar.hoverBackground #ffdddd HSL 0 100 93 javax.swing.plaf.ColorUIResource [UI]
MenuBar.itemMargins 3,8,3,8 javax.swing.plaf.InsetsUIResource [UI] MenuBar.itemMargins 3,8,3,8 javax.swing.plaf.InsetsUIResource [UI]
MenuBar.selectionArc 8 MenuBar.selectionArc 8
MenuBar.selectionEmbeddedInsets 0,0,0,0 javax.swing.plaf.InsetsUIResource [UI] MenuBar.selectionBackground #ff0000 HSL 0 100 50 javax.swing.plaf.ColorUIResource [UI]
MenuBar.selectionEmbeddedInsets 2,3,2,3 javax.swing.plaf.InsetsUIResource [UI]
MenuBar.selectionForeground #00ff00 HSL 120 100 50 javax.swing.plaf.ColorUIResource [UI]
MenuBar.selectionInsets 1,3,1,3 javax.swing.plaf.InsetsUIResource [UI] MenuBar.selectionInsets 1,3,1,3 javax.swing.plaf.InsetsUIResource [UI]
MenuBar.shadow #a0a0a0 HSL 0 0 63 javax.swing.plaf.ColorUIResource [UI] MenuBar.shadow #a0a0a0 HSL 0 0 63 javax.swing.plaf.ColorUIResource [UI]
MenuBar.underlineSelectionBackground #00ff00 HSL 120 100 50 javax.swing.plaf.ColorUIResource [UI] MenuBar.underlineSelectionBackground #00ff00 HSL 120 100 50 javax.swing.plaf.ColorUIResource [UI]

View File

@@ -242,10 +242,13 @@ Menu.icon.disabledArrowColor = #ABABAB
#---- MenuBar ---- #---- MenuBar ----
MenuBar.selectionInsets = 1,3,1,3 MenuBar.selectionInsets = 1,3,1,3
MenuBar.selectionEmbeddedInsets = 2,3,2,3
MenuBar.selectionArc = 8 MenuBar.selectionArc = 8
MenuBar.borderColor = #44f MenuBar.borderColor = #44f
MenuBar.hoverBackground = #fdd MenuBar.hoverBackground = #fdd
MenuBar.selectionBackground = #f00
MenuBar.selectionForeground = #0f0
MenuBar.underlineSelectionBackground = #0f0 MenuBar.underlineSelectionBackground = #0f0
MenuBar.underlineSelectionColor = #ff0 MenuBar.underlineSelectionColor = #ff0

View File

@@ -463,7 +463,9 @@ MenuBar.highlight
MenuBar.hoverBackground MenuBar.hoverBackground
MenuBar.itemMargins MenuBar.itemMargins
MenuBar.selectionArc MenuBar.selectionArc
MenuBar.selectionBackground
MenuBar.selectionEmbeddedInsets MenuBar.selectionEmbeddedInsets
MenuBar.selectionForeground
MenuBar.selectionInsets MenuBar.selectionInsets
MenuBar.shadow MenuBar.shadow
MenuBar.underlineSelectionBackground MenuBar.underlineSelectionBackground