From 757b0812bac7e90bbc4915fae507b903f3e8f4dd Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 20 Jan 2020 23:35:50 +0100 Subject: [PATCH] Menu: highlight items in menu bar on mouse hover (issue #49) --- CHANGELOG.md | 1 + .../com/formdev/flatlaf/ui/FlatMenuUI.java | 60 +++++++++++++++++++ .../formdev/flatlaf/FlatDarkLaf.properties | 1 + .../formdev/flatlaf/FlatLightLaf.properties | 1 + .../flatlaf/testing/FlatTestLaf.properties | 2 + 5 files changed, 65 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee19ebb6..7bb6465d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ FlatLaf Change Log - Menus: - Changed menu bar and popup menu background colors (made brighter in light themes and darker in dark themes). + - Highlight items in menu bar on mouse hover. (issue #49) - Popup menus now have empty space at the top and bottom. - Menu items now have larger left and right margins. - Made `JMenu`, `JMenuItem`, `JCheckBoxMenuItem` and `JRadioButtonMenuItem` 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 3706b564..aa282875 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 @@ -17,11 +17,17 @@ package com.formdev.flatlaf.ui; import static com.formdev.flatlaf.util.UIScale.scale; +import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; +import java.awt.event.MouseEvent; import java.beans.PropertyChangeListener; +import javax.swing.ButtonModel; import javax.swing.JComponent; +import javax.swing.JMenu; import javax.swing.JMenuItem; +import javax.swing.UIManager; +import javax.swing.event.MouseInputListener; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicMenuUI; @@ -51,11 +57,17 @@ import javax.swing.plaf.basic.BasicMenuUI; * @uiDefault Menu.useMenuBarBackgroundForTopLevel boolean default is false * @uiDefault MenuBar.background Color used if Menu.useMenuBarBackgroundForTopLevel is true * + * + * + * @uiDefault MenuBar.hoverBackground Color + * * @author Karl Tauber */ public class FlatMenuUI extends BasicMenuUI { + private Color hoverBackground; + public static ComponentUI createUI( JComponent c ) { return new FlatMenuUI(); } @@ -64,10 +76,21 @@ public class FlatMenuUI protected void installDefaults() { super.installDefaults(); + menuItem.setRolloverEnabled( true ); + + hoverBackground = UIManager.getColor( "MenuBar.hoverBackground" ); + // scale defaultTextIconGap = scale( defaultTextIconGap ); } + @Override + protected void uninstallDefaults() { + super.uninstallDefaults(); + + hoverBackground = null; + } + /** * Scale defaultTextIconGap again if iconTextGap property has changed. */ @@ -81,6 +104,43 @@ public class FlatMenuUI }; } + @Override + protected MouseInputListener createMouseInputListener( JComponent c ) { + return new BasicMenuUI.MouseInputHandler() { + @Override + public void mouseEntered( MouseEvent e ) { + super.mouseEntered( e ); + rollover( e, true ); + } + + @Override + public void mouseExited( MouseEvent e ) { + super.mouseExited( e ); + rollover( e, false ); + } + + private void rollover( MouseEvent e, boolean rollover ) { + JMenu menu = (JMenu) e.getSource(); + if( menu.isTopLevelMenu() && menu.isRolloverEnabled() ) { + menu.getModel().setRollover( rollover ); + menu.repaint(); + } + } + }; + } + + @Override + protected void paintBackground( Graphics g, JMenuItem menuItem, Color bgColor ) { + ButtonModel model = menuItem.getModel(); + if( model.isArmed() || model.isSelected() ) { + super.paintBackground( g, menuItem, bgColor ); + } else if( model.isRollover() && model.isEnabled() && ((JMenu)menuItem).isTopLevelMenu() ) { + FlatUIUtils.setColor( g, hoverBackground, menuItem.getBackground() ); + g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() ); + } else + super.paintBackground( g, menuItem, bgColor ); + } + @Override protected void paintText( Graphics g, JMenuItem menuItem, Rectangle textRect, String text ) { FlatMenuItemUI.paintText( g, menuItem, textRect, text, disabledForeground, selectionForeground ); 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 47ab98dd..e529f9ff 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatDarkLaf.properties @@ -145,6 +145,7 @@ Menu.icon.disabledArrowColor=#606060 #---- MenuBar ---- MenuBar.borderColor=#515151 +MenuBar.hoverBackground=lighten($MenuBar.background,10%) #---- MenuItemCheckBox ---- 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 cfac801c..6039030a 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLightLaf.properties @@ -152,6 +152,7 @@ Menu.icon.disabledArrowColor=#ABABAB #---- MenuBar ---- MenuBar.borderColor=#cdcdcd +MenuBar.hoverBackground=darken($MenuBar.background,10%) #---- MenuItemCheckBox ---- diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties index f3fa729e..15353a21 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties @@ -24,6 +24,7 @@ @selectionInactiveForeground=#ffffff @disabledText=#000088 @textComponentBackground=#ffffff +@menuBackground=#fff @cellFocusColor=#ff0000 @icon=#afafaf @@ -157,6 +158,7 @@ Menu.icon.disabledArrowColor=#ABABAB #---- MenuBar ---- MenuBar.borderColor=#4444ff +MenuBar.hoverBackground=#fdd #---- MenuItemCheckBox ----