Skip to content

Commit

Permalink
Menus: new menu item layout and renderer
Browse files Browse the repository at this point in the history
- stable left margin (always space for one icon)
- right aligned accelerators
- larger gap between text and accelerator

current limitations:
- no HTML text support
- text not vertically aligned with other menu items if icons have different sizes
- vertical/horizontal alignment/textPosition properties are ignored

(issues #3 and #54)
  • Loading branch information
DevCharly committed Apr 26, 2020
1 parent c9c703f commit fcbb3ae
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

package com.formdev.flatlaf.ui;

import static com.formdev.flatlaf.util.UIScale.scale;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.beans.PropertyChangeListener;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicCheckBoxMenuItemUI;

Expand All @@ -48,11 +46,20 @@
* @uiDefault CheckBoxMenuItem.opaque boolean
* @uiDefault CheckBoxMenuItem.evenHeight boolean
*
* <!-- FlatMenuItemRenderer -->
*
* @uiDefault MenuItem.minimumIconSize Dimension
* @uiDefault MenuItem.textAcceleratorGap int
* @uiDefault MenuItem.acceleratorArrowGap int
* @uiDefault MenuItem.textArrowGap int
*
* @author Karl Tauber
*/
public class FlatCheckBoxMenuItemUI
extends BasicCheckBoxMenuItemUI
{
private FlatMenuItemRenderer renderer;

public static ComponentUI createUI( JComponent c ) {
return new FlatCheckBoxMenuItemUI();
}
Expand All @@ -61,25 +68,28 @@ public static ComponentUI createUI( JComponent c ) {
protected void installDefaults() {
super.installDefaults();

// scale
defaultTextIconGap = scale( defaultTextIconGap );
renderer = createRenderer();
}

@Override
protected void uninstallDefaults() {
super.uninstallDefaults();

renderer = null;
}

protected FlatMenuItemRenderer createRenderer() {
return new FlatMenuItemRenderer( menuItem, checkIcon, arrowIcon, acceleratorFont, acceleratorDelimiter );
}

/**
* Scale defaultTextIconGap again if iconTextGap property has changed.
*/
@Override
protected PropertyChangeListener createPropertyChangeListener( JComponent c ) {
PropertyChangeListener superListener = super.createPropertyChangeListener( c );
return e -> {
superListener.propertyChange( e );
if( e.getPropertyName() == "iconTextGap" )
defaultTextIconGap = scale( defaultTextIconGap );
};
protected Dimension getPreferredMenuItemSize( JComponent c, Icon checkIcon, Icon arrowIcon, int defaultTextIconGap ) {
return renderer.getPreferredMenuItemSize();
}

@Override
protected void paintText( Graphics g, JMenuItem menuItem, Rectangle textRect, String text ) {
FlatMenuItemUI.paintText( g, menuItem, textRect, text, disabledForeground, selectionForeground );
public void paint( Graphics g, JComponent c ) {
renderer.paintMenuItem( g, selectionBackground, selectionForeground, disabledForeground,
acceleratorForeground, acceleratorSelectionForeground );
}
}
Loading

0 comments on commit fcbb3ae

Please sign in to comment.