diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java index 1c0ed4da..4361e203 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java @@ -74,6 +74,8 @@ public class FlatMenuBarUI /** @since 3 */ @Styleable protected Insets selectionEmbeddedInsets; /** @since 3 */ @Styleable protected int selectionArc = -1; /** @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 underlineSelectionColor; /** @since 2 */ @Styleable protected int underlineSelectionHeight = -1; diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java index f6e7cba5..b572d748 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java @@ -21,6 +21,7 @@ import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Insets; +import java.awt.Rectangle; import java.awt.Window; import java.awt.event.MouseEvent; import java.beans.PropertyChangeListener; @@ -79,6 +80,8 @@ import com.formdev.flatlaf.util.LoggingFacade; * @uiDefault MenuBar.selectionEmbeddedInsets Insets * @uiDefault MenuBar.selectionArc int * @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.underlineSelectionColor Color * @uiDefault MenuBar.underlineSelectionHeight int @@ -226,9 +229,11 @@ public class FlatMenuUI /** @since 3 */ protected Insets menuBarSelectionEmbeddedInsets = UIManager.getInsets( "MenuBar.selectionEmbeddedInsets" ); /** @since 3 */ protected int menuBarSelectionArc = UIManager.getInt( "MenuBar.selectionArc" ); protected Color hoverBackground = UIManager.getColor( "MenuBar.hoverBackground" ); - protected Color menuBarUnderlineSelectionBackground = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionBackground", underlineSelectionBackground ); - protected Color menuBarUnderlineSelectionColor = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionColor", underlineSelectionColor ); - protected int menuBarUnderlineSelectionHeight = FlatUIUtils.getUIInt( "MenuBar.underlineSelectionHeight", underlineSelectionHeight ); + /** @since 3 */ protected Color menuBarSelectionBackground = UIManager.getColor( "MenuBar.selectionBackground" ); + /** @since 3 */ protected Color menuBarSelectionForeground = UIManager.getColor( "MenuBar.selectionForeground" ); + 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, Font acceleratorFont, String acceleratorDelimiter ) @@ -239,18 +244,15 @@ public class FlatMenuUI /** @since 3 */ @Override protected void paintBackground( Graphics g ) { - if( ((JMenu)menuItem).isTopLevelMenu() ) { - ButtonModel model = menuItem.getModel(); - if( model.isRollover() && !model.isArmed() && !model.isSelected() && model.isEnabled() ) { - // paint hover background - Color color = deriveBackground( getStyleFromMenuBarUI( ui -> ui.hoverBackground, hoverBackground ) ); - if( isUnderlineSelection() ) { - g.setColor( color ); - g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() ); - } else - paintSelection( g, color, selectionInsets, selectionArc ); - return; - } + if( ((JMenu)menuItem).isTopLevelMenu() && isHover() ) { + // paint hover background + Color color = deriveBackground( getStyleFromMenuBarUI( ui -> ui.hoverBackground, hoverBackground ) ); + if( isUnderlineSelection() ) { + g.setColor( color ); + g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() ); + } else + paintSelection( g, color, selectionInsets, selectionArc ); + return; } super.paintBackground( g ); @@ -258,10 +260,11 @@ public class FlatMenuUI /** @since 3 */ @Override - protected void paintSelection( Graphics g, Color selectionBackground, Insets selectionInsets, - int selectionArc ) - { + protected void paintSelection( Graphics g, Color selectionBackground, Insets selectionInsets, int selectionArc ) { if( ((JMenu)menuItem).isTopLevelMenu() ) { + if( !isHover() ) + selectionBackground = getStyleFromMenuBarUI( ui -> ui.selectionBackground, menuBarSelectionBackground, selectionBackground ); + JMenuBar menuBar = (JMenuBar) menuItem.getParent(); JRootPane rootPane = SwingUtilities.getRootPane( menuBar ); if( rootPane != null && rootPane.getParent() instanceof Window && @@ -271,6 +274,7 @@ public class FlatMenuUI selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionEmbeddedInsets, menuBarSelectionEmbeddedInsets ); } else selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionInsets, menuBarSelectionInsets ); + selectionArc = getStyleFromMenuBarUI( ui -> (ui.selectionArc != -1) ? ui.selectionArc : null, menuBarSelectionArc ); } @@ -284,15 +288,32 @@ public class FlatMenuUI Color underlineSelectionColor, int underlineSelectionHeight ) { if( ((JMenu)menuItem).isTopLevelMenu() ) { - underlineSelectionBackground = getStyleFromMenuBarUI( ui -> ui.underlineSelectionBackground, menuBarUnderlineSelectionBackground ); - underlineSelectionColor = getStyleFromMenuBarUI( ui -> ui.underlineSelectionColor, menuBarUnderlineSelectionColor ); - underlineSelectionHeight = getStyleFromMenuBarUI( ui -> (ui.underlineSelectionHeight != -1) - ? ui.underlineSelectionHeight : null, menuBarUnderlineSelectionHeight ); + underlineSelectionBackground = getStyleFromMenuBarUI( ui -> ui.underlineSelectionBackground, menuBarUnderlineSelectionBackground, underlineSelectionBackground ); + underlineSelectionColor = getStyleFromMenuBarUI( ui -> ui.underlineSelectionColor, menuBarUnderlineSelectionColor, underlineSelectionColor ); + underlineSelectionHeight = getStyleFromMenuBarUI( ui -> (ui.underlineSelectionHeight != -1) ? ui.underlineSelectionHeight : null, + (menuBarUnderlineSelectionHeight != -1) ? menuBarUnderlineSelectionHeight : 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 getStyleFromMenuBarUI( Function f, T defaultValue, T defaultValue2 ) { + return getStyleFromMenuBarUI( f, (defaultValue != null) ? defaultValue : defaultValue2 ); + } + private T getStyleFromMenuBarUI( Function f, T defaultValue ) { MenuBarUI ui = ((JMenuBar)menuItem.getParent()).getUI(); if( !(ui instanceof FlatMenuBarUI) ) diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 066ce761..807f3df3 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -422,9 +422,9 @@ MenuBar.border = com.formdev.flatlaf.ui.FlatMenuBarBorder MenuBar.background = @menuBackground MenuBar.hoverBackground = @menuHoverBackground MenuBar.itemMargins = 3,8,3,8 -MenuBar.selectionInsets = 0,0,0,0 -MenuBar.selectionEmbeddedInsets = 0,0,0,0 -MenuBar.selectionArc = 0 +MenuBar.selectionInsets = $MenuItem.selectionInsets +MenuBar.selectionEmbeddedInsets = $MenuItem.selectionInsets +MenuBar.selectionArc = $MenuItem.selectionArc #---- MenuItem ---- diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java index 044860d6..f577dde7 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableInfo.java @@ -275,6 +275,8 @@ public class TestFlatStyleableInfo "selectionEmbeddedInsets", Insets.class, "selectionArc", int.class, "hoverBackground", Color.class, + "selectionBackground", Color.class, + "selectionForeground", Color.class, "underlineSelectionBackground", Color.class, "underlineSelectionColor", Color.class, "underlineSelectionHeight", int.class, diff --git a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java index c9f9da9a..51c107db 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyling.java @@ -427,6 +427,8 @@ public class TestFlatStyling ui.applyStyle( "selectionEmbeddedInsets: 1,2,3,4" ); ui.applyStyle( "selectionArc: 8" ); ui.applyStyle( "hoverBackground: #fff" ); + ui.applyStyle( "selectionBackground: #fff" ); + ui.applyStyle( "selectionForeground: #fff" ); ui.applyStyle( "underlineSelectionBackground: #fff" ); ui.applyStyle( "underlineSelectionColor: #fff" ); ui.applyStyle( "underlineSelectionHeight: 3" ); diff --git a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt index 3101df67..10bfa958 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt @@ -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.itemMargins 3,8,3,8 javax.swing.plaf.InsetsUIResource [UI] 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.shadow #a0a0a0 HSL 0 0 63 javax.swing.plaf.ColorUIResource [UI] MenuBar.underlineSelectionBackground #00ff00 HSL 120 100 50 javax.swing.plaf.ColorUIResource [UI] diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties index f31817e1..37482bdb 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties @@ -242,10 +242,13 @@ Menu.icon.disabledArrowColor = #ABABAB #---- MenuBar ---- MenuBar.selectionInsets = 1,3,1,3 +MenuBar.selectionEmbeddedInsets = 2,3,2,3 MenuBar.selectionArc = 8 MenuBar.borderColor = #44f MenuBar.hoverBackground = #fdd +MenuBar.selectionBackground = #f00 +MenuBar.selectionForeground = #0f0 MenuBar.underlineSelectionBackground = #0f0 MenuBar.underlineSelectionColor = #ff0 diff --git a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt index 22cf9d7f..613bdb83 100644 --- a/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt +++ b/flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt @@ -463,7 +463,9 @@ MenuBar.highlight MenuBar.hoverBackground MenuBar.itemMargins MenuBar.selectionArc +MenuBar.selectionBackground MenuBar.selectionEmbeddedInsets +MenuBar.selectionForeground MenuBar.selectionInsets MenuBar.shadow MenuBar.underlineSelectionBackground