From d301f6e104e77b49b4529581e135a1a0fa421bfd Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sun, 11 Sep 2022 01:04:35 +0200 Subject: [PATCH] MenuBar: support different menu selection style UI defaults for `MenuBar` and `MenuItem` (issue #587) --- CHANGELOG.md | 2 ++ .../com/formdev/flatlaf/ui/FlatMenuBarUI.java | 2 ++ .../com/formdev/flatlaf/ui/FlatMenuUI.java | 19 +++++++++++++++++++ .../flatlaf/ui/TestFlatStyleableInfo.java | 2 ++ .../flatlaf/ui/TestFlatStyleableValue.java | 2 ++ .../formdev/flatlaf/ui/TestFlatStyling.java | 2 ++ .../flatlaf/themeeditor/FlatLafUIKeys.txt | 3 +++ 7 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68905028..a0a9edd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ FlatLaf Change Log #### New features and improvements +- MenuBar: Support different menu selection style UI defaults for `MenuBar` and + `MenuItem`. (issue #587) - TabbedPane: New option to disable tab run rotation in wrap layout. Set UI value `TabbedPane.rotateTabRuns` to `false`. (issue #574) - Native window decorations (Windows 10/11 only): Added client property to mark 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 f3ef6855..ff18dd9d 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 @@ -76,6 +76,8 @@ public class FlatMenuBarUI // used in FlatMenuUI /** @since 2 */ @Styleable protected Color hoverBackground; + /** @since 2.5 */ @Styleable protected Color selectionBackground; + /** @since 2.5 */ @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 5587121b..25d9a49d 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 @@ -20,6 +20,7 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; +import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.beans.PropertyChangeListener; import java.lang.invoke.MethodHandles; @@ -75,6 +76,8 @@ import com.formdev.flatlaf.util.LoggingFacade; * * * @uiDefault MenuBar.hoverBackground Color + * @uiDefault MenuBar.selectionBackground Color + * @uiDefault MenuBar.selectionForeground Color * @uiDefault MenuBar.underlineSelectionBackground Color * @uiDefault MenuBar.underlineSelectionColor Color * @uiDefault MenuBar.underlineSelectionHeight int @@ -223,6 +226,8 @@ public class FlatMenuUI extends FlatMenuItemRenderer { protected Color hoverBackground = UIManager.getColor( "MenuBar.hoverBackground" ); + protected Color menuBarSelectionBackground = UIManager.getColor( "MenuBar.selectionBackground" ); + protected Color menuBarSelectionForeground = UIManager.getColor( "MenuBar.selectionForeground" ); protected Color menuBarUnderlineSelectionBackground = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionBackground", underlineSelectionBackground ); protected Color menuBarUnderlineSelectionColor = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionColor", underlineSelectionColor ); protected int menuBarUnderlineSelectionHeight = FlatUIUtils.getUIInt( "MenuBar.underlineSelectionHeight", underlineSelectionHeight ); @@ -238,6 +243,10 @@ public class FlatMenuUI if( ((JMenu)menuItem).isTopLevelMenu() ) { if( isUnderlineSelection() ) selectionBackground = getStyleFromMenuBarUI( ui -> ui.underlineSelectionBackground, menuBarUnderlineSelectionBackground ); + else { + selectionBackground = getStyleFromMenuBarUI( ui -> ui.selectionBackground, + menuBarSelectionBackground != null ? menuBarSelectionBackground : selectionBackground ); + } ButtonModel model = menuItem.getModel(); if( model.isRollover() && !model.isArmed() && !model.isSelected() && model.isEnabled() ) { @@ -250,6 +259,16 @@ public class FlatMenuUI super.paintBackground( g, selectionBackground ); } + @Override + protected void paintText( Graphics g, Rectangle textRect, String text, Color selectionForeground, Color disabledForeground ) { + if( ((JMenu)menuItem).isTopLevelMenu() && !isUnderlineSelection() ) { + selectionForeground = getStyleFromMenuBarUI( ui -> ui.selectionForeground, + menuBarSelectionForeground != null ? menuBarSelectionForeground : selectionForeground ); + } + + super.paintText( g, textRect, text, selectionForeground, disabledForeground ); + } + @Override protected void paintUnderlineSelection( Graphics g, Color underlineSelectionColor, int underlineSelectionHeight ) { if( ((JMenu)menuItem).isTopLevelMenu() ) { 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 cbc87636..417795e7 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 @@ -284,6 +284,8 @@ public class TestFlatStyleableInfo Map> expected = expectedMap( "itemMargins", Insets.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/TestFlatStyleableValue.java b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableValue.java index 1cf294b7..d96db0fa 100644 --- a/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableValue.java +++ b/flatlaf-core/src/test/java/com/formdev/flatlaf/ui/TestFlatStyleableValue.java @@ -380,6 +380,8 @@ public class TestFlatStyleableValue testInsets( c, ui, "itemMargins", 1,2,3,4 ); testColor( c, ui, "hoverBackground", 0x123456 ); + testColor( c, ui, "selectionBackground", 0x123456 ); + testColor( c, ui, "selectionForeground", 0x123456 ); testColor( c, ui, "underlineSelectionBackground", 0x123456 ); testColor( c, ui, "underlineSelectionColor", 0x123456 ); testInteger( c, ui, "underlineSelectionHeight", 123 ); 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 fd28dca5..b1e3e956 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 @@ -436,6 +436,8 @@ public class TestFlatStyling ui.applyStyle( "itemMargins: 1,2,3,4" ); 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-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 a6a59cc2..288990f6 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 @@ -476,6 +476,8 @@ MenuBar.foreground MenuBar.highlight MenuBar.hoverBackground MenuBar.itemMargins +MenuBar.selectionBackground +MenuBar.selectionForeground MenuBar.shadow MenuBar.underlineSelectionBackground MenuBar.underlineSelectionColor @@ -503,6 +505,7 @@ MenuItem.minimumWidth MenuItem.opaque MenuItem.selectionBackground MenuItem.selectionForeground +MenuItem.selectionType MenuItem.textAcceleratorGap MenuItem.textNoAcceleratorGap MenuItem.underlineSelectionBackground