- use derived colors for menu bar hover
- use derived colors for menu item selected background
- top-level JMenu now uses foreground color from parent JMenuBar

This allows changing menu bar background to dark with:
  UIManager.put( "MenuBar.background", Color.DARK_GRAY );
  UIManager.put( "MenuBar.foreground", Color.WHITE );
or
  menuBar.setBackground( Color.DARK_GRAY );
  menuBar.setForeground( Color.WHITE );

(issue #117)
This commit is contained in:
Karl Tauber
2020-06-25 11:36:36 +02:00
parent c25ff57b61
commit 46d3204bc3
8 changed files with 57 additions and 20 deletions

View File

@@ -261,7 +261,9 @@ debug*/
// paint background
g.setColor( armedOrSelected
? (isUnderlineSelection() ? underlineSelectionBackground : selectionBackground)
? (isUnderlineSelection()
? deriveBackground( underlineSelectionBackground )
: selectionBackground)
: menuItem.getBackground() );
g.fillRect( 0, 0, width, height );
@@ -283,12 +285,20 @@ debug*/
}
}
protected Color deriveBackground( Color background ) {
Color baseColor = menuItem.isOpaque()
? menuItem.getBackground()
: FlatUIUtils.getParentBackground( menuItem );
return FlatUIUtils.deriveColor( background, baseColor );
}
protected void paintIcon( Graphics g, Rectangle iconRect, Icon 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)
if( menuItem.isSelected() && checkIcon != null && icon != checkIcon ) {
Rectangle r = FlatUIUtils.addInsets( iconRect, scale( checkMargins ) );
g.setColor( isUnderlineSelection() ? underlineSelectionCheckBackground : checkBackground );
g.setColor( deriveBackground( isUnderlineSelection() ? underlineSelectionCheckBackground : checkBackground ) );
g.fillRect( r.x, r.y, r.width, r.height );
}
@@ -303,7 +313,7 @@ debug*/
}
int mnemonicIndex = FlatLaf.isShowMnemonics() ? menuItem.getDisplayedMnemonicIndex() : -1;
Color foreground = menuItem.getForeground();
Color foreground = (isTopLevelMenu( menuItem ) ? menuItem.getParent() : menuItem).getForeground();
paintText( g, menuItem, textRect, text, mnemonicIndex, menuItem.getFont(),
foreground, isUnderlineSelection() ? foreground : selectionForeground, disabledForeground );

View File

@@ -151,7 +151,7 @@ public class FlatMenuUI
if( model.isRollover() && !model.isArmed() && !model.isSelected() &&
model.isEnabled() && ((JMenu)menuItem).isTopLevelMenu() )
{
g.setColor( FlatUIUtils.deriveColor( hoverBackground, menuItem.getBackground() ) );
g.setColor( deriveBackground( hoverBackground ) );
g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() );
} else
super.paintBackground( g, selectionBackground );

View File

@@ -29,9 +29,9 @@
@disabledText=#777777
@textComponentBackground=#45494A
@menuBackground=darken(@background,5%)
@menuHoverBackground=lighten(@menuBackground,10%)
@menuCheckBackground=lighten(@menuBackground,10%)
@menuCheckHoverBackground=lighten(@menuBackground,20%)
@menuHoverBackground=lighten(@menuBackground,10%,derived)
@menuCheckBackground=lighten(@menuBackground,10%,derived)
@menuCheckHoverBackground=lighten(@menuBackground,20%,derived)
@cellFocusColor=#000000
@icon=#adadad

View File

@@ -29,9 +29,9 @@
@disabledText=#8C8C8C
@textComponentBackground=#ffffff
@menuBackground=#fff
@menuHoverBackground=darken(@menuBackground,10%)
@menuCheckBackground=darken(@menuBackground,10%)
@menuCheckHoverBackground=darken(@menuBackground,20%)
@menuHoverBackground=darken(@menuBackground,10%,derived)
@menuCheckBackground=darken(@menuBackground,10%,derived)
@menuCheckHoverBackground=darken(@menuBackground,20%,derived)
@cellFocusColor=#000000
@icon=#afafaf

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.testing;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
@@ -23,6 +24,7 @@ import java.awt.Graphics;
import java.awt.event.*;
import java.util.function.Supplier;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.util.UIScale;
import net.miginfocom.swing.*;
@@ -69,6 +71,13 @@ public class FlatMenusTest
FlatLaf.updateUI();
}
private void popupMenubackgroundChanged() {
UIManager.put( "PopupMenu.background", popupMenubackgroundCheckBox.isSelected()
? new ColorUIResource( Color.yellow )
: null );
FlatLaf.updateUI();
}
private void showPopupMenuButtonActionPerformed(ActionEvent e) {
Component invoker = (Component) e.getSource();
PopupMenu popupMenu = new PopupMenu();
@@ -207,6 +216,7 @@ public class FlatMenusTest
JButton showPopupMenuButton = new JButton();
armedCheckBox = new JCheckBox();
underlineCheckBox = new JCheckBox();
popupMenubackgroundCheckBox = new JCheckBox();
//======== this ========
setLayout(new MigLayout(
@@ -222,6 +232,7 @@ public class FlatMenusTest
"[top]" +
"[]" +
"[]" +
"[]" +
"[]"));
//---- menuBarLabel ----
@@ -738,6 +749,11 @@ public class FlatMenusTest
underlineCheckBox.addActionListener(e -> underlineChanged());
add(underlineCheckBox, "cell 0 4 2 1");
//---- popupMenubackgroundCheckBox ----
popupMenubackgroundCheckBox.setText("yellow popup menu background");
popupMenubackgroundCheckBox.addActionListener(e -> popupMenubackgroundChanged());
add(popupMenubackgroundCheckBox, "cell 0 5 2 1");
//---- buttonGroup1 ----
ButtonGroup buttonGroup1 = new ButtonGroup();
buttonGroup1.add(radioButtonMenuItem5);
@@ -756,6 +772,7 @@ public class FlatMenusTest
private JCheckBox accelCheckBox;
private JCheckBox armedCheckBox;
private JCheckBox underlineCheckBox;
private JCheckBox popupMenubackgroundCheckBox;
// JFormDesigner - End of variables declaration //GEN-END:variables
//---- class PopupMenu ----------------------------------------------------

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.1.0.272" Java: "13.0.2" encoding: "UTF-8"
JFDML JFormDesigner: "7.0.2.0.298" Java: "14" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -9,7 +9,7 @@ new FormModel {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,insets dialog,hidemode 3"
"$columnConstraints": "[125][][][][]"
"$rowConstraints": "[][top][][][]"
"$rowConstraints": "[][top][][][][]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
@@ -561,6 +561,16 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4 2 1"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "popupMenubackgroundCheckBox"
"text": "yellow popup menu background"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "popupMenubackgroundChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 5 2 1"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 790, 380 )

View File

@@ -515,7 +515,7 @@ MenuBar.borderColor #515151 javax.swing.plaf.ColorUIResource [UI]
MenuBar.font [active] $defaultFont [UI]
MenuBar.foreground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
MenuBar.highlight #242424 javax.swing.plaf.ColorUIResource [UI]
MenuBar.hoverBackground #484c4f javax.swing.plaf.ColorUIResource [UI]
MenuBar.hoverBackground #484c4f com.formdev.flatlaf.util.DerivedColor [UI] lighten(10% autoInverse)
MenuBar.itemMargins 3,8,3,8 javax.swing.plaf.InsetsUIResource [UI]
MenuBar.shadow #646464 javax.swing.plaf.ColorUIResource [UI]
MenuBar.windowBindings length=2 [Ljava.lang.Object;
@@ -535,7 +535,7 @@ MenuItem.arrowIcon [lazy] 6,10 com.formdev.flatlaf.icons.FlatMenu
MenuItem.background #303234 javax.swing.plaf.ColorUIResource [UI]
MenuItem.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatMenuItemBorder [UI]
MenuItem.borderPainted true
MenuItem.checkBackground #484c4f javax.swing.plaf.ColorUIResource [UI]
MenuItem.checkBackground #484c4f com.formdev.flatlaf.util.DerivedColor [UI] lighten(10% autoInverse)
MenuItem.checkMargins 2,2,2,2 javax.swing.plaf.InsetsUIResource [UI]
MenuItem.disabledForeground #777777 javax.swing.plaf.ColorUIResource [UI]
MenuItem.font [active] $defaultFont [UI]
@@ -549,8 +549,8 @@ MenuItem.selectionBackground #4b6eaf javax.swing.plaf.ColorUIResource [UI]
MenuItem.selectionForeground #bbbbbb javax.swing.plaf.ColorUIResource [UI]
MenuItem.textAcceleratorGap 24
MenuItem.textNoAcceleratorGap 6
MenuItem.underlineSelectionBackground #484c4f javax.swing.plaf.ColorUIResource [UI]
MenuItem.underlineSelectionCheckBackground #616569 javax.swing.plaf.ColorUIResource [UI]
MenuItem.underlineSelectionBackground #484c4f com.formdev.flatlaf.util.DerivedColor [UI] lighten(10% autoInverse)
MenuItem.underlineSelectionCheckBackground #616569 com.formdev.flatlaf.util.DerivedColor [UI] lighten(20% autoInverse)
MenuItem.underlineSelectionColor #4a88c7 javax.swing.plaf.ColorUIResource [UI]
MenuItem.underlineSelectionHeight 3

View File

@@ -520,7 +520,7 @@ MenuBar.borderColor #cdcdcd javax.swing.plaf.ColorUIResource [UI]
MenuBar.font [active] $defaultFont [UI]
MenuBar.foreground #000000 javax.swing.plaf.ColorUIResource [UI]
MenuBar.highlight #ffffff javax.swing.plaf.ColorUIResource [UI]
MenuBar.hoverBackground #e6e6e6 javax.swing.plaf.ColorUIResource [UI]
MenuBar.hoverBackground #e6e6e6 com.formdev.flatlaf.util.DerivedColor [UI] darken(10% autoInverse)
MenuBar.itemMargins 3,8,3,8 javax.swing.plaf.InsetsUIResource [UI]
MenuBar.shadow #c4c4c4 javax.swing.plaf.ColorUIResource [UI]
MenuBar.windowBindings length=2 [Ljava.lang.Object;
@@ -540,7 +540,7 @@ MenuItem.arrowIcon [lazy] 6,10 com.formdev.flatlaf.icons.FlatMenu
MenuItem.background #ffffff javax.swing.plaf.ColorUIResource [UI]
MenuItem.border [lazy] 0,0,0,0 false com.formdev.flatlaf.ui.FlatMenuItemBorder [UI]
MenuItem.borderPainted true
MenuItem.checkBackground #e6e6e6 javax.swing.plaf.ColorUIResource [UI]
MenuItem.checkBackground #e6e6e6 com.formdev.flatlaf.util.DerivedColor [UI] darken(10% autoInverse)
MenuItem.checkMargins 2,2,2,2 javax.swing.plaf.InsetsUIResource [UI]
MenuItem.disabledForeground #8c8c8c javax.swing.plaf.ColorUIResource [UI]
MenuItem.font [active] $defaultFont [UI]
@@ -554,8 +554,8 @@ MenuItem.selectionBackground #2675bf javax.swing.plaf.ColorUIResource [UI]
MenuItem.selectionForeground #ffffff javax.swing.plaf.ColorUIResource [UI]
MenuItem.textAcceleratorGap 24
MenuItem.textNoAcceleratorGap 6
MenuItem.underlineSelectionBackground #e6e6e6 javax.swing.plaf.ColorUIResource [UI]
MenuItem.underlineSelectionCheckBackground #cccccc javax.swing.plaf.ColorUIResource [UI]
MenuItem.underlineSelectionBackground #e6e6e6 com.formdev.flatlaf.util.DerivedColor [UI] darken(10% autoInverse)
MenuItem.underlineSelectionCheckBackground #cccccc com.formdev.flatlaf.util.DerivedColor [UI] darken(20% autoInverse)
MenuItem.underlineSelectionColor #4083c9 javax.swing.plaf.ColorUIResource [UI]
MenuItem.underlineSelectionHeight 3