MenuBar: support different menu selection style UI defaults for MenuBar and MenuItem (issue #587)

This commit is contained in:
Karl Tauber
2022-09-11 01:04:35 +02:00
parent eb9fa585f7
commit d301f6e104
7 changed files with 32 additions and 0 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;
* <!-- 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
@@ -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() ) {

View File

@@ -284,6 +284,8 @@ public class TestFlatStyleableInfo
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,

View File

@@ -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 );

View File

@@ -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" );

View File

@@ -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