mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27: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:
|
- Menus:
|
||||||
- Changed menu bar and popup menu background colors (made brighter in light
|
- Changed menu bar and popup menu background colors (made brighter in light
|
||||||
themes and darker in dark themes).
|
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.
|
- Popup menus now have empty space at the top and bottom.
|
||||||
- Menu items now have larger left and right margins.
|
- Menu items now have larger left and right margins.
|
||||||
- Made `JMenu`, `JMenuItem`, `JCheckBoxMenuItem` and `JRadioButtonMenuItem`
|
- Made `JMenu`, `JMenuItem`, `JCheckBoxMenuItem` and `JRadioButtonMenuItem`
|
||||||
|
|||||||
@@ -17,11 +17,17 @@
|
|||||||
package com.formdev.flatlaf.ui;
|
package com.formdev.flatlaf.ui;
|
||||||
|
|
||||||
import static com.formdev.flatlaf.util.UIScale.scale;
|
import static com.formdev.flatlaf.util.UIScale.scale;
|
||||||
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
import java.beans.PropertyChangeListener;
|
import java.beans.PropertyChangeListener;
|
||||||
|
import javax.swing.ButtonModel;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
|
import javax.swing.JMenu;
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
|
import javax.swing.UIManager;
|
||||||
|
import javax.swing.event.MouseInputListener;
|
||||||
import javax.swing.plaf.ComponentUI;
|
import javax.swing.plaf.ComponentUI;
|
||||||
import javax.swing.plaf.basic.BasicMenuUI;
|
import javax.swing.plaf.basic.BasicMenuUI;
|
||||||
|
|
||||||
@@ -51,11 +57,17 @@ import javax.swing.plaf.basic.BasicMenuUI;
|
|||||||
* @uiDefault Menu.useMenuBarBackgroundForTopLevel boolean default is false
|
* @uiDefault Menu.useMenuBarBackgroundForTopLevel boolean default is false
|
||||||
* @uiDefault MenuBar.background Color used if Menu.useMenuBarBackgroundForTopLevel is true
|
* @uiDefault MenuBar.background Color used if Menu.useMenuBarBackgroundForTopLevel is true
|
||||||
*
|
*
|
||||||
|
* <!-- FlatMenuUI -->
|
||||||
|
*
|
||||||
|
* @uiDefault MenuBar.hoverBackground Color
|
||||||
|
*
|
||||||
* @author Karl Tauber
|
* @author Karl Tauber
|
||||||
*/
|
*/
|
||||||
public class FlatMenuUI
|
public class FlatMenuUI
|
||||||
extends BasicMenuUI
|
extends BasicMenuUI
|
||||||
{
|
{
|
||||||
|
private Color hoverBackground;
|
||||||
|
|
||||||
public static ComponentUI createUI( JComponent c ) {
|
public static ComponentUI createUI( JComponent c ) {
|
||||||
return new FlatMenuUI();
|
return new FlatMenuUI();
|
||||||
}
|
}
|
||||||
@@ -64,10 +76,21 @@ public class FlatMenuUI
|
|||||||
protected void installDefaults() {
|
protected void installDefaults() {
|
||||||
super.installDefaults();
|
super.installDefaults();
|
||||||
|
|
||||||
|
menuItem.setRolloverEnabled( true );
|
||||||
|
|
||||||
|
hoverBackground = UIManager.getColor( "MenuBar.hoverBackground" );
|
||||||
|
|
||||||
// scale
|
// scale
|
||||||
defaultTextIconGap = scale( defaultTextIconGap );
|
defaultTextIconGap = scale( defaultTextIconGap );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void uninstallDefaults() {
|
||||||
|
super.uninstallDefaults();
|
||||||
|
|
||||||
|
hoverBackground = null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scale defaultTextIconGap again if iconTextGap property has changed.
|
* 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
|
@Override
|
||||||
protected void paintText( Graphics g, JMenuItem menuItem, Rectangle textRect, String text ) {
|
protected void paintText( Graphics g, JMenuItem menuItem, Rectangle textRect, String text ) {
|
||||||
FlatMenuItemUI.paintText( g, menuItem, textRect, text, disabledForeground, selectionForeground );
|
FlatMenuItemUI.paintText( g, menuItem, textRect, text, disabledForeground, selectionForeground );
|
||||||
|
|||||||
@@ -145,6 +145,7 @@ Menu.icon.disabledArrowColor=#606060
|
|||||||
#---- MenuBar ----
|
#---- MenuBar ----
|
||||||
|
|
||||||
MenuBar.borderColor=#515151
|
MenuBar.borderColor=#515151
|
||||||
|
MenuBar.hoverBackground=lighten($MenuBar.background,10%)
|
||||||
|
|
||||||
|
|
||||||
#---- MenuItemCheckBox ----
|
#---- MenuItemCheckBox ----
|
||||||
|
|||||||
@@ -152,6 +152,7 @@ Menu.icon.disabledArrowColor=#ABABAB
|
|||||||
#---- MenuBar ----
|
#---- MenuBar ----
|
||||||
|
|
||||||
MenuBar.borderColor=#cdcdcd
|
MenuBar.borderColor=#cdcdcd
|
||||||
|
MenuBar.hoverBackground=darken($MenuBar.background,10%)
|
||||||
|
|
||||||
|
|
||||||
#---- MenuItemCheckBox ----
|
#---- MenuItemCheckBox ----
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
@selectionInactiveForeground=#ffffff
|
@selectionInactiveForeground=#ffffff
|
||||||
@disabledText=#000088
|
@disabledText=#000088
|
||||||
@textComponentBackground=#ffffff
|
@textComponentBackground=#ffffff
|
||||||
|
@menuBackground=#fff
|
||||||
@cellFocusColor=#ff0000
|
@cellFocusColor=#ff0000
|
||||||
@icon=#afafaf
|
@icon=#afafaf
|
||||||
|
|
||||||
@@ -157,6 +158,7 @@ Menu.icon.disabledArrowColor=#ABABAB
|
|||||||
#---- MenuBar ----
|
#---- MenuBar ----
|
||||||
|
|
||||||
MenuBar.borderColor=#4444ff
|
MenuBar.borderColor=#4444ff
|
||||||
|
MenuBar.hoverBackground=#fdd
|
||||||
|
|
||||||
|
|
||||||
#---- MenuItemCheckBox ----
|
#---- MenuItemCheckBox ----
|
||||||
|
|||||||
Reference in New Issue
Block a user