Menus: fixed icon in top-level JMenu (issue #3)

This commit is contained in:
Karl Tauber
2020-04-26 13:40:49 +02:00
parent 41dd0acfa3
commit 2735185eb9
3 changed files with 82 additions and 23 deletions

View File

@@ -76,15 +76,13 @@ public class FlatMenuItemRenderer
boolean isTopLevelMenu = isTopLevelMenu( menuItem ); boolean isTopLevelMenu = isTopLevelMenu( menuItem );
// icon size // icon size
if( !isTopLevelMenu ) { Dimension iconSize = getIconSize();
Dimension iconSize = getIconSize(); width += iconSize.width;
width += iconSize.width; height = Math.max( iconSize.height, height );
height = Math.max( iconSize.height, height );
// gap between icon and text // gap between icon and text
if( iconSize.width > 0 ) if( iconSize.width > 0 )
width += scale( menuItem.getIconTextGap() ); width += scale( menuItem.getIconTextGap() );
}
// text size // text size
View htmlView = (View) menuItem.getClientProperty( BasicHTML.propertyKey ); View htmlView = (View) menuItem.getClientProperty( BasicHTML.propertyKey );
@@ -135,10 +133,8 @@ public class FlatMenuItemRenderer
private void layout( Rectangle viewRect, Rectangle iconRect, Rectangle textRect, private void layout( Rectangle viewRect, Rectangle iconRect, Rectangle textRect,
Rectangle accelRect, Rectangle arrowRect ) Rectangle accelRect, Rectangle arrowRect )
{ {
boolean isTopLevelMenu = isTopLevelMenu( menuItem );
// layout icon // layout icon
iconRect.setSize( !isTopLevelMenu ? getIconSize() : new Dimension() ); iconRect.setSize( getIconSize() );
iconRect.y = viewRect.y + ((viewRect.height - iconRect.height) / 2); iconRect.y = viewRect.y + ((viewRect.height - iconRect.height) / 2);
// layout text // layout text
@@ -148,7 +144,7 @@ public class FlatMenuItemRenderer
textRect.y = viewRect.y + ((viewRect.height - textRect.height) / 2); textRect.y = viewRect.y + ((viewRect.height - textRect.height) / 2);
// layout arrow // layout arrow
Icon arrowIcon = !isTopLevelMenu ? this.arrowIcon : null; Icon arrowIcon = !isTopLevelMenu( menuItem ) ? this.arrowIcon : null;
arrowRect.width = (arrowIcon != null) ? arrowIcon.getIconWidth() : 0; arrowRect.width = (arrowIcon != null) ? arrowIcon.getIconWidth() : 0;
arrowRect.height = (arrowIcon != null) ? arrowIcon.getIconHeight() : 0; arrowRect.height = (arrowIcon != null) ? arrowIcon.getIconHeight() : 0;
arrowRect.y = viewRect.y + ((viewRect.height - arrowRect.height) / 2); arrowRect.y = viewRect.y + ((viewRect.height - arrowRect.height) / 2);
@@ -168,7 +164,7 @@ public class FlatMenuItemRenderer
// left-to-right // left-to-right
iconRect.x = viewRect.x; iconRect.x = viewRect.x;
textRect.x = iconRect.x + iconRect.width textRect.x = iconRect.x + iconRect.width
+ (!isTopLevelMenu && iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0); + (iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0);
arrowRect.x = viewRect.x + viewRect.width - arrowRect.width; arrowRect.x = viewRect.x + viewRect.width - arrowRect.width;
if( accelText != null ) if( accelText != null )
accelRect.x = arrowRect.x - accelRect.width; accelRect.x = arrowRect.x - accelRect.width;
@@ -176,7 +172,7 @@ public class FlatMenuItemRenderer
// right-to-left // right-to-left
iconRect.x = viewRect.x + viewRect.width - iconRect.width; iconRect.x = viewRect.x + viewRect.width - iconRect.width;
textRect.x = iconRect.x - textRect.width textRect.x = iconRect.x - textRect.width
- (!isTopLevelMenu && iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0); - (iconRect.width > 0 ? scale( menuItem.getIconTextGap() ) : 0);
arrowRect.x = viewRect.x; arrowRect.x = viewRect.x;
if( accelText != null ) if( accelText != null )
accelRect.x = arrowRect.x + arrowRect.width; accelRect.x = arrowRect.x + arrowRect.width;
@@ -213,8 +209,7 @@ debug*/
boolean isTopLevelMenu = isTopLevelMenu( menuItem ); boolean isTopLevelMenu = isTopLevelMenu( menuItem );
paintBackground( g, selectionBackground ); paintBackground( g, selectionBackground );
if( !isTopLevelMenu ) paintIcon( g, iconRect, getIconForPainting() );
paintIcon( g, iconRect, getIconForPainting() );
paintText( g, textRect, menuItem.getText(), selectionForeground, disabledForeground ); paintText( g, textRect, menuItem.getText(), selectionForeground, disabledForeground );
paintAccelerator( g, accelRect, getAcceleratorText(), acceleratorForeground, acceleratorSelectionForeground, disabledForeground ); paintAccelerator( g, accelRect, getAcceleratorText(), acceleratorForeground, acceleratorSelectionForeground, disabledForeground );
if( !isTopLevelMenu ) if( !isTopLevelMenu )
@@ -300,12 +295,8 @@ debug*/
return menuItem instanceof JMenu && ((JMenu)menuItem).isTopLevelMenu(); return menuItem instanceof JMenu && ((JMenu)menuItem).isTopLevelMenu();
} }
private Icon getIcon() {
return (checkIcon != null) ? checkIcon : menuItem.getIcon();
}
private Icon getIconForPainting() { private Icon getIconForPainting() {
if( checkIcon != null ) if( checkIcon != null && !isTopLevelMenu( menuItem ) )
return checkIcon; return checkIcon;
Icon icon = menuItem.getIcon(); Icon icon = menuItem.getIcon();
@@ -325,7 +316,14 @@ debug*/
} }
private Dimension getIconSize() { private Dimension getIconSize() {
Icon icon = getIcon(); if( isTopLevelMenu( menuItem ) ) {
Icon icon = menuItem.getIcon();
return (icon != null)
? new Dimension( icon.getIconWidth(), icon.getIconHeight() )
: new Dimension();
}
Icon icon = (checkIcon != null) ? checkIcon : menuItem.getIcon();
int iconWidth = (icon != null) ? icon.getIconWidth() : 0; int iconWidth = (icon != null) ? icon.getIconWidth() : 0;
int iconHeight = (icon != null) ? icon.getIconHeight() : 0; int iconHeight = (icon != null) ? icon.getIconHeight() : 0;
return new Dimension( return new Dimension(

View File

@@ -31,6 +31,7 @@ public class FlatMenusTest
public static void main( String[] args ) { public static void main( String[] args ) {
SwingUtilities.invokeLater( () -> { SwingUtilities.invokeLater( () -> {
FlatTestFrame frame = FlatTestFrame.create( args, "FlatMenusTest" ); FlatTestFrame frame = FlatTestFrame.create( args, "FlatMenusTest" );
frame.useApplyComponentOrientation = true;
frame.showFrame( FlatMenusTest::new ); frame.showFrame( FlatMenusTest::new );
} ); } );
} }
@@ -68,6 +69,10 @@ public class FlatMenusTest
JMenu menu5 = new JMenu(); JMenu menu5 = new JMenu();
JMenuItem menuItem7 = new JMenuItem(); JMenuItem menuItem7 = new JMenuItem();
JMenuItem menuItem8 = new JMenuItem(); JMenuItem menuItem8 = new JMenuItem();
JCheckBoxMenuItem checkBoxMenuItem6 = new JCheckBoxMenuItem();
JRadioButtonMenuItem radioButtonMenuItem5 = new JRadioButtonMenuItem();
JRadioButtonMenuItem radioButtonMenuItem6 = new JRadioButtonMenuItem();
JRadioButtonMenuItem radioButtonMenuItem7 = new JRadioButtonMenuItem();
JMenu menu6 = new JMenu(); JMenu menu6 = new JMenu();
JMenuItem menuItem5 = new JMenuItem(); JMenuItem menuItem5 = new JMenuItem();
JMenuItem menuItem6 = new JMenuItem(); JMenuItem menuItem6 = new JMenuItem();
@@ -127,16 +132,37 @@ public class FlatMenusTest
{ {
menu5.setText("text"); menu5.setText("text");
menu5.setMnemonic('T'); menu5.setMnemonic('T');
menu5.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess.png")));
//---- menuItem7 ---- //---- menuItem7 ----
menuItem7.setText("text"); menuItem7.setText("text");
menuItem7.setMnemonic('X'); menuItem7.setMnemonic('X');
menuItem7.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showWriteAccess.png")));
menu5.add(menuItem7); menu5.add(menuItem7);
//---- menuItem8 ---- //---- menuItem8 ----
menuItem8.setText("text"); menuItem8.setText("text");
menuItem8.setMnemonic('E'); menuItem8.setMnemonic('E');
menuItem8.setIcon(new ImageIcon(getClass().getResource("/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess@2x.png")));
menu5.add(menuItem8); menu5.add(menuItem8);
//---- checkBoxMenuItem6 ----
checkBoxMenuItem6.setText("check");
checkBoxMenuItem6.setSelected(true);
menu5.add(checkBoxMenuItem6);
//---- radioButtonMenuItem5 ----
radioButtonMenuItem5.setText("radio 1");
radioButtonMenuItem5.setSelected(true);
menu5.add(radioButtonMenuItem5);
//---- radioButtonMenuItem6 ----
radioButtonMenuItem6.setText("radio 2");
menu5.add(radioButtonMenuItem6);
//---- radioButtonMenuItem7 ----
radioButtonMenuItem7.setText("radio 3");
menu5.add(radioButtonMenuItem7);
} }
menuBar1.add(menu5); menuBar1.add(menu5);
@@ -361,6 +387,12 @@ public class FlatMenusTest
armedCheckBox.setMnemonic('A'); armedCheckBox.setMnemonic('A');
armedCheckBox.addActionListener(e -> armedChanged()); armedCheckBox.addActionListener(e -> armedChanged());
add(armedCheckBox, "cell 0 3"); add(armedCheckBox, "cell 0 3");
//---- buttonGroup1 ----
ButtonGroup buttonGroup1 = new ButtonGroup();
buttonGroup1.add(radioButtonMenuItem5);
buttonGroup1.add(radioButtonMenuItem6);
buttonGroup1.add(radioButtonMenuItem7);
// JFormDesigner - End of component initialization //GEN-END:initComponents // JFormDesigner - End of component initialization //GEN-END:initComponents
} }

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.0.0.194" Java: "13.0.1" encoding: "UTF-8" JFDML JFormDesigner: "7.0.1.0.272" Java: "13.0.2" encoding: "UTF-8"
new FormModel { new FormModel {
contentType: "form/swing" contentType: "form/swing"
@@ -24,15 +24,39 @@ new FormModel {
name: "menu5" name: "menu5"
"text": "text" "text": "text"
"mnemonic": 84 "mnemonic": 84
"icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess.png" )
add( new FormComponent( "javax.swing.JMenuItem" ) { add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem7" name: "menuItem7"
"text": "text" "text": "text"
"mnemonic": 88 "mnemonic": 88
"icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showWriteAccess.png" )
} ) } )
add( new FormComponent( "javax.swing.JMenuItem" ) { add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem8" name: "menuItem8"
"text": "text" "text": "text"
"mnemonic": 69 "mnemonic": 69
"icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/disabled_icons_test/intellij-showReadAccess@2x.png" )
} )
add( new FormComponent( "javax.swing.JCheckBoxMenuItem" ) {
name: "checkBoxMenuItem6"
"text": "check"
"selected": true
} )
add( new FormComponent( "javax.swing.JRadioButtonMenuItem" ) {
name: "radioButtonMenuItem5"
"text": "radio 1"
"$buttonGroup": new FormReference( "buttonGroup1" )
"selected": true
} )
add( new FormComponent( "javax.swing.JRadioButtonMenuItem" ) {
name: "radioButtonMenuItem6"
"text": "radio 2"
"$buttonGroup": new FormReference( "buttonGroup1" )
} )
add( new FormComponent( "javax.swing.JRadioButtonMenuItem" ) {
name: "radioButtonMenuItem7"
"text": "radio 3"
"$buttonGroup": new FormReference( "buttonGroup1" )
} ) } )
} ) } )
add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) { add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) {
@@ -321,5 +345,10 @@ new FormModel {
}, new FormLayoutConstraints( null ) { }, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 430 ) "location": new java.awt.Point( 0, 430 )
} ) } )
add( new FormNonVisual( "javax.swing.ButtonGroup" ) {
name: "buttonGroup1"
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 564 )
} )
} }
} }