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

View File

@@ -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> 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 ) {
MenuBarUI ui = ((JMenuBar)menuItem.getParent()).getUI();
if( !(ui instanceof FlatMenuBarUI) )

View File

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

View File

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

View File

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

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.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]

View File

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

View File

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