diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java index d1904120..2c2103e8 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java @@ -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 ); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java index 5b3cae88..163f6ee1 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuUI.java @@ -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 ); diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties index c4f01421..ce773fdb 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -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 diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties index 3a2cf32c..0febccb4 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -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 diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java index 5d57730c..082a08ac 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java @@ -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 ---------------------------------------------------- diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd index be75e601..0d0eb616 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd @@ -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 ) diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt index 4b6562d4..e5d5ca84 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatDarkLaf_1.8.0_202.txt @@ -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 diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt index 3563280d..49d42457 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/uidefaults/FlatLightLaf_1.8.0_202.txt @@ -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