MenuBar: support different underline menu selection style UI defaults for MenuBar and MenuItem. (PR #217; issue #216)

This commit is contained in:
Karl Tauber
2020-12-05 11:56:38 +01:00
parent f149d2b7cd
commit 14df490b2a
6 changed files with 65 additions and 34 deletions

View File

@@ -10,6 +10,8 @@ FlatLaf Change Log
- Added "Gradianto Nature Green" theme. - Added "Gradianto Nature Green" theme.
- Updated "Arc Dark", "Cyan", "Dark purple", "Gradianto", "Gray", "Gruvbox" - Updated "Arc Dark", "Cyan", "Dark purple", "Gradianto", "Gray", "Gruvbox"
and "One Dark" themes. and "One Dark" themes.
- MenuBar: Support different underline menu selection style UI defaults for
`MenuBar` and `MenuItem`. (PR #217; issue #216)
#### Fixed bugs #### Fixed bugs

View File

@@ -39,6 +39,7 @@ import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicHTML; import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View; import javax.swing.text.View;
import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.DerivedColor;
import com.formdev.flatlaf.util.Graphics2DProxy; import com.formdev.flatlaf.util.Graphics2DProxy;
import com.formdev.flatlaf.util.HiDPIUtils; import com.formdev.flatlaf.util.HiDPIUtils;
import com.formdev.flatlaf.util.SystemInfo; import com.formdev.flatlaf.util.SystemInfo;
@@ -55,7 +56,7 @@ import com.formdev.flatlaf.util.SystemInfo;
* @uiDefault MenuItem.underlineSelectionBackground Color * @uiDefault MenuItem.underlineSelectionBackground Color
* @uiDefault MenuItem.underlineSelectionCheckBackground Color * @uiDefault MenuItem.underlineSelectionCheckBackground Color
* @uiDefault MenuItem.underlineSelectionColor Color * @uiDefault MenuItem.underlineSelectionColor Color
* @uiDefault MenuItem.underlineSelectionHeight Color * @uiDefault MenuItem.underlineSelectionHeight int
* *
* @author Karl Tauber * @author Karl Tauber
*/ */
@@ -246,30 +247,32 @@ public class FlatMenuItemRenderer
g.setColor( Color.orange ); g.drawRect( arrowRect.x, arrowRect.y, arrowRect.width - 1, arrowRect.height - 1 ); g.setColor( Color.orange ); g.drawRect( arrowRect.x, arrowRect.y, arrowRect.width - 1, arrowRect.height - 1 );
debug*/ debug*/
paintBackground( g, selectionBackground, underlineSelectionColor ); boolean underlineSelection = isUnderlineSelection();
paintIcon( g, iconRect, getIconForPainting() ); paintBackground( g, underlineSelection ? underlineSelectionBackground : selectionBackground );
if( underlineSelection && isArmedOrSelected( menuItem ) )
paintUnderlineSelection( g, underlineSelectionColor, underlineSelectionHeight );
paintIcon( g, iconRect, getIconForPainting(), underlineSelection ? underlineSelectionCheckBackground : checkBackground );
paintText( g, textRect, menuItem.getText(), selectionForeground, disabledForeground ); paintText( g, textRect, menuItem.getText(), selectionForeground, disabledForeground );
paintAccelerator( g, accelRect, getAcceleratorText(), acceleratorForeground, acceleratorSelectionForeground, disabledForeground ); paintAccelerator( g, accelRect, getAcceleratorText(), acceleratorForeground, acceleratorSelectionForeground, disabledForeground );
if( !isTopLevelMenu( menuItem ) ) if( !isTopLevelMenu( menuItem ) )
paintArrowIcon( g, arrowRect, arrowIcon ); paintArrowIcon( g, arrowRect, arrowIcon );
} }
protected void paintBackground( Graphics g, Color selectionBackground, Color underlineSelectionColor ) { protected void paintBackground( Graphics g, Color selectionBackground ) {
boolean armedOrSelected = isArmedOrSelected( menuItem ); boolean armedOrSelected = isArmedOrSelected( menuItem );
if( menuItem.isOpaque() || armedOrSelected ) { if( menuItem.isOpaque() || armedOrSelected ) {
// paint background
g.setColor( armedOrSelected
? deriveBackground( selectionBackground )
: menuItem.getBackground() );
g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() );
}
}
protected void paintUnderlineSelection( Graphics g, Color underlineSelectionColor, int underlineSelectionHeight ) {
int width = menuItem.getWidth(); int width = menuItem.getWidth();
int height = menuItem.getHeight(); int height = menuItem.getHeight();
// paint background
g.setColor( armedOrSelected
? (isUnderlineSelection()
? deriveBackground( underlineSelectionBackground )
: selectionBackground)
: menuItem.getBackground() );
g.fillRect( 0, 0, width, height );
// paint underline
if( armedOrSelected && isUnderlineSelection() ) {
int underlineHeight = scale( underlineSelectionHeight ); int underlineHeight = scale( underlineSelectionHeight );
g.setColor( underlineSelectionColor ); g.setColor( underlineSelectionColor );
if( isTopLevelMenu( menuItem ) ) { if( isTopLevelMenu( menuItem ) ) {
@@ -283,10 +286,11 @@ debug*/
g.fillRect( width - underlineHeight, 0, underlineHeight, height ); g.fillRect( width - underlineHeight, 0, underlineHeight, height );
} }
} }
}
}
protected Color deriveBackground( Color background ) { protected Color deriveBackground( Color background ) {
if( !(background instanceof DerivedColor) )
return background;
Color baseColor = menuItem.isOpaque() Color baseColor = menuItem.isOpaque()
? menuItem.getBackground() ? menuItem.getBackground()
: FlatUIUtils.getParentBackground( menuItem ); : FlatUIUtils.getParentBackground( menuItem );
@@ -294,12 +298,12 @@ debug*/
return FlatUIUtils.deriveColor( background, baseColor ); return FlatUIUtils.deriveColor( background, baseColor );
} }
protected void paintIcon( Graphics g, Rectangle iconRect, Icon icon ) { protected void paintIcon( Graphics g, Rectangle iconRect, Icon icon, Color checkBackground ) {
// if checkbox/radiobutton menu item is selected and also has a custom icon, // if checkbox/radiobutton menu item is selected and also has a custom icon,
// then use filled icon background to indicate selection (instead of using checkIcon) // then use filled icon background to indicate selection (instead of using checkIcon)
if( menuItem.isSelected() && checkIcon != null && icon != checkIcon ) { if( menuItem.isSelected() && checkIcon != null && icon != checkIcon ) {
Rectangle r = FlatUIUtils.addInsets( iconRect, scale( checkMargins ) ); Rectangle r = FlatUIUtils.addInsets( iconRect, scale( checkMargins ) );
g.setColor( deriveBackground( isUnderlineSelection() ? underlineSelectionCheckBackground : checkBackground ) ); g.setColor( deriveBackground( checkBackground ) );
g.fillRect( r.x, r.y, r.width, r.height ); g.fillRect( r.x, r.y, r.width, r.height );
} }

View File

@@ -60,16 +60,19 @@ import javax.swing.plaf.basic.BasicMenuUI;
* <!-- FlatMenuUI --> * <!-- FlatMenuUI -->
* *
* @uiDefault MenuItem.iconTextGap int * @uiDefault MenuItem.iconTextGap int
* @uiDefault MenuBar.hoverBackground * @uiDefault MenuBar.hoverBackground Color
*
* <!-- FlatMenuRenderer -->
*
* @uiDefault MenuBar.underlineSelectionBackground Color
* @uiDefault MenuBar.underlineSelectionColor Color * @uiDefault MenuBar.underlineSelectionColor Color
* @uiDefault MenuBar.underlineSelectionHeight int
* *
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatMenuUI public class FlatMenuUI
extends BasicMenuUI extends BasicMenuUI
{ {
protected final Color menuBarUnderlineSelectionColor = UIManager.getColor( "MenuBar.underlineSelectionColor" );
private Color hoverBackground; private Color hoverBackground;
private FlatMenuItemRenderer renderer; private FlatMenuItemRenderer renderer;
@@ -150,6 +153,10 @@ public class FlatMenuUI
protected class FlatMenuRenderer protected class FlatMenuRenderer
extends FlatMenuItemRenderer extends FlatMenuItemRenderer
{ {
protected final Color menuBarUnderlineSelectionBackground = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionBackground", underlineSelectionBackground );
protected final Color menuBarUnderlineSelectionColor = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionColor", underlineSelectionColor );
protected final int menuBarUnderlineSelectionHeight = FlatUIUtils.getUIInt( "MenuBar.underlineSelectionHeight", underlineSelectionHeight );
protected FlatMenuRenderer( JMenuItem menuItem, Icon checkIcon, Icon arrowIcon, protected FlatMenuRenderer( JMenuItem menuItem, Icon checkIcon, Icon arrowIcon,
Font acceleratorFont, String acceleratorDelimiter ) Font acceleratorFont, String acceleratorDelimiter )
{ {
@@ -157,7 +164,10 @@ public class FlatMenuUI
} }
@Override @Override
protected void paintBackground( Graphics g, Color selectionBackground, Color menuItemUnderlineSelectionColor ) { protected void paintBackground( Graphics g, Color selectionBackground ) {
if( isUnderlineSelection() && ((JMenu)menuItem).isTopLevelMenu() )
selectionBackground = menuBarUnderlineSelectionBackground;
ButtonModel model = menuItem.getModel(); ButtonModel model = menuItem.getModel();
if( model.isRollover() && !model.isArmed() && !model.isSelected() && if( model.isRollover() && !model.isArmed() && !model.isSelected() &&
model.isEnabled() && ((JMenu)menuItem).isTopLevelMenu() ) model.isEnabled() && ((JMenu)menuItem).isTopLevelMenu() )
@@ -165,7 +175,17 @@ public class FlatMenuUI
g.setColor( deriveBackground( hoverBackground ) ); g.setColor( deriveBackground( hoverBackground ) );
g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() ); g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() );
} else } else
super.paintBackground( g, selectionBackground, ((JMenu)menuItem).isTopLevelMenu() ? menuBarUnderlineSelectionColor : menuItemUnderlineSelectionColor ); super.paintBackground( g, selectionBackground );
}
@Override
protected void paintUnderlineSelection( Graphics g, Color underlineSelectionColor, int underlineSelectionHeight ) {
if( ((JMenu)menuItem).isTopLevelMenu() ) {
underlineSelectionColor = menuBarUnderlineSelectionColor;
underlineSelectionHeight = menuBarUnderlineSelectionHeight;
}
super.paintUnderlineSelection( g, underlineSelectionColor, underlineSelectionHeight );
} }
} }
} }

View File

@@ -337,7 +337,6 @@ MenuBar.border=com.formdev.flatlaf.ui.FlatMenuBarBorder
MenuBar.background=@menuBackground MenuBar.background=@menuBackground
MenuBar.hoverBackground=@menuHoverBackground MenuBar.hoverBackground=@menuHoverBackground
MenuBar.itemMargins=3,8,3,8 MenuBar.itemMargins=3,8,3,8
MenuBar.underlineSelectionColor=$TabbedPane.underlineColor
#---- MenuItem ---- #---- MenuItem ----

View File

@@ -518,6 +518,9 @@ MenuBar.highlight #ffffff javax.swing.plaf.ColorUIResource [UI]
MenuBar.hoverBackground #ffdddd javax.swing.plaf.ColorUIResource [UI] MenuBar.hoverBackground #ffdddd javax.swing.plaf.ColorUIResource [UI]
MenuBar.itemMargins 3,8,3,8 javax.swing.plaf.InsetsUIResource [UI] MenuBar.itemMargins 3,8,3,8 javax.swing.plaf.InsetsUIResource [UI]
MenuBar.shadow #a0a0a0 javax.swing.plaf.ColorUIResource [UI] MenuBar.shadow #a0a0a0 javax.swing.plaf.ColorUIResource [UI]
MenuBar.underlineSelectionBackground #00ff00 javax.swing.plaf.ColorUIResource [UI]
MenuBar.underlineSelectionColor #ff0000 javax.swing.plaf.ColorUIResource [UI]
MenuBar.underlineSelectionHeight 5
MenuBar.windowBindings length=2 [Ljava.lang.Object; MenuBar.windowBindings length=2 [Ljava.lang.Object;
[0] F10 [0] F10
[1] takeFocus [1] takeFocus

View File

@@ -192,6 +192,9 @@ Menu.icon.disabledArrowColor=#ABABAB
MenuBar.borderColor=#44f MenuBar.borderColor=#44f
MenuBar.hoverBackground=#fdd MenuBar.hoverBackground=#fdd
MenuBar.underlineSelectionBackground=#0f0
MenuBar.underlineSelectionColor=#f00
MenuBar.underlineSelectionHeight=5
#---- MenuItemCheckBox ---- #---- MenuItemCheckBox ----