Skip to content

Commit

Permalink
MenuBar: support different menu selection style UI defaults for `Menu…
Browse files Browse the repository at this point in the history
…Bar` and `MenuItem` (issue #587)
  • Loading branch information
DevCharly committed Sep 11, 2022
1 parent eb9fa58 commit d301f6e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -75,6 +76,8 @@
* <!-- FlatMenuRenderer -->
*
* @uiDefault MenuBar.hoverBackground Color
* @uiDefault MenuBar.selectionBackground Color
* @uiDefault MenuBar.selectionForeground Color
* @uiDefault MenuBar.underlineSelectionBackground Color
* @uiDefault MenuBar.underlineSelectionColor Color
* @uiDefault MenuBar.underlineSelectionHeight int
Expand Down Expand Up @@ -223,6 +226,8 @@ protected class FlatMenuRenderer
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 );
Expand All @@ -238,6 +243,10 @@ protected void paintBackground( Graphics g, Color selectionBackground ) {
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() ) {
Expand All @@ -250,6 +259,16 @@ protected void paintBackground( Graphics g, Color selectionBackground ) {
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() ) {
Expand Down
Expand Up @@ -284,6 +284,8 @@ void menuBar() {
Map<String, Class<?>> expected = expectedMap(
"itemMargins", Insets.class,
"hoverBackground", Color.class,
"selectionBackground", Color.class,
"selectionForeground", Color.class,
"underlineSelectionBackground", Color.class,
"underlineSelectionColor", Color.class,
"underlineSelectionHeight", int.class,
Expand Down
Expand Up @@ -380,6 +380,8 @@ void menuBar() {

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 );
Expand Down
Expand Up @@ -436,6 +436,8 @@ void menuBar() {

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" );
Expand Down
Expand Up @@ -476,6 +476,8 @@ MenuBar.foreground
MenuBar.highlight
MenuBar.hoverBackground
MenuBar.itemMargins
MenuBar.selectionBackground
MenuBar.selectionForeground
MenuBar.shadow
MenuBar.underlineSelectionBackground
MenuBar.underlineSelectionColor
Expand Down Expand Up @@ -503,6 +505,7 @@ MenuItem.minimumWidth
MenuItem.opaque
MenuItem.selectionBackground
MenuItem.selectionForeground
MenuItem.selectionType
MenuItem.textAcceleratorGap
MenuItem.textNoAcceleratorGap
MenuItem.underlineSelectionBackground
Expand Down

0 comments on commit d301f6e

Please sign in to comment.