mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 07:17:13 -06:00
Menus: support rounded selection
This commit is contained in:
@@ -70,6 +70,9 @@ public class FlatMenuBarUI
|
||||
/** @since 2 */ @Styleable protected Insets itemMargins;
|
||||
|
||||
// used in FlatMenuUI
|
||||
/** @since 3 */ @Styleable protected Insets selectionInsets;
|
||||
/** @since 3 */ @Styleable protected Insets selectionEmbeddedInsets;
|
||||
/** @since 3 */ @Styleable protected int selectionArc = -1;
|
||||
/** @since 2 */ @Styleable protected Color hoverBackground;
|
||||
/** @since 2 */ @Styleable protected Color underlineSelectionBackground;
|
||||
/** @since 2 */ @Styleable protected Color underlineSelectionColor;
|
||||
|
||||
@@ -63,6 +63,8 @@ import com.formdev.flatlaf.util.SystemInfo;
|
||||
* @uiDefault MenuItem.acceleratorArrowGap int
|
||||
* @uiDefault MenuItem.checkBackground Color
|
||||
* @uiDefault MenuItem.checkMargins Insets
|
||||
* @uiDefault MenuItem.selectionInsets Insets
|
||||
* @uiDefault MenuItem.selectionArc int
|
||||
* @uiDefault MenuItem.selectionType String null (default) or underline
|
||||
* @uiDefault MenuItem.underlineSelectionBackground Color
|
||||
* @uiDefault MenuItem.underlineSelectionCheckBackground Color
|
||||
@@ -91,6 +93,9 @@ public class FlatMenuItemRenderer
|
||||
@Styleable protected Color checkBackground = UIManager.getColor( "MenuItem.checkBackground" );
|
||||
@Styleable protected Insets checkMargins = UIManager.getInsets( "MenuItem.checkMargins" );
|
||||
|
||||
/** @since 3 */ @Styleable protected Insets selectionInsets = UIManager.getInsets( "MenuItem.selectionInsets" );
|
||||
/** @since 3 */ @Styleable protected int selectionArc = UIManager.getInt( "MenuItem.selectionArc" );
|
||||
|
||||
@Styleable protected Color underlineSelectionBackground = UIManager.getColor( "MenuItem.underlineSelectionBackground" );
|
||||
@Styleable protected Color underlineSelectionCheckBackground = UIManager.getColor( "MenuItem.underlineSelectionCheckBackground" );
|
||||
@Styleable protected Color underlineSelectionColor = UIManager.getColor( "MenuItem.underlineSelectionColor" );
|
||||
@@ -321,10 +326,16 @@ public class FlatMenuItemRenderer
|
||||
g.setColor( Color.orange ); g.drawRect( arrowRect.x, arrowRect.y, arrowRect.width - 1, arrowRect.height - 1 );
|
||||
debug*/
|
||||
|
||||
boolean armedOrSelected = isArmedOrSelected( menuItem );
|
||||
boolean underlineSelection = isUnderlineSelection();
|
||||
paintBackground( g, underlineSelection ? underlineSelectionBackground : selectionBackground );
|
||||
if( underlineSelection && isArmedOrSelected( menuItem ) )
|
||||
paintUnderlineSelection( g, underlineSelectionColor, underlineSelectionHeight );
|
||||
|
||||
paintBackground( g );
|
||||
if( armedOrSelected ) {
|
||||
if( underlineSelection )
|
||||
paintUnderlineSelection( g, underlineSelectionBackground, underlineSelectionColor, underlineSelectionHeight );
|
||||
else
|
||||
paintSelection( g, selectionBackground, selectionInsets, selectionArc );
|
||||
}
|
||||
paintIcon( g, iconRect, getIconForPainting(), underlineSelection ? underlineSelectionCheckBackground : checkBackground, selectionBackground );
|
||||
paintText( g, textRect, menuItem.getText(), selectionForeground, disabledForeground );
|
||||
paintAccelerator( g, accelRect, getAcceleratorText(), acceleratorForeground, acceleratorSelectionForeground, disabledForeground );
|
||||
@@ -332,21 +343,39 @@ debug*/
|
||||
paintArrowIcon( g, arrowRect, arrowIcon );
|
||||
}
|
||||
|
||||
protected void paintBackground( Graphics g, Color selectionBackground ) {
|
||||
boolean armedOrSelected = isArmedOrSelected( menuItem );
|
||||
if( menuItem.isOpaque() || armedOrSelected ) {
|
||||
// paint background
|
||||
g.setColor( armedOrSelected
|
||||
? deriveBackground( selectionBackground )
|
||||
: menuItem.getBackground() );
|
||||
/** @since 3 */
|
||||
protected void paintBackground( Graphics g ) {
|
||||
if( menuItem.isOpaque() ) {
|
||||
g.setColor( menuItem.getBackground() );
|
||||
g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() );
|
||||
}
|
||||
}
|
||||
|
||||
protected void paintUnderlineSelection( Graphics g, Color underlineSelectionColor, int underlineSelectionHeight ) {
|
||||
/** @since 3 */
|
||||
protected void paintSelection( Graphics g, Color selectionBackground, Insets selectionInsets, int selectionArc ) {
|
||||
Rectangle r = FlatUIUtils.subtractInsets( new Rectangle( menuItem.getSize() ), scale( selectionInsets ) );
|
||||
|
||||
g.setColor( deriveBackground( selectionBackground ) );
|
||||
if( selectionArc > 0 ) {
|
||||
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints( g );
|
||||
FlatUIUtils.paintComponentBackground( (Graphics2D) g, r.x, r.y, r.width, r.height, 0, scale( selectionArc ) );
|
||||
FlatUIUtils.resetRenderingHints( g, oldRenderingHints );
|
||||
} else
|
||||
g.fillRect( r.x, r.y, r.width, r.height );
|
||||
}
|
||||
|
||||
/** @since 3 */
|
||||
protected void paintUnderlineSelection( Graphics g, Color underlineSelectionBackground,
|
||||
Color underlineSelectionColor, int underlineSelectionHeight )
|
||||
{
|
||||
int width = menuItem.getWidth();
|
||||
int height = menuItem.getHeight();
|
||||
|
||||
// paint background
|
||||
g.setColor( deriveBackground( underlineSelectionBackground ) );
|
||||
g.fillRect( 0, 0, width, height );
|
||||
|
||||
// paint underline
|
||||
int underlineHeight = scale( underlineSelectionHeight );
|
||||
g.setColor( underlineSelectionColor );
|
||||
if( isTopLevelMenu( menuItem ) ) {
|
||||
|
||||
@@ -20,6 +20,8 @@ import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Insets;
|
||||
import java.awt.Window;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.util.Map;
|
||||
@@ -30,7 +32,9 @@ import javax.swing.JComponent;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JRootPane;
|
||||
import javax.swing.LookAndFeel;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.event.MouseInputListener;
|
||||
import javax.swing.plaf.ComponentUI;
|
||||
@@ -71,6 +75,9 @@ import com.formdev.flatlaf.util.LoggingFacade;
|
||||
*
|
||||
* <!-- FlatMenuRenderer -->
|
||||
*
|
||||
* @uiDefault MenuBar.selectionInsets Insets
|
||||
* @uiDefault MenuBar.selectionEmbeddedInsets Insets
|
||||
* @uiDefault MenuBar.selectionArc int
|
||||
* @uiDefault MenuBar.hoverBackground Color
|
||||
* @uiDefault MenuBar.underlineSelectionBackground Color
|
||||
* @uiDefault MenuBar.underlineSelectionColor Color
|
||||
@@ -215,6 +222,9 @@ public class FlatMenuUI
|
||||
protected class FlatMenuRenderer
|
||||
extends FlatMenuItemRenderer
|
||||
{
|
||||
/** @since 3 */ protected Insets menuBarSelectionInsets = UIManager.getInsets( "MenuBar.selectionInsets" );
|
||||
/** @since 3 */ protected Insets menuBarSelectionEmbeddedInsets = UIManager.getInsets( "MenuBar.selectionEmbeddedInsets" );
|
||||
/** @since 3 */ protected int menuBarSelectionArc = UIManager.getInt( "MenuBar.selectionArc" );
|
||||
protected Color hoverBackground = UIManager.getColor( "MenuBar.hoverBackground" );
|
||||
protected Color menuBarUnderlineSelectionBackground = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionBackground", underlineSelectionBackground );
|
||||
protected Color menuBarUnderlineSelectionColor = FlatUIUtils.getUIColor( "MenuBar.underlineSelectionColor", underlineSelectionColor );
|
||||
@@ -226,32 +236,61 @@ public class FlatMenuUI
|
||||
super( menuItem, checkIcon, arrowIcon, acceleratorFont, acceleratorDelimiter );
|
||||
}
|
||||
|
||||
/** @since 3 */
|
||||
@Override
|
||||
protected void paintBackground( Graphics g, Color selectionBackground ) {
|
||||
protected void paintBackground( Graphics g ) {
|
||||
if( ((JMenu)menuItem).isTopLevelMenu() ) {
|
||||
if( isUnderlineSelection() )
|
||||
selectionBackground = getStyleFromMenuBarUI( ui -> ui.underlineSelectionBackground, menuBarUnderlineSelectionBackground );
|
||||
|
||||
ButtonModel model = menuItem.getModel();
|
||||
if( model.isRollover() && !model.isArmed() && !model.isSelected() && model.isEnabled() ) {
|
||||
g.setColor( deriveBackground( getStyleFromMenuBarUI( ui -> ui.hoverBackground, hoverBackground ) ) );
|
||||
g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() );
|
||||
// paint hover background
|
||||
Color color = deriveBackground( getStyleFromMenuBarUI( ui -> ui.hoverBackground, hoverBackground ) );
|
||||
if( isUnderlineSelection() ) {
|
||||
g.setColor( color );
|
||||
g.fillRect( 0, 0, menuItem.getWidth(), menuItem.getHeight() );
|
||||
} else
|
||||
paintSelection( g, color, selectionInsets, selectionArc );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
super.paintBackground( g, selectionBackground );
|
||||
super.paintBackground( g );
|
||||
}
|
||||
|
||||
/** @since 3 */
|
||||
@Override
|
||||
protected void paintUnderlineSelection( Graphics g, Color underlineSelectionColor, int underlineSelectionHeight ) {
|
||||
protected void paintSelection( Graphics g, Color selectionBackground, Insets selectionInsets,
|
||||
int selectionArc )
|
||||
{
|
||||
if( ((JMenu)menuItem).isTopLevelMenu() ) {
|
||||
JMenuBar menuBar = (JMenuBar) menuItem.getParent();
|
||||
JRootPane rootPane = SwingUtilities.getRootPane( menuBar );
|
||||
if( rootPane != null && rootPane.getParent() instanceof Window &&
|
||||
rootPane.getJMenuBar() == menuBar &&
|
||||
FlatRootPaneUI.isMenuBarEmbedded( rootPane ) )
|
||||
{
|
||||
selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionEmbeddedInsets, menuBarSelectionEmbeddedInsets );
|
||||
} else
|
||||
selectionInsets = getStyleFromMenuBarUI( ui -> ui.selectionInsets, menuBarSelectionInsets );
|
||||
selectionArc = getStyleFromMenuBarUI( ui -> (ui.selectionArc != -1)
|
||||
? ui.selectionArc : null, menuBarSelectionArc );
|
||||
}
|
||||
|
||||
super.paintSelection( g, selectionBackground, selectionInsets, selectionArc );
|
||||
}
|
||||
|
||||
/** @since 3 */
|
||||
@Override
|
||||
protected void paintUnderlineSelection( Graphics g, Color underlineSelectionBackground,
|
||||
Color underlineSelectionColor, int underlineSelectionHeight )
|
||||
{
|
||||
if( ((JMenu)menuItem).isTopLevelMenu() ) {
|
||||
underlineSelectionBackground = getStyleFromMenuBarUI( ui -> ui.underlineSelectionBackground, menuBarUnderlineSelectionBackground );
|
||||
underlineSelectionColor = getStyleFromMenuBarUI( ui -> ui.underlineSelectionColor, menuBarUnderlineSelectionColor );
|
||||
underlineSelectionHeight = getStyleFromMenuBarUI( ui -> (ui.underlineSelectionHeight != -1)
|
||||
? ui.underlineSelectionHeight : null, menuBarUnderlineSelectionHeight );
|
||||
}
|
||||
|
||||
super.paintUnderlineSelection( g, underlineSelectionColor, underlineSelectionHeight );
|
||||
super.paintUnderlineSelection( g, underlineSelectionBackground, underlineSelectionColor, underlineSelectionHeight );
|
||||
}
|
||||
|
||||
private <T> T getStyleFromMenuBarUI( Function<FlatMenuBarUI, T> f, T defaultValue ) {
|
||||
|
||||
@@ -422,6 +422,9 @@ MenuBar.border = com.formdev.flatlaf.ui.FlatMenuBarBorder
|
||||
MenuBar.background = @menuBackground
|
||||
MenuBar.hoverBackground = @menuHoverBackground
|
||||
MenuBar.itemMargins = 3,8,3,8
|
||||
MenuBar.selectionInsets = 0,0,0,0
|
||||
MenuBar.selectionEmbeddedInsets = 0,0,0,0
|
||||
MenuBar.selectionArc = 0
|
||||
|
||||
|
||||
#---- MenuItem ----
|
||||
@@ -444,6 +447,8 @@ MenuItem.textNoAcceleratorGap = 6
|
||||
MenuItem.acceleratorArrowGap = 2
|
||||
MenuItem.acceleratorDelimiter = +
|
||||
[mac]MenuItem.acceleratorDelimiter =
|
||||
MenuItem.selectionInsets = 0,0,0,0
|
||||
MenuItem.selectionArc = 0
|
||||
|
||||
# for MenuItem.selectionType = underline
|
||||
MenuItem.underlineSelectionBackground = @menuHoverBackground
|
||||
|
||||
@@ -271,6 +271,9 @@ public class TestFlatStyleableInfo
|
||||
|
||||
Map<String, Class<?>> expected = expectedMap(
|
||||
"itemMargins", Insets.class,
|
||||
"selectionInsets", Insets.class,
|
||||
"selectionEmbeddedInsets", Insets.class,
|
||||
"selectionArc", int.class,
|
||||
"hoverBackground", Color.class,
|
||||
"underlineSelectionBackground", Color.class,
|
||||
"underlineSelectionColor", Color.class,
|
||||
@@ -355,6 +358,9 @@ public class TestFlatStyleableInfo
|
||||
"checkBackground", Color.class,
|
||||
"checkMargins", Insets.class,
|
||||
|
||||
"selectionInsets", Insets.class,
|
||||
"selectionArc", int.class,
|
||||
|
||||
"underlineSelectionBackground", Color.class,
|
||||
"underlineSelectionCheckBackground", Color.class,
|
||||
"underlineSelectionColor", Color.class,
|
||||
|
||||
@@ -423,6 +423,9 @@ public class TestFlatStyling
|
||||
FlatMenuBarUI ui = (FlatMenuBarUI) c.getUI();
|
||||
|
||||
ui.applyStyle( "itemMargins: 1,2,3,4" );
|
||||
ui.applyStyle( "selectionInsets: 1,2,3,4" );
|
||||
ui.applyStyle( "selectionEmbeddedInsets: 1,2,3,4" );
|
||||
ui.applyStyle( "selectionArc: 8" );
|
||||
ui.applyStyle( "hoverBackground: #fff" );
|
||||
ui.applyStyle( "underlineSelectionBackground: #fff" );
|
||||
ui.applyStyle( "underlineSelectionColor: #fff" );
|
||||
@@ -509,6 +512,9 @@ public class TestFlatStyling
|
||||
applyStyle.accept( "checkBackground: #fff" );
|
||||
applyStyle.accept( "checkMargins: 1,2,3,4" );
|
||||
|
||||
applyStyle.accept( "selectionInsets: 1,2,3,4" );
|
||||
applyStyle.accept( "selectionArc: 8" );
|
||||
|
||||
applyStyle.accept( "underlineSelectionBackground: #fff" );
|
||||
applyStyle.accept( "underlineSelectionCheckBackground: #fff" );
|
||||
applyStyle.accept( "underlineSelectionColor: #fff" );
|
||||
|
||||
Reference in New Issue
Block a user