Menus: support HTML in new layout (issue #3)

This commit is contained in:
Karl Tauber
2020-04-26 10:35:23 +02:00
parent fcbb3aeed1
commit 115a2df2b0
3 changed files with 20 additions and 6 deletions

View File

@@ -32,6 +32,8 @@ import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;
import com.formdev.flatlaf.FlatLaf;
/**
@@ -85,10 +87,16 @@ public class FlatMenuItemRenderer
}
// text size
String text = menuItem.getText();
FontMetrics fm = menuItem.getFontMetrics( menuItem.getFont() );
width += SwingUtilities.computeStringWidth( fm, text );
height = Math.max( fm.getHeight(), height );
View htmlView = (View) menuItem.getClientProperty( BasicHTML.propertyKey );
if( htmlView != null ) {
width += htmlView.getPreferredSpan( View.X_AXIS );
height = Math.max( (int) htmlView.getPreferredSpan( View.Y_AXIS ), height );
} else {
String text = menuItem.getText();
FontMetrics fm = menuItem.getFontMetrics( menuItem.getFont() );
width += SwingUtilities.computeStringWidth( fm, text );
height = Math.max( fm.getHeight(), height );
}
// accelerator size
String accelText = getAcceleratorText();
@@ -226,6 +234,12 @@ debug*/
}
protected void paintText( Graphics g, Rectangle textRect, String text, Color selectionForeground, Color disabledForeground ) {
View htmlView = (View) menuItem.getClientProperty( BasicHTML.propertyKey );
if( htmlView != null ) {
htmlView.paint( g, textRect );
return;
}
int mnemonicIndex = FlatLaf.isShowMnemonics() ? menuItem.getDisplayedMnemonicIndex() : -1;
paintText( g, menuItem, textRect, text, mnemonicIndex, menuItem.getFont(),

View File

@@ -197,7 +197,7 @@ public class FlatMenusTest
panel1.add(checkBoxMenuItemLabel, "cell 0 2");
//---- checkBoxMenuItem1 ----
checkBoxMenuItem1.setText("enabled");
checkBoxMenuItem1.setText("<html>en<b>abl</b>ed</html>");
checkBoxMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
checkBoxMenuItem1.setMnemonic('A');
panel1.add(checkBoxMenuItem1, "cell 1 2");

View File

@@ -92,7 +92,7 @@ new FormModel {
} )
add( new FormComponent( "javax.swing.JCheckBoxMenuItem" ) {
name: "checkBoxMenuItem1"
"text": "enabled"
"text": "<html>en<b>abl</b>ed</html>"
"accelerator": &KeyStroke0 static javax.swing.KeyStroke getKeyStroke( 112, 0, false )
"mnemonic": 65
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {