From 873a7e857236fca5ce9cec0d470c5a1851a3fc15 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sun, 30 Oct 2022 10:10:22 +0100 Subject: [PATCH] Menu: fixed missing background on hover if top-level JMenu is opaque and `selectionInsets` or `selectionArc` are set (PR #536) --- .../com/formdev/flatlaf/ui/FlatMenuUI.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) 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 72087479c..4bfa55579 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 @@ -251,6 +251,8 @@ protected FlatMenuRenderer( JMenuItem menuItem, Icon checkIcon, Icon arrowIcon, /** @since 3 */ @Override protected void paintBackground( Graphics g ) { + super.paintBackground( g ); + if( ((JMenu)menuItem).isTopLevelMenu() && isHover() ) { // paint hover background Color color = deriveBackground( getStyleFromMenuBarUI( ui -> ui.hoverBackground, hoverBackground ) ); @@ -259,10 +261,7 @@ protected void paintBackground( Graphics g ) { g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() ); } else paintSelection( g, color, selectionInsets, selectionArc ); - return; } - - super.paintBackground( g ); } /** @since 3 */ @@ -289,14 +288,6 @@ protected void paintSelection( Graphics g, Color selectionBackground, Insets sel super.paintSelection( g, selectionBackground, selectionInsets, selectionArc ); } - @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, selectionForeground ); - - super.paintText( g, textRect, text, selectionForeground, disabledForeground ); - } - /** @since 3 */ @Override protected void paintUnderlineSelection( Graphics g, Color underlineSelectionBackground, @@ -312,6 +303,14 @@ protected void paintUnderlineSelection( Graphics g, Color underlineSelectionBack super.paintUnderlineSelection( g, underlineSelectionBackground, underlineSelectionColor, underlineSelectionHeight ); } + @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, selectionForeground ); + + super.paintText( g, textRect, text, selectionForeground, disabledForeground ); + } + private boolean isHover() { ButtonModel model = menuItem.getModel(); return model.isRollover() && !model.isArmed() && !model.isSelected() && model.isEnabled();