mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-10 22:17:13 -06:00
Menu: highlight items in menu bar on mouse hover (issue #49)
This commit is contained in:
@@ -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`
|
||||
|
||||
@@ -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
|
||||
*
|
||||
* <!-- FlatMenuUI -->
|
||||
*
|
||||
* @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 );
|
||||
|
||||
@@ -145,6 +145,7 @@ Menu.icon.disabledArrowColor=#606060
|
||||
#---- MenuBar ----
|
||||
|
||||
MenuBar.borderColor=#515151
|
||||
MenuBar.hoverBackground=lighten($MenuBar.background,10%)
|
||||
|
||||
|
||||
#---- MenuItemCheckBox ----
|
||||
|
||||
@@ -152,6 +152,7 @@ Menu.icon.disabledArrowColor=#ABABAB
|
||||
#---- MenuBar ----
|
||||
|
||||
MenuBar.borderColor=#cdcdcd
|
||||
MenuBar.hoverBackground=darken($MenuBar.background,10%)
|
||||
|
||||
|
||||
#---- MenuItemCheckBox ----
|
||||
|
||||
@@ -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 ----
|
||||
|
||||
Reference in New Issue
Block a user